text
stringlengths
17
3.65k
code
stringlengths
70
5.84k
Program to add two integers of given base | Function to find the sum of two integers of base B ; Padding 0 in front of the number to make both numbers equal ; Condition to check if the strings have lengths mis - match ; Loop to find the find the sum of two integers of base B ; Current Place value for the resultant sum ; Update carry ; Find current digit ; Update sum result ; Driver Code ; Function Call
def sumBaseB ( a , b , base ) : NEW_LINE INDENT len_a = len ( a ) NEW_LINE len_b = len ( b ) NEW_LINE s = " " ; NEW_LINE sum = " " ; NEW_LINE diff = abs ( len_a - len_b ) ; NEW_LINE for i in range ( 1 , diff + 1 ) : NEW_LINE INDENT s += "0" NEW_LINE DEDENT if ( len_a < len_b ) : NEW_LINE INDENT a = s + a NEW_LINE DEDENT else : NEW_LINE INDENT b = s + b ; NEW_LINE DEDENT carry = 0 ; NEW_LINE for i in range ( max ( len_a , len_b ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT curr = carry + ( ord ( a [ i ] ) - ord ( '0' ) ) + ( ord ( b [ i ] ) - ord ( '0' ) ) ; NEW_LINE carry = curr // base NEW_LINE curr = curr % base ; NEW_LINE sum = chr ( curr + ord ( '0' ) ) + sum NEW_LINE DEDENT if ( carry > 0 ) : NEW_LINE INDENT sum = chr ( carry + ord ( '0' ) ) + sum ; NEW_LINE DEDENT return sum NEW_LINE DEDENT a = "123" NEW_LINE b = "234" NEW_LINE base = 6 NEW_LINE sum = sumBaseB ( a , b , base ) ; NEW_LINE print ( sum ) NEW_LINE
Minimum number of swaps required to make a number divisible by 60 | Function that prminimum number of swap operations required ; Condition if more than one zero exist ; Condition if zero_exist ; Computing total sum of all digits ; Condition if zero does not exist or the sum is not divisible by 3 ; Condition to find a digit that is multiple of 2 other than one zero ; Condition if multiple of 2 do not exist ; Condition for zero swaps means the number is already is divisible by 60 ; Condition for only one swap ; Otherwise 2 swaps required ; Driver Code
def MinimumSwapOperations ( s ) : NEW_LINE INDENT zero_exist = False NEW_LINE multiple_of_2 = False NEW_LINE sum = 0 NEW_LINE index_of_zero = 0 NEW_LINE more_zero = False NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT val = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE if ( zero_exist == True ) : NEW_LINE INDENT more_zero = True NEW_LINE DEDENT if ( val == 0 ) : NEW_LINE INDENT zero_exist = True NEW_LINE index_of_zero = i NEW_LINE DEDENT sum += val NEW_LINE DEDENT if ( zero_exist == False or sum % 3 != 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT for i in range ( len ( s ) ) : NEW_LINE INDENT val = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE if ( val % 2 == 0 and i != index_of_zero ) : NEW_LINE INDENT multiple_of_2 = True NEW_LINE DEDENT DEDENT if ( multiple_of_2 == False ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT last_val = ord ( s [ len ( s ) - 1 ] ) - ord ( '0' ) NEW_LINE second_last_val = ord ( s [ len ( s ) - 2 ] ) - ord ( '0' ) NEW_LINE if ( last_val == 0 and second_last_val % 2 == 0 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT elif ( ( last_val == 0 and second_last_val % 2 != 0 ) or ( last_val % 2 == 0 and second_last_val == 0 ) ) : NEW_LINE INDENT print ( 1 ) NEW_LINE DEDENT elif ( more_zero == True and ( last_val == 0 and second_last_val % 2 != 0 ) ) : NEW_LINE INDENT print ( 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( 2 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = "20" NEW_LINE MinimumSwapOperations ( N ) NEW_LINE DEDENT
Largest palindrome which is product of two N | Function to check if a number is a Palindrome or not ; Taking the string value of the number ; Loop to check if every i - th character from beginning is equal to every ( N - i ) th char ; Function to find the largest palindrome which is a product of two N digited numbers ; Find lowerBound , upperBound for a given nDigits . for n = 2 [ 10 , 99 ] ; Result variables ; Keep p decrementing by 11 ; Find the nearest number divisible by 11 ; Keep decrementing q by 1 ; Update the result if t > r and is a palindrome ; Printing the final result ; Driver code
def isPalindrome ( x ) : NEW_LINE INDENT num = str ( x ) NEW_LINE result = True NEW_LINE i = 0 NEW_LINE j = len ( num ) - 1 NEW_LINE while ( i < j and result ) : NEW_LINE INDENT result = num [ i ] == num [ j ] NEW_LINE i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT return result NEW_LINE DEDENT def find ( nDigits ) : NEW_LINE INDENT lowerBound = pow ( 10 , nDigits - 1 ) NEW_LINE upperBound = ( lowerBound * 10 ) - 1 NEW_LINE resultP = 0 NEW_LINE resultQ = 0 NEW_LINE resultR = 0 NEW_LINE for p in range ( upperBound , lowerBound , - 11 ) : NEW_LINE INDENT while ( p % 11 != 0 ) : NEW_LINE INDENT p -= 1 NEW_LINE DEDENT for q in range ( upperBound , lowerBound , - 1 ) : NEW_LINE INDENT t = p * q NEW_LINE if ( t > resultR and isPalindrome ( t ) ) : NEW_LINE INDENT resultP = p NEW_LINE resultQ = q NEW_LINE resultR = t NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT print ( resultR ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 NEW_LINE find ( N ) NEW_LINE DEDENT
Minimize the cost of selecting two numbers whose product is X | Python3 implementation of the above approach ; max_prime [ i ] represents maximum prime number that divides the number i ; min_prime [ i ] represents minimum prime number that divides the number i ; Function to store the minimum prime factor and the maximum prime factor in two arrays ; Check for prime number if min_prime [ i ] > 0 , then it is not a prime number ; If i is a prime number , then both minimum and maximum prime numbers that divide the number is the number itself ; If this number is being visited for first time then this divisor must be the smallest prime number that divides this number ; The last prime number that divides this number will be maximum . ; Function to minimize the cost of finding two numbers for every number such that the product of those two is equal to X ; Pre - calculation ; If X == 1 , then there is no way to find N and M . Print - 1 ; Case 3 is always valid and cost for that is C + X C for choosing 1 and M = X / 1 ; Case 1 N is prime , first number cost is fixed N is max_prime number divides this number ; If X is prime then the maximum prime number is the number itself . For this case , M becomes 1 and this shouldn 't be considered. ; Find M for this case ; Add cost for the second number also ; Update min_cost , if the cost for prime is minimum ; Case 2 If N is composite For this find the minimum prime number that divides A [ i ] and consider this as M ; Find N for that number ; Check if this number is composite or not if N is prime then there is no way to find any composite number that divides X If N = min_prime [ N ] then N is prime ; Update min_cost , if the cost for the composite is minimum ; Driver code
MAX = 10000 ; NEW_LINE max_prime = [ 0 ] * MAX ; NEW_LINE min_prime = [ 0 ] * MAX ; NEW_LINE def sieve ( n ) : NEW_LINE INDENT for i in range ( 2 , n ) : NEW_LINE INDENT if ( min_prime [ i ] > 0 ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT min_prime [ i ] = i ; NEW_LINE max_prime [ i ] = i ; NEW_LINE j = i + i ; NEW_LINE while ( j < n ) : NEW_LINE INDENT if ( min_prime [ j ] == 0 ) : NEW_LINE INDENT min_prime [ j ] = i ; NEW_LINE DEDENT max_prime [ j ] = i ; NEW_LINE j += i ; NEW_LINE DEDENT DEDENT DEDENT def findCost ( A , B , C , X ) : NEW_LINE INDENT sieve ( MAX ) ; NEW_LINE if ( X == 1 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT min_cost = C + X ; NEW_LINE cost_for_prime = A ; NEW_LINE N = max_prime [ X ] ; NEW_LINE if ( N != X ) : NEW_LINE INDENT M = X // N ; NEW_LINE cost_for_prime += M ; NEW_LINE min_cost = min ( min_cost , cost_for_prime ) ; NEW_LINE DEDENT M = min_prime [ X ] ; NEW_LINE N = X // M ; NEW_LINE if ( N != min_prime [ N ] ) : NEW_LINE INDENT cost_for_comp = B + M ; NEW_LINE min_cost = min ( min_cost , cost_for_comp ) ; NEW_LINE DEDENT return min_cost ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 7 ; B = 11 ; C = 2 ; X = 20 ; NEW_LINE print ( findCost ( A , B , C , X ) ) ; NEW_LINE DEDENT
Count the occurrence of digit K in a given number N using Recursion | Function to count the digit K in the given number N ; Extracting least significant digit ; Driver Code
def countdigits ( n , k ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT digit = n % 10 NEW_LINE if digit == k : NEW_LINE INDENT return 1 + countdigits ( n / 10 , k ) NEW_LINE DEDENT return countdigits ( n / 10 , k ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1000 ; NEW_LINE k = 0 ; NEW_LINE print ( countdigits ( n , k ) ) NEW_LINE DEDENT
Product of all Subarrays of an Array | Function to find product of all subarrays ; Variable to store the product ; Compute the product while traversing for subarrays ; Printing product of all subarray ; Driver code ; Function call
def product_subarrays ( arr , n ) : NEW_LINE INDENT res = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT product = 1 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT product *= arr [ j ] ; NEW_LINE res = res * product NEW_LINE DEDENT DEDENT print ( res ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 3 , 7 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE product_subarrays ( arr , n ) ; NEW_LINE DEDENT
Find numbers that divide X and Y to produce the same remainder | Function to find the required number as M ; Finding the maximum value among X and Y ; Loop to iterate through maximum value among X and Y . ; If the condition satisfies , then print the value of M ; Driver code
def printModulus ( X , Y ) : NEW_LINE INDENT n = max ( X , Y ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( X % i == Y % i ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT X = 10 NEW_LINE Y = 20 NEW_LINE printModulus ( X , Y ) NEW_LINE
Find numbers that divide X and Y to produce the same remainder | Function to prall the possible values of M such that X % M = Y % M ; Finding the absolute difference of X and Y ; Iterating from 1 ; Loop to prall the factors of D ; If i is a factor of d , then pri ; If d / i is a factor of d , then prd / i ; Driver code
def printModulus ( X , Y ) : NEW_LINE INDENT d = abs ( X - Y ) ; NEW_LINE i = 1 ; NEW_LINE while ( i * i <= d ) : NEW_LINE INDENT if ( d % i == 0 ) : NEW_LINE INDENT print ( i , end = " " ) ; NEW_LINE if ( d // i != i ) : NEW_LINE INDENT print ( d // i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 10 ; NEW_LINE Y = 26 ; NEW_LINE printModulus ( X , Y ) ; NEW_LINE DEDENT
Check whether a number can be represented as difference of two squares | Function to check whether a number can be represented by the difference of two squares ; Checking if n % 4 = 2 or not ; Driver code
def difSquare ( n ) : NEW_LINE INDENT if ( n % 4 != 2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 45 NEW_LINE if ( difSquare ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Count of Fibonacci divisors of a given number | Python3 program to count number of divisors of N which are Fibonacci numbers ; Function to create hash table to check Fibonacci numbers ; Function to count number of divisors of N which are fibonacci numbers ; If divisors are equal , check and count only one ; Otherwise check and count both ; Driver code
from math import sqrt , ceil , floor NEW_LINE def createHash ( maxElement ) : NEW_LINE INDENT prev = 0 NEW_LINE curr = 1 NEW_LINE d = dict ( ) NEW_LINE d [ prev ] = 1 NEW_LINE d [ curr ] = 1 NEW_LINE while ( curr <= maxElement ) : NEW_LINE INDENT temp = curr + prev NEW_LINE d [ temp ] = 1 NEW_LINE prev = curr NEW_LINE curr = temp NEW_LINE DEDENT return d NEW_LINE DEDENT def countFibonacciDivisors ( n ) : NEW_LINE INDENT hash = createHash ( n ) NEW_LINE cnt = 0 NEW_LINE for i in range ( 1 , ceil ( sqrt ( n ) ) ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( ( n // i == i ) and ( n // i in hash ) ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( n // i in hash ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT if ( n // ( n // i ) in hash ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT n = 12 NEW_LINE print ( countFibonacciDivisors ( n ) ) NEW_LINE
Count of subtrees in a Binary Tree having XOR value K | A binary tree node ; A utility function to allocate a new node ; Base Case : If node is None , return 0 ; Calculating the XOR of the current subtree ; Increment res if xr is equal to k ; Return the XOR value of the current subtree ; Function to find the required count ; Initialize result variable 'res ; Recursively traverse the tree and compute the count ; return the count 'res ; Driver program ; Create the binary tree by adding nodes to it
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT newNode = Node ( data ) NEW_LINE return newNode NEW_LINE DEDENT def rec ( root , res , k ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return [ 0 , res ] ; NEW_LINE DEDENT xr = root . data ; NEW_LINE tmp , res = rec ( root . left , res , k ) ; NEW_LINE xr ^= tmp NEW_LINE tmp , res = rec ( root . right , res , k ) ; NEW_LINE xr ^= tmp NEW_LINE if ( xr == k ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT return xr , res ; NEW_LINE DEDENT def findCount ( root , K ) : NEW_LINE ' NEW_LINE INDENT res = 0 ; NEW_LINE tmp , res = rec ( root , res , K ) ; NEW_LINE DEDENT ' NEW_LINE INDENT return res ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 2 ) ; NEW_LINE root . left = newNode ( 1 ) ; NEW_LINE root . right = newNode ( 9 ) ; NEW_LINE root . left . left = newNode ( 10 ) ; NEW_LINE root . left . right = newNode ( 5 ) ; NEW_LINE K = 5 ; NEW_LINE print ( findCount ( root , K ) ) NEW_LINE DEDENT
Make array elements equal with minimum cost | Function to find the minimum cost required to make array elements equal ; Loop to find the count of odd elements ; Driver Code
def makearrayequal ( arr , n ) : NEW_LINE INDENT x = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT x += arr [ i ] & 1 ; NEW_LINE DEDENT print ( min ( x , n - x ) ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 3 , 2 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE makearrayequal ( arr , n ) ; NEW_LINE DEDENT
Count of Prime digits in a Number | Function to find the count of prime digits in a number ; Loop to compute all the digits of the number ; Finding every digit of the given number ; Checking if digit is prime or not Only 2 , 3 , 5 and 7 are prime one - digit number ; Driver code
def countDigit ( n ) : NEW_LINE INDENT temp = n NEW_LINE count = 0 NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT d = temp % 10 NEW_LINE temp //= 10 NEW_LINE if ( d == 2 or d == 3 or d == 5 or d == 7 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 1234567890 NEW_LINE print ( countDigit ( n ) ) NEW_LINE DEDENT
Check if an Array is a permutation of numbers from 1 to N : Set 2 | Function to check if an array represents a permutation or not ; Counting the frequency ; Check if each frequency is 1 only ; Driver code
def permutation ( arr , N ) : NEW_LINE INDENT hash = [ 0 ] * ( N + 1 ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT hash [ arr [ i ] ] += 1 ; NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( hash [ i ] != 1 ) : NEW_LINE INDENT return " No " ; NEW_LINE DEDENT DEDENT return " Yes " ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 5 , 5 , 3 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( permutation ( arr , n ) ) ; NEW_LINE DEDENT
Check if the XOR of an array of integers is Even or Odd | Function to check if the XOR of an array of integers is Even or Odd ; Count the number of odd elements ; If count of odd elements is odd , then XOR will be odd ; Else even ; Driver Code ; Function call
def check ( arr , n ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT count = count + 1 ; NEW_LINE DEDENT DEDENT if ( count & 1 ) : NEW_LINE INDENT return " Odd " ; NEW_LINE DEDENT else : NEW_LINE INDENT return " Even " ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 9 , 12 , 13 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE print ( check ( arr , n ) ) NEW_LINE DEDENT
Length of Longest Prime Subsequence in an Array | Python 3 program to find the length of Longest Prime Subsequence in an Array ; Function to create Sieve to check primes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to find the longest subsequence which contain all prime numbers ; Precompute N primes ; Find the length of longest prime subsequence ; Driver code ; Function call
N = 100005 NEW_LINE def SieveOfEratosthenes ( prime , p_size ) : NEW_LINE INDENT prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE p = 2 NEW_LINE while p * p <= p_size : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT for i in range ( p * 2 , p_size + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def longestPrimeSubsequence ( arr , n ) : NEW_LINE INDENT prime = [ True ] * ( N + 1 ) NEW_LINE SieveOfEratosthenes ( prime , N ) NEW_LINE answer = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ arr [ i ] ] ) : NEW_LINE INDENT answer += 1 NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 4 , 11 , 2 , 9 , 21 ] NEW_LINE n = len ( arr ) NEW_LINE print ( longestPrimeSubsequence ( arr , n ) ) NEW_LINE DEDENT
Check if an Octal number is Even or Odd | Check if the number is odd or even ; Check if the last digit is either '0' , '2' , '4' , or '6 ; Driver code
def even_or_odd ( N ) : NEW_LINE INDENT l = len ( N ) ; NEW_LINE DEDENT ' NEW_LINE INDENT if ( N [ l - 1 ] == '0' or N [ l - 1 ] == '2' or N [ l - 1 ] == '4' or N [ l - 1 ] == '6' ) : NEW_LINE INDENT return ( " Even " ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( " Odd " ) NEW_LINE DEDENT DEDENT N = "735" NEW_LINE print ( even_or_odd ( N ) ) NEW_LINE
Runge | A sample differential equation " dy / dx ▁ = ▁ ( x ▁ - ▁ y ) /2" ; Finds value of y for a given x using step size h and initial value y0 at x0 . ; Count number of iterations using step size or step height h ; Iterate for number of iterations ; Apply Runge Kutta Formulas to find next value of y ; Update next value of y ; Update next value of x ; Driver Code
def dydx ( x , y ) : NEW_LINE INDENT return ( x + y - 2 ) ; NEW_LINE DEDENT def rungeKutta ( x0 , y0 , x , h ) : NEW_LINE INDENT n = round ( ( x - x0 ) / h ) ; NEW_LINE y = y0 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT k1 = h * dydx ( x0 , y ) ; NEW_LINE k2 = h * dydx ( x0 + 0.5 * h , y + 0.5 * k1 ) ; NEW_LINE y = y + ( 1.0 / 6.0 ) * ( k1 + 2 * k2 ) ; NEW_LINE x0 = x0 + h ; NEW_LINE DEDENT return y ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x0 = 0 ; y = 1 ; NEW_LINE x = 2 ; h = 0.2 ; NEW_LINE print ( " y ( x ) ▁ = " , rungeKutta ( x0 , y , x , h ) ) ; NEW_LINE DEDENT
Count of Fibonacci pairs which satisfy the given equation | Python program to find the count of Fibonacci pairs ( x , y ) which satisfy the equation Ax + By = N ; Array to store the Fibonacci numbers ; Array to store the number of ordered pairs ; Function to find if a number is a perfect square ; Function that returns 1 if N is non - fibonacci number else 0 ; N is Fibinacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both are perferct square ; Function to store the fibonacci numbers and their frequency in form a * x + b * y ; Storing the Fibonacci numbers ; For loop to find all the possible combinations of the Fibonacci numbers ; Finding the number of ordered pairs ; Driver code ; Find the ordered pair for every query
import math NEW_LINE size = 101 NEW_LINE fib = [ 0 ] * 100010 NEW_LINE freq = [ 0 ] * ( 100010 ) NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT s = int ( math . sqrt ( x ) ) NEW_LINE return ( s * s ) == x NEW_LINE DEDENT def isFibonacci ( n ) : NEW_LINE INDENT if ( isPerfectSquare ( 5 * n * n + 4 ) or isPerfectSquare ( 5 * n * n - 4 ) ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT return 0 ; NEW_LINE DEDENT def compute ( a , b ) : NEW_LINE INDENT for i in range ( 1 , 100010 ) : NEW_LINE INDENT fib [ i ] = isFibonacci ( i ) NEW_LINE DEDENT for x in range ( 1 , 100010 ) : NEW_LINE INDENT for y in range ( 1 , size ) : NEW_LINE INDENT if ( fib [ x ] == 1 and fib [ y ] == 1 and a * x + b * y < 100010 ) : NEW_LINE INDENT freq [ a * x + b * y ] += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT Q = 2 NEW_LINE A = 5 NEW_LINE B = 10 NEW_LINE compute ( A , B ) ; NEW_LINE arr = [ 50 , 150 ] NEW_LINE for i in range ( Q ) : NEW_LINE INDENT print ( freq [ arr [ i ] ] , end = " ▁ " ) NEW_LINE DEDENT
Maximise the sum of two Numbers using at most one swap between them | Python program to maximise the sum of two Numbers using at most one swap between them ; Function to maximize the sum by swapping only one digit ; Store digits of max ( n1 , n2 ) ; Store digits of min ( n1 , n2 ) ; If length of the two numbers are unequal ; Find the most significant number and the most significant index for swapping ; If both numbers are of equal length ; Fetch the index of current maximum digit present in both the arrays ; Compute the difference ; Find the most significant index and the most significant digit to be swapped ; Restore the new numbers ; Display the maximized sum ; Driver function
MAX = 100 NEW_LINE def findMaxSum ( n1 , n2 ) : NEW_LINE INDENT arr1 = [ 0 ] * ( MAX ) NEW_LINE arr2 = [ 0 ] * ( MAX ) NEW_LINE l1 = 0 NEW_LINE l2 = 0 NEW_LINE max1 = max ( n1 , n2 ) ; NEW_LINE min1 = min ( n1 , n2 ) ; NEW_LINE i = max1 NEW_LINE while i > 0 : NEW_LINE INDENT arr1 [ l1 ] = ( i % 10 ) NEW_LINE l1 += 1 NEW_LINE i //= 10 NEW_LINE DEDENT i = min1 NEW_LINE while i > 0 : NEW_LINE INDENT arr2 [ l2 ] = ( i % 10 ) NEW_LINE l2 += 1 NEW_LINE i //= 10 NEW_LINE DEDENT f = 0 NEW_LINE if ( l1 != l2 ) : NEW_LINE INDENT index = arr2 . index ( max ( arr2 ) ) NEW_LINE for i in range ( l1 - 1 , ( l2 - 1 ) , - 1 ) : NEW_LINE INDENT if ( arr1 [ i ] < arr2 [ index ] ) : NEW_LINE INDENT ( arr1 [ i ] , arr2 [ index ] ) = ( arr2 [ index ] , arr1 [ i ] ) NEW_LINE f = 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if ( f != 1 ) : NEW_LINE INDENT index1 = 0 NEW_LINE index2 = 0 NEW_LINE diff1 = 0 NEW_LINE diff2 = 0 NEW_LINE for i in range ( l2 - 1 , - 1 , - 1 ) : NEW_LINE INDENT index1 = arr1 . index ( max ( arr1 [ : i ] ) ) NEW_LINE index2 = arr2 . index ( max ( arr2 [ : i ] ) ) NEW_LINE diff1 = ( arr2 [ index2 ] - arr1 [ i ] ) ; NEW_LINE diff2 = ( arr1 [ index1 ] - arr2 [ i ] ) ; NEW_LINE if ( diff1 > 0 or diff2 > 0 ) : NEW_LINE INDENT if ( diff1 > diff2 ) : NEW_LINE INDENT arr1 [ i ] , arr2 [ index2 ] = arr2 [ index2 ] , arr1 [ i ] NEW_LINE break NEW_LINE DEDENT elif ( diff2 > diff1 ) : NEW_LINE INDENT arr2 [ i ] , arr1 [ index1 ] = arr1 [ index1 ] , arr2 [ i ] NEW_LINE break NEW_LINE DEDENT elif ( diff1 == diff2 ) : NEW_LINE INDENT if ( index1 <= index2 ) : NEW_LINE INDENT arr2 [ i ] , arr1 [ index1 ] = arr1 [ index1 ] , arr2 [ i ] NEW_LINE break NEW_LINE DEDENT elif ( index2 <= index1 ) : NEW_LINE INDENT arr1 [ i ] , arr2 [ index2 ] = arr2 [ index2 ] , arr1 [ i ] NEW_LINE break ; NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT f_n1 = 0 NEW_LINE f_n2 = 0 NEW_LINE for i in range ( l1 - 1 , - 1 , - 1 ) : NEW_LINE INDENT f_n1 = ( f_n1 * 10 ) + arr1 [ i ] NEW_LINE f_n2 = ( f_n2 * 10 ) + arr2 [ i ] NEW_LINE DEDENT print ( f_n1 + f_n2 ) NEW_LINE DEDENT N1 = 9987 NEW_LINE N2 = 123 NEW_LINE findMaxSum ( N1 , N2 ) NEW_LINE
Check if a number is divisible by 47 or not | Function to check if the number is divisible by 47 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Subtracting fourteen times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 43 or not ; Driver Code
def isDivisible ( n ) : NEW_LINE INDENT while n // 100 : NEW_LINE INDENT d = n % 10 NEW_LINE n //= 10 NEW_LINE n = abs ( n - ( d * 14 ) ) NEW_LINE DEDENT return ( n % 47 == 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 59173 NEW_LINE if ( isDivisible ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Check if a HexaDecimal number is Even or Odd | Check if the number is odd or even ; check if the last digit is either '0' , '2' , '4' , '6' , '8' , ' A ' ( = 10 ) , ' C ' ( = 12 ) or ' E ' ( = 14 ) ; Driver code
def even_or_odd ( N ) : NEW_LINE INDENT l = len ( N ) NEW_LINE if ( N [ l - 1 ] == '0' or N [ l - 1 ] == '2' or N [ l - 1 ] == '4' or N [ l - 1 ] == '6' or N [ l - 1 ] == '8' or N [ l - 1 ] == ' A ' or N [ l - 1 ] == ' C ' or N [ l - 1 ] == ' E ' ) : NEW_LINE INDENT return ( " Even " ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( " Odd " ) NEW_LINE DEDENT DEDENT N = " AB3454D " NEW_LINE print ( even_or_odd ( N ) ) NEW_LINE
Check if a number is divisible by 31 or not | Function to check if the number is divisible by 31 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Subtracting three times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 31 or not ; Driver Code
def isDivisible ( n ) : NEW_LINE INDENT while n // 100 : NEW_LINE INDENT d = n % 10 NEW_LINE n //= 10 NEW_LINE n = abs ( n - ( d * 3 ) ) NEW_LINE DEDENT return ( n % 31 == 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1922 NEW_LINE if ( isDivisible ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Check if the number is divisible 43 or not | Function to check if the number is divisible by 43 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Adding thirteen times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 43 or not ; Driver Code
def isDivisible ( n ) : NEW_LINE INDENT while n // 100 : NEW_LINE INDENT d = n % 10 NEW_LINE n //= 10 NEW_LINE n = abs ( n + ( d * 13 ) ) NEW_LINE DEDENT return ( n % 43 == 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2795 NEW_LINE if ( isDivisible ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Find the next Non | Python 3 implementation of the approach ; Function to check if a number is perfect square ; Function to check if a number is Fibinacci Number ; N is Fibinacci if either ( 5 * N * N + 4 ) , ( 5 * N * N - 4 ) or both is a perferct square ; Function to find the next Non - Fibinacci Number ; Case 1 If N <= 3 , then 4 will be next Non - Fibinacci Number ; Case 2 If N + 1 is Fibinacci , then N + 2 will be next Non - Fibinacci Number ; If N + 1 is Non - Fibinacci , then N + 2 will be next Non - Fibinacci Number ; Driver code
from math import sqrt NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT s = sqrt ( x ) NEW_LINE return ( s * s == x ) NEW_LINE DEDENT def isFibonacci ( N ) : NEW_LINE INDENT return isPerfectSquare ( 5 * N * N + 4 ) or isPerfectSquare ( 5 * N * N - 4 ) NEW_LINE DEDENT def nextNonFibonacci ( N ) : NEW_LINE INDENT if ( N <= 3 ) : NEW_LINE INDENT return 4 NEW_LINE DEDENT if ( isFibonacci ( N + 1 ) ) : NEW_LINE INDENT return N + 2 NEW_LINE DEDENT else : NEW_LINE INDENT return N NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE print ( nextNonFibonacci ( N ) ) NEW_LINE N = 4 NEW_LINE print ( nextNonFibonacci ( N ) ) NEW_LINE N = 7 NEW_LINE print ( nextNonFibonacci ( N ) ) NEW_LINE DEDENT
Represent K ^ N as the sum of exactly N numbers | Python 3 program to represent K ^ N as the sum of exactly N numbers ; Function to print N numbers whose sum is a power of K ; Printing K ^ 1 ; Loop to print the difference of powers from K ^ 2 ; Driver code
from math import pow NEW_LINE def printf ( n , k ) : NEW_LINE INDENT print ( int ( k ) , end = " ▁ " ) NEW_LINE for i in range ( 2 , n + 1 , 1 ) : NEW_LINE INDENT x = pow ( k , i ) - pow ( k , i - 1 ) NEW_LINE print ( int ( x ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE K = 4 NEW_LINE printf ( N , K ) NEW_LINE DEDENT
Check if the given number is divisible by 71 or not | Function to check if the number is divisible by 71 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Subtracting seven times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 71 or not ; Driver Code
def isDivisible ( n ) : NEW_LINE INDENT while n // 100 : NEW_LINE INDENT d = n % 10 NEW_LINE n //= 10 NEW_LINE n = abs ( n - ( d * 7 ) ) NEW_LINE DEDENT return ( n % 71 == 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5041 NEW_LINE if ( isDivisible ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Count of prime digits of a Number which divides the number | Function to find the number of digits in number which divides the number and is also a prime number ; Only 2 , 3 , 5 and 7 are prime one - digit number ; Loop to compute all the digits of the number untill it is not equal to the zero ; Fetching each digit of the number ; Checking if digit is greater than 0 and can divides n and is prime too ; Driver Code
def countDigit ( n ) : NEW_LINE INDENT prime = [ False ] * 10 NEW_LINE prime [ 2 ] = True NEW_LINE prime [ 3 ] = True ; NEW_LINE prime [ 5 ] = True NEW_LINE prime [ 7 ] = True ; NEW_LINE temp = n NEW_LINE count = 0 ; NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT d = temp % 10 ; NEW_LINE temp //= 10 ; NEW_LINE if ( d > 0 and n % d == 0 and prime [ d ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT n = 1032 NEW_LINE print ( countDigit ( n ) ) NEW_LINE
Bitwise Operations on Digits of a Number | Python 3 implementation of the approach ; Function to find the digits ; Function to Find OR of all digits of a number ; Find OR of all digits ; return OR of digits ; Function to Find AND of all digits of a number ; Find AND of all digits ; return AND of digits ; Function to Find XOR of all digits of a number ; Find XOR of all digits ; return XOR of digits ; Driver code ; Find and store all digits ; Find XOR of digits ; Find OR of digits ; Find AND of digits ; Driver code
digit = [ 0 ] * ( 100000 ) NEW_LINE def findDigits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT digit [ count ] = n % 10 ; NEW_LINE n = n // 10 ; NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def OR_of_Digits ( n , count ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( count ) : NEW_LINE INDENT ans = ans | digit [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT def AND_of_Digits ( n , count ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( count ) : NEW_LINE INDENT ans = ans & digit [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT def XOR_of_Digits ( n , count ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( count ) : NEW_LINE INDENT ans = ans ^ digit [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT def bitwise_operation ( N ) : NEW_LINE INDENT countOfDigit = findDigits ( N ) NEW_LINE print ( " XOR ▁ = ▁ " , XOR_of_Digits ( N , countOfDigit ) ) NEW_LINE print ( " OR ▁ = ▁ " , OR_of_Digits ( N , countOfDigit ) ) NEW_LINE print ( " AND ▁ = ▁ " , AND_of_Digits ( N , countOfDigit ) ) NEW_LINE DEDENT N = 123456 ; NEW_LINE bitwise_operation ( N ) NEW_LINE
Length of longest subarray with product greater than or equal to 0 | Function that count the Length of longest subarray with product greater than or equals to zero ; If product is greater than zero , return array size ; Traverse the array and if any negative element found then update the Length of longest subarray with the Length of left and right subarray ; Driver Code
def maxLength ( arr , N ) : NEW_LINE INDENT product = 1 NEW_LINE Len = 0 NEW_LINE for i in arr : NEW_LINE INDENT product *= i NEW_LINE DEDENT if ( product >= 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT Len = max ( Len , max ( N - i - 1 , i ) ) NEW_LINE DEDENT DEDENT return Len NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 1 , 1 , 1 , - 2 , 3 , 2 , - 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maxLength ( arr , N ) ) NEW_LINE DEDENT
Print a pair of numbers with the given Sum and Product | Python3 program to find any pair which has sum S and product P . ; Prints roots of quadratic equation ax * 2 + bx + c = 0 ; calculating the sq root value for b * b - 4 * a * c ; Finding the roots ; Check if the roots are valid or not ; Finding the roots ; Check if the roots are valid or not ; when d < 0 ; No such pair exists in this case ; Driver code
from math import sqrt NEW_LINE def findRoots ( b , c ) : NEW_LINE INDENT a = 1 NEW_LINE d = b * b - 4 * a * c NEW_LINE sqrt_val = sqrt ( abs ( d ) ) NEW_LINE if ( d > 0 ) : NEW_LINE INDENT x = - b + sqrt_val NEW_LINE y = - b - sqrt_val NEW_LINE root1 = ( x ) // ( 2 * a ) NEW_LINE root2 = ( y ) // ( 2 * a ) NEW_LINE if ( root1 + root2 == - 1 * b and root1 * root2 == c ) : NEW_LINE INDENT print ( int ( root1 ) , " , " , int ( root2 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT elif ( d == 0 ) : NEW_LINE INDENT root = - b // ( 2 * a ) NEW_LINE if ( root + root == - 1 * b and root * root == c ) : NEW_LINE INDENT print ( root , " , " , root ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = 5 NEW_LINE P = 6 NEW_LINE findRoots ( - S , P ) NEW_LINE S = 5 NEW_LINE P = 9 NEW_LINE findRoots ( - S , P ) NEW_LINE DEDENT
Print N numbers such that their product is a Perfect Cube | Function to find the N numbers such that their product is a perfect cube ; Loop to traverse each number from 1 to N ; Print the cube of i as the ith term of the output ; Driver Code ; Function Call
def findNumbers ( N ) : NEW_LINE INDENT i = 1 NEW_LINE while ( i <= N ) : NEW_LINE INDENT print ( ( i * i * i ) , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE findNumbers ( N ) NEW_LINE DEDENT
Sum of all proper divisors from 1 to N | Utility function to find sum of all proper divisor of number up to N ; Loop to iterate over all the numbers from 1 to N ; Find all divisors of i and add them ; Subtracting ' i ' so that the number itself is not included ; Driver Code
def properDivisorSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT if j * j > i : NEW_LINE INDENT break NEW_LINE DEDENT if ( i % j == 0 ) : NEW_LINE INDENT if ( i // j == j ) : NEW_LINE INDENT sum += j NEW_LINE DEDENT else : NEW_LINE INDENT sum += j + i // j NEW_LINE DEDENT DEDENT DEDENT sum = sum - i NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE print ( properDivisorSum ( n ) ) NEW_LINE n = 5 NEW_LINE print ( properDivisorSum ( n ) ) NEW_LINE DEDENT
Sum of all proper divisors from 1 to N | Utility function to find sum of all proper divisor of number up to N ; Loop to find the proper divisor of every number from 1 to N ; Driver Code
def properDivisorSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sum += ( n // i ) * i NEW_LINE DEDENT return sum - n * ( n + 1 ) // 2 NEW_LINE DEDENT n = 4 NEW_LINE print ( properDivisorSum ( n ) ) NEW_LINE n = 5 NEW_LINE print ( properDivisorSum ( n ) ) NEW_LINE
Minimum change in given value so that it lies in all given Ranges | Function to find the minimum difference in the number D such that D is inside of every range ; Loop to find the common range out of the given array of ranges . ; Storing the start and end index ; Sorting the range ; Finding the maximum starting value of common segment ; Finding the minimum ending value of common segment ; If there is no common segment ; If the given number is between the computed common range . ; Finding the minimum distance ; Driver Code ; Given array of ranges
def findMinimumOperation ( n , d , arrays ) : NEW_LINE INDENT cnt = 0 NEW_LINE first = - 10 ** 9 NEW_LINE end = 10 ** 9 NEW_LINE while ( n ) : NEW_LINE INDENT arr = [ arrays [ cnt ] [ 0 ] , arrays [ cnt ] [ 1 ] ] NEW_LINE arr = sorted ( arr ) NEW_LINE first = max ( first , arr [ 0 ] ) NEW_LINE end = min ( end , arr [ 1 ] ) NEW_LINE cnt += 1 NEW_LINE n -= 1 NEW_LINE DEDENT if ( first > end ) : NEW_LINE INDENT print ( " - 1" , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( d >= first and d <= end ) : NEW_LINE INDENT print ( "0" , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( min ( abs ( first - d ) , abs ( d - end ) ) , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE d = 3 NEW_LINE arrays = [ [ 0 , 7 ] , [ 2 , 14 ] , [ 4 , 6 ] ] NEW_LINE findMinimumOperation ( n , d , arrays ) NEW_LINE DEDENT
Number of factors of very large number N modulo M where M is any prime number | Python 3 implementation to find the Number of factors of very large number N modulo M ; Function for modular multiplication ; Function to find the number of factors of large Number N ; Count the number of times 2 divides the number N ; Condition to check if 2 divides it ; Check for all the possible numbers that can divide it ; Loop to check the number of times prime number i divides it ; Condition to check if prime number i divides it ; Condition to check if N at the end is a prime number . ; Driver Code
from math import sqrt NEW_LINE mod = 1000000007 NEW_LINE def mult ( a , b ) : NEW_LINE INDENT return ( ( a % mod ) * ( b % mod ) ) % mod NEW_LINE DEDENT def calculate_factors ( n ) : NEW_LINE INDENT cnt = 0 NEW_LINE ans = 1 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE n = n // 2 NEW_LINE DEDENT if ( cnt ) : NEW_LINE INDENT ans = mult ( ans , ( cnt + 1 ) ) NEW_LINE DEDENT for i in range ( 3 , int ( sqrt ( n ) ) , 2 ) : NEW_LINE INDENT cnt = 0 NEW_LINE while ( n % i == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE n = n // i NEW_LINE DEDENT if ( cnt ) : NEW_LINE INDENT ans = mult ( ans , ( cnt + 1 ) ) NEW_LINE DEDENT DEDENT if ( n > 2 ) : NEW_LINE INDENT ans = mult ( ans , 2 ) NEW_LINE DEDENT return ans % mod NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 19374857 NEW_LINE mod = 17 NEW_LINE print ( calculate_factors ( n ) ) NEW_LINE DEDENT
Maximum value of B less than A such that A ^ B = A + B | Function to find the maximum value of B such that A ^ B = A + B ; Binary Representation of A ; Loop to find the negation of the integer A ; output ; Driver Code ; Function Call
def maxValue ( a ) : NEW_LINE INDENT a = bin ( a ) [ 2 : ] NEW_LINE b = ' ' NEW_LINE for i in list ( a ) : NEW_LINE INDENT b += str ( int ( not int ( i ) ) ) NEW_LINE DEDENT print ( int ( b , 2 ) ) NEW_LINE return int ( b , 2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 4 NEW_LINE maxValue ( a ) NEW_LINE DEDENT
Print all possible pair with prime XOR in the Array | Python implementation of the above approach ; Function for Sieve of Eratosthenes ; If i is prime , then make all multiples of i false ; Function to prall Pairs whose XOR is prime ; if A [ i ] ^ A [ j ] is prime , then prthis pair ; Driver Code ; Generate all the prime number ; Function Call
sz = 10 ** 5 NEW_LINE isPrime = [ True ] * ( sz + 1 ) NEW_LINE def generatePrime ( ) : NEW_LINE INDENT i , j = 0 , 0 NEW_LINE isPrime [ 0 ] = isPrime [ 1 ] = False NEW_LINE for i in range ( 2 , sz + 1 ) : NEW_LINE INDENT if i * i > sz : NEW_LINE INDENT break NEW_LINE DEDENT if ( isPrime [ i ] ) : NEW_LINE INDENT for j in range ( i * i , sz , i ) : NEW_LINE INDENT isPrime [ j ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def Pair_of_PrimeXor ( A , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( isPrime [ ( A [ i ] ^ A [ j ] ) ] ) : NEW_LINE INDENT print ( " ( " , A [ i ] , " , " , A [ j ] , " ) " , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 3 , 6 , 11 ] NEW_LINE n = len ( A ) NEW_LINE generatePrime ( ) NEW_LINE Pair_of_PrimeXor ( A , n ) NEW_LINE DEDENT
Check if N can be expressed as product of 3 distinct numbers | function to find 3 distinct number with given product ; Declare a vector to store divisors ; store all divisors of number in array ; store all the occurence of divisors ; check if n is not equals to - 1 then n is also a prime factor ; Initialize the variables with 1 ; check for first number a ; check for second number b ; check for third number c ; check for all unwanted condition ; Driver function
def getnumbers ( n ) : NEW_LINE INDENT divisor = [ ] NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT while ( n % i == 0 ) : NEW_LINE INDENT divisor . append ( i ) NEW_LINE n //= i NEW_LINE DEDENT DEDENT if ( n != 1 ) : NEW_LINE INDENT divisor . append ( n ) NEW_LINE DEDENT a , b , c , size = 0 , 0 , 0 , 0 NEW_LINE a = b = c = 1 NEW_LINE size = len ( divisor ) NEW_LINE for i in range ( size ) : NEW_LINE INDENT if ( a == 1 ) : NEW_LINE INDENT a = a * divisor [ i ] NEW_LINE DEDENT elif ( b == 1 or b == a ) : NEW_LINE INDENT b = b * divisor [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT c = c * divisor [ i ] NEW_LINE DEDENT DEDENT if ( a == 1 or b == 1 or c == 1 or a == b or b == c or a == c ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( a , b , c ) NEW_LINE DEDENT DEDENT n = 64 NEW_LINE getnumbers ( n ) NEW_LINE
Check if all Prime factors of number N are unique or not | Function that returns the all the distinct prime factors in a vector ; If n is divisible by 2 ; Divide n till all factors of 2 ; Check for the prime numbers other than 2 ; Store i in Prime [ ] i is a factor of n ; Divide n till all factors of i ; If n is greater than 2 , then n is prime number after n divided by all factors ; Returns the vector Prime ; Function that check whether N is the product of distinct prime factors or not ; Returns the vector to store all the distinct prime factors ; To find the product of all distinct prime factors ; Find the product ; If product is equals to N , print YES , else print NO ; Driver Code
def primeFactors ( n ) : NEW_LINE INDENT Prime = [ ] ; NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT Prime . append ( 2 ) ; NEW_LINE DEDENT while ( n % 2 == 0 ) : NEW_LINE INDENT n = n // 2 ; NEW_LINE DEDENT for i in range ( 3 , int ( n ** ( 1 / 2 ) ) , 2 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT Prime . append ( i ) ; NEW_LINE DEDENT while ( n % i == 0 ) : NEW_LINE INDENT n = n // i ; NEW_LINE DEDENT DEDENT if ( n > 2 ) : NEW_LINE INDENT Prime . append ( n ) ; NEW_LINE DEDENT return Prime ; NEW_LINE DEDENT def checkDistinctPrime ( n ) : NEW_LINE INDENT Prime = primeFactors ( n ) ; NEW_LINE product = 1 ; NEW_LINE for i in Prime : NEW_LINE INDENT product *= i ; NEW_LINE DEDENT if ( product == n ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 30 ; NEW_LINE checkDistinctPrime ( N ) ; NEW_LINE DEDENT
Count perfect power of K in a range [ L , R ] | Python 3 implementation to find the count of numbers those are powers of K in range L to R ; Function to find the Nth root of the number ; initially guessing a random number between 0 to 9 ; Smaller eps , denotes more accuracy ; Initializing difference between two roots by INT_MAX ; loo3p until we reach desired accuracy ; calculating current value from previous value ; Function to count the perfect powers of K in range L to R ; Driver code
import sys NEW_LINE from math import pow , ceil , floor NEW_LINE import random NEW_LINE def nthRoot ( A , N ) : NEW_LINE INDENT xPre = ( random . randint ( 0 , 9 ) ) % 10 NEW_LINE eps = 1e-3 NEW_LINE delX = sys . maxsize NEW_LINE while ( delX > eps ) : NEW_LINE INDENT xK = ( ( N - 1.0 ) * xPre + A / pow ( xPre , N - 1 ) ) / N NEW_LINE delX = abs ( xK - xPre ) NEW_LINE xPre = xK NEW_LINE DEDENT return xK NEW_LINE DEDENT def countPowers ( a , b , k ) : NEW_LINE INDENT return ( floor ( nthRoot ( b , k ) ) - ceil ( nthRoot ( a , k ) ) + 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 7 NEW_LINE b = 28 NEW_LINE k = 2 NEW_LINE print ( " Count ▁ of ▁ Powers ▁ is " , countPowers ( a , b , k ) ) NEW_LINE DEDENT
Minimum number of primes required such that their sum is equal to N | Function to check if n is prime ; Function to count the minimum prime required for given sum N ; Case 1 : ; Case 2 : ; Case 3 : ; Case 3 a : ; Case 3 b : ; Driver Code ; Function Call
def isPrime ( n ) : NEW_LINE INDENT for i in range ( 2 , int ( n ** ( 1 / 2 ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def printMinCountPrime ( N ) : NEW_LINE INDENT if ( isPrime ( N ) ) : NEW_LINE INDENT minCount = 1 ; NEW_LINE DEDENT elif ( N % 2 == 0 ) : NEW_LINE INDENT minCount = 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( isPrime ( N - 2 ) ) : NEW_LINE INDENT minCount = 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT minCount = 3 ; NEW_LINE DEDENT DEDENT print ( minCount ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 100 ; NEW_LINE printMinCountPrime ( N ) ; NEW_LINE DEDENT
Long Division Method to find Square root with Examples | Python3 program to find the square root of a number by using long division method ; Function to find the square root of a number by using long division method ; udigit , j = 0 , 0 Loop counters ; Dividing the number into segments ; Last index of the array of segments ; Start long division from the last segment ( j = i ) ; Initialising the remainder to the maximum value ; Including the next segment in new dividend ; Loop to check for the perfect square closest to each segment ; This condition is to find the divisor after adding a digit in the range 0 to 9 ; Calculating the remainder ; Updating the units digit of the quotient ; Adding units digit to the quotient ; New divisor is two times quotient ; Including the remainder in new dividend ; Driver code
INFINITY_ = 9999999 NEW_LINE def sqrtByLongDivision ( n ) : NEW_LINE INDENT i = 0 NEW_LINE cur_divisor = 0 NEW_LINE quotient_units_digit = 0 NEW_LINE cur_quotient = 0 NEW_LINE cur_dividend = 0 NEW_LINE cur_remainder = 0 NEW_LINE a = [ 0 ] * 10 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT a [ i ] = n % 100 NEW_LINE n = n // 100 NEW_LINE i += 1 NEW_LINE DEDENT i -= 1 NEW_LINE for j in range ( i , - 1 , - 1 ) : NEW_LINE INDENT cur_remainder = INFINITY_ NEW_LINE cur_dividend = cur_dividend * 100 + a [ j ] NEW_LINE for udigit in range ( 10 ) : NEW_LINE INDENT if ( cur_remainder >= cur_dividend - ( ( cur_divisor * 10 + udigit ) * udigit ) and cur_dividend - ( ( cur_divisor * 10 + udigit ) * udigit ) >= 0 ) : NEW_LINE INDENT cur_remainder = cur_dividend - ( ( cur_divisor * 10 + udigit ) * udigit ) NEW_LINE quotient_units_digit = udigit NEW_LINE DEDENT DEDENT cur_quotient = cur_quotient * 10 + quotient_units_digit NEW_LINE cur_divisor = cur_quotient * 2 NEW_LINE cur_dividend = cur_remainder NEW_LINE DEDENT return cur_quotient NEW_LINE DEDENT x = 1225 NEW_LINE print ( sqrtByLongDivision ( x ) ) NEW_LINE
Count of consecutive Fibonacci pairs in the given Array | Python3 implementation to count the consecutive fibonacci pairs in the array ; Function to find the previous fibonacci for the number N ; Function to find the next fibonacci number for the number N ; Function to check that a Number is a perfect square or not ; Function to check that a number is fibonacci number or not ; N is Fibinacci if one of ( 5 * n * n + 4 ) or ( 5 * n * n - 4 ) is a perferct square ; Function to count the fibonacci pairs in the array ; Loop to iterate over the array to choose all pairs of the array ; Condition to check if both the number of pair is a fibonacci number ; Condition to check if both the number form consecutive fibonacci numbers ; Driver Code
from math import sqrt NEW_LINE def previousFibonacci ( n ) : NEW_LINE INDENT a = n / ( ( 1 + sqrt ( 5 ) ) / 2.0 ) NEW_LINE return round ( a ) NEW_LINE DEDENT def nextFibonacci ( n ) : NEW_LINE INDENT a = n * ( 1 + sqrt ( 5 ) ) / 2.0 NEW_LINE return round ( a ) NEW_LINE DEDENT def isPerfectSquare ( x ) : NEW_LINE INDENT s = sqrt ( x ) NEW_LINE return ( s * s == x ) NEW_LINE DEDENT def isFibonacci ( n ) : NEW_LINE INDENT return ( isPerfectSquare ( 5 * n * n + 4 ) or isPerfectSquare ( 5 * n * n - 4 ) ) NEW_LINE DEDENT def countFibonacciPairs ( arr , n ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( isFibonacci ( arr [ i ] ) and isFibonacci ( arr [ j ] ) ) : NEW_LINE INDENT prevFib = previousFibonacci ( arr [ i ] ) NEW_LINE nextFib = nextFibonacci ( arr [ i ] ) NEW_LINE if ( prevFib == arr [ j ] or nextFib == arr [ j ] ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return res NEW_LINE DEDENT a = [ 3 , 5 , 8 , 11 ] NEW_LINE n = len ( a ) NEW_LINE print ( countFibonacciPairs ( a , n ) ) NEW_LINE
Count all distinct pairs with product equal to K | Function to count the number of pairs whose product is equal to K ; Pick all elements one by one ; Check if the product of this pair is equal to K ; Driver code
def countPairsWithProdK ( arr , n , k ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] * arr [ j ] == k ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 1 , 5 , 3 , 4 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE print ( countPairsWithProdK ( arr , N , K ) ) NEW_LINE
Count all distinct pairs with product equal to K | Python3 program to count the number of pairs whose product is equal to K ; Function to count the number of pairs whose product is equal to K ; Initialize the count ; Initialize empty hashmap . ; Insert array elements to hashmap ; Checking if the index is a whole number and present in the hashmap ; Driver code
MAX = 100000 ; NEW_LINE def countPairsWithProductK ( arr , n , k ) : NEW_LINE INDENT count = 0 ; NEW_LINE hashmap = [ False ] * MAX ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT hashmap [ arr [ i ] ] = True ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT x = arr [ i ] ; NEW_LINE index = 1.0 * k / arr [ i ] ; NEW_LINE if ( index >= 0 and ( ( index - int ( index ) ) == 0 ) and hashmap [ k // x ] ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT hashmap [ x ] = False ; NEW_LINE DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 5 , 3 , 4 , 2 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE K = 3 ; NEW_LINE print ( countPairsWithProductK ( arr , N , K ) ) ; NEW_LINE DEDENT
Find any K distinct odd integers such that their sum is equal to N | Function to find K odd integers such that their sum is N ; Condition to check if there exist such K integers ; Loop to find first K - 1 distinct odd integers ; Final Kth odd number ; Driver code
def oddIntegers ( n , k ) : NEW_LINE INDENT if ( n % 2 != k % 2 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE return ; NEW_LINE DEDENT sum = 0 ; NEW_LINE i = 1 ; NEW_LINE j = 1 ; NEW_LINE while ( j < k ) : NEW_LINE INDENT sum += i ; NEW_LINE print ( i , end = " ▁ " ) ; NEW_LINE i += 2 ; NEW_LINE j += 1 ; NEW_LINE DEDENT finalOdd = n - sum ; NEW_LINE print ( finalOdd ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 ; NEW_LINE k = 2 ; NEW_LINE oddIntegers ( n , k ) ; NEW_LINE DEDENT
Sum of product of proper divisors of all Numbers lying in range [ L , R ] | Python3 implementation to find the sum of the product of proper divisors of all the numbers lying in the range [ L , R ] ; Vector to store the product of the proper divisors of a number ; Variable to store the prefix sum of the product array ; Function to precompute the product of proper divisors of a number at it 's corresponding index ; Modificatino of sieve to store the product of the proper divisors ; Multiplying the existing value with i because i is the proper divisor of ans [ j ] ; Loop to store the prefix sum of the previously computed product array ; Computing the prefix sum ; Function to prthe sum for each query ; Function to prte sum of product of proper divisors of a number in [ L , R ] ; Calling the function that pre computes the sum of product of proper divisors ; Iterate over all Queries to prthe sum ; Driver code
mod = 1000000007 NEW_LINE ans = [ 1 ] * ( 100002 ) NEW_LINE pref = [ 0 ] * 100002 NEW_LINE def preCompute ( ) : NEW_LINE INDENT for i in range ( 2 , 100000 // 2 + 1 ) : NEW_LINE INDENT for j in range ( 2 * i , 100000 + 1 , i ) : NEW_LINE INDENT ans [ j ] = ( ans [ j ] * i ) % mod NEW_LINE DEDENT DEDENT for i in range ( 1 , 100002 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + ans [ i ] NEW_LINE pref [ i ] %= mod NEW_LINE DEDENT DEDENT def printSum ( L , R ) : NEW_LINE INDENT print ( pref [ R ] - pref [ L - 1 ] , end = " ▁ " ) NEW_LINE DEDENT def printSumProper ( arr , Q ) : NEW_LINE INDENT preCompute ( ) NEW_LINE for i in range ( Q ) : NEW_LINE INDENT printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Q = 2 NEW_LINE arr = [ [ 10 , 20 ] , [ 12 , 16 ] ] NEW_LINE printSumProper ( arr , Q ) NEW_LINE DEDENT
Generate an array of size N according to the given rules | Function to search the most recent location of element N If not present in the array it will return - 1 ; Function to generate an array of size N by following the given rules ; Loop to fill the array as per the given rules ; Check for the occurrence of arr [ i - 1 ] ; Driver code
def search ( a , k , x ) : NEW_LINE INDENT for j in range ( k - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( a [ j ] == x ) : NEW_LINE INDENT return j NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def genArray ( arr , N ) : NEW_LINE INDENT for i in range ( 0 , N - 1 , 1 ) : NEW_LINE INDENT if ( search ( arr , i , arr [ i ] ) == - 1 ) : NEW_LINE INDENT arr [ i + 1 ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i + 1 ] = ( i - search ( arr , i , arr [ i ] ) ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE size = N + 1 NEW_LINE a = [ 0 ] * N NEW_LINE genArray ( a , N ) NEW_LINE print ( a ) NEW_LINE DEDENT
Count of pairs in an Array whose sum is a Perfect Cube | Function to return an ArrayList containing all the perfect cubes upto n ; while current perfect cube is less than or equal to n ; Function to print the sum of maximum two elements from the array ; Function to return the count of numbers that when added with n give a perfect cube ; temp > n is checked so that pairs ( x , y ) and ( y , x ) don 't get counted twice ; Function to count the pairs whose sum is a perfect cube ; Sum of the maximum two elements from the array ; List of perfect cubes upto max ; Contains all the array elements ; Add count of the elements that when added with arr [ i ] give a perfect cube ; Driver code
def getPerfectcubes ( n ) : NEW_LINE INDENT perfectcubes = [ ] ; NEW_LINE current = 1 ; NEW_LINE i = 1 ; NEW_LINE while ( current <= n ) : NEW_LINE INDENT perfectcubes . append ( current ) ; NEW_LINE i += 1 ; NEW_LINE current = int ( pow ( i , 3 ) ) ; NEW_LINE DEDENT return perfectcubes ; NEW_LINE DEDENT def maxPairSum ( arr ) : NEW_LINE INDENT n = len ( arr ) ; NEW_LINE max = 0 ; NEW_LINE secondMax = 0 ; NEW_LINE if ( arr [ 0 ] > arr [ 1 ] ) : NEW_LINE INDENT max = arr [ 0 ] ; NEW_LINE secondMax = arr [ 1 ] ; NEW_LINE DEDENT else : NEW_LINE INDENT max = arr [ 1 ] ; NEW_LINE secondMax = arr [ 0 ] ; NEW_LINE DEDENT for i in range ( 2 , n ) : NEW_LINE INDENT if ( arr [ i ] > max ) : NEW_LINE INDENT secondMax = max ; NEW_LINE max = arr [ i ] ; NEW_LINE DEDENT elif ( arr [ i ] > secondMax ) : NEW_LINE INDENT secondMax = arr [ i ] ; NEW_LINE DEDENT DEDENT return ( max + secondMax ) ; NEW_LINE DEDENT def countPairsWith ( n , perfectcubes , nums ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( len ( perfectcubes ) ) : NEW_LINE INDENT temp = perfectcubes [ i ] - n ; NEW_LINE if ( temp > n and ( temp in nums ) ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT def countPairs ( arr ) : NEW_LINE INDENT n = len ( arr ) ; NEW_LINE max = maxPairSum ( arr ) ; NEW_LINE perfectcubes = getPerfectcubes ( max ) ; NEW_LINE nums = [ ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT nums . append ( arr [ i ] ) ; NEW_LINE DEDENT count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT count += countPairsWith ( arr [ i ] , perfectcubes , nums ) ; NEW_LINE DEDENT return count ; NEW_LINE DEDENT arr = [ 2 , 6 , 18 , 9 , 999 , 1 ] ; NEW_LINE print ( countPairs ( arr ) ) ; NEW_LINE
Maximum Squares possible parallel to both axes from N distinct points | Function that returns the count of squares parallel to both X and Y - axis from a given set of points ; Initialize result ; Initialize a set to store points ; Initialize a map to store the points in the same vertical line ; Store the points in a set ; Store the points in the same vertical line i . e . with same X co - ordinates ; Check for every two points in the same vertical line ; Check if other two point are present or not ; Driver Code
def countSquares ( X , Y , N ) : NEW_LINE INDENT count = 0 ; NEW_LINE points = [ ] ; NEW_LINE vertical = dict . fromkeys ( X , None ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT points . append ( ( X [ i ] , Y [ i ] ) ) ; NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if vertical [ X [ i ] ] is None : NEW_LINE INDENT vertical [ X [ i ] ] = [ Y [ i ] ] ; NEW_LINE DEDENT else : NEW_LINE INDENT vertical [ X [ i ] ] . append ( Y [ i ] ) ; NEW_LINE DEDENT DEDENT for line in vertical : NEW_LINE INDENT X1 = line ; NEW_LINE yList = vertical [ line ] ; NEW_LINE for i in range ( len ( yList ) ) : NEW_LINE INDENT Y1 = yList [ i ] ; NEW_LINE for j in range ( i + 1 , len ( yList ) ) : NEW_LINE INDENT Y2 = yList [ j ] ; NEW_LINE side = abs ( Y1 - Y2 ) ; NEW_LINE X2 = X1 + side ; NEW_LINE if ( X2 , Y1 ) in points and ( X2 , Y2 ) in points : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = [ 0 , 2 , 0 , 2 ] ; Y = [ 0 , 2 , 2 , 0 ] ; NEW_LINE N = len ( X ) ; NEW_LINE print ( countSquares ( X , Y , N ) ) ; NEW_LINE DEDENT
Number of perfect cubes between two given numbers | Function to count cubes between two numbers ; Traverse through all numbers ; Check if current number ' i ' is perfect cube ; Driver code
def countCubes ( a , b ) : NEW_LINE INDENT for i in range ( a , b + 1 ) : NEW_LINE INDENT for j in range ( i + 1 ) : NEW_LINE INDENT if j * j * j > i : NEW_LINE INDENT break NEW_LINE DEDENT if j * j * j == i : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 7 NEW_LINE b = 30 NEW_LINE print ( " Count ▁ of ▁ Cubes ▁ is ▁ " , countCubes ( a , b ) ) NEW_LINE DEDENT
Number of perfect cubes between two given numbers | An Efficient Method to count cubes between a and b ; Function to count cubes between two numbers ; Driver code
from math import * NEW_LINE def countCubes ( a , b ) : NEW_LINE INDENT return ( floor ( b ** ( 1. / 3. ) ) - ceil ( a ** ( 1. / 3. ) ) + 1 ) NEW_LINE DEDENT a = 7 NEW_LINE b = 28 NEW_LINE print ( " Count ▁ of ▁ cubes ▁ is " , countCubes ( a , b ) ) NEW_LINE
XOR and OR of all N | Function to check if a number is Armstrong or not ; Function to find XOR of all N - digits Armstrong number ; To store the XOR and OR of all Armstrong number ; Starting N - digit Armstrong number ; Ending N - digit Armstrong number ; Iterate over starting and ending number ; To check if i is Armstrong or not ; Print the XOR and OR of all Armstrong number ; Driver Code
def isArmstrong ( x , n ) : NEW_LINE INDENT sum1 = 0 NEW_LINE temp = x NEW_LINE while temp > 0 : NEW_LINE INDENT digit = temp % 10 NEW_LINE sum1 += digit ** n NEW_LINE temp //= 10 NEW_LINE DEDENT return sum1 == x NEW_LINE DEDENT def CalculateXORandOR ( n ) : NEW_LINE INDENT CalculateXOR = 0 NEW_LINE CalculateOR = 0 NEW_LINE start = 10 ** ( n - 1 ) NEW_LINE end = ( 10 ** n ) - 1 NEW_LINE for i in range ( start , end + 1 ) : NEW_LINE INDENT if ( isArmstrong ( i , n ) ) : NEW_LINE INDENT CalculateXOR = CalculateXOR ^ i NEW_LINE CalculateOR = CalculateOR | i NEW_LINE DEDENT DEDENT print ( " XOR ▁ = ▁ " , CalculateXOR ) NEW_LINE print ( " OR ▁ = ▁ " , CalculateOR ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 ; NEW_LINE CalculateXORandOR ( n ) ; NEW_LINE DEDENT
Perfect Cube | Function to check if a number is a perfect Cube or not ; Iterate from 1 - N ; Find the cube of every number ; Check if cube equals N or not ; Driver code ; Function call
def perfectCube ( N ) : NEW_LINE INDENT cube = 0 ; NEW_LINE for i in range ( N + 1 ) : NEW_LINE INDENT cube = i * i * i ; NEW_LINE if ( cube == N ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE return ; NEW_LINE DEDENT elif ( cube > N ) : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE return ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 216 ; NEW_LINE perfectCube ( N ) ; NEW_LINE DEDENT
Perfect Cube | Function to check if a number is a perfect Cube using inbuilt function ; If cube of cube_root is equals to N , then print Yes Else print No ; Driver 's code ; Function call to check N is cube or not
def perfectCube ( N ) : NEW_LINE INDENT cube_root = round ( N ** ( 1 / 3 ) ) ; NEW_LINE if cube_root * cube_root * cube_root == N : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE return ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE return ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 125 ; NEW_LINE perfectCube ( N ) ; NEW_LINE DEDENT
Check if given array can be made 0 with given operations performed any number of times | Function to whether the array can be made zero or not ; Count for even elements ; Count for odd elements ; Traverse the array to count the even and odd ; If arr [ i ] is odd ; If arr [ i ] is even ; Check if count of even is zero or count of odd is zero ; Driver 's Code
def check ( arr , N ) : NEW_LINE INDENT even = 0 ; NEW_LINE odd = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 1 ) : NEW_LINE INDENT odd += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT even += 1 ; NEW_LINE DEDENT DEDENT if ( even == N or odd == N ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 3 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE check ( arr , N ) ; NEW_LINE DEDENT
Calculate sum of all integers from 1 to N , excluding perfect power of 2 | Python 3 implementation of the approach ; Function to find the required summation ; Find the sum of first N integers using the formula ; Find the sum of numbers which are exact power of 2 by using the formula ; Print the final Sum ; Driver 's Code ; Function to find the sum
from math import log2 , pow NEW_LINE def findSum ( N ) : NEW_LINE INDENT sum = ( N ) * ( N + 1 ) // 2 NEW_LINE r = log2 ( N ) + 1 NEW_LINE expSum = pow ( 2 , r ) - 1 NEW_LINE print ( int ( sum - expSum ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE findSum ( N ) NEW_LINE DEDENT
Largest Even and Odd N | Function to print the largest N - digit even and odd numbers of base B ; Initialise the Number ; If Base B is even , then B ^ n will give largest Even number of N + 1 digit ; To get even number of N digit subtract 2 from B ^ n ; To get odd number of N digit subtract 1 from B ^ n ; If Base B is odd , then B ^ n will give largest Odd number of N + 1 digit ; To get even number of N digit subtract 1 from B ^ n ; To get odd number of N digit subtract 2 from B ^ n ; Driver 's Code ; Function to find the numbers
def findNumbers ( n , b ) : NEW_LINE INDENT even = 0 ; NEW_LINE odd = 0 ; NEW_LINE if ( b % 2 == 0 ) : NEW_LINE INDENT even = pow ( b , n ) - 2 ; NEW_LINE odd = pow ( b , n ) - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT even = pow ( b , n ) - 1 ; NEW_LINE odd = pow ( b , n ) - 2 ; NEW_LINE DEDENT print ( " Even ▁ Number ▁ = ▁ " , int ( even ) ) ; NEW_LINE print ( " Odd ▁ Number ▁ = ▁ " , int ( odd ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 ; NEW_LINE B = 5 ; NEW_LINE findNumbers ( N , B ) ; NEW_LINE DEDENT
Minimum divisor of a number to make the number perfect cube | Returns the minimum divisor ; Since 2 is only even prime , compute its power seprately . ; If count is not divisible by 3 , it must be removed by dividing n by prime number power . ; If count is not divisible by 3 , it must be removed by dividing n by prime number power . ; if n is a prime number ; Driver code
def findMinNumber ( n ) : NEW_LINE INDENT count = 0 ; NEW_LINE ans = 1 ; NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE n /= 2 ; NEW_LINE DEDENT if ( count % 3 != 0 ) : NEW_LINE INDENT ans *= pow ( 2 , ( count % 3 ) ) ; NEW_LINE DEDENT for i in range ( 3 , int ( pow ( n , 1 / 2 ) ) , 2 ) : NEW_LINE INDENT count = 0 ; NEW_LINE while ( n % i == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE n /= i ; NEW_LINE DEDENT if ( count % 3 != 0 ) : NEW_LINE INDENT ans *= pow ( i , ( count % 3 ) ) ; NEW_LINE DEDENT DEDENT if ( n > 2 ) : NEW_LINE INDENT ans *= n ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 128 ; NEW_LINE print ( findMinNumber ( n ) ) ; NEW_LINE DEDENT
Check whether the number can be made perfect square after adding K | Python3 program to check whether the number can be made perfect square after adding K ; Function to check whether the number can be made perfect square after adding K ; Computing the square root of the number ; Print Yes if the number is a perfect square ; Driver code
from math import sqrt NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT sr = int ( sqrt ( x ) ) ; NEW_LINE if ( sr * sr == x ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 7 ; k = 2 ; NEW_LINE isPerfectSquare ( n + k ) ; NEW_LINE DEDENT
Number of times the largest Perfect Cube can be subtracted from N | Python3 implementation of the approach ; Function to return the count of steps ; Variable to store the count of steps ; Iterate while N > 0 ; Get the largest perfect cube and subtract it from N ; Increment steps ; Return the required count ; Driver code
from math import floor NEW_LINE def countSteps ( n ) : NEW_LINE INDENT steps = 0 NEW_LINE while ( n ) : NEW_LINE INDENT largest = floor ( n ** ( 1 / 3 ) ) NEW_LINE n -= ( largest * largest * largest ) NEW_LINE steps += 1 NEW_LINE DEDENT return steps NEW_LINE DEDENT n = 150 NEW_LINE print ( countSteps ( n ) ) NEW_LINE
Product of all Subsets of a set formed by first N natural numbers | Function to find the product of all elements in all subsets in natural numbers from 1 to N ; Driver Code
def product ( N ) : NEW_LINE INDENT ans = 1 ; NEW_LINE val = 2 ** ( N - 1 ) ; NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT ans *= ( i ** val ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 ; NEW_LINE print ( product ( N ) ) ; NEW_LINE DEDENT
Radii of the three tangent circles of equal radius which are inscribed within a circle of given radius | Python3 program to find the radii of the three tangent circles of equal radius when the radius of the circumscribed circle is given ; Driver code
def threetangcircle ( R ) : NEW_LINE INDENT print ( " The ▁ radii ▁ of ▁ the ▁ tangent " , " circles ▁ is ▁ " , end = " " ) ; NEW_LINE print ( 0.4645 * R ) ; NEW_LINE DEDENT R = 4 ; NEW_LINE threetangcircle ( R ) ; NEW_LINE
Number of common tangents between two circles if their centers and radius is given | Python3 program to find the number of common tangents between the two circles ; Driver code
def circle ( x1 , y1 , x2 , y2 , r1 , r2 ) : NEW_LINE INDENT distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) NEW_LINE radSumSq = ( r1 + r2 ) * ( r1 + r2 ) NEW_LINE if ( distSq == radSumSq ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif ( distSq > radSumSq ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT x1 , y1 = - 10 , 8 ; NEW_LINE x2 , y2 = 14 , - 24 ; NEW_LINE r1 , r2 = 30 , 10 ; NEW_LINE t = circle ( x1 , y1 , x2 , y2 , r1 , r2 ) ; NEW_LINE if ( t == 1 ) : NEW_LINE INDENT print ( " There ▁ are ▁ 3 ▁ common ▁ tangents ▁ between ▁ the ▁ circles . " ) NEW_LINE DEDENT elif ( t < 0 ) : NEW_LINE INDENT print ( " There ▁ are ▁ 4 ▁ common ▁ tangents ▁ between ▁ the ▁ circles . " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " There ▁ are ▁ 2 ▁ common ▁ tangents ▁ between ▁ the ▁ circles . " ) NEW_LINE DEDENT
Ratio of the distance between the centers of the circles and the point of intersection of two transverse common tangents to the circles | Python3 program to find the ratio of the distance between the centres of the circles and the point of intersection of two transverse common tangents to the circles which do not touch each other ; Function to find the ratio ; Driver code
def GCD ( a , b ) : NEW_LINE INDENT if ( b != 0 ) : NEW_LINE INDENT return GCD ( b , a % b ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return a ; NEW_LINE DEDENT DEDENT def ratiotang ( r1 , r2 ) : NEW_LINE INDENT print ( " The ▁ ratio ▁ is " , r1 // GCD ( r1 , r2 ) , " : " , r2 // GCD ( r1 , r2 ) ) ; NEW_LINE DEDENT r1 = 4 ; r2 = 8 ; NEW_LINE ratiotang ( r1 , r2 ) ; NEW_LINE
Program to find the number of region in Planar Graph | Function to return the number of regions in a Planar Graph ; Driver code
def Regions ( Vertices , Edges ) : NEW_LINE INDENT R = Edges + 2 - Vertices ; NEW_LINE return R ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT V = 5 ; E = 7 ; NEW_LINE print ( Regions ( V , E ) ) ; NEW_LINE DEDENT
Ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles | Function to find the GCD ; Function to find the ratio ; Driver code
from math import gcd NEW_LINE def ratiotang ( r1 , r2 ) : NEW_LINE INDENT print ( " The ▁ ratio ▁ is " , int ( r1 / gcd ( r1 , r2 ) ) , " : " , int ( r2 / gcd ( r1 , r2 ) ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT r1 = 4 NEW_LINE r2 = 6 NEW_LINE ratiotang ( r1 , r2 ) NEW_LINE DEDENT
Length of the transverse common tangent between the two non intersecting circles | python 3 program to find the length of the transverse common tangent between two circles which do not touch each other ; Function to find the length of the transverse common tangent ; Driver code
from math import sqrt , pow NEW_LINE def lengthOfTangent ( r1 , r2 , d ) : NEW_LINE INDENT print ( " The ▁ length ▁ of ▁ the ▁ transverse " , " common ▁ tangent ▁ is " , ' { 0 : . 6g } ' . format ( sqrt ( pow ( d , 2 ) - pow ( ( r1 + r2 ) , 2 ) ) ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT r1 = 4 NEW_LINE r2 = 6 NEW_LINE d = 12 NEW_LINE lengthOfTangent ( r1 , r2 , d ) NEW_LINE DEDENT
Area of plot remaining at the end | Function to return the area of the remaining plot ; Continue while plot has positive area and there are persons left ; If length > breadth then subtract breadth from length ; Else subtract length from breadth ; Driver code
def remainingArea ( N , M , K ) : NEW_LINE INDENT while ( K > 0 and N > 0 and M > 0 ) : NEW_LINE INDENT if ( N > M ) : NEW_LINE INDENT N = N - M ; NEW_LINE DEDENT else : NEW_LINE INDENT M = M - N ; NEW_LINE DEDENT K = K - 1 ; NEW_LINE DEDENT if ( N > 0 and M > 0 ) : NEW_LINE INDENT return N * M ; NEW_LINE DEDENT else : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT N = 5 ; NEW_LINE M = 3 ; NEW_LINE K = 2 ; NEW_LINE print ( remainingArea ( N , M , K ) ) ; NEW_LINE
Length of the direct common tangent between two externally touching circles | Function to find the length of the direct common tangent ; Driver code
def lengtang ( r1 , r2 ) : NEW_LINE INDENT print ( " The ▁ length ▁ of ▁ the ▁ direct " , " common ▁ tangent ▁ is " , 2 * ( r1 * r2 ) ** ( 1 / 2 ) ) ; NEW_LINE DEDENT r1 = 5 ; r2 = 9 ; NEW_LINE lengtang ( r1 , r2 ) ; NEW_LINE
Shortest distance between a point and a circle | Function to find the shortest distance ; Driver code
def dist ( x1 , y1 , x2 , y2 , r ) : NEW_LINE INDENT print ( " The ▁ shortest ▁ distance ▁ between ▁ a ▁ point ▁ and ▁ a ▁ circle ▁ is ▁ " , ( ( ( ( x2 - x1 ) ** 2 ) + ( ( y2 - y1 ) ** 2 ) ) ** ( 1 / 2 ) ) - r ) ; NEW_LINE DEDENT x1 = 4 ; NEW_LINE y1 = 6 ; NEW_LINE x2 = 35 ; NEW_LINE y2 = 42 ; NEW_LINE r = 5 ; NEW_LINE dist ( x1 , y1 , x2 , y2 , r ) ; NEW_LINE
Distance between two parallel lines | Function to find the distance between parallel lines ; Driver Code
def dist ( m , b1 , b2 ) : NEW_LINE INDENT d = abs ( b2 - b1 ) / ( ( m * m ) - 1 ) ; NEW_LINE return d ; NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT m , b1 , b2 = 2 , 4 , 3 ; NEW_LINE print ( dist ( m , b1 , b2 ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
Length of the normal from origin on a straight line whose intercepts are given | Python3 implementation of the approach ; Function to find the normal of the straight line ; Length of the normal ; Driver code
import math ; NEW_LINE def normal ( m , n ) : NEW_LINE INDENT N = ( ( abs ( m ) * abs ( n ) ) / math . sqrt ( ( abs ( m ) * abs ( m ) ) + ( abs ( n ) * abs ( n ) ) ) ) ; NEW_LINE return N ; NEW_LINE DEDENT m = - 5 ; n = 3 ; NEW_LINE print ( normal ( m , n ) ) ; NEW_LINE
Check if it is possible to create a polygon with given n sides | Function to check whether it is possible to create a polygon with given sides length ; Sum stores the sum of all the sides and maxS stores the length of the largest side ; If the length of the largest side is less than the sum of the other remaining sides ; Driver code
def isPossible ( a , n ) : NEW_LINE INDENT sum = 0 NEW_LINE maxS = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE maxS = max ( a [ i ] , maxS ) NEW_LINE DEDENT if ( ( sum - maxS ) > maxS ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT a = [ 2 , 3 , 4 ] NEW_LINE n = len ( a ) NEW_LINE if ( isPossible ( a , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find the area of the shaded region formed by the intersection of four semicircles in a square | Function to return the area of the shaded region ; Area of the square ; Area of the semicircle ; There are 4 semicircles shadedArea = Area of 4 semicircles - Area of square ; Driver code
def findAreaShaded ( a ) : NEW_LINE INDENT sqArea = a * a ; NEW_LINE semiCircleArea = ( 3.14 * ( a * a ) / 8 ) NEW_LINE ShadedArea = 4 * semiCircleArea - sqArea ; NEW_LINE return ShadedArea ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 10 NEW_LINE print ( findAreaShaded ( a ) ) NEW_LINE DEDENT
Number of steps required to reach point ( x , y ) from ( 0 , 0 ) using zig | Function to return the required position ; Driver Code
def countSteps ( x , y ) : NEW_LINE INDENT if x < y : NEW_LINE INDENT return x + y + 2 * ( ( y - x ) // 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT return x + y + 2 * ( ( ( x - y ) + 1 ) // 2 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x , y = 4 , 3 NEW_LINE print ( countSteps ( x , y ) ) NEW_LINE DEDENT
Find whether only two parallel lines contain all coordinates points or not | Find if slope is good with only two intercept ; if set of lines have only two distinct intercept ; Function to check if required solution exist ; check the result by processing the slope by starting three points ; Driver code
def isSlopeGood ( slope , arr , n ) : NEW_LINE INDENT setOfLines = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT setOfLines [ arr [ i ] - slope * ( i ) ] = 1 NEW_LINE DEDENT return len ( setOfLines ) == 2 NEW_LINE DEDENT def checkForParallel ( arr , n ) : NEW_LINE INDENT slope1 = isSlopeGood ( arr [ 1 ] - arr [ 0 ] , arr , n ) NEW_LINE slope2 = isSlopeGood ( arr [ 2 ] - arr [ 1 ] , arr , n ) NEW_LINE slope3 = isSlopeGood ( ( arr [ 2 ] - arr [ 0 ] ) // 2 , arr , n ) NEW_LINE return ( slope1 or slope2 or slope3 ) NEW_LINE DEDENT arr = [ 1 , 6 , 3 , 8 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE if checkForParallel ( arr , n ) : NEW_LINE INDENT print ( 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT
Check whether the point ( x , y ) lies on a given line | Function that return true if the given point lies on the given line ; If ( x , y ) satisfies the equation of the line ; Driver code
def pointIsOnLine ( m , c , x , y ) : NEW_LINE INDENT if ( y == ( ( m * x ) + c ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT m = 3 ; c = 2 ; NEW_LINE x = 1 ; y = 5 ; NEW_LINE if ( pointIsOnLine ( m , c , x , y ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Find the coordinates of the fourth vertex of a rectangle with given 3 vertices | Function that return the coordinates of the fourth vertex of the rectangle ; Save the coordinates of the given vertices of the rectangle ; 1 - based indexing ; Driver code
def findFourthVertex ( n , m , s ) : NEW_LINE INDENT row = dict . fromkeys ( range ( n ) , 0 ) NEW_LINE col = dict . fromkeys ( range ( m ) , 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( s [ i ] [ j ] == ' * ' ) : NEW_LINE INDENT row [ i ] += 1 ; NEW_LINE col [ j ] += 1 ; NEW_LINE DEDENT DEDENT DEDENT for keys , values in row . items ( ) : NEW_LINE INDENT if ( values == 1 ) : NEW_LINE INDENT x = keys ; NEW_LINE DEDENT DEDENT for keys , values in col . items ( ) : NEW_LINE INDENT if ( values == 1 ) : NEW_LINE INDENT y = keys ; NEW_LINE DEDENT DEDENT return ( x + 1 , y + 1 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = [ " * . * " , " * . . " , " . . . " ] NEW_LINE n = len ( s ) ; NEW_LINE m = len ( s [ 0 ] ) ; NEW_LINE rs = findFourthVertex ( n , m , s ) ; NEW_LINE print ( rs [ 0 ] , rs [ 1 ] ) NEW_LINE DEDENT
Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse | Python3 Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within an ellipse ; Function to find the biggest reuleaux triangle ; length of the axes cannot be negative ; height of the reuleaux triangle ; area of the reuleaux triangle ; Driver code
import math ; NEW_LINE def Area ( a , b ) : NEW_LINE INDENT if ( a < 0 and b < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT h = math . sqrt ( ( ( pow ( a , 2 ) + pow ( b , 2 ) ) / ( pow ( a , 2 ) * pow ( b , 2 ) ) ) ) ; NEW_LINE A = 0.70477 * pow ( h , 2 ) ; NEW_LINE return A ; NEW_LINE DEDENT a = 5 ; NEW_LINE b = 4 ; NEW_LINE print ( round ( Area ( a , b ) , 7 ) ) ; NEW_LINE
Maximum given sized rectangles that can be cut out of a sheet of paper | Function to return the maximum rectangles possible ; Cut rectangles horizontally if possible ; One rectangle is a single cell ; Total rectangles = total cells ; Cut rectangles vertically if possible ; Return the maximum possible rectangles ; Driver code
def maxRectangles ( L , B , l , b ) : NEW_LINE INDENT horizontal , vertical = 0 , 0 NEW_LINE if l <= L and b <= B : NEW_LINE INDENT columns = B // b NEW_LINE rows = L // l NEW_LINE horizontal = rows * columns NEW_LINE DEDENT if l <= B and b <= L : NEW_LINE INDENT columns = L // b NEW_LINE rows = B // l NEW_LINE vertical = rows * columns NEW_LINE DEDENT return max ( horizontal , vertical ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L , B , l , b = 10 , 7 , 4 , 3 NEW_LINE print ( maxRectangles ( L , B , l , b ) ) NEW_LINE DEDENT
Largest right circular cone that can be inscribed within a sphere which is inscribed within a cube | Python3 Program to find the biggest right circular cone that can be inscribed within a right circular cone which in turn is inscribed within a cube ; Function to find the biggest right circular cone ; side cannot be negative ; radius of right circular cone ; height of right circular cone ; volume of right circular cone ; Driver code
import math NEW_LINE def cone ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT r = ( a * math . sqrt ( 2 ) ) / 3 ; NEW_LINE h = ( 2 * a ) / 3 ; NEW_LINE V = 3.14 * math . pow ( r , 2 ) * h ; NEW_LINE return V ; NEW_LINE DEDENT a = 5 ; NEW_LINE print ( cone ( a ) ) ; NEW_LINE
Biggest Reuleaux Triangle inscribed within a square which is inscribed within a hexagon | Python3 Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within a hexagon ; Function to find the biggest reuleaux triangle ; side cannot be negative ; height of the reuleaux triangle ; area of the reuleaux triangle ; Driver code
import math NEW_LINE def Area ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT h = 1.268 * a NEW_LINE A = 0.70477 * math . pow ( h , 2 ) NEW_LINE return A NEW_LINE DEDENT a = 5 NEW_LINE print ( Area ( a ) , end = " " ) NEW_LINE
Biggest Reuleaux Triangle inscirbed within a square inscribed in a semicircle | Python3 Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within a semicircle ; Function to find the biggest reuleaux triangle ; radius cannot be negative ; height of the reuleaux triangle ; area of the reuleaux triangle ; Driver code
import math as mt NEW_LINE def Area ( r ) : NEW_LINE INDENT if ( r < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT x = ( 2 * r ) / mt . sqrt ( 5 ) NEW_LINE A = 0.70477 * pow ( x , 2 ) NEW_LINE return A NEW_LINE DEDENT r = 5 NEW_LINE print ( Area ( r ) ) NEW_LINE
Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle | Python3 Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within an equilateral triangle ; Function to find the biggest reuleaux triangle ; side cannot be negative ; height of the reuleaux triangle ; area of the reuleaux triangle ; Driver code
import math as mt NEW_LINE def Area ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT x = 0.464 * a NEW_LINE A = 0.70477 * pow ( x , 2 ) NEW_LINE return A NEW_LINE DEDENT a = 5 NEW_LINE print ( Area ( a ) ) NEW_LINE
Program for Area Of Square after N | Function to calculate area of square after given number of folds ; Driver Code
def areaSquare ( side , fold ) : NEW_LINE INDENT area = side * side NEW_LINE ans = area / pow ( 2 , fold ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT side = 4 NEW_LINE fold = 2 NEW_LINE print ( areaSquare ( side , fold ) ) NEW_LINE DEDENT
Biggest Reuleaux Triangle within a Square which is inscribed within a Circle | Python3 Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within a circle ; Function to find the Area of the Reuleaux triangle ; radius cannot be negative ; Area of the Reuleaux triangle ; Driver code
import math as mt NEW_LINE def ReuleauxArea ( r ) : NEW_LINE INDENT if ( r < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT A = 0.70477 * 2 * pow ( r , 2 ) NEW_LINE return A NEW_LINE DEDENT r = 6 NEW_LINE print ( ReuleauxArea ( r ) ) NEW_LINE
Largest right circular cylinder that can be inscribed within a cone which is in turn inscribed within a cube | Python3 Program to find the biggest right circular cylinder that can be inscribed within a right circular cone which in turn is inscribed within a cube ; Function to find the biggest right circular cylinder ; side cannot be negative ; radius of right circular cylinder ; height of right circular cylinder ; volume of right circular cylinder ; Driver code
import math as mt NEW_LINE def cyl ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT r = ( 2 * a * mt . sqrt ( 2 ) ) / 3 NEW_LINE h = ( 2 * a ) / 3 NEW_LINE V = 3.14 * pow ( r , 2 ) * h NEW_LINE return V NEW_LINE DEDENT a = 5 NEW_LINE print ( cyl ( a ) ) NEW_LINE
Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle | Python3 Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within a circle ; Function to find the biggest reuleaux triangle ; the height or base or hypotenuse cannot be negative ; height of the reuleaux triangle ; area of the reuleaux triangle ; Driver code
import math as mt NEW_LINE def Area ( l , b , h ) : NEW_LINE INDENT if ( l < 0 or b < 0 or h < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT x = ( l * b ) / ( l + b ) NEW_LINE A = 0.70477 * pow ( x , 2 ) NEW_LINE return A NEW_LINE DEDENT l , b , h = 5 , 12 , 13 NEW_LINE print ( Area ( l , b , h ) ) NEW_LINE
Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle | Function to find the side of the square ; Side cannot be negative ; side of the square ; Driver code
def squareSide ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT x = 0.423 * a NEW_LINE return x NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 8 NEW_LINE print ( squareSide ( a ) ) NEW_LINE DEDENT
Check if it is possible to draw a straight line with the given direction cosines | Python3 implementation of the approach ; Function that returns true if a straight line is possible ; Driver code
from math import ceil , floor NEW_LINE def isPossible ( x , y , z ) : NEW_LINE INDENT a = x * x + y * y + z * z NEW_LINE a = round ( a , 8 ) NEW_LINE if ( ceil ( a ) == 1 & floor ( a ) == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 0.70710678 NEW_LINE m = 0.5 NEW_LINE n = 0.5 NEW_LINE if ( isPossible ( l , m , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Length of Diagonal of a n | Python3 Program to find the diagonal of a regular polygon with given side length ; Function to find the diagonal of a regular polygon ; Side and side length cannot be negative ; diagonal degree converted to radians ; Driver code
import math as mt NEW_LINE def polydiagonal ( n , a ) : NEW_LINE INDENT if ( a < 0 and n < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return ( 2 * a * mt . sin ( ( ( ( n - 2 ) * 180 ) / ( 2 * n ) ) * 3.14159 / 180 ) ) NEW_LINE DEDENT a , n = 9 , 10 NEW_LINE print ( polydiagonal ( n , a ) ) NEW_LINE
Diagonal of a Regular Decagon | Function to return the diagonal of a regular decagon ; Side cannot be negative ; Length of the diagonal ; Driver code
def decdiagonal ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT d = 1.902 * a NEW_LINE return d NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 9 NEW_LINE print ( decdiagonal ( a ) ) NEW_LINE DEDENT
Diagonal of a Regular Heptagon | Function to return the diagonal of a regular heptagon ; Side cannot be negative ; Length of the diagonal ; Driver code
def heptdiagonal ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT d = 1.802 * a NEW_LINE return round ( d , 3 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 6 NEW_LINE print ( heptdiagonal ( a ) ) NEW_LINE DEDENT
Diagonal of a Regular Hexagon | Function to find the diagonal of a regular hexagon ; Side cannot be negative ; Length of the diagonal ; Driver code
def hexDiagonal ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT d = 1.73 * a ; NEW_LINE return d ; NEW_LINE DEDENT a = 9 ; NEW_LINE print ( hexDiagonal ( a ) ) ; NEW_LINE
Area of Reuleaux Triangle | Python3 program to find the area of Reuleaux triangle ; function to the area of the Reuleaux triangle ; Side cannot be negative ; Area of the Reauleax triangle ; Driver code
import math as mt NEW_LINE def ReuleauxArea ( a ) : NEW_LINE INDENT if a < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return 0.70477 * pow ( a , 2 ) NEW_LINE DEDENT a = 6 NEW_LINE print ( ReuleauxArea ( a ) ) NEW_LINE
Largest Square that can be inscribed within a hexagon | Function to find the area of the square ; Side cannot be negative ; Area of the square ; Driver code
def squareArea ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT area = ( 1.268 ** 2 ) * ( a ** 2 ) ; NEW_LINE return area ; NEW_LINE DEDENT a = 6 ; NEW_LINE print ( squareArea ( a ) ) ; NEW_LINE
Volume of cube using its space diagonal | Python 3 program to find the volume occupied by Cube with given space diagonal ; Function to calculate Volume ; Formula to find Volume ; Drivers code ; space diagonal of Cube
from math import sqrt , pow NEW_LINE def CubeVolume ( d ) : NEW_LINE INDENT Volume = ( sqrt ( 3 ) * pow ( d , 3 ) ) / 9 NEW_LINE return Volume NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT d = 5 NEW_LINE print ( " Volume ▁ of ▁ Cube : " , ' { 0 : . 6 } ' . format ( CubeVolume ( d ) ) ) NEW_LINE DEDENT
Perimeter and Area of Varignon 's Parallelogram | Function to find the perimeter ; Function to find the area ; Driver code
def per ( a , b ) : NEW_LINE INDENT return ( a + b ) NEW_LINE DEDENT def area ( s ) : NEW_LINE INDENT return ( s / 2 ) NEW_LINE DEDENT a = 7 NEW_LINE b = 8 NEW_LINE s = 10 NEW_LINE print ( per ( a , b ) ) NEW_LINE print ( area ( s ) ) NEW_LINE