text
stringlengths
17
3.65k
code
stringlengths
70
5.84k
Program to find the profit or loss when CP of N items is equal to SP of M items | Function to calculate Profit or loss ; Driver Code
def profitLoss ( N , M ) : NEW_LINE INDENT if ( N == M ) : NEW_LINE INDENT print ( " No ▁ Profit ▁ nor ▁ Loss " ) NEW_LINE DEDENT else : NEW_LINE INDENT result = 0.0 NEW_LINE result = float ( abs ( N - M ) ) / M NEW_LINE if ( N - M < 0 ) : NEW_LINE INDENT print ( " Loss ▁ = ▁ - " , ' { 0 : . 6 } ' . format ( result * 100 ) , " % " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Profit ▁ = ▁ " , ' { 0 : . 6 } ' . format ( result * 100 ) , " % " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 NEW_LINE M = 9 NEW_LINE profitLoss ( N , M ) NEW_LINE DEDENT
Find all good indices in the given Array | Python3 program to find all good indices in the given array ; Function to find all good indices in the given array ; hash to store frequency of each element ; Storing frequency of each element and calculating sum simultaneously ; check if array is good after removing i - th index element ; print good indices ; Driver Code
from collections import defaultdict NEW_LINE def niceIndices ( A , n ) : NEW_LINE INDENT Sum = 0 NEW_LINE m = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ A [ i ] ] += 1 NEW_LINE Sum += A [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT k = Sum - A [ i ] NEW_LINE if k % 2 == 0 : NEW_LINE INDENT k = k >> 1 NEW_LINE if k in m : NEW_LINE INDENT if ( ( A [ i ] == k and m [ k ] > 1 ) or ( A [ i ] != k ) ) : NEW_LINE INDENT print ( ( i + 1 ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 8 , 3 , 5 , 2 ] NEW_LINE n = len ( A ) NEW_LINE niceIndices ( A , n ) NEW_LINE DEDENT
Count pieces of circle after N cuts | Function to find number of pieces of circle after N cuts ; Driver Code
def countPieces ( N ) : NEW_LINE INDENT return 2 * N NEW_LINE DEDENT N = 100 NEW_LINE print ( countPieces ( N ) ) NEW_LINE
Sum of all the multiples of 3 and 7 below N | Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of 3 and 7 below N ; Since , we need the sum of multiples less than N ; Driver code
def sumAP ( n , d ) : NEW_LINE INDENT n = int ( n / d ) ; NEW_LINE return ( n ) * ( 1 + n ) * ( d / 2 ) ; NEW_LINE DEDENT def sumMultiples ( n ) : NEW_LINE INDENT n -= 1 ; NEW_LINE return int ( sumAP ( n , 3 ) + sumAP ( n , 7 ) - sumAP ( n , 21 ) ) ; NEW_LINE DEDENT n = 24 ; NEW_LINE print ( sumMultiples ( n ) ) ; NEW_LINE
Check whether product of digits at even places is divisible by sum of digits at odd place of a number | Below function checks whether product of digits at even places is divisible by sum of digits at odd places ; if size is even ; if size is odd ; Driver code
def productSumDivisible ( n , size ) : NEW_LINE INDENT sum = 0 NEW_LINE product = 1 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT if ( size % 2 == 0 ) : NEW_LINE INDENT product *= n % 10 NEW_LINE DEDENT else : NEW_LINE INDENT sum += n % 10 NEW_LINE DEDENT n = n // 10 NEW_LINE size -= 1 NEW_LINE DEDENT if ( product % sum == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1234 NEW_LINE len = 4 NEW_LINE if ( productSumDivisible ( n , len ) ) : NEW_LINE INDENT print ( " TRUE " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " FALSE " ) NEW_LINE DEDENT DEDENT
Check whether product of digits at even places is divisible by sum of digits at odd place of a number | Below function checks whether product of digits at even places is divisible by sum of digits at odd places ; Converting integer to string ; Traveersing the string ; Driver code
def productSumDivisible ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE product = 1 NEW_LINE num = str ( n ) NEW_LINE for i in range ( len ( num ) ) : NEW_LINE INDENT if ( i % 2 != 0 ) : NEW_LINE INDENT product = product * int ( num [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT sum = sum + int ( num [ i ] ) NEW_LINE DEDENT DEDENT if ( product % sum == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1234 NEW_LINE if ( productSumDivisible ( n ) ) : NEW_LINE INDENT print ( " TRUE " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " FALSE " ) NEW_LINE DEDENT DEDENT
GCD of a number raised to some power and another number | Python 3 program to find GCD of a ^ n and b . ; Returns GCD of a ^ n and b . ; Driver code
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT def powGCD ( a , n , b ) : NEW_LINE INDENT for i in range ( 0 , n + 1 , 1 ) : NEW_LINE INDENT a = a * a NEW_LINE DEDENT return gcd ( a , b ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 10 NEW_LINE b = 5 NEW_LINE n = 2 NEW_LINE print ( powGCD ( a , n , b ) ) NEW_LINE DEDENT
Position after taking N steps to the right and left in an alternate manner | Function to return the last destination ; Driver Code
def lastCoordinate ( n , a , b ) : NEW_LINE INDENT return ( ( ( n + 1 ) // 2 ) * a - ( n // 2 ) * b ) NEW_LINE DEDENT n = 3 NEW_LINE a = 5 NEW_LINE b = 2 NEW_LINE print ( lastCoordinate ( n , a , b ) ) NEW_LINE
Smallest number greater than or equal to N divisible by K | Function to find the smallest number greater than or equal to N that is divisible by k ; Driver Code
def findNum ( N , K ) : NEW_LINE INDENT rem = ( N + K ) % K ; NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT else : NEW_LINE INDENT return ( N + K - rem ) NEW_LINE DEDENT DEDENT N = 45 NEW_LINE K = 6 NEW_LINE print ( ' Smallest ▁ number ▁ greater ▁ than ' , ' or ▁ equal ▁ to ' , N , ' that ▁ is ▁ divisible ▁ by ' , K , ' is ' , findNum ( 45 , 6 ) ) NEW_LINE
Sum and Product of digits in a number that divide the number | Print the sum and product of digits that divides the number . ; Fetching each digit of the number ; Checking if digit is greater than 0 and can divides n . ; Driver code
def countDigit ( n ) : NEW_LINE INDENT temp = n NEW_LINE sum = 0 NEW_LINE product = 1 NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT d = temp % 10 NEW_LINE temp //= 10 NEW_LINE if ( d > 0 and n % d == 0 ) : NEW_LINE INDENT sum += d NEW_LINE product *= d NEW_LINE DEDENT DEDENT print ( " Sum ▁ = " , sum ) NEW_LINE print ( " Product ▁ = " , product ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 1012 NEW_LINE countDigit ( n ) NEW_LINE DEDENT
Largest number smaller than or equal to N divisible by K | Function to find the largest number smaller than or equal to N that is divisible by k ; Driver code
def findNum ( N , K ) : NEW_LINE INDENT rem = N % K NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT else : NEW_LINE INDENT return N - rem NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 45 NEW_LINE K = 6 NEW_LINE print ( " Largest ▁ number ▁ smaller ▁ than ▁ or ▁ equal ▁ to " + str ( N ) + " that ▁ is ▁ divisible ▁ by " + str ( K ) + " is " , findNum ( N , K ) ) NEW_LINE DEDENT
Check if any permutation of a number is divisible by 3 and is Palindromic | Function to check if any permutation of a number is divisible by 3 and is Palindromic ; Hash array to store frequency of digits of n ; traverse the digits of integer and store their frequency ; Calculate the sum of digits simultaneously ; Check if number is not divisible by 3 ; If more than one digits have odd frequency , palindromic permutation not possible ; Driver Code
def isDivisiblePalindrome ( n ) : NEW_LINE INDENT hash = [ 0 ] * 10 NEW_LINE digitSum = 0 NEW_LINE while ( n ) : NEW_LINE INDENT digitSum += n % 10 NEW_LINE hash [ n % 10 ] += 1 NEW_LINE n //= 10 NEW_LINE DEDENT if ( digitSum % 3 != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT oddCount = 0 NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT if ( hash [ i ] % 2 != 0 ) : NEW_LINE INDENT oddCount += 1 NEW_LINE DEDENT DEDENT if ( oddCount > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT n = 34734 NEW_LINE if ( isDivisiblePalindrome ( n ) ) : NEW_LINE INDENT print ( " True " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " ) NEW_LINE DEDENT
Minimum time to return array to its original state after given modifications | Python implementation of above approach ; Function to return lcm of two numbers ; Make a graph ; Add edge ; Count reachable nodes from every node . ; Driver code
import math NEW_LINE def lcm ( a , b ) : NEW_LINE INDENT return ( a * b ) // ( math . gcd ( a , b ) ) NEW_LINE DEDENT def dfs ( src , adj , visited ) : NEW_LINE INDENT visited [ src ] = True NEW_LINE count = 1 NEW_LINE if adj [ src ] != 0 : NEW_LINE INDENT for i in range ( len ( adj [ src ] ) ) : NEW_LINE INDENT if ( not visited [ adj [ src ] [ i ] ] ) : NEW_LINE INDENT count += dfs ( adj [ src ] [ i ] , adj , visited ) NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT def findMinTime ( arr , P , n ) : NEW_LINE INDENT adj = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if adj [ arr [ i ] ] == 0 : NEW_LINE INDENT adj [ arr [ i ] ] = [ ] NEW_LINE DEDENT adj [ arr [ i ] ] . append ( P [ i ] ) NEW_LINE DEDENT visited = [ 0 ] * ( n + 1 ) NEW_LINE ans = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( not visited [ i ] ) : NEW_LINE INDENT ans = lcm ( ans , dfs ( i , adj , visited ) ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 2 , 3 ] NEW_LINE P = [ 3 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMinTime ( arr , P , n ) ) NEW_LINE
Check whether product of digits at even places of a number is divisible by K | below function checks whether product of digits at even places is divisible by K ; if position is even ; Driver code
def productDivisible ( n , k ) : NEW_LINE INDENT product = 1 NEW_LINE position = 1 NEW_LINE while n > 0 : NEW_LINE INDENT if position % 2 == 0 : NEW_LINE INDENT product *= n % 10 NEW_LINE DEDENT n = n / 10 NEW_LINE position += 1 NEW_LINE DEDENT if product % k == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT n = 321922 NEW_LINE k = 3 NEW_LINE if productDivisible ( n , k ) == True : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Check whether product of digits at even places of a number is divisible by K | Function checks whether product of digits at even places is divisible by K ; Converting integer to string ; Traversing the string ; Driver code
def productDivisible ( n , k ) : NEW_LINE INDENT product = 1 NEW_LINE num = str ( n ) NEW_LINE for i in range ( len ( num ) ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT product = product * int ( num [ i ] ) NEW_LINE DEDENT DEDENT if product % k == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT n = 321922 NEW_LINE k = 3 NEW_LINE if productDivisible ( n , k ) == True : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Permutations of n things taken r at a time with k things together | def to find factorial of a number ; def to calculate p ( n , r ) ; def to find the number of permutations of n different things taken r at a time with k things grouped together ; Driver code
def factorial ( n ) : NEW_LINE INDENT fact = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT fact = fact * i ; NEW_LINE DEDENT return fact ; NEW_LINE DEDENT def npr ( n , r ) : NEW_LINE INDENT pnr = factorial ( n ) / factorial ( n - r ) ; NEW_LINE return pnr ; NEW_LINE DEDENT def countPermutations ( n , r , k ) : NEW_LINE INDENT return int ( factorial ( k ) * ( r - k + 1 ) * npr ( n - k , r - k ) ) ; NEW_LINE DEDENT n = 8 ; NEW_LINE r = 5 ; NEW_LINE k = 2 ; NEW_LINE print ( countPermutations ( n , r , k ) ) ; NEW_LINE
Greatest Integer Function | Python3 program to illustrate greatest integer Function ; Function to calculate the GIF value of a number ; GIF is the floor of a number ; Driver code
import math NEW_LINE def GIF ( n ) : NEW_LINE INDENT return int ( math . floor ( n ) ) ; NEW_LINE DEDENT n = 2.3 ; NEW_LINE print ( GIF ( n ) ) ; NEW_LINE
Number of triangles formed from a set of points on three lines | Returns factorial of a number ; calculate c ( n , r ) ; Driver code
def factorial ( n ) : NEW_LINE INDENT fact = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT fact = fact * i NEW_LINE DEDENT return fact NEW_LINE DEDENT def ncr ( n , r ) : NEW_LINE INDENT return ( factorial ( n ) // ( factorial ( r ) * factorial ( n - r ) ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT m = 3 NEW_LINE n = 4 NEW_LINE k = 5 NEW_LINE totalTriangles = ( ncr ( m + n + k , 3 ) - ncr ( m , 3 ) - ncr ( n , 3 ) - ncr ( k , 3 ) ) NEW_LINE print ( totalTriangles ) NEW_LINE DEDENT
Check whether sum of digits at odd places of a number is divisible by K | function that checks the divisibility of the sum of the digits at odd places of the given number ; if position is odd ; Driver code
def SumDivisible ( n , k ) : NEW_LINE INDENT sum = 0 NEW_LINE position = 1 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT if ( position % 2 == 1 ) : NEW_LINE INDENT sum += n % 10 NEW_LINE DEDENT n = n // 10 NEW_LINE position += 1 NEW_LINE DEDENT if ( sum % k == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 592452 NEW_LINE k = 3 NEW_LINE if ( SumDivisible ( n , k ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Check whether sum of digits at odd places of a number is divisible by K | Python3 implementation of the above approach ; Converting integer to string ; Traversing the string ; Driver code
def sumDivisible ( n , k ) : NEW_LINE INDENT sum = 0 NEW_LINE num = str ( n ) NEW_LINE for i in range ( len ( num ) ) : NEW_LINE INDENT if ( i % 2 != 0 ) : NEW_LINE INDENT sum = sum + int ( num [ i ] ) NEW_LINE DEDENT DEDENT if sum % k == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT n = 592452 NEW_LINE k = 3 NEW_LINE if sumDivisible ( n , k ) == True : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Check if a triangle of positive area is possible with the given angles | Python program to check if a triangle of positive area is possible with the given angles ; Checking if the sum of three angles is 180 and none of the angles is zero ; Checking if sum of any two angles is greater than equal to the third one ; Driver Code
def isTriangleExists ( a , b , c ) : NEW_LINE INDENT if ( a != 0 and b != 0 and c != 0 and ( a + b + c ) == 180 ) : NEW_LINE INDENT if ( ( a + b ) >= c or ( b + c ) >= a or ( a + c ) >= b ) : NEW_LINE INDENT return " YES " NEW_LINE DEDENT else : NEW_LINE INDENT return " NO " NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return " NO " NEW_LINE DEDENT DEDENT a , b , c = 50 , 60 , 70 NEW_LINE print ( isTriangleExists ( 50 , 60 , 70 ) ) NEW_LINE
Find maximum value of x such that n ! % ( k ^ x ) = 0 | Python 3 program to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Function to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Find square root of k and add 1 to it ; Run the loop from 2 to m and k should be greater than 1 ; optimize the value of k ; Minimum store ; Driver Code
import math NEW_LINE def findX ( n , k ) : NEW_LINE INDENT r = n NEW_LINE m = int ( math . sqrt ( k ) ) + 1 NEW_LINE i = 2 NEW_LINE while i <= m and k > 1 : NEW_LINE INDENT if ( i == m ) : NEW_LINE INDENT i = k NEW_LINE DEDENT u = 0 NEW_LINE v = 0 NEW_LINE while k % i == 0 : NEW_LINE INDENT k //= i NEW_LINE v += 1 NEW_LINE DEDENT if ( v > 0 ) : NEW_LINE INDENT t = n NEW_LINE while ( t > 0 ) : NEW_LINE INDENT t //= i NEW_LINE u += t NEW_LINE DEDENT r = min ( r , u // v ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return r NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE k = 2 NEW_LINE print ( findX ( n , k ) ) NEW_LINE DEDENT
Ways of selecting men and women from a group to make a team | Returns factorial of the number ; Function to calculate ncr ; Function to calculate the total possible ways ; Driver code
def fact ( n ) : NEW_LINE INDENT fact = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT fact *= i NEW_LINE DEDENT return fact NEW_LINE DEDENT def ncr ( n , r ) : NEW_LINE INDENT ncr = fact ( n ) // ( fact ( r ) * fact ( n - r ) ) NEW_LINE return ncr NEW_LINE DEDENT def ways ( m , w , n , k ) : NEW_LINE INDENT ans = 0 NEW_LINE while ( m >= k ) : NEW_LINE INDENT ans += ncr ( m , k ) * ncr ( w , n - k ) NEW_LINE k += 1 NEW_LINE DEDENT return ans ; NEW_LINE DEDENT m = 7 NEW_LINE w = 6 NEW_LINE n = 5 NEW_LINE k = 3 NEW_LINE print ( ways ( m , w , n , k ) ) NEW_LINE
Product of every K ’ th prime number in an array | Python 3 implementation of the approach ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; compute the answer ; count of primes ; product of the primes ; traverse the array ; if the number is a prime ; increase the count ; if it is the K 'th prime ; Driver code ; create the sieve
MAX = 1000000 NEW_LINE prime = [ True ] * ( MAX + 1 ) NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT prime [ 1 ] = False ; NEW_LINE prime [ 0 ] = False ; NEW_LINE p = 2 NEW_LINE while p * p <= MAX : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , MAX + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def productOfKthPrimes ( arr , n , k ) : NEW_LINE INDENT c = 0 NEW_LINE product = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ arr [ i ] ] ) : NEW_LINE INDENT c += 1 NEW_LINE if ( c % k == 0 ) : NEW_LINE INDENT product *= arr [ i ] NEW_LINE c = 0 NEW_LINE DEDENT DEDENT DEDENT print ( product ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT SieveOfEratosthenes ( ) NEW_LINE n = 5 NEW_LINE k = 2 NEW_LINE arr = [ 2 , 3 , 5 , 7 , 11 ] NEW_LINE productOfKthPrimes ( arr , n , k ) NEW_LINE DEDENT
Sum of greatest odd divisor of numbers in given range | Function to return sum of first n odd numbers ; Recursive function to return sum of greatest odd divisor of numbers in range [ 1 , n ] ; Odd n ; Even n ; Function to return sum of greatest odd divisor of numbers in range [ a , b ] ; Driver code
def square ( n ) : NEW_LINE INDENT return n * n ; NEW_LINE DEDENT def sum ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( n % 2 == 1 ) : NEW_LINE INDENT return ( square ( int ( ( n + 1 ) / 2 ) ) + sum ( int ( n / 2 ) ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( square ( int ( n / 2 ) ) + sum ( int ( n / 2 ) ) ) ; NEW_LINE DEDENT DEDENT def oddDivSum ( a , b ) : NEW_LINE INDENT return sum ( b ) - sum ( a - 1 ) ; NEW_LINE DEDENT a , b = 3 , 9 ; NEW_LINE print ( oddDivSum ( a , b ) ) ; NEW_LINE
Minimum numbers needed to express every integer below N as a sum | function to count length of binary expression of n ; Driver code
def countBits ( n ) : NEW_LINE INDENT count = 0 ; NEW_LINE while ( n ) : NEW_LINE INDENT count += 1 ; NEW_LINE n >>= 1 ; NEW_LINE DEDENT return count ; NEW_LINE DEDENT n = 32 ; NEW_LINE print ( " Minimum ▁ value ▁ of ▁ K ▁ is ▁ = " , countBits ( n ) ) ; NEW_LINE
Check if a number is an Achilles number or not | Program to check if the given number is an Achilles Number ; function to check if the number is powerful number ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; if n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Utility function to check if number is a perfect power or not ; Function to check Achilles Number ; Driver Code
from math import sqrt , log NEW_LINE def isPowerful ( n ) : NEW_LINE INDENT while ( n % 2 == 0 ) : NEW_LINE INDENT power = 0 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT n /= 2 NEW_LINE power += 1 NEW_LINE DEDENT if ( power == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT p = int ( sqrt ( n ) ) + 1 NEW_LINE for factor in range ( 3 , p , 2 ) : NEW_LINE INDENT power = 0 NEW_LINE while ( n % factor == 0 ) : NEW_LINE INDENT n = n / factor NEW_LINE power += 1 NEW_LINE DEDENT if ( power == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return ( n == 1 ) NEW_LINE DEDENT def isPower ( a ) : NEW_LINE INDENT if ( a == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT p = int ( sqrt ( a ) ) + 1 NEW_LINE for i in range ( 2 , a , 1 ) : NEW_LINE INDENT val = log ( a ) / log ( i ) NEW_LINE if ( ( val - int ( val ) ) < 0.00000001 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def isAchillesNumber ( n ) : NEW_LINE INDENT if ( isPowerful ( n ) == True and isPower ( n ) == False ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 72 NEW_LINE if ( isAchillesNumber ( n ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT n = 36 NEW_LINE if ( isAchillesNumber ( n ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Program to find count of numbers having odd number of divisors in given range | Function to count numbers having odd number of divisors in range [ A , B ] ; variable to odd divisor count ; iterate from a to b and count their number of divisors ; variable to divisor count ; if count of divisor is odd then increase res by 1 ; Driver code
def OddDivCount ( a , b ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( a , b + 1 ) : NEW_LINE INDENT divCount = 0 NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT if ( i % j == 0 ) : NEW_LINE INDENT divCount += 1 NEW_LINE DEDENT DEDENT if ( divCount % 2 ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 1 NEW_LINE b = 10 NEW_LINE print ( OddDivCount ( a , b ) ) NEW_LINE DEDENT
Largest factor of a given number which is a perfect square | Python 3 program to find the largest factor of a number which is also a perfect square ; Function to find the largest factor of a given number which is a perfect square ; Initialise the answer to 1 ; Finding the prime factors till sqrt ( num ) ; Frequency of the prime factor in the factorisation initialised to 0 ; If the frequency is odd then multiply i frequency - 1 times to the answer ; Else if it is even , multiply it frequency times ; Driver Code
import math NEW_LINE def largestSquareFactor ( num ) : NEW_LINE INDENT answer = 1 NEW_LINE for i in range ( 2 , int ( math . sqrt ( num ) ) ) : NEW_LINE INDENT cnt = 0 NEW_LINE j = i NEW_LINE while ( num % j == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE j *= i NEW_LINE DEDENT if ( cnt & 1 ) : NEW_LINE INDENT cnt -= 1 NEW_LINE answer *= pow ( i , cnt ) NEW_LINE DEDENT else : NEW_LINE INDENT answer *= pow ( i , cnt ) NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 420 NEW_LINE print ( largestSquareFactor ( N ) ) NEW_LINE DEDENT
Find the Nth term of the series 2 + 6 + 13 + 23 + . . . | calculate Nth term of given series ; Driver code
def Nth_Term ( n ) : NEW_LINE INDENT return ( 3 * pow ( n , 2 ) - n + 2 ) // ( 2 ) NEW_LINE DEDENT N = 5 NEW_LINE print ( Nth_Term ( N ) ) NEW_LINE
All possible numbers of N digits and base B without leading zeros | function to count all permutations ; count of all permutations ; count of permutations with leading zeros ; Return the permutations without leading zeros ; Driver code
def countPermutations ( N , B ) : NEW_LINE INDENT x = B ** N NEW_LINE y = B ** ( N - 1 ) NEW_LINE print ( x - y ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , B = 6 , 4 NEW_LINE countPermutations ( N , B ) NEW_LINE DEDENT
Absolute difference between the Product of Non | Function to find the difference between the product of non - primes and the product of primes of an array . ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store the product of primes in P1 and the product of non primes in P2 ; the number is prime ; the number is non - prime ; Return the absolute difference ; Driver Code ; Find the absolute difference
def calculateDifference ( arr , n ) : NEW_LINE INDENT max_val = max ( arr ) NEW_LINE prime = ( max_val + 1 ) * [ True ] NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE p = 2 NEW_LINE while p * p <= max_val : NEW_LINE INDENT if prime [ p ] == True : NEW_LINE INDENT for i in range ( p * 2 , max_val + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT P1 = 1 ; P2 = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if prime [ arr [ i ] ] : NEW_LINE INDENT P1 *= arr [ i ] NEW_LINE DEDENT elif arr [ i ] != 1 : NEW_LINE INDENT P2 *= arr [ i ] NEW_LINE DEDENT DEDENT return abs ( P2 - P1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 5 , 10 , 15 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( calculateDifference ( arr , n ) ) NEW_LINE DEDENT
Maximum count of equal numbers in an array after performing given operations | Function to find the maximum number of equal numbers in an array ; to store sum of elements ; if sum of numbers is not divisible by n ; Driver Code ; size of an array
def EqualNumbers ( a , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += a [ i ] ; NEW_LINE DEDENT if ( sum % n ) : NEW_LINE INDENT return n - 1 ; NEW_LINE DEDENT return n ; NEW_LINE DEDENT a = [ 1 , 4 , 1 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( EqualNumbers ( a , n ) ) ; NEW_LINE
Count number of ordered pairs with Even and Odd Sums | function to count odd sum pair ; if number is even ; if number is odd ; count of ordered pairs ; function to count even sum pair ; Driver code
def count_odd_pair ( n , a ) : NEW_LINE INDENT odd = 0 NEW_LINE even = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( a [ i ] % 2 == 0 ) : NEW_LINE INDENT even = even + 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd = odd + 1 NEW_LINE DEDENT DEDENT ans = odd * even * 2 NEW_LINE return ans NEW_LINE DEDENT def count_even_pair ( odd_sum_pairs , n ) : NEW_LINE INDENT total_pairs = ( n * ( n - 1 ) ) NEW_LINE ans = total_pairs - odd_sum_pairs NEW_LINE return ans NEW_LINE DEDENT n = 6 NEW_LINE a = [ 2 , 4 , 5 , 9 , 1 , 8 ] NEW_LINE odd_sum_pairs = count_odd_pair ( n , a ) NEW_LINE even_sum_pairs = count_even_pair ( odd_sum_pairs , n ) NEW_LINE print ( " Even ▁ Sum ▁ Pairs ▁ = " , even_sum_pairs ) NEW_LINE print ( " Odd ▁ Sum ▁ Pairs = " , odd_sum_pairs ) NEW_LINE
Steps required to visit M points in order on a circular ring of N points | Function to count the steps required ; Start at 1 ; Initialize steps ; If nxt is greater than cur ; Now we are at a [ i ] ; Driver code
def findSteps ( n , m , a ) : NEW_LINE INDENT cur = 1 NEW_LINE steps = 0 NEW_LINE for i in range ( 0 , m ) : NEW_LINE INDENT if ( a [ i ] >= cur ) : NEW_LINE INDENT steps += ( a [ i ] - cur ) NEW_LINE DEDENT else : NEW_LINE INDENT steps += ( n - cur + a [ i ] ) NEW_LINE DEDENT cur = a [ i ] NEW_LINE DEDENT return steps NEW_LINE DEDENT n = 3 NEW_LINE m = 3 NEW_LINE a = [ 2 , 1 , 2 ] NEW_LINE print ( findSteps ( n , m , a ) ) NEW_LINE
Program to Convert Hexadecimal Number to Binary | Function to convert Hexadecimal to Binary Number ; Driver code ; Get the Hexadecimal number ; Convert HexaDecimal to Binary
def HexToBin ( hexdec ) : NEW_LINE INDENT for i in hexdec : NEW_LINE INDENT if i == '0' : NEW_LINE INDENT print ( '0000' , end = ' ' ) NEW_LINE DEDENT elif i == '1' : NEW_LINE INDENT print ( '0001' , end = ' ' ) NEW_LINE DEDENT elif i == '2' : NEW_LINE INDENT print ( '0010' , end = ' ' ) NEW_LINE DEDENT elif i == '3' : NEW_LINE INDENT print ( '0011' , end = ' ' ) NEW_LINE DEDENT elif i == '4' : NEW_LINE INDENT print ( '0100' , end = ' ' ) NEW_LINE DEDENT elif i == '5' : NEW_LINE INDENT print ( '0101' , end = ' ' ) NEW_LINE DEDENT elif i == '6' : NEW_LINE INDENT print ( '0110' , end = ' ' ) NEW_LINE DEDENT elif i == '7' : NEW_LINE INDENT print ( '0111' , end = ' ' ) NEW_LINE DEDENT elif i == '8' : NEW_LINE INDENT print ( '1000' , end = ' ' ) NEW_LINE DEDENT elif i == '9' : NEW_LINE INDENT print ( '1001' , end = ' ' ) NEW_LINE DEDENT elif i == ' A ' or i == ' a ' : NEW_LINE INDENT print ( '1010' , end = ' ' ) NEW_LINE DEDENT elif i == ' B ' or i == ' b ' : NEW_LINE INDENT print ( '1011' , end = ' ' ) NEW_LINE DEDENT elif i == ' C ' or i == ' c ' : NEW_LINE INDENT print ( '1100' , end = ' ' ) NEW_LINE DEDENT elif i == ' D ' or i == ' d ' : NEW_LINE INDENT print ( '1101' , end = ' ' ) NEW_LINE DEDENT elif i == ' E ' or i == ' e ' : NEW_LINE INDENT print ( '1110' , end = ' ' ) NEW_LINE DEDENT elif i == ' F ' or i == ' f ' : NEW_LINE INDENT print ( '1111' , end = ' ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Invalid hexadecimal digit " + str ( hexdec [ i ] ) , end = ' ' ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT hexdec = "1AC5" ; NEW_LINE print ( " Equivalent ▁ Binary ▁ value ▁ is ▁ : ▁ " , end = ' ' ) NEW_LINE HexToBin ( hexdec ) NEW_LINE DEDENT
Count unordered pairs ( i , j ) such that product of a [ i ] and a [ j ] is power of two | Function to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; Function to Count unordered pairs ; is a number can be expressed as power of two ; count total number of unordered pairs ; Driver code
def isPowerOfTwo ( x ) : NEW_LINE INDENT return ( x and ( not ( x & ( x - 1 ) ) ) ) NEW_LINE DEDENT def Count_pairs ( a , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if isPowerOfTwo ( a [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT ans = ( count * ( count - 1 ) ) / 2 NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 5 , 8 , 16 , 128 ] NEW_LINE n = len ( a ) NEW_LINE Count_pairs ( a , n ) NEW_LINE DEDENT
Ways of dividing a group into two halves such that two elements are in different groups | This function will return the factorial of a given number ; This function will calculate nCr of given n and r ; This function will Calculate number of ways ; Driver code
def factorial ( n ) : NEW_LINE INDENT result = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT result *= i NEW_LINE DEDENT return result NEW_LINE DEDENT def nCr ( n , r ) : NEW_LINE INDENT return ( factorial ( n ) // ( factorial ( r ) * factorial ( n - r ) ) ) NEW_LINE DEDENT def calculate_result ( n ) : NEW_LINE INDENT result = 2 * nCr ( ( n - 2 ) , ( n // 2 - 1 ) ) NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b = 2 , 4 NEW_LINE print ( calculate_result ( 2 * a ) ) NEW_LINE print ( calculate_result ( 2 * b ) ) NEW_LINE DEDENT
Print values of ' a ' in equation ( a + b ) <= n and a + b is divisible by x | function to Find values of a , in equation ( a + b ) <= n and a + b is divisible by x . ; least possible which is divisible by x ; run a loop to get required answer ; increase value by x ; answer is possible ; Driver code ; function call
def PossibleValues ( b , x , n ) : NEW_LINE INDENT leastdivisible = int ( b / x + 1 ) * x NEW_LINE flag = 1 NEW_LINE while ( leastdivisible <= n ) : NEW_LINE INDENT if ( leastdivisible - b >= 1 ) : NEW_LINE INDENT print ( leastdivisible - b , end = " ▁ " ) NEW_LINE leastdivisible += x NEW_LINE flag = 0 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( flag != 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT b = 10 NEW_LINE x = 6 NEW_LINE n = 40 NEW_LINE PossibleValues ( b , x , n ) NEW_LINE DEDENT
Check if all sub | Function to calculate product of digits between given indexes ; Function to check if all sub - numbers have distinct Digit product ; Length of number N ; Digit array ; set to maintain digit products ; Finding all possible subarrays ; Driver Code
def digitProduct ( digits , start , end ) : NEW_LINE INDENT pro = 1 NEW_LINE for i in range ( start , end + 1 ) : NEW_LINE INDENT pro *= digits [ i ] NEW_LINE DEDENT return pro NEW_LINE DEDENT def isDistinct ( N ) : NEW_LINE INDENT s = str ( N ) NEW_LINE length = len ( s ) NEW_LINE digits = [ None ] * length NEW_LINE products = set ( ) NEW_LINE for i in range ( 0 , length ) : NEW_LINE INDENT digits [ i ] = int ( s [ i ] ) NEW_LINE DEDENT for i in range ( 0 , length ) : NEW_LINE INDENT for j in range ( i , length ) : NEW_LINE INDENT val = digitProduct ( digits , i , j ) NEW_LINE if val in products : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT products . add ( val ) NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 324 NEW_LINE if isDistinct ( N ) == True : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Ulam Number Sequence | Python3 code to print nth Ulam number ; Array to store Ulam Number ; function to compute ulam Number ; Set to search specific Ulam number efficiently ; push First 2 two term of the sequence in the array and set for further calculation ; loop to generate Ulam number ; traverse the array and check if i can be represented as sum of two distinct element of the array ; Check if i - arr [ j ] exist in the array or not using set . If yes , Then i can be represented as sum of arr [ j ] + ( i - arr [ j ] ) ; if Count is greater than 2 break the loop ; If count is 2 that means i can be represented as sum of two distinct terms of the sequence ; i is ulam number ; Driver Code ; pre compute Ulam Number sequence ; print nth Ulam number
MAX = 10000 NEW_LINE arr = [ ] NEW_LINE def ulam ( ) : NEW_LINE INDENT s = set ( ) NEW_LINE arr . append ( 1 ) NEW_LINE s . add ( 1 ) NEW_LINE arr . append ( 2 ) NEW_LINE s . add ( 2 ) NEW_LINE for i in range ( 3 , MAX ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( 0 , len ( arr ) ) : NEW_LINE INDENT if ( i - arr [ j ] ) in s and arr [ j ] != ( i - arr [ j ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if count > 2 : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if count == 2 : NEW_LINE INDENT arr . append ( i ) NEW_LINE s . add ( i ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT ulam ( ) NEW_LINE n = 9 NEW_LINE print ( arr [ n - 1 ] ) NEW_LINE DEDENT
Hilbert Number | Utility function to return Nth Hilbert Number ; Driver code
def nthHilbertNumber ( n ) : NEW_LINE INDENT return 4 * ( n - 1 ) + 1 NEW_LINE DEDENT n = 5 NEW_LINE print ( nthHilbertNumber ( n ) ) NEW_LINE
Program to find the nth Kynea number | Function to calculate nth kynea number ; Firstly calculate 2 ^ n + 1 ; Now calculate ( 2 ^ n + 1 ) ^ 2 ; Now calculate ( ( 2 ^ n + 1 ) ^ 2 ) - 2 ; return nth Kynea number ; Driver Code ; print nth kynea number
def nthKyneaNumber ( n ) : NEW_LINE INDENT n = ( 1 << n ) + 1 NEW_LINE n = n * n NEW_LINE n = n - 2 NEW_LINE return n NEW_LINE DEDENT n = 2 NEW_LINE print ( nthKyneaNumber ( n ) ) NEW_LINE
Program to find the nth Kynea number | Function to calculate nth kynea number ; Calculate nth kynea number ; Driver Code ; print nth kynea number
def nthKyneaNumber ( n ) : NEW_LINE INDENT return ( ( 1 << ( 2 * n ) ) + ( 1 << ( n + 1 ) ) - 1 ) NEW_LINE DEDENT n = 2 NEW_LINE print ( nthKyneaNumber ( n ) ) NEW_LINE
Program to check whether a number is Proth number or not | Utility function to Check power of two ; Function to check if the Given number is Proth number or not ; check if k divides n or not ; Check if n / k is power of 2 or not ; update k to next odd number ; If we reach here means there exists no value of K Such that k is odd number and n / k is a power of 2 greater than k ; Get n ; Check n for Proth Number
def isPowerOfTwo ( n ) : NEW_LINE INDENT return ( n and ( not ( n & ( n - 1 ) ) ) ) NEW_LINE DEDENT def isProthNumber ( n ) : NEW_LINE INDENT k = 1 NEW_LINE while ( k < ( n // k ) ) : NEW_LINE INDENT if ( n % k == 0 ) : NEW_LINE INDENT if ( isPowerOfTwo ( n // k ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT k = k + 2 NEW_LINE DEDENT return False NEW_LINE int n = 25 ; NEW_LINE DEDENT if ( isProthNumber ( n - 1 ) ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT
Find last two digits of sum of N factorials | Function to find the unit ' s ▁ and ▁ ten ' s place digit ; Let us write for cases when N is smaller than or equal to 10 ; We know following ( 1 ! + 2 ! + 3 ! + 4 ! ... + 10 ! ) % 100 = 13 ( N >= 10 ) ; Driver Code
def get_last_two_digit ( N ) : NEW_LINE INDENT if N <= 10 : NEW_LINE INDENT ans = 0 NEW_LINE fac = 1 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT fac = fac * i NEW_LINE ans += fac NEW_LINE DEDENT ans = ans % 100 NEW_LINE return ans NEW_LINE DEDENT else : NEW_LINE INDENT return 13 NEW_LINE DEDENT DEDENT N = 1 NEW_LINE for N in range ( 1 , 11 ) : NEW_LINE INDENT print ( " For ▁ N ▁ = ▁ " , N , " : ▁ " , get_last_two_digit ( N ) , sep = ' ▁ ' ) NEW_LINE DEDENT
Check whether product of ' n ' numbers is even or odd | function to check whether product of ' n ' numbers is even or odd ; if a single even number is found , then final product will be an even number ; product is an odd number ; Driver Code
def isProductEven ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( ( arr [ i ] & 1 ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT arr = [ 2 , 4 , 3 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE if ( isProductEven ( arr , n ) ) : NEW_LINE INDENT print ( " Even " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Odd " ) NEW_LINE DEDENT
Sum of squares of Fibonacci numbers | Function to calculate sum of squares of Fibonacci numbers ; Initialize result ; Add remaining terms ; Driver Code
def calculateSquareSum ( n ) : NEW_LINE INDENT fibo = [ 0 ] * ( n + 1 ) ; NEW_LINE if ( n <= 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT fibo [ 0 ] = 0 ; NEW_LINE fibo [ 1 ] = 1 ; NEW_LINE sum = ( ( fibo [ 0 ] * fibo [ 0 ] ) + ( fibo [ 1 ] * fibo [ 1 ] ) ) ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT fibo [ i ] = ( fibo [ i - 1 ] + fibo [ i - 2 ] ) ; NEW_LINE sum += ( fibo [ i ] * fibo [ i ] ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT n = 6 ; NEW_LINE print ( " Sum ▁ of ▁ squares ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : " , calculateSquareSum ( n ) ) ; NEW_LINE
Value of the series ( 1 ^ 3 + 2 ^ 3 + 3 ^ 3 + ... + n ^ 3 ) mod 4 for a given n | function for obtaining the value of f ( n ) mod 4 ; Find the remainder of n when divided by 4 ; If n is of the form 4 k or 4 k + 3 ; If n is of the form 4 k + 1 or 4 k + 2 ; Driver code
def fnMod ( n ) : NEW_LINE INDENT rem = n % 4 NEW_LINE if ( rem == 0 or rem == 3 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( rem == 1 or rem == 2 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 6 NEW_LINE print ( fnMod ( n ) ) NEW_LINE DEDENT
Minimum increment operations to make the array in increasing order | function to find minimum moves required to make the array in increasing order ; to store answer ; iterate over an array ; non - increasing order ; add moves to answer ; increase the element ; return required answer ; Driver code
def MinimumMoves ( a , n , x ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if a [ i ] <= a [ i - 1 ] : NEW_LINE INDENT p = ( a [ i - 1 ] - a [ i ] ) // x + 1 NEW_LINE ans += p NEW_LINE a [ i ] += p * x NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 3 , 2 ] NEW_LINE x = 2 NEW_LINE n = len ( arr ) NEW_LINE print ( MinimumMoves ( arr , n , x ) ) NEW_LINE DEDENT
Check if a large number is divisible by 2 , 3 and 5 or not | function to return sum of digits of a number ; function to Check if a large number is divisible by 2 , 3 and 5 or not ; Driver code
def SumOfDigits ( str , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum += int ( ord ( str [ i ] ) - ord ( '0' ) ) NEW_LINE DEDENT return sum NEW_LINE DEDENT def Divisible ( str , n ) : NEW_LINE INDENT if ( ( SumOfDigits ( str , n ) % 3 == 0 and str [ n - 1 ] == '0' ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = "263730746028908374890" NEW_LINE n = len ( str ) NEW_LINE if ( Divisible ( str , n ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Count all the numbers in a range with smallest factor as K | Function to check if k is a prime number or not ; Corner case ; Check from 2 to n - 1 ; Function to check if a number is not divisible by any number between 2 and K - 1 ; to check if the num is divisible by any numbers between 2 and k - 1 ; if not divisible by any number between 2 and k - 1 but divisible by k ; Function to find count of numbers in range [ a , b ] with smallest factor as K ; a number can be divisible only by k and not by any number less than k only if k is a prime ; to check if a number has smallest factor as K ; Driver code
def isPrime ( k ) : NEW_LINE INDENT if ( k <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , k ) : NEW_LINE INDENT if ( k % i == 0 ) : NEW_LINE INDENT return false NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def check ( num , k ) : NEW_LINE INDENT flag = 1 NEW_LINE for i in range ( 2 , k ) : NEW_LINE INDENT if ( num % i == 0 ) : NEW_LINE INDENT flag = 0 NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT if ( num % k == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def findCount ( a , b , k ) : NEW_LINE INDENT count = 0 NEW_LINE if ( not isPrime ( k ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( a , b + 1 ) : NEW_LINE INDENT ans = check ( i , k ) NEW_LINE if ( ans == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT continue NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 2020 NEW_LINE b = 6300 NEW_LINE k = 29 NEW_LINE print ( findCount ( a , b , k ) ) NEW_LINE DEDENT
Largest number with the given set of N digits that is divisible by 2 , 3 and 5 | Function to find the largest integer with the given set ; find sum of all the digits look if any 0 is present or not ; if 0 is not present , the resultant number won 't be divisible by 5 ; sort all the elements in a non - decreasing manner ; if there is just one element 0 ; find the remainder of the sum of digits when divided by 3 ; there can a remainder as 1 or 2 ; traverse from the end of the digits ; first element which has the same remainder , remove it ; if there is no element which has a same remainder as y ; subtract it by 3 ( could be one or two ) ; delete two minimal digits which has a remainder as y ; print all the digits as a single integer ; Driver code ; initialize the number of set of digits ; initialize all the set of digits in a vector
def findLargest ( n , v ) : NEW_LINE INDENT flag = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( v [ i ] == 0 ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT sum += v [ i ] NEW_LINE DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( " Not ▁ possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT v . sort ( reverse = True ) NEW_LINE if ( v [ 0 ] == 0 ) : NEW_LINE INDENT print ( "0" ) NEW_LINE return 0 NEW_LINE DEDENT else : NEW_LINE INDENT flag = 0 NEW_LINE y = sum % 3 NEW_LINE if ( y != 0 ) : NEW_LINE INDENT i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( v [ i ] % 3 == y ) : NEW_LINE INDENT v . remove ( v [ i ] ) NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT if ( flag == 0 ) : NEW_LINE INDENT y = 3 - y NEW_LINE cnt = 0 NEW_LINE i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( v [ i ] % 3 == y ) : NEW_LINE INDENT v . remove ( v [ i ] ) NEW_LINE cnt += 1 NEW_LINE if ( cnt >= 2 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT i -= 1 NEW_LINE DEDENT DEDENT DEDENT for i in ( v ) : NEW_LINE print ( i , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 11 NEW_LINE v = [ 3 , 9 , 9 , 6 , 4 , 3 , 6 , 4 , 9 , 6 , 0 ] NEW_LINE findLargest ( n , v ) NEW_LINE DEDENT
Find the k | Python3 program to find the K - th smallest factor ; Function to find the k 'th divisor ; initialize vectors v1 and v2 ; store all the divisors in the two vectors accordingly ; reverse the vector v2 to sort it in increasing order ; if k is greater than the size of vectors then no divisor can be possible ; else print the ( k - 1 ) th value of vector ; If K is lying in first vector ; If K is lying in second vector ; Driver code
import math as mt NEW_LINE def findkth ( n , k ) : NEW_LINE INDENT v1 = list ( ) NEW_LINE v2 = list ( ) NEW_LINE for i in range ( 1 , mt . ceil ( n ** ( .5 ) ) ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT v1 . append ( i ) NEW_LINE if ( i != mt . ceil ( mt . sqrt ( n ) ) ) : NEW_LINE INDENT v2 . append ( n // i ) NEW_LINE DEDENT DEDENT DEDENT v2 [ : : - 1 ] NEW_LINE if ( k > ( len ( v1 ) + len ( v2 ) ) ) : NEW_LINE INDENT print ( " Doesn ' t ▁ Exist " , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( k <= len ( v1 ) ) : NEW_LINE INDENT print ( v1 [ k - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( v2 [ k - len ( v1 ) - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT n = 15 NEW_LINE k = 2 NEW_LINE findkth ( n , k ) NEW_LINE
Number of solutions for x < y , where a <= x <= b and c <= y <= d and x , y are integers | function to Find the number of solutions for x < y , where a <= x <= b and c <= y <= d and x , y integers . ; to store answer ; iterate explicitly over all possible values of x ; return answer ; Driver code ; function call
def NumberOfSolutions ( a , b , c , d ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( a , b + 1 ) : NEW_LINE INDENT if d >= max ( c , i + 1 ) : NEW_LINE INDENT ans += d - max ( c , i + 1 ) + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b , c , d = 2 , 3 , 3 , 4 NEW_LINE print ( NumberOfSolutions ( a , b , c , d ) ) NEW_LINE DEDENT
Minimum value possible of a given function from the given set | Function to find the value of F ( n ) ; Driver code
def findF_N ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = ans + ( i + 1 ) * ( n - i - 1 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 3 NEW_LINE print ( findF_N ( n ) ) NEW_LINE
Find N digits number which is divisible by D | Function to return N digits number which is divisible by D ; to store answer ; Driver code
def findNumber ( n , d ) : NEW_LINE INDENT ans = " " NEW_LINE if ( d != 10 ) : NEW_LINE INDENT ans += str ( d ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT ans += '0' NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT ans += " Impossible " NEW_LINE DEDENT else : NEW_LINE INDENT ans += '1' NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT ans += '0' NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 12 NEW_LINE d = 3 NEW_LINE print ( findNumber ( n , d ) ) NEW_LINE DEDENT
Count all the numbers less than 10 ^ 6 whose minimum prime factor is N | Python3 implementation of above approach ; the sieve of prime number and count of minimum prime factor ; form the prime sieve ; 1 is not a prime number ; form the sieve ; if i is prime ; if i is the least prime factor ; mark the number j as non prime ; count the numbers whose least prime factor is i ; form the sieve ; display ; display
MAX = 1000000 NEW_LINE sieve_Prime = [ 0 for i in range ( MAX + 4 ) ] NEW_LINE sieve_count = [ 0 for i in range ( MAX + 4 ) ] NEW_LINE def form_sieve ( ) : NEW_LINE INDENT sieve_Prime [ 1 ] = 1 NEW_LINE for i in range ( 2 , MAX + 1 ) : NEW_LINE INDENT if sieve_Prime [ i ] == 0 : NEW_LINE INDENT for j in range ( i * 2 , MAX + 1 , i ) : NEW_LINE INDENT if sieve_Prime [ j ] == 0 : NEW_LINE INDENT sieve_Prime [ j ] = 1 NEW_LINE sieve_count [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT form_sieve ( ) NEW_LINE n = 2 NEW_LINE print ( " Count ▁ = " , sieve_count [ n ] + 1 ) NEW_LINE n = 3 NEW_LINE print ( " Count ▁ = " , sieve_count [ n ] + 1 ) NEW_LINE
How to access elements of a Square Matrix | Python3 Program to read a square matrix and print the elements above secondary diagonal ; Get the square matrix ; Display the matrix ; Print the elements above secondary diagonal ; check for elements above secondary diagonal
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT matrix = [ [ 0 for i in range ( 5 ) ] for j in range ( 5 ) ] ; NEW_LINE row_index , column_index , x , size = 0 , 0 , 0 , 5 ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT x += 1 ; NEW_LINE matrix [ row_index ] [ column_index ] += x ; NEW_LINE DEDENT DEDENT print ( " The ▁ matrix ▁ is " ) ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT print ( matrix [ row_index ] [ column_index ] , end = " TABSYMBOL " ) ; NEW_LINE DEDENT print ( " " ) ; NEW_LINE DEDENT print ( " Elements above Secondary diagonal are : " ) ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT if ( ( row_index + column_index ) < size - 1 ) : NEW_LINE INDENT print ( matrix [ row_index ] [ column_index ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT
How to access elements of a Square Matrix | Python3 Program to read a square matrix and print the Corner Elements ; Get the square matrix ; Display the matrix ; Print the Corner elements ; check for corner elements
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT matrix = [ [ 0 for i in range ( 5 ) ] for j in range ( 5 ) ] NEW_LINE row_index , column_index , x , size = 0 , 0 , 0 , 5 ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT x += 1 ; NEW_LINE matrix [ row_index ] [ column_index ] = x ; NEW_LINE DEDENT DEDENT print ( " The ▁ matrix ▁ is " ) ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT print ( matrix [ row_index ] [ column_index ] , end = " TABSYMBOL " ) ; NEW_LINE DEDENT print ( " " ) ; NEW_LINE DEDENT print ( " Corner Elements are : " ) ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT if ( ( row_index == 0 or row_index == size - 1 ) and ( column_index == 0 or column_index == size - 1 ) ) : NEW_LINE INDENT print ( matrix [ row_index ] [ column_index ] , end = " TABSYMBOL " ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT
Find the largest good number in the divisors of given number N | function to return distinct prime factors ; to store distinct prime factors ; run a loop upto sqrt ( n ) ; place this prime factor in vector ; This condition is to handle the case when n is a prime number greater than 1 ; function that returns good number ; distinct prime factors ; to store answer ; product of all distinct prime factors is required answer ; Driver code ; function call
def PrimeFactors ( n ) : NEW_LINE INDENT v = [ ] NEW_LINE x = n NEW_LINE i = 2 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT v . append ( i ) NEW_LINE while ( x % i == 0 ) : NEW_LINE INDENT x //= i NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT if ( x > 1 ) : NEW_LINE INDENT v . append ( x ) NEW_LINE DEDENT return v NEW_LINE DEDENT def GoodNumber ( n ) : NEW_LINE INDENT v = PrimeFactors ( n ) NEW_LINE ans = 1 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT ans *= v [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 12 NEW_LINE print ( GoodNumber ( n ) ) NEW_LINE DEDENT
Find Largest Special Prime which is less than or equal to a given number | Function to check whether the number is a special prime or not ; While number is not equal to zero ; If the number is not prime return false . ; Else remove the last digit by dividing the number by 10. ; If the number has become zero then the number is special prime , hence return true ; Function to find the Largest Special Prime which is less than or equal to a given number ; Initially all numbers are considered Primes . ; There is always an answer possible ; Checking if the number is a special prime or not ; If yes print the number and break the loop . ; Else decrement the number . ; Driver code
def checkSpecialPrime ( sieve , num ) : NEW_LINE INDENT while ( num ) : NEW_LINE INDENT if ( not sieve [ num ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT num //= 10 NEW_LINE DEDENT return True NEW_LINE DEDENT def findSpecialPrime ( N ) : NEW_LINE INDENT sieve = [ True ] * ( N + 10 ) NEW_LINE sieve [ 0 ] = sieve [ 1 ] = False ; NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT if ( sieve [ i ] ) : NEW_LINE INDENT for j in range ( i * i , N + 1 , i ) : NEW_LINE INDENT sieve [ j ] = False NEW_LINE DEDENT DEDENT DEDENT while ( True ) : NEW_LINE INDENT if ( checkSpecialPrime ( sieve , N ) ) : NEW_LINE INDENT print ( N ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT N -= 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT findSpecialPrime ( 379 ) NEW_LINE findSpecialPrime ( 100 ) NEW_LINE DEDENT
Check if an integer can be expressed as a sum of two semi | Python3 Code to check if an integer can be expressed as sum of two semi - primes ; Utility function to compute semi - primes in a range ; Increment count of prime numbers ; If number is greater than 1 , add it to the count variable as it indicates the number remain is prime number ; if count is equal to '2' then number is semi - prime ; Utility function to check if a number sum of two semi - primes ; arr [ i ] is already a semi - prime if n - arr [ i ] is also a semi - prime then a number can be expressed as sum of two semi - primes ; Driver code
MAX = 10000 NEW_LINE arr = [ ] NEW_LINE sprime = [ False ] * ( MAX ) NEW_LINE def computeSemiPrime ( ) : NEW_LINE INDENT for i in range ( 2 , MAX ) : NEW_LINE INDENT cnt , num , j = 0 , i , 2 NEW_LINE while cnt < 2 and j * j <= num : NEW_LINE INDENT while num % j == 0 : NEW_LINE INDENT num /= j NEW_LINE cnt += 1 NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if num > 1 : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT if cnt == 2 : NEW_LINE INDENT sprime [ i ] = True NEW_LINE arr . append ( i ) NEW_LINE DEDENT DEDENT DEDENT def checkSemiPrime ( n ) : NEW_LINE INDENT i = 0 NEW_LINE while arr [ i ] <= n // 2 : NEW_LINE INDENT if sprime [ n - arr [ i ] ] == True : NEW_LINE INDENT return True NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT computeSemiPrime ( ) NEW_LINE n = 30 NEW_LINE if checkSemiPrime ( n ) == True : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Check if a number is a Pythagorean Prime or not | Utility function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver Code ; Check if number is prime and of the form 4 n + 1
def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = i + 6 NEW_LINE DEDENT return True NEW_LINE DEDENT n = 13 NEW_LINE if ( isPrime ( n ) and ( n % 4 == 1 ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Divide an isosceles triangle in two parts with ratio of areas as n : m | Python 3 program , to find height h which divide isosceles triangle into ratio n : m ; Function to return the height ; type cast the n , m into float ; calculate the height for cut ; Driver code
from math import sqrt NEW_LINE def heightCalculate ( H , n , m ) : NEW_LINE INDENT N = n * 1.0 NEW_LINE M = m * 1.0 NEW_LINE h = H * sqrt ( N / ( N + M ) ) NEW_LINE return h NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT H = 10 NEW_LINE n = 3 NEW_LINE m = 4 NEW_LINE print ( " { 0 : . 6 } " . format ( heightCalculate ( H , n , m ) ) ) ; NEW_LINE DEDENT
Check n ^ 2 | Check a number is prime or not ; run a loop upto square of given number ; Check if n ^ 2 - m ^ 2 is prime ; Driver code
def isprime ( x ) : NEW_LINE INDENT for i in range ( 2 , math . sqrt ( x ) ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def isNSqMinusnMSqPrime ( m , n ) : NEW_LINE INDENT if ( n - m == 1 and isprime ( m + n ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT m = 13 ; NEW_LINE n = 16 ; NEW_LINE if ( isNSqMinusnMSqPrime ( m , n ) ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT
Find ' N ' number of solutions with the given inequality equations | Function to calculate all the solutions ; there is no solutions ; print first element as y - n + 1 ; print rest n - 1 elements as 1 ; initialize the number of elements and the value of x an y
def findsolution ( n , x , y ) : NEW_LINE INDENT if ( ( y - n + 1 ) * ( y - n + 1 ) + n - 1 < x or y < n ) : NEW_LINE INDENT print ( " No ▁ solution " ) ; NEW_LINE return ; NEW_LINE DEDENT print ( y - n + 1 ) ; NEW_LINE while ( n > 1 ) : NEW_LINE INDENT print ( 1 ) ; NEW_LINE n -= 1 ; NEW_LINE DEDENT DEDENT n = 5 ; NEW_LINE x = 15 ; NEW_LINE y = 15 ; NEW_LINE findsolution ( n , x , y ) ; NEW_LINE
Number of different positions where a person can stand | Function to find the position ; Driver code
def findPosition ( n , f , b ) : NEW_LINE INDENT return n - max ( f + 1 , n - b ) + 1 ; NEW_LINE DEDENT n , f , b = 5 , 2 , 3 NEW_LINE print ( findPosition ( n , f , b ) ) NEW_LINE
Program for n | Function to find the nth odd number ; Driver code
def nthOdd ( n ) : NEW_LINE INDENT return ( 2 * n - 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE print ( nthOdd ( n ) ) NEW_LINE DEDENT
Program for n | Function to find the nth Even number ; Driver code
def nthEven ( n ) : NEW_LINE INDENT return ( 2 * n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE print ( nthEven ( n ) ) NEW_LINE DEDENT
Program to find the Nth Harmonic Number | Function to find N - th Harmonic Number ; H1 = 1 ; loop to apply the forumula Hn = H1 + H2 + H3 ... + Hn - 1 + Hn - 1 + 1 / n ; Driver code
def nthHarmonic ( N ) : NEW_LINE INDENT harmonic = 1.00 NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT harmonic += 1 / i NEW_LINE DEDENT return harmonic NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 8 NEW_LINE print ( round ( nthHarmonic ( N ) , 5 ) ) NEW_LINE DEDENT
Program to find Nth term of series 0 , 7 , 18 , 33 , 51 , 75 , 102 , 133 , ... . . | calculate Nth term of series ; Driver code
def nthTerm ( n ) : NEW_LINE INDENT return 2 * pow ( n , 2 ) + n - 3 NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE
Program to find Nth term of series 0 , 10 , 30 , 60 , 99 , 150 , 210 , 280. ... ... ... . | calculate Nth term of series ; Driver code
def nthTerm ( n ) : NEW_LINE INDENT return 5 * pow ( n , 2 ) - 5 * n NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE
Program to find Nth term of series 2 , 12 , 28 , 50 , 77 , 112 , 152 , 198 , ... . . | calculate Nth term of series ; Driver code
def nthTerm ( n ) : NEW_LINE INDENT return 3 * pow ( n , 2 ) + n - 2 NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE
Program to find Nth term of series 4 , 14 , 28 , 46 , 68 , 94 , 124 , 158 , ... . . | calculate Nth term of series ; Driver code
def nthTerm ( n ) : NEW_LINE INDENT return 2 * pow ( n , 2 ) + 4 * n - 2 NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE
Program to find Nth term of series 0 , 11 , 28 , 51 , 79 , 115 , 156 , 203 , ... . | calculate Nth term of series ; Driver code
def nthTerm ( n ) : NEW_LINE INDENT return 3 * pow ( n , 2 ) + 2 * n - 5 NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE
Program to find Nth term of series 0 , 9 , 22 , 39 , 60 , 85 , 114 , 147 , ... . . | calculate Nth term of series ; Driver code
def nthTerm ( n ) : NEW_LINE INDENT return 2 * pow ( n , 2 ) + 3 * n - 5 NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE
Program to find Nth term of series 3 , 12 , 29 , 54 , 86 , 128 , 177 , 234 , ... . . | calculate Nth term of series ; Driver code
def nthTerm ( n ) : NEW_LINE INDENT return 4 * pow ( n , 2 ) - 3 * n + 2 NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE
Find other two sides and angles of a right angle triangle | Python 3 program to print all sides and angles of right angle triangle given one side ; Function to find angle A Angle in front of side a ; applied cosine rule ; convert into degrees ; Function to find angle B Angle in front of side b ; applied cosine rule ; convert into degrees and return ; Function to print all angles of the right angled triangle ; for calculate angle A ; for calculate angle B ; Function to find other two sides of the right angled triangle ; if n is odd ; case of n = 1 handled separately ; case of n = 2 handled separately ; Print angles of the triangle ; Driver Code
import math NEW_LINE PI = 3.1415926535 NEW_LINE def findAnglesA ( a , b , c ) : NEW_LINE INDENT A = math . acos ( ( b * b + c * c - a * a ) / ( 2 * b * c ) ) NEW_LINE return A * 180 / PI NEW_LINE DEDENT def findAnglesB ( a , b , c ) : NEW_LINE INDENT B = math . acos ( ( a * a + c * c - b * b ) / ( 2 * a * c ) ) NEW_LINE return B * 180 / PI NEW_LINE DEDENT def printAngles ( a , b , c ) : NEW_LINE INDENT x = a NEW_LINE y = b NEW_LINE z = c NEW_LINE A = findAnglesA ( x , y , z ) NEW_LINE B = findAnglesB ( x , y , z ) NEW_LINE print ( " Angles ▁ are ▁ A ▁ = ▁ " , A , " , ▁ B ▁ = ▁ " , B , " , ▁ C ▁ = ▁ " , "90 ▁ " ) NEW_LINE DEDENT def printOtherSides ( n ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT b = ( n * n - 1 ) // 2 NEW_LINE c = ( n * n + 1 ) // 2 NEW_LINE print ( " Side ▁ b ▁ = ▁ " , b , " ▁ Side ▁ c ▁ = ▁ " , c ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( n == 2 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT b = n * n // 4 - 1 ; NEW_LINE c = n * n // 4 + 1 ; NEW_LINE print ( " Side ▁ b ▁ = ▁ " , b , " , ▁ Side ▁ c ▁ = ▁ " , c ) NEW_LINE DEDENT DEDENT printAngles ( n , b , c ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 12 NEW_LINE printOtherSides ( a ) NEW_LINE DEDENT
Sum of the first N terms of the series 2 , 6 , 12 , 20 , 30. ... | Function to calculate the sum ; number of terms to be included in the sum ; find the Sum
def calculateSum ( n ) : NEW_LINE INDENT return ( n * ( n + 1 ) // 2 + n * ( n + 1 ) * ( 2 * n + 1 ) // 6 ) NEW_LINE DEDENT n = 3 NEW_LINE print ( " Sum ▁ = ▁ " , calculateSum ( n ) ) NEW_LINE
Program to find the Nth term of the series 0 , 5 , 14 , 27 , 44 , ... ... . . | Calculate Nth term of series ; Driver code
def nthTerm ( n ) : NEW_LINE INDENT return 2 * pow ( n , 2 ) - n - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE DEDENT
Program to find the Nth term of the series 0 , 5 , 18 , 39 , 67 , 105 , 150 , 203 , ... | calculate Nth term of series ; Driver code
def nthTerm ( n ) : NEW_LINE INDENT return 4 * pow ( n , 2 ) - 7 * n + 3 NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE
Check whether a given Number is Power | Python3 program to find whether a number is power - isolated or not ; for 2 as prime factor ; for odd prime factor ; calculate product of powers and prime factors ; check result for power - isolation ; Driver code
def checkIfPowerIsolated ( num ) : NEW_LINE INDENT input1 = num ; NEW_LINE count = 0 ; NEW_LINE factor = [ 0 ] * ( num + 1 ) ; NEW_LINE if ( num % 2 == 0 ) : NEW_LINE INDENT while ( num % 2 == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE num //= 2 ; NEW_LINE DEDENT factor [ 2 ] = count ; NEW_LINE DEDENT i = 3 ; NEW_LINE while ( i * i <= num ) : NEW_LINE INDENT count = 0 ; NEW_LINE while ( num % i == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE num //= i ; NEW_LINE DEDENT if ( count > 0 ) : NEW_LINE INDENT factor [ i ] = count ; NEW_LINE DEDENT i += 2 ; NEW_LINE DEDENT if ( num > 1 ) : NEW_LINE INDENT factor [ num ] = 1 ; NEW_LINE DEDENT product = 1 ; NEW_LINE for i in range ( 0 , len ( factor ) ) : NEW_LINE INDENT if ( factor [ i ] > 0 ) : NEW_LINE INDENT product = product * factor [ i ] * i ; NEW_LINE DEDENT DEDENT if ( product == input1 ) : NEW_LINE INDENT print ( " Power - isolated ▁ Integer " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ a ▁ Power - isolated ▁ Integer " ) ; NEW_LINE DEDENT DEDENT checkIfPowerIsolated ( 12 ) ; NEW_LINE checkIfPowerIsolated ( 18 ) ; NEW_LINE checkIfPowerIsolated ( 35 ) ; NEW_LINE
Program to find the Nth term of the series 3 , 7 , 13 , 21 , 31. ... . | Function to calculate sum ; Return Nth term ; driver code ; declaration of number of terms ; Get the Nth term
def getNthTerm ( N ) : NEW_LINE INDENT return ( pow ( N , 2 ) + N + 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 11 NEW_LINE print ( getNthTerm ( N ) ) NEW_LINE DEDENT
Sum of the numbers upto N that are divisible by 2 or 5 | Function to find the sum ; sum2 is sum of numbers divisible by 2 ; sum5 is sum of number divisible by 5 ; sum10 of numbers divisible by 2 and 5 ; Driver code
def findSum ( n ) : NEW_LINE INDENT sum2 = ( ( n // 2 ) * ( 4 + ( n // 2 - 1 ) * 2 ) ) // 2 NEW_LINE sum5 = ( ( n // 5 ) * ( 10 + ( n // 5 - 1 ) * 5 ) ) // 2 NEW_LINE sum10 = ( ( n // 10 ) * ( 20 + ( n // 10 - 1 ) * 10 ) ) // 2 NEW_LINE return sum2 + sum5 - sum10 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( int ( findSum ( n ) ) ) NEW_LINE DEDENT
Find four factors of N with maximum product and sum equal to N | Set 3 | Python3 implementation of above approach ; Function to find primes ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to find factors ; run a loop upto square root of that number ; if the n is perfect square ; otherwise push it 's two divisors ; sort the divisors ; Function to find max product ; To store factors of 'n ; find factors ; if it is divisible by 4. ; if it is prime ; otherwise answer will be possible ; include last third factor ; nested loop to find other two factors ; Driver code ; function call
from math import sqrt , ceil , floor NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 5 , ceil ( sqrt ( n ) ) , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def factors ( N , v ) : NEW_LINE INDENT for i in range ( 2 , N ) : NEW_LINE INDENT for j in range ( 1 , ceil ( sqrt ( i ) ) + 1 ) : NEW_LINE INDENT if ( i % j == 0 ) : NEW_LINE INDENT if ( i // j == j ) : NEW_LINE INDENT v [ i ] . append ( j ) NEW_LINE DEDENT else : NEW_LINE INDENT v [ i ] . append ( j ) NEW_LINE v [ i ] . append ( i // j ) NEW_LINE DEDENT DEDENT DEDENT v = sorted ( v ) NEW_LINE DEDENT DEDENT def product ( n ) : NEW_LINE ' NEW_LINE INDENT v = [ [ ] ] * ( n + 100 ) NEW_LINE factors ( n + 100 , v ) NEW_LINE if ( n % 4 == 0 ) : NEW_LINE INDENT x = n // 4 NEW_LINE x *= x NEW_LINE return x * x NEW_LINE DEDENT else : NEW_LINE INDENT if ( isPrime [ n ] ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans = - 1 NEW_LINE if ( len ( v [ n ] ) > 2 ) : NEW_LINE INDENT fac = v [ n ] [ len ( v [ n ] ) - 3 ] NEW_LINE for i in range ( len ( v [ n ] - 1 ) , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( len ( v [ n ] - 1 ) , - 1 , - 1 ) : NEW_LINE INDENT if ( ( fac * 2 ) + ( v [ n ] [ j ] + v [ n ] [ i ] ) == n ) : NEW_LINE INDENT ans = max ( ans , fac * fac * v [ n ] [ j ] * v [ n ] [ i ] ) NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT DEDENT DEDENT n = 24 NEW_LINE print ( product ( n ) ) NEW_LINE
Ratio of mth and nth terms of an A . P . with given ratio of sums | function to calculate ratio of mth and nth term ; ratio will be tm / tn = ( 2 * m - 1 ) / ( 2 * n - 1 ) ; Driver code
def CalculateRatio ( m , n ) : NEW_LINE INDENT return ( 2 * m - 1 ) / ( 2 * n - 1 ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT m = 6 ; NEW_LINE n = 2 ; NEW_LINE print ( float ( CalculateRatio ( m , n ) ) ) ; NEW_LINE DEDENT
Find the sum of n terms of the series 1 , 8 , 27 , 64 ... . | Function to calculate the sum ; return total sum ; Driver code
def calculateSum ( n ) : NEW_LINE INDENT return ( n * ( n + 1 ) / 2 ) ** 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE print ( calculateSum ( n ) ) NEW_LINE DEDENT
Sum of Digits in a ^ n till a single digit | This function finds single digit sum of n . ; Returns single digit sum of a ^ n . We use modular exponentiation technique . ; Driver Code
def digSum ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif n % 9 == 0 : NEW_LINE INDENT return 9 NEW_LINE DEDENT else : NEW_LINE INDENT return n % 9 NEW_LINE DEDENT DEDENT def powerDigitSum ( a , n ) : NEW_LINE INDENT res = 1 NEW_LINE while ( n ) : NEW_LINE INDENT if n % 2 == 1 : NEW_LINE INDENT res = res * digSum ( a ) NEW_LINE res = digSum ( res ) NEW_LINE DEDENT a = digSum ( digSum ( a ) * digSum ( a ) ) NEW_LINE n //= 2 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , n = 9 , 4 NEW_LINE print ( powerDigitSum ( a , n ) ) NEW_LINE DEDENT
Program to find total number of edges in a Complete Graph | Function to find the total number of edges in a complete graph with N vertices ; Driver Code
def totEdge ( n ) : NEW_LINE INDENT result = ( n * ( n - 1 ) ) // 2 NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 6 NEW_LINE print ( totEdge ( n ) ) NEW_LINE DEDENT
Find minimum number of Log value needed to calculate Log upto N | Python3 program to find number of log values needed to calculate all the log values from 1 to N ; In this list prime [ i ] will store true if prime [ i ] is prime , else store false ; Using sieve of Eratosthenes to find all prime upto N ; Function to find number of log values needed to calculate all the log values from 1 to N ; calculate primes upto N ; Driver code
MAX = 1000005 NEW_LINE prime = [ True for i in range ( MAX ) ] NEW_LINE def sieve ( N ) : NEW_LINE INDENT prime [ 0 ] , prime [ 1 ] = False , False NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT for j in range ( 2 , N + 1 ) : NEW_LINE INDENT if ( i * j > N ) : NEW_LINE INDENT break NEW_LINE DEDENT prime [ i * j ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def countLogNeeded ( N ) : NEW_LINE INDENT count = 0 NEW_LINE sieve ( N ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE print ( countLogNeeded ( N ) ) NEW_LINE DEDENT
Program to find the count of coins of each type from the given ratio | function to calculate coin . ; Converting each of them in rupees . As we are given totalRupees = 1800 ; Driver code
def coin ( totalRupees , X , Y , Z ) : NEW_LINE INDENT one = X * 1 NEW_LINE fifty = ( ( Y * 1 ) / 2.0 ) NEW_LINE twentyfive = ( ( Z * 1 ) / 4.0 ) NEW_LINE total = one + fifty + twentyfive NEW_LINE result = ( ( totalRupees ) / total ) NEW_LINE return int ( result ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT totalRupees = 1800 NEW_LINE X , Y , Z = 1 , 2 , 4 NEW_LINE Rupees = coin ( totalRupees , X , Y , Z ) NEW_LINE print ( "1 ▁ rupess ▁ coins ▁ = ▁ " , Rupees * 1 ) NEW_LINE print ( "50 ▁ paisa ▁ coins ▁ = ▁ " , Rupees * 2 ) NEW_LINE print ( "25 ▁ paisa ▁ coins ▁ = ▁ " , Rupees * 4 ) NEW_LINE DEDENT
Find the sum of series 0. X + 0. XX + 0. XXX + ... upto k terms | function which return the sum of series ; Driver code
def sumOfSeries ( x , k ) : NEW_LINE INDENT return ( float ( x ) / 81 ) * ( 9 * k - 1 + 10 ** ( ( - 1 ) * k ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 9 NEW_LINE k = 20 NEW_LINE print ( sumOfSeries ( x , k ) ) NEW_LINE DEDENT
Find four factors of N with maximum product and sum equal to N | Set | Python3 program to find four factors of N with maximum product and sum equal to N ; Function to find factors and to prthose four factors ; push all the factors in the container ; number of factors ; Initial maximum ; hash - array to mark the pairs ; form all the pair sums ; if the pair sum is less than n ; push in another container ; mark the sum with the elements formed ; mark in the map that v [ i ] + v [ j ] is present ; new size of all the pair sums ; iterate for all pair sum ; the required part ; if the required part is also present in pair sum ; find the elements with which the first pair is formed ; find the elements with which the second pair is formed ; check for previous maximum ; Driver code
from math import sqrt , ceil , floor NEW_LINE def findfactors ( n ) : NEW_LINE INDENT mpp = dict ( ) NEW_LINE v = [ ] NEW_LINE v1 = [ ] NEW_LINE for i in range ( 1 , ceil ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT v . append ( i ) NEW_LINE if ( i != ( n // i ) and i != 1 ) : NEW_LINE INDENT v . append ( n // i ) NEW_LINE DEDENT DEDENT DEDENT s = len ( v ) NEW_LINE maxi = - 1 NEW_LINE mp1 = [ 0 ] * ( n + 5 ) NEW_LINE for i in range ( s ) : NEW_LINE INDENT for j in range ( i , s ) : NEW_LINE INDENT if ( v [ i ] + v [ j ] < n ) : NEW_LINE INDENT v1 . append ( v [ i ] + v [ j ] ) NEW_LINE mp1 [ v [ i ] + v [ j ] ] = [ v [ i ] , v [ j ] ] NEW_LINE mpp [ v [ i ] + v [ j ] ] = 1 NEW_LINE DEDENT DEDENT DEDENT s = len ( v1 ) NEW_LINE for i in range ( s ) : NEW_LINE INDENT el = n - ( v1 [ i ] ) NEW_LINE if ( el in mpp ) : NEW_LINE INDENT a = mp1 [ v1 [ i ] ] [ 0 ] NEW_LINE b = mp1 [ v1 [ i ] ] [ 1 ] NEW_LINE c = mp1 [ n - v1 [ i ] ] [ 0 ] NEW_LINE d = mp1 [ n - v1 [ i ] ] [ 1 ] NEW_LINE maxi = max ( a * b * c * d , maxi ) NEW_LINE DEDENT DEDENT if ( maxi == - 1 ) : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The ▁ maximum ▁ product ▁ is ▁ " , maxi ) NEW_LINE DEDENT DEDENT n = 50 NEW_LINE findfactors ( n ) NEW_LINE
Maximize the product of four factors of a Number | Python 3 implementation of above approach ; Declare the vector of factors for storing the ; function to find out the factors of a number ; Loop until the i reaches the sqrt ( n ) ; Check if i is a factor of n ; if both the factors are same we only push one factor ; factor1 is pushed ; factor2 is pushed ; Function to find the maximum product ; Initialize the product with - 1 ; Find the sum of factors and store it in s ; Compare whether it is equal to the n ; product of factors ; Check whether we have a better p now if yes update ; Driver code ; initializes the vectors with the divisors of n ; prints out the maximised product .
from math import sqrt NEW_LINE factors = [ ] NEW_LINE def findFactors ( n ) : NEW_LINE INDENT for i in range ( 1 , int ( sqrt ( n ) ) + 1 , 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( ( n / i ) == i ) : NEW_LINE INDENT factors . append ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT factors . append ( n / i ) NEW_LINE factors . append ( i ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def findProduct ( n ) : NEW_LINE INDENT product = - 1 NEW_LINE si = len ( factors ) NEW_LINE for i in range ( si ) : NEW_LINE INDENT for j in range ( si ) : NEW_LINE INDENT for k in range ( si ) : NEW_LINE INDENT for l in range ( si ) : NEW_LINE INDENT s = ( factors [ i ] + factors [ j ] + factors [ k ] + factors [ l ] ) NEW_LINE if ( s == n ) : NEW_LINE INDENT p = ( factors [ i ] * factors [ j ] * factors [ k ] * factors [ l ] ) NEW_LINE if ( p > product ) : NEW_LINE INDENT product = p NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT DEDENT return product NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE findFactors ( n ) NEW_LINE print ( int ( findProduct ( n ) ) ) NEW_LINE DEDENT
Maximize the product of four factors of a Number | For calculation of a ^ b ; Function to check ; every odd and number less than 3. ; every number divisible by 4. ; every number divisible by 6. ; every number divisible by 10. ; for every even number which is not divisible by above values . ; Driver code
def modExp ( a , b ) : NEW_LINE INDENT result = 1 NEW_LINE while ( b > 0 ) : NEW_LINE INDENT if ( int ( b ) & 1 ) : NEW_LINE INDENT result = result * a NEW_LINE DEDENT a = a * a NEW_LINE b /= 2 NEW_LINE DEDENT return result NEW_LINE DEDENT def check ( num ) : NEW_LINE INDENT if ( num & 1 or num < 3 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT elif ( num % 4 == 0 ) : NEW_LINE INDENT return modExp ( num / 4 , 4 ) NEW_LINE DEDENT elif ( num % 6 == 0 ) : NEW_LINE INDENT return modExp ( num / 3 , 2 ) * modExp ( num / 6 , 2 ) NEW_LINE DEDENT elif ( num % 10 == 0 ) : NEW_LINE INDENT return modExp ( num / 5 , 2 ) * ( num / 10 ) * ( num / 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT num = 10 NEW_LINE print ( int ( check ( num ) ) ) NEW_LINE DEDENT
Check if any large number is divisible by 17 or not | Function to check if the number is divisible by 17 or not ; Extracting the last digit ; Truncating the number ; Subtracting the five times the last digit from the remaining number ; Return n is divisible by 17 ; Driver Code
def isDivisible ( n ) : NEW_LINE INDENT while ( n // 100 ) : NEW_LINE INDENT d = n % 10 NEW_LINE n //= 10 NEW_LINE n -= d * 5 NEW_LINE DEDENT return ( n % 17 == 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 19877658 NEW_LINE if isDivisible ( n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Minimum number of elements to be removed to make XOR maximum | Python 3 to find minimum number of elements to remove to get maximum XOR value ; First n in the below condition is for the case where n is 0 ; Function to find minimum number of elements to be removed . ; Driver Code ; print minimum number of elements to be removed
def nextPowerOf2 ( n ) : NEW_LINE INDENT count = 0 NEW_LINE if ( n and not ( n and ( n - 1 ) ) ) : NEW_LINE INDENT return n NEW_LINE DEDENT while n != 0 : NEW_LINE INDENT n >>= 1 NEW_LINE count += 1 NEW_LINE DEDENT return 1 << count NEW_LINE DEDENT def removeElement ( n ) : NEW_LINE INDENT if n == 1 or n == 2 : NEW_LINE INDENT return 0 NEW_LINE DEDENT a = nextPowerOf2 ( n ) NEW_LINE if n == a or n == a - 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif n == a - 2 : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif n % 2 == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 2 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE print ( removeElement ( n ) ) NEW_LINE DEDENT
Program to find Length of Bridge using Speed and Length of Train | function to calculate the length of bridge . ; Driver Code ; Assuming the input variables
def bridge_length ( trainLength , Speed , Time ) : NEW_LINE INDENT return ( ( Time * Speed ) - trainLength ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT trainLength = 120 NEW_LINE Speed = 30 NEW_LINE Time = 18 NEW_LINE print ( " Length ▁ of ▁ bridge ▁ = ▁ " , bridge_length ( trainLength , Speed , Time ) , " meters " ) NEW_LINE DEDENT
Program to find sum of the given sequence | Python3 program to find the sum of the given sequence ; function to find moudulo inverse under 10 ^ 9 + 7 ; Function to find the sum of the given sequence ; Driver code
MOD = 1000000007 ; NEW_LINE def modInv ( x ) : NEW_LINE INDENT n = MOD - 2 ; NEW_LINE result = 1 ; NEW_LINE while ( n ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT result = result * x % MOD ; NEW_LINE DEDENT x = x * x % MOD ; NEW_LINE n = int ( n / 2 ) ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT def getSum ( n , k ) : NEW_LINE INDENT ans = 1 ; NEW_LINE for i in range ( n + 1 , n - k , - 1 ) : NEW_LINE INDENT ans = ans * i % MOD ; NEW_LINE DEDENT ans = ans * modInv ( k + 1 ) % MOD ; NEW_LINE return ans ; NEW_LINE DEDENT n = 3 ; NEW_LINE k = 2 ; NEW_LINE print ( getSum ( n , k ) ) ; NEW_LINE