text
stringlengths
17
3.65k
code
stringlengths
70
5.84k
Centered Square Number | Function to calculate Centered square number function ; Formula to calculate nth Centered square number ; Driver Code
def centered_square_num ( n ) : NEW_LINE INDENT return n * n + ( ( n - 1 ) * ( n - 1 ) ) NEW_LINE DEDENT n = 7 NEW_LINE print ( " % sth ▁ Centered ▁ square ▁ number : ▁ " % n , centered_square_num ( n ) ) NEW_LINE
Sum of first n natural numbers | Function to find the sum of series ; Driver code
def seriesSum ( n ) : NEW_LINE INDENT return int ( ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ) NEW_LINE DEDENT n = 4 NEW_LINE print ( seriesSum ( n ) ) NEW_LINE
Dodecagonal number | Function to calculate Dodecagonal number ; Formula to calculate nth Dodecagonal number ; Driver Code
def Dodecagonal_number ( n ) : NEW_LINE INDENT return 5 * n * n - 4 * n NEW_LINE DEDENT n = 7 NEW_LINE print ( Dodecagonal_number ( n ) ) NEW_LINE n = 12 NEW_LINE print ( Dodecagonal_number ( n ) ) NEW_LINE
Arithmetic Number | Python3 program to check if a number is Arithmetic number or not ; Sieve Of Eratosthenes ; 1 is not a prime number ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Storing primes in an array ; Update value in primesquare [ p * p ] , if p is prime . ; Function to count divisors ; If number is 1 , then it will have only 1 as a factor . So , total factors will be 1. ; for storing primes upto n ; Calling SieveOfEratosthenes to store prime factors of n and to store square of prime factors of n ; ans will contain total number of distinct divisors ; Loop for counting factors of n ; a [ i ] is not less than cube root n ; Calculating power of a [ i ] in n . cnt is power of prime a [ i ] in n . ; if a [ i ] is a factor of n ; incrementing power ; Calculating number of divisors . If n = a ^ p * b ^ q then total divisors of n are ( p + 1 ) * ( q + 1 ) ; First case ; Second case ; Third casse ; Returns sum of all factors of n . ; Traversing through all prime factors . ; This condition is to handle the case when n is a prime number greater than 2. ; Check if number is Arithmetic Number or not . ; Driver code
import math NEW_LINE def SieveOfEratosthenes ( n , prime , primesquare , a ) : NEW_LINE INDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT prime [ i ] = True ; NEW_LINE DEDENT for i in range ( ( n * n + 1 ) + 1 ) : NEW_LINE INDENT primesquare [ i ] = False ; NEW_LINE DEDENT prime [ 1 ] = False ; NEW_LINE p = 2 ; NEW_LINE while ( p * p <= n ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False ; NEW_LINE DEDENT DEDENT p += 1 ; NEW_LINE DEDENT j = 0 ; NEW_LINE for p in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT a [ j ] = p ; NEW_LINE primesquare [ p * p ] = True ; NEW_LINE j += 1 ; NEW_LINE DEDENT DEDENT DEDENT def countDivisors ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT prime = [ False ] * ( n + 2 ) ; NEW_LINE primesquare = [ False ] * ( n * n + 3 ) ; NEW_LINE a = [ 0 ] * n ; NEW_LINE SieveOfEratosthenes ( n , prime , primesquare , a ) ; NEW_LINE ans = 1 ; NEW_LINE for i in range ( 0 , True ) : NEW_LINE INDENT if ( a [ i ] * a [ i ] * a [ i ] > n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT cnt = 1 ; NEW_LINE while ( n % a [ i ] == 0 ) : NEW_LINE INDENT n //= a [ i ] ; NEW_LINE cnt = cnt + 1 ; NEW_LINE DEDENT ans = ans * cnt ; NEW_LINE DEDENT if ( prime [ n ] ) : NEW_LINE INDENT ans = ans * 2 ; NEW_LINE DEDENT elif ( primesquare [ n ] ) : NEW_LINE INDENT ans = ans * 3 ; NEW_LINE DEDENT elif ( n != 1 ) : NEW_LINE INDENT ans = ans * 4 ; NEW_LINE DEDENT DEDENT def sumofFactors ( n ) : NEW_LINE INDENT res = 1 ; NEW_LINE for i in range ( 2 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT count = 0 ; NEW_LINE curr_sum = 1 ; NEW_LINE curr_term = 1 ; NEW_LINE while ( n % i == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE n //= i ; NEW_LINE curr_term *= i ; NEW_LINE curr_sum += curr_term ; NEW_LINE DEDENT res *= curr_sum ; NEW_LINE DEDENT if ( n >= 2 ) : NEW_LINE INDENT res *= ( 1 + n ) ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT def checkArithmetic ( n ) : NEW_LINE INDENT count = countDivisors ( n ) ; NEW_LINE sum = sumofFactors ( n ) ; NEW_LINE return ( sum % count == 0 ) ; NEW_LINE DEDENT n = 6 ; NEW_LINE if ( checkArithmetic ( n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Finding n | A formula based Python program to find sum of series with cubes of first n natural numbers ; Driver Function
def magicOfSequence ( N ) : NEW_LINE INDENT return ( N * ( N + 1 ) / 2 ) + 2 * N NEW_LINE DEDENT N = 6 NEW_LINE print ( int ( magicOfSequence ( N ) ) ) NEW_LINE
Form a number using corner digits of powers | Storing N raised to power 0 ; Find next power by multiplying N with current power ; Store digits of Power one by one . ; Calculate carry . ; Store carry in Power array . ; Prints number formed by corner digits of powers of N . ; Initializing empty result ; One by one compute next powers and add their corner digits . ; Call Function that store power in Power array . ; Store unit and last digits of power in res . ; Driver Code
power = [ ] NEW_LINE def nextPower ( N ) : NEW_LINE INDENT global power NEW_LINE carry = 0 NEW_LINE for i in range ( 0 , len ( power ) ) : NEW_LINE INDENT prod = ( power [ i ] * N ) + carry NEW_LINE power [ i ] = prod % 10 NEW_LINE carry = ( int ) ( prod / 10 ) NEW_LINE DEDENT while ( carry ) : NEW_LINE INDENT power . append ( carry % 10 ) NEW_LINE carry = ( int ) ( carry / 10 ) NEW_LINE DEDENT DEDENT def printPowerNumber ( X , N ) : NEW_LINE INDENT global power NEW_LINE power . append ( 1 ) NEW_LINE res = [ ] NEW_LINE for i in range ( 1 , X + 1 ) : NEW_LINE INDENT nextPower ( N ) NEW_LINE res . append ( power [ - 1 ] ) NEW_LINE res . append ( power [ 0 ] ) NEW_LINE DEDENT for i in range ( 0 , len ( res ) ) : NEW_LINE INDENT print ( res [ i ] , end = " " ) NEW_LINE DEDENT DEDENT N = 19 NEW_LINE X = 4 NEW_LINE printPowerNumber ( X , N ) NEW_LINE
First digit in factorial of a number | Python3 program for finding the First digit of the large factorial number ; Removing trailing 0 s as this does not change first digit . ; loop for divide the fact until it become the single digit and return the fact ; Driver Code
import math NEW_LINE def firstDigit ( n ) : NEW_LINE INDENT fact = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT fact = fact * i NEW_LINE while ( fact % 10 == 0 ) : NEW_LINE INDENT fact = int ( fact / 10 ) NEW_LINE DEDENT DEDENT while ( fact >= 10 ) : NEW_LINE INDENT fact = int ( fact / 10 ) NEW_LINE DEDENT return math . floor ( fact ) NEW_LINE DEDENT n = 5 NEW_LINE print ( firstDigit ( n ) ) NEW_LINE
Sum of the series 1.2 . 3 + 2.3 . 4 + ... + n ( n + 1 ) ( n + 2 ) | Python 3 program to find sum of the series 1.2 . 3 + 2.3 . 4 + 3.4 . 5 + ... ; Driver Program
def sumofseries ( n ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT res += ( i ) * ( i + 1 ) * ( i + 2 ) NEW_LINE DEDENT return res NEW_LINE DEDENT print ( sumofseries ( 3 ) ) NEW_LINE
Find N Geometric Means between A and B | Python3 program to find n geometric means between A and B ; Prints N geometric means between A and B . ; calculate common ratio ( R ) ; for finding N the Geometric mean between A and B ; Driver Code
import math NEW_LINE def printGMeans ( A , B , N ) : NEW_LINE INDENT R = ( math . pow ( ( B / A ) , 1.0 / ( N + 1 ) ) ) ; NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( int ( A * math . pow ( R , i ) ) , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT A = 3 ; NEW_LINE B = 81 ; NEW_LINE N = 2 ; NEW_LINE printGMeans ( A , B , N ) ; NEW_LINE
Numbers having difference with digit sum more than s | function for digit sum ; function to calculate count of integer s . t . integer - digSum > s ; if n < s no integer possible ; iterate for s range and then calculate total count of such integer if starting integer is found ; if no integer found return 0 ; driver code
def digitSum ( n ) : NEW_LINE INDENT digSum = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT digSum += n % 10 NEW_LINE n //= 10 NEW_LINE DEDENT return digSum NEW_LINE DEDENT def countInteger ( n , s ) : NEW_LINE INDENT if ( n < s ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for i in range ( s , min ( n , s + 163 ) + 1 ) : NEW_LINE INDENT if ( ( i - digitSum ( i ) ) > s ) : NEW_LINE INDENT return ( n - i + 1 ) NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT n = 1000 NEW_LINE s = 100 NEW_LINE print ( countInteger ( n , s ) ) NEW_LINE
Division without using ' / ' operator | Function to find division without using ' / ' operator ; Handling negative numbers ; if num1 is greater than equal to num2 subtract num2 from num1 and increase quotient by one . ; checking if neg equals to 1 then making quotient negative ; Driver program
def division ( num1 , num2 ) : NEW_LINE INDENT if ( num1 == 0 ) : return 0 NEW_LINE if ( num2 == 0 ) : return INT_MAX NEW_LINE negResult = 0 NEW_LINE if ( num1 < 0 ) : NEW_LINE INDENT num1 = - num1 NEW_LINE if ( num2 < 0 ) : NEW_LINE INDENT num2 = - num2 NEW_LINE DEDENT else : NEW_LINE INDENT negResult = true NEW_LINE DEDENT DEDENT elif ( num2 < 0 ) : NEW_LINE INDENT num2 = - num2 NEW_LINE negResult = true NEW_LINE DEDENT quotient = 0 NEW_LINE while ( num1 >= num2 ) : NEW_LINE INDENT num1 = num1 - num2 NEW_LINE quotient += 1 NEW_LINE DEDENT if ( negResult ) : NEW_LINE INDENT quotient = - quotient NEW_LINE DEDENT return quotient NEW_LINE DEDENT num1 = 13 ; num2 = 2 NEW_LINE print ( division ( num1 , num2 ) ) NEW_LINE
Nonagonal number | Function to find nonagonal number series . ; driver Function
def Nonagonal ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( int ( i * ( 7 * i - 5 ) / 2 ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 10 NEW_LINE Nonagonal ( n ) NEW_LINE
Find n | func for calualtion ; for summation of square of first n - natural nos . ; summation of first n natural nos . ; return result ; Driver Code
def seriesFunc ( n ) : NEW_LINE INDENT sumSquare = ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 NEW_LINE sumNatural = ( n * ( n + 1 ) / 2 ) NEW_LINE return ( sumSquare + sumNatural + 1 ) NEW_LINE DEDENT n = 8 NEW_LINE print ( int ( seriesFunc ( n ) ) ) NEW_LINE n = 13 NEW_LINE print ( int ( seriesFunc ( n ) ) ) NEW_LINE
Program to check Plus Perfect Number | Python 3 implementation to check if the number is plus perfect or not ; function to check plus perfect number ; calculating number of digits ; calculating plus perfect number ; checking whether number is plus perfect or not ; driver program
import math NEW_LINE def checkplusperfect ( x ) : NEW_LINE INDENT temp = x NEW_LINE n = 0 NEW_LINE while ( x != 0 ) : NEW_LINE INDENT x = x // 10 NEW_LINE n = n + 1 NEW_LINE DEDENT x = temp NEW_LINE sm = 0 NEW_LINE while ( x != 0 ) : NEW_LINE INDENT sm = sm + ( int ) ( math . pow ( x % 10 , n ) ) NEW_LINE x = x // 10 NEW_LINE DEDENT return ( sm == temp ) NEW_LINE DEDENT x = 9474 NEW_LINE if ( checkplusperfect ( x ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Number of distinct subsets of a set | Python3 program to count number of distinct subsets in an array of distinct numbers ; Returns 2 ^ n ; driver code
import math NEW_LINE def subsetCount ( arr , n ) : NEW_LINE INDENT return 1 << n NEW_LINE DEDENT A = [ 1 , 2 , 3 ] NEW_LINE n = len ( A ) NEW_LINE print ( subsetCount ( A , n ) ) NEW_LINE
Program to calculate GST from original and net prices | Python3 Program to compute GST from original and net prices . ; return value after calculate GST % ; Driver program to test above functions
def Calculate_GST ( org_cost , N_price ) : NEW_LINE INDENT return ( ( ( N_price - org_cost ) * 100 ) / org_cost ) ; NEW_LINE DEDENT org_cost = 100 NEW_LINE N_price = 120 NEW_LINE print ( " GST ▁ = ▁ " , end = ' ' ) NEW_LINE print ( round ( Calculate_GST ( org_cost , N_price ) ) , end = ' ' ) NEW_LINE print ( " % " ) NEW_LINE
Centered hexagonal number | Function to find centered hexagonal number ; Formula to calculate nth centered hexagonal ; Driver Code
def centeredHexagonalNumber ( n ) : NEW_LINE INDENT return 3 * n * ( n - 1 ) + 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE print ( n , " th ▁ centered ▁ hexagonal ▁ number : ▁ " , centeredHexagonalNumber ( n ) ) NEW_LINE DEDENT ' NEW_LINE
Find the distance covered to collect items at equal distances | function to calculate the distance ; main function
def find_distance ( n ) : NEW_LINE INDENT return n * ( ( 3 * n ) + 7 ) NEW_LINE DEDENT n = 5 NEW_LINE ans = find_distance ( n ) NEW_LINE print ( ans ) NEW_LINE
Twin Prime Numbers | Python3 code to check twin prime ; Function to check whether a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Returns true if n1 and n2 are twin primes ; Driver code
import math 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 , int ( math . sqrt ( n ) + 1 ) , 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 twinPrime ( n1 , n2 ) : NEW_LINE INDENT return ( isPrime ( n1 ) and isPrime ( n2 ) and abs ( n1 - n2 ) == 2 ) NEW_LINE DEDENT n1 = 137 NEW_LINE n2 = 139 NEW_LINE if twinPrime ( n1 , n2 ) : NEW_LINE INDENT print ( " Twin ▁ Prime " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Twin ▁ Prime " ) NEW_LINE DEDENT
Sum of the sequence 2 , 22 , 222 , ... ... ... | Python3 code to find sum of series 2 , 22 , 222 , . . ; function which return the sum of series ; driver code
import math NEW_LINE def sumOfSeries ( n ) : NEW_LINE INDENT return 0.0246 * ( math . pow ( 10 , n ) - 1 - ( 9 * n ) ) NEW_LINE DEDENT n = 3 NEW_LINE print ( sumOfSeries ( n ) ) NEW_LINE
Find sum of even index binomial coefficients | Python program to find sum even indexed Binomial Coefficient ; Returns value of even indexed Binomial Coefficient Sum which is 2 raised to power n - 1. ; Driver method
import math NEW_LINE def evenbinomialCoeffSum ( n ) : NEW_LINE INDENT return ( 1 << ( n - 1 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE print evenbinomialCoeffSum ( n ) NEW_LINE DEDENT
Program to print triangular number series till n | Function to find triangular number ; For each iteration increase j by 1 and add it into k ; Increasing j by 1 ; Add value of j into k and update k ; Driven Code
def triangular_series ( n ) : NEW_LINE INDENT j = 1 NEW_LINE k = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( k , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT j = j + 1 NEW_LINE INDENT k = k + j NEW_LINE DEDENT n = 5 NEW_LINE triangular_series ( n ) NEW_LINE
Sum of the series 1 + ( 1 + 3 ) + ( 1 + 3 + 5 ) + ( 1 + 3 + 5 + 7 ) + Γƒ Β’ Γ’ β€šΒ¬Β¦ Γƒ Β’ Γ’ β€šΒ¬Β¦ + ( 1 + 3 + 5 + 7 + Γƒ Β’ Γ’ β€šΒ¬Β¦ + ( 2 n | functionn to find the sum of the given series ; required sum ; Driver program to test above
def sumOfTheSeries ( n ) : NEW_LINE INDENT return int ( ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ) NEW_LINE DEDENT n = 5 NEW_LINE print ( " Sum ▁ = " , sumOfTheSeries ( n ) ) NEW_LINE
Sum of the series 1 + ( 1 + 2 ) + ( 1 + 2 + 3 ) + ( 1 + 2 + 3 + 4 ) + ... ... + ( 1 + 2 + 3 + 4 + ... + n ) | Function to find sum of series ; Driver Code
def sumOfSeries ( n ) : NEW_LINE INDENT return sum ( [ i * ( i + 1 ) / 2 for i in range ( 1 , n + 1 ) ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE print ( sumOfSeries ( n ) ) NEW_LINE DEDENT
Number of triangles after N moves | function to calculate number of triangles in Nth step ; Driver code
def numberOfTriangles ( n ) : NEW_LINE INDENT ans = 2 * ( pow ( 3 , n ) ) - 1 ; NEW_LINE return ans ; NEW_LINE DEDENT n = 2 NEW_LINE print ( numberOfTriangles ( n ) ) NEW_LINE
Motzkin number | Return the nth Motzkin Number . ; Base case ; Finding i - th Motzkin number . ; Driver code
def motzkin ( n ) : NEW_LINE INDENT dp = [ None ] * ( n + 1 ) NEW_LINE dp [ 0 ] = dp [ 1 ] = 1 ; NEW_LINE i = 2 NEW_LINE while i <= n : NEW_LINE INDENT dp [ i ] = ( ( 2 * i + 1 ) * dp [ i - 1 ] + ( 3 * i - 3 ) * dp [ i - 2 ] ) / ( i + 2 ) ; NEW_LINE i = i + 1 NEW_LINE DEDENT return dp [ n ] ; NEW_LINE DEDENT n = 8 NEW_LINE print ( motzkin ( n ) ) NEW_LINE
NicomachusΓƒΖ’Γ† ’ Γƒ † Γ’ €ℒ ΓƒΖ’Γ’ € Γƒ Β’ Γ’ β€šΒ¬ Γ’ β€žΒ’Β’ ΓƒΖ’Γ† ’ Γƒ † Γ’ €ℒ’ ΓƒΖ’Γ† ’’ ΓƒΖ’ Β’ Γƒ Β’ Γ’ β€šΒ¬ Γ… ‘¬ ΓƒΖ’Γ’ €¦‘¬ ΓƒΖ’Γ† ’ Γƒ † Γ’ €ℒ’ ΓƒΖ’Γ† ’’ ΓƒΖ’ Β’ Γƒ Β’ Γ’ β€šΒ¬ Γ… ‘¬ ΓƒΖ’Γ’ €¦¾’ s Theorem ( Sum of k | Return the sum of kth group of positive odd integer . ; Driver code
def kthgroupsum ( k ) : NEW_LINE INDENT return k * k * k NEW_LINE DEDENT k = 3 NEW_LINE print ( kthgroupsum ( k ) ) NEW_LINE
Find x , y , z that satisfy 2 / n = 1 / x + 1 / y + 1 / z | function to find x y and z that satisfy given equation . ; driver code to test the above function
def printXYZ ( n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " x ▁ is ▁ " , n ) NEW_LINE print ( " y ▁ is ▁ " , n + 1 ) NEW_LINE print ( " z ▁ is ▁ " , n * ( n + 1 ) ) NEW_LINE DEDENT DEDENT n = 7 NEW_LINE printXYZ ( n ) NEW_LINE
Find n | Function to print nth term of series 1 3 6 10 ... . ; Driver code
def term ( n ) : NEW_LINE INDENT return n * ( n + 1 ) / 2 NEW_LINE DEDENT n = 4 NEW_LINE print term ( n ) NEW_LINE
Find Harmonic mean using Arithmetic mean and Geometric mean | Python 3 implementation of compution of arithmetic mean , geometric mean and harmonic mean ; Function to calculate arithmetic mean , geometric mean and harmonic mean ; Driver function
import math NEW_LINE def compute ( a , b ) : NEW_LINE INDENT AM = ( a + b ) / 2 NEW_LINE GM = math . sqrt ( a * b ) NEW_LINE HM = ( GM * GM ) / AM NEW_LINE return HM NEW_LINE DEDENT a = 5 NEW_LINE b = 15 NEW_LINE HM = compute ( a , b ) NEW_LINE print ( " Harmonic ▁ Mean ▁ between ▁ " , a , " ▁ and ▁ " , b , " ▁ is ▁ " , HM ) NEW_LINE
Find n | Returns n - th element of the series ; Driver Code
def series ( n ) : NEW_LINE INDENT print ( ( 8 * n ** 2 ) + 1 ) NEW_LINE DEDENT series ( 5 ) NEW_LINE
Check if a number is divisible by all prime divisors of another number | python program to find if all prime factors of y divide x . ; Returns true if all prime factors of y divide x . ; Driver Code
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT else : NEW_LINE INDENT return gcd ( b , a % b ) NEW_LINE DEDENT DEDENT def isDivisible ( x , y ) : NEW_LINE INDENT if ( y == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT z = gcd ( x , y ) ; NEW_LINE if ( z == 1 ) : NEW_LINE INDENT return false ; NEW_LINE DEDENT return isDivisible ( x , y / z ) ; NEW_LINE DEDENT x = 18 NEW_LINE y = 12 NEW_LINE if ( isDivisible ( x , y ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Program to find Sum of a Series a ^ 1 / 1 ! + a ^ 2 / 2 ! + a ^ 3 / 3 ! + a ^ 4 / 4 ! + Γƒ Β’ Γ’ β€šΒ¬Β¦ Γƒ Β’ Γ’ β€šΒ¬Β¦ . + a ^ n / n ! | Python program to print the sum of series . function to calculate sum of given series . ; multiply ( a / i ) to previous term ; store result in res ; Driver code
from __future__ import division NEW_LINE def sumOfSeries ( a , num ) : NEW_LINE INDENT res = 0 NEW_LINE prev = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT prev *= ( a / i ) NEW_LINE res = res + prev NEW_LINE DEDENT return res NEW_LINE DEDENT n = 5 NEW_LINE a = 2 NEW_LINE print ( round ( sumOfSeries ( a , n ) , 4 ) ) NEW_LINE
Program for Celsius To Fahrenheit conversion | Python code to convert Celsius scale to Fahrenheit scale ; Used the formula ; Driver Code
def Cel_To_Fah ( n ) : NEW_LINE INDENT return ( n * 1.8 ) + 32 NEW_LINE DEDENT n = 20 NEW_LINE print ( int ( Cel_To_Fah ( n ) ) ) NEW_LINE
Series with largest GCD and sum equals to n | Python3 code to find the series with largest GCD and sum equals to n ; stores the maximum gcd that can be possible of sequence . ; if maximum gcd comes out to be zero then not possible ; the smallest gcd possible is 1 ; traverse the array to find out the max gcd possible ; checks if the number is divisible or not ; x = x + 1 ; checks if x is smaller then the max gcd possible and x is greater then the resultant gcd till now , then r = x ; checks if n / x is smaller than the max gcd possible and n / x is greater then the resultant gcd till now , then r = x ; x = x + 1 ; traverses and prints d , 2d , 3d , ... , ( k - 1 ) d , ; main driver
def print_sequence ( n , k ) : NEW_LINE INDENT b = int ( n / ( k * ( k + 1 ) / 2 ) ) ; NEW_LINE if b == 0 : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT r = 1 ; NEW_LINE x = 1 NEW_LINE while x ** 2 <= n : NEW_LINE INDENT if n % x != 0 : NEW_LINE INDENT continue ; NEW_LINE DEDENT elif x <= b and x > r : NEW_LINE INDENT r = x NEW_LINE DEDENT elif n / x <= b and n / x > r : NEW_LINE INDENT r = n / x NEW_LINE DEDENT x = x + 1 NEW_LINE DEDENT i = 1 NEW_LINE while i < k : NEW_LINE INDENT print ( r * i , end = " ▁ " ) NEW_LINE i = i + 1 NEW_LINE DEDENT last_term = n - ( r * ( k * ( k - 1 ) / 2 ) ) NEW_LINE print ( last_term ) NEW_LINE DEDENT DEDENT print_sequence ( 24 , 4 ) NEW_LINE print_sequence ( 24 , 5 ) NEW_LINE print_sequence ( 6 , 4 ) NEW_LINE
Number of compositions of a natural number | Python code to find the total number of compositions of a natural number ; function to return the total number of composition of n ; Driver Code
def countCompositions ( n ) : NEW_LINE INDENT return ( 2 ** ( n - 1 ) ) NEW_LINE DEDENT print ( countCompositions ( 4 ) ) NEW_LINE
Program to count digits in an integer ( 4 Different Methods ) | Recursive Python program to count number of digits in a number ; Driver Code
def countDigit ( n ) : NEW_LINE INDENT if n / 10 == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 1 + countDigit ( n // 10 ) NEW_LINE DEDENT n = 345289467 NEW_LINE print ( " Number ▁ of ▁ digits ▁ : ▁ % ▁ d " % ( countDigit ( n ) ) ) NEW_LINE
Tribonacci Numbers | A DP based Python 3 program to print first n Tribonacci numbers . ; Driver code
def printTrib ( n ) : NEW_LINE INDENT dp = [ 0 ] * n NEW_LINE dp [ 0 ] = dp [ 1 ] = 0 ; NEW_LINE dp [ 2 ] = 1 ; NEW_LINE for i in range ( 3 , n ) : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + dp [ i - 3 ] ; NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( dp [ i ] , " ▁ " , end = " " ) NEW_LINE DEDENT DEDENT n = 10 NEW_LINE printTrib ( n ) NEW_LINE
Tribonacci Numbers | Program to print first n tribonacci numbers Matrix Multiplication function for 3 * 3 matrix ; Recursive function to raise the matrix T to the power n ; base condition . ; recursively call to square the matrix ; calculating square of the matrix T ; if n is odd multiply it one time with M ; base condition ; T [ 0 ] [ 0 ] contains the tribonacci number so return it ; Driver Code
def multiply ( T , M ) : NEW_LINE INDENT a = ( T [ 0 ] [ 0 ] * M [ 0 ] [ 0 ] + T [ 0 ] [ 1 ] * M [ 1 ] [ 0 ] + T [ 0 ] [ 2 ] * M [ 2 ] [ 0 ] ) NEW_LINE b = ( T [ 0 ] [ 0 ] * M [ 0 ] [ 1 ] + T [ 0 ] [ 1 ] * M [ 1 ] [ 1 ] + T [ 0 ] [ 2 ] * M [ 2 ] [ 1 ] ) NEW_LINE c = ( T [ 0 ] [ 0 ] * M [ 0 ] [ 2 ] + T [ 0 ] [ 1 ] * M [ 1 ] [ 2 ] + T [ 0 ] [ 2 ] * M [ 2 ] [ 2 ] ) NEW_LINE d = ( T [ 1 ] [ 0 ] * M [ 0 ] [ 0 ] + T [ 1 ] [ 1 ] * M [ 1 ] [ 0 ] + T [ 1 ] [ 2 ] * M [ 2 ] [ 0 ] ) NEW_LINE e = ( T [ 1 ] [ 0 ] * M [ 0 ] [ 1 ] + T [ 1 ] [ 1 ] * M [ 1 ] [ 1 ] + T [ 1 ] [ 2 ] * M [ 2 ] [ 1 ] ) NEW_LINE f = ( T [ 1 ] [ 0 ] * M [ 0 ] [ 2 ] + T [ 1 ] [ 1 ] * M [ 1 ] [ 2 ] + T [ 1 ] [ 2 ] * M [ 2 ] [ 2 ] ) NEW_LINE g = ( T [ 2 ] [ 0 ] * M [ 0 ] [ 0 ] + T [ 2 ] [ 1 ] * M [ 1 ] [ 0 ] + T [ 2 ] [ 2 ] * M [ 2 ] [ 0 ] ) NEW_LINE h = ( T [ 2 ] [ 0 ] * M [ 0 ] [ 1 ] + T [ 2 ] [ 1 ] * M [ 1 ] [ 1 ] + T [ 2 ] [ 2 ] * M [ 2 ] [ 1 ] ) NEW_LINE i = ( T [ 2 ] [ 0 ] * M [ 0 ] [ 2 ] + T [ 2 ] [ 1 ] * M [ 1 ] [ 2 ] + T [ 2 ] [ 2 ] * M [ 2 ] [ 2 ] ) NEW_LINE T [ 0 ] [ 0 ] = a NEW_LINE T [ 0 ] [ 1 ] = b NEW_LINE T [ 0 ] [ 2 ] = c NEW_LINE T [ 1 ] [ 0 ] = d NEW_LINE T [ 1 ] [ 1 ] = e NEW_LINE T [ 1 ] [ 2 ] = f NEW_LINE T [ 2 ] [ 0 ] = g NEW_LINE T [ 2 ] [ 1 ] = h NEW_LINE T [ 2 ] [ 2 ] = i NEW_LINE DEDENT def power ( T , n ) : NEW_LINE INDENT if ( n == 0 or n == 1 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT M = [ [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] ] NEW_LINE power ( T , n // 2 ) NEW_LINE multiply ( T , T ) NEW_LINE if ( n % 2 ) : NEW_LINE INDENT multiply ( T , M ) NEW_LINE DEDENT DEDENT def tribonacci ( n ) : NEW_LINE INDENT T = [ [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] ] NEW_LINE if ( n == 0 or n == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT power ( T , n - 2 ) NEW_LINE DEDENT return T [ 0 ] [ 0 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( tribonacci ( i ) , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT
Geometric mean ( Two Methods ) | Program to calculate the geometric mean of the given array elements . ; function to calculate geometric mean and return float value . ; declare sum variable and initialize it to 1. ; Compute the sum of all the elements in the array . ; compute geometric mean through formula antilog ( ( ( log ( 1 ) + log ( 2 ) + . . ... + log ( n ) ) / n ) and return the value to main function . ; Driver Code ; function call
import math NEW_LINE def geometricMean ( arr , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + math . log ( arr [ i ] ) ; NEW_LINE DEDENT sum = sum / n ; NEW_LINE return math . exp ( sum ) ; NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( geometricMean ( arr , n ) ) ; NEW_LINE
Smallest number k such that the product of digits of k is equal to n | Python3 implementation to find smallest number k such that the product of digits of k is equal to n ; function to find smallest number k such that the product of digits of k is equal to n ; if ' n ' is a single digit number , then it is the required number ; stack the store the digits ; repeatedly divide ' n ' by the numbers from 9 to 2 until all the numbers are used or ' n ' > 1 ; save the digit ' i ' that divides ' n ' onto the stack ; if true , then no number ' k ' can be formed ; pop digits from the stack ' digits ' and add them to 'k ; required smallest number ; Driver Code
import math as mt NEW_LINE def smallestNumber ( n ) : NEW_LINE INDENT if ( n >= 0 and n <= 9 ) : NEW_LINE INDENT return n NEW_LINE DEDENT digits = list ( ) NEW_LINE for i in range ( 9 , 1 , - 1 ) : NEW_LINE INDENT while ( n % i == 0 ) : NEW_LINE INDENT digits . append ( i ) NEW_LINE n = n // i NEW_LINE DEDENT DEDENT if ( n != 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT k = 0 NEW_LINE while ( len ( digits ) != 0 ) : NEW_LINE INDENT k = k * 10 + digits [ - 1 ] NEW_LINE digits . pop ( ) NEW_LINE DEDENT return k NEW_LINE DEDENT n = 100 NEW_LINE print ( smallestNumber ( n ) ) NEW_LINE
Check if a number is magic ( Recursive sum of digits is 1 ) | Python3 program to check if a number is Magic number . ; Note that the loop continues if n is 0 and sum is non - zero . It stops when n becomes 0 and sum becomes single digit . ; Return true if sum becomes 1. ; Driver code
def isMagic ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE while ( n > 0 or sum > 9 ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT n = sum ; NEW_LINE sum = 0 ; NEW_LINE DEDENT sum = sum + n % 10 ; NEW_LINE n = int ( n / 10 ) ; NEW_LINE DEDENT return True if ( sum == 1 ) else False ; NEW_LINE DEDENT n = 1234 ; NEW_LINE if ( isMagic ( n ) ) : NEW_LINE INDENT print ( " Magic ▁ Number " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ a ▁ magic ▁ Number " ) ; NEW_LINE DEDENT
Check if a number is magic ( Recursive sum of digits is 1 ) | Accepting sample input ; Condition to check Magic number
x = 1234 NEW_LINE if ( x % 9 == 1 ) : NEW_LINE INDENT print ( " Magic ▁ Number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ a ▁ Magic ▁ Number " ) NEW_LINE DEDENT
Rearrangement of a number which is also divisible by it | Perform hashing for given n ; perform hashing ; check whether any arrangement exists ; Create a hash for given number n The hash is of size 10 and stores count of each digit in n . ; check for all possible multipliers ; check hash table for both . ; Driver Code
def storeDigitCounts ( n , Hash ) : NEW_LINE INDENT while n > 0 : NEW_LINE INDENT Hash [ n % 10 ] += 1 NEW_LINE n //= 10 NEW_LINE DEDENT DEDENT def rearrange ( n ) : NEW_LINE INDENT hash_n = [ 0 ] * 10 NEW_LINE storeDigitCounts ( n , hash_n ) NEW_LINE for mult in range ( 2 , 10 ) : NEW_LINE INDENT curr = n * mult NEW_LINE hash_curr = [ 0 ] * 10 NEW_LINE storeDigitCounts ( curr , hash_curr ) NEW_LINE if hash_n == hash_curr : NEW_LINE INDENT return curr NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10035 NEW_LINE print ( rearrange ( n ) ) NEW_LINE DEDENT
Sylvester 's sequence | Python Code for Sylvester sequence ; To store the product . ; To store the current number . ; Loop till n . ; Driver program to test above function
def printSequence ( n ) : NEW_LINE a = 1 NEW_LINE ans = 2 NEW_LINE INDENT N = 1000000007 NEW_LINE i = 1 NEW_LINE while i <= n : NEW_LINE INDENT print ans , NEW_LINE ans = ( ( a % N ) * ( ans % N ) ) % N NEW_LINE a = ans NEW_LINE ans = ( ans + 1 ) % N NEW_LINE i = i + 1 NEW_LINE DEDENT DEDENT n = 6 NEW_LINE printSequence ( n ) NEW_LINE
Program to find sum of first n natural numbers | Returns sum of first n natural numbers ; Driver code
def findSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE x = 1 NEW_LINE while x <= n : NEW_LINE INDENT sum = sum + x NEW_LINE x = x + 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 5 NEW_LINE print findSum ( n ) NEW_LINE
Hailstone Numbers | function to print hailstone numbers and to calculate the number of steps required ; N is initially 1. ; N is reduced to 1. ; If N is Even . ; N is Odd . ; Driver Code ; Function to generate Hailstone Numbers ; Output : Number of Steps
def HailstoneNumbers ( N , c ) : NEW_LINE INDENT print ( N , end = " ▁ " ) NEW_LINE if ( N == 1 and c == 0 ) : NEW_LINE INDENT return c NEW_LINE DEDENT elif ( N == 1 and c != 0 ) : NEW_LINE INDENT c = c + 1 NEW_LINE DEDENT elif ( N % 2 == 0 ) : NEW_LINE INDENT c = c + 1 NEW_LINE c = HailstoneNumbers ( int ( N / 2 ) , c ) NEW_LINE DEDENT elif ( N % 2 != 0 ) : NEW_LINE INDENT c = c + 1 NEW_LINE c = HailstoneNumbers ( 3 * N + 1 , c ) NEW_LINE DEDENT return c NEW_LINE DEDENT N = 7 NEW_LINE x = HailstoneNumbers ( N , 0 ) NEW_LINE print ( " Number of Steps : " , x ) NEW_LINE
Count number of digits after decimal on dividing a number | Python3 program to count digits after dot when a number is divided by another . ; ans = 0 Initialize result ; calculating remainder ; if this remainder appeared before then the numbers are irrational and would not converge to a solution the digits after decimal will be infinite ; Driver Code
def count ( x , y ) : NEW_LINE INDENT m = dict ( ) NEW_LINE while x % y != 0 : NEW_LINE INDENT x %= y NEW_LINE ans += 1 NEW_LINE if x in m : NEW_LINE INDENT return - 1 NEW_LINE DEDENT m [ x ] = 1 NEW_LINE x *= 10 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT res = count ( 1 , 2 ) NEW_LINE print ( " INF " ) if res == - 1 else print ( res ) NEW_LINE res = count ( 5 , 3 ) NEW_LINE print ( " INF " ) if res == - 1 else print ( res ) NEW_LINE res = count ( 3 , 5 ) NEW_LINE print ( " INF " ) if res == - 1 else print ( res ) NEW_LINE DEDENT
Find smallest number n such that n XOR n + 1 equals to given k . | function to return the required n ; if k is of form 2 ^ i - 1 ; driver program
def xorCalc ( k ) : NEW_LINE INDENT if ( k == 1 ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT if ( ( ( k + 1 ) & k ) == 0 ) : NEW_LINE INDENT return k / 2 NEW_LINE DEDENT return 1 ; NEW_LINE DEDENT k = 31 NEW_LINE print ( int ( xorCalc ( k ) ) ) NEW_LINE
Find n | Python3 program to find n - th number containing only 4 and 7. ; If n is odd , append 4 and move to parent ; If n is even , append7 and move to parent ; Reverse res and return . ; Driver code
def reverse ( s ) : NEW_LINE INDENT if len ( s ) == 0 : NEW_LINE INDENT return s NEW_LINE DEDENT else : NEW_LINE INDENT return reverse ( s [ 1 : ] ) + s [ 0 ] NEW_LINE DEDENT DEDENT def findNthNo ( n ) : NEW_LINE INDENT res = " " ; NEW_LINE while ( n >= 1 ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT res = res + "4" ; NEW_LINE n = ( int ) ( ( n - 1 ) / 2 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT res = res + "7" ; NEW_LINE n = ( int ) ( ( n - 2 ) / 2 ) ; NEW_LINE DEDENT DEDENT return reverse ( res ) ; NEW_LINE DEDENT n = 13 ; NEW_LINE print ( findNthNo ( n ) ) ; NEW_LINE
Total number of divisors for a given number | program for finding no . of divisors ; sieve method for prime calculation ; Traversing through all prime numbers ; calculate number of divisor with formula total div = ( p1 + 1 ) * ( p2 + 1 ) * ... . . * ( pn + 1 ) where n = ( a1 ^ p1 ) * ( a2 ^ p2 ) . ... * ( an ^ pn ) ai being prime divisor for n and pi are their respective power in factorization ; Driver Code
def divCount ( n ) : NEW_LINE INDENT hh = [ 1 ] * ( n + 1 ) ; NEW_LINE p = 2 ; NEW_LINE while ( ( p * p ) < n ) : NEW_LINE INDENT if ( hh [ p ] == 1 ) : NEW_LINE INDENT for i in range ( ( p * 2 ) , n , p ) : NEW_LINE INDENT hh [ i ] = 0 ; NEW_LINE DEDENT DEDENT p += 1 ; NEW_LINE DEDENT total = 1 ; NEW_LINE for p in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( hh [ p ] == 1 ) : NEW_LINE INDENT count = 0 ; NEW_LINE if ( n % p == 0 ) : NEW_LINE INDENT while ( n % p == 0 ) : NEW_LINE INDENT n = int ( n / p ) ; NEW_LINE count += 1 ; NEW_LINE DEDENT total *= ( count + 1 ) ; NEW_LINE DEDENT DEDENT DEDENT return total ; NEW_LINE DEDENT n = 24 ; NEW_LINE print ( divCount ( n ) ) ; NEW_LINE
Maximum number of unique prime factors | Return smallest number having maximum prime factors . ; Sieve of eratosthenes method to count number of unique prime factors . ; Return maximum element in arr [ ] ; Driver Code
def maxPrimefactorNum ( N ) : NEW_LINE INDENT arr = [ 0 ] * ( N + 1 ) ; NEW_LINE i = 2 ; NEW_LINE while ( i * i <= N ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT for j in range ( 2 * i , N + 1 , i ) : NEW_LINE INDENT arr [ j ] += 1 ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE arr [ i ] = 1 ; NEW_LINE DEDENT return max ( arr ) ; NEW_LINE DEDENT N = 40 ; NEW_LINE print ( maxPrimefactorNum ( N ) ) ; NEW_LINE
Difference between two given times | Remove ' : ' and convert it into an integer ; Main function which finds difference ; Change string ( eg . 2 : 21 -- > 221 , 00 : 23 -- > 23 ) ; Difference between hours ; Difference between minutes ; Convert answer again in string with ' : ' ; Driver code
def removeColon ( s ) : NEW_LINE INDENT if ( len ( s ) == 4 ) : NEW_LINE INDENT s = s [ : 1 ] + s [ 2 : ] NEW_LINE DEDENT if ( len ( s ) == 5 ) : NEW_LINE INDENT s = s [ : 2 ] + s [ 3 : ] NEW_LINE DEDENT return int ( s ) NEW_LINE DEDENT def diff ( s1 , s2 ) : NEW_LINE INDENT time1 = removeColon ( s1 ) NEW_LINE time2 = removeColon ( s2 ) NEW_LINE hourDiff = time2 // 100 - time1 // 100 - 1 ; NEW_LINE minDiff = time2 % 100 + ( 60 - time1 % 100 ) NEW_LINE if ( minDiff >= 60 ) : NEW_LINE INDENT hourDiff += 1 NEW_LINE minDiff = minDiff - 60 NEW_LINE DEDENT res = str ( hourDiff ) + ' : ' + str ( minDiff ) NEW_LINE return res NEW_LINE DEDENT s1 = "14:00" NEW_LINE s2 = "16:45" NEW_LINE print ( diff ( s1 , s2 ) ) NEW_LINE
Sum of array elements that is first continuously increasing then decreasing | Efficient python method to find sum of the elements of array that is halfway increasing and then halfway decreassing ; Driver code
def arraySum ( arr , n ) : NEW_LINE INDENT x = ( n + 1 ) / 2 NEW_LINE return ( arr [ 0 ] - 1 ) * n + x * x NEW_LINE DEDENT arr = [ 10 , 11 , 12 , 13 , 12 , 11 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE print ( arraySum ( arr , n ) ) NEW_LINE
Number of digits in the product of two numbers | Python 3 implementation to count number of digits in the product of two numbers ; function to count number of digits in the product of two numbers ; if either of the number is 0 , then product will be 0 ; required count of digits ; Driver program to test above
import math NEW_LINE def countDigits ( a , b ) : NEW_LINE INDENT if ( a == 0 or b == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return math . floor ( math . log10 ( abs ( a ) ) + math . log10 ( abs ( b ) ) ) + 1 NEW_LINE DEDENT a = 33 NEW_LINE b = - 24 NEW_LINE print ( countDigits ( a , b ) ) NEW_LINE
Distributing M items in a circle of size N starting from K | n == > Size of circle m == > Number of items k == > Initial position ; n - k + 1 is number of positions before we reach beginning of circle If m is less than this value , then we can simply return ( m - 1 ) th position ; Let us compute remaining items before we reach beginning . ; We compute m % n to skip all complete rounds . If we reach end , we return n else we return m % n ; Driver code
def lastPosition ( n , m , k ) : NEW_LINE INDENT if ( m <= n - k + 1 ) : NEW_LINE return m + k - 1 NEW_LINE m = m - ( n - k + 1 ) NEW_LINE if ( m % n == 0 ) : NEW_LINE INDENT return n NEW_LINE DEDENT else : NEW_LINE INDENT return m % n NEW_LINE DEDENT DEDENT n = 5 NEW_LINE m = 8 NEW_LINE k = 2 NEW_LINE print lastPosition ( n , m , k ) NEW_LINE
An interesting solution to get all prime numbers smaller than n | Python3 program to prints prime numbers smaller than n ; Compute factorials and apply Wilson 's theorem. ; Driver code
def primesInRange ( n ) : NEW_LINE INDENT fact = 1 NEW_LINE for k in range ( 2 , n ) : NEW_LINE INDENT fact = fact * ( k - 1 ) NEW_LINE if ( ( fact + 1 ) % k == 0 ) : NEW_LINE INDENT print k NEW_LINE DEDENT DEDENT DEDENT n = 15 NEW_LINE primesInRange ( n ) NEW_LINE
A product array puzzle | Set 2 ( O ( 1 ) Space ) | Python program for product array puzzle with O ( n ) time and O ( 1 ) space . ; epsilon value to maintain precision ; to hold sum of all values ; output product for each index antilog to find original product value ; Driver code
import math NEW_LINE EPS = 1e-9 NEW_LINE def productPuzzle ( a , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += math . log10 ( a [ i ] ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print int ( ( EPS + pow ( 10.00 , sum - math . log10 ( a [ i ] ) ) ) ) , NEW_LINE DEDENT return NEW_LINE DEDENT a = [ 10 , 3 , 5 , 6 , 2 ] NEW_LINE n = len ( a ) NEW_LINE print " The ▁ product ▁ array ▁ is : ▁ " NEW_LINE productPuzzle ( a , n ) NEW_LINE
Change all even bits in a number to 0 | Returns modified number with all even bits 0. ; To store sum of bits at even positions . ; To store bits to shift ; One by one put all even bits to end ; If current last bit is set , add it to ans ; Next shift position ; Driver code
def changeEvenBits ( n ) : NEW_LINE INDENT to_subtract = 0 NEW_LINE m = 0 NEW_LINE x = n NEW_LINE while ( x ) : NEW_LINE INDENT if ( x & 1 ) : NEW_LINE INDENT to_subtract += ( 1 << m ) NEW_LINE DEDENT m += 2 NEW_LINE x >>= 2 NEW_LINE DEDENT return n - to_subtract NEW_LINE DEDENT n = 30 NEW_LINE print changeEvenBits ( n ) NEW_LINE
Find the number closest to n and divisible by m | Function to find the number closest to n and divisible by m ; Find the quotient ; 1 st possible closest number ; 2 nd possible closest number ; if true , then n1 is the required closest number ; else n2 is the required closest number ; Driver program to test above
def closestNumber ( n , m ) : NEW_LINE INDENT q = int ( n / m ) NEW_LINE n1 = m * q NEW_LINE if ( ( n * m ) > 0 ) : NEW_LINE INDENT n2 = ( m * ( q + 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT n2 = ( m * ( q - 1 ) ) NEW_LINE DEDENT if ( abs ( n - n1 ) < abs ( n - n2 ) ) : NEW_LINE INDENT return n1 NEW_LINE DEDENT return n2 NEW_LINE DEDENT n = 13 ; m = 4 NEW_LINE print ( closestNumber ( n , m ) ) NEW_LINE n = - 15 ; m = 6 NEW_LINE print ( closestNumber ( n , m ) ) NEW_LINE n = 0 ; m = 8 NEW_LINE print ( closestNumber ( n , m ) ) NEW_LINE n = 18 ; m = - 7 NEW_LINE print ( closestNumber ( n , m ) ) NEW_LINE
Check if a given number is Pronic | Python program to check and print Pronic Numbers upto 200 ; function to check Pronic Number ; Checking Pronic Number by multiplying consecutive numbers ; Printing Pronic Numbers upto 200
import math NEW_LINE def checkPronic ( x ) : NEW_LINE INDENT i = 0 NEW_LINE while ( i <= ( int ) ( math . sqrt ( x ) ) ) : NEW_LINE INDENT if ( x == i * ( i + 1 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT return False NEW_LINE DEDENT i = 0 NEW_LINE while ( i <= 200 ) : NEW_LINE INDENT if checkPronic ( i ) : NEW_LINE INDENT print i , NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT
Find minimum sum of factors of number | To find minimum sum of product of number ; Find factors of number and add to the sum ; Return sum of numbers having minimum product ; Driver Code
def findMinSum ( num ) : NEW_LINE INDENT sum = 0 NEW_LINE i = 2 NEW_LINE while ( i * i <= num ) : NEW_LINE INDENT while ( num % i == 0 ) : NEW_LINE INDENT sum += i NEW_LINE num /= i NEW_LINE DEDENT i += 1 NEW_LINE DEDENT sum += num NEW_LINE return sum NEW_LINE DEDENT num = 12 NEW_LINE print findMinSum ( num ) NEW_LINE
Minimum number with digits as 4 and 7 only and given sum | Prints minimum number with given digit sum and only allowed digits as 4 and 7. ; Cases where all remaining digits are 4 or 7 ( Remaining sum of digits should be multiple of 4 or 7 ) ; If both 4 s and 7 s are there in digit sum , we subtract a 4. ; Driver code
def findMin ( s ) : NEW_LINE INDENT a , b = 0 , 0 NEW_LINE while ( s > 0 ) : NEW_LINE INDENT if ( s % 7 == 0 ) : NEW_LINE INDENT b += 1 NEW_LINE s -= 7 NEW_LINE DEDENT elif ( s % 4 == 0 ) : NEW_LINE INDENT a += 1 NEW_LINE s -= 4 NEW_LINE DEDENT else : NEW_LINE INDENT a += 1 NEW_LINE s -= 4 NEW_LINE DEDENT DEDENT string = " " NEW_LINE if ( s < 0 ) : NEW_LINE INDENT string = " - 1" NEW_LINE return string NEW_LINE DEDENT string += "4" * a NEW_LINE string += "7" * b NEW_LINE return string NEW_LINE DEDENT print findMin ( 15 ) NEW_LINE
Add minimum number to an array so that the sum becomes even | Function to find out minimum number ; Count odd number of terms in array ; Driver code
def minNum ( arr , n ) : NEW_LINE INDENT odd = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 ) : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT DEDENT if ( odd % 2 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 2 NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print minNum ( arr , n ) NEW_LINE
Find maximum number that can be formed using digits of a given number | Function to print maximum number ; Hashed array to store count of digits ; Converting given number to string ; Updating the count array ; Result stores final number ; traversing the count array to calculate the maximum number ; return the result ; Driver code
def printMaximum ( inum ) : NEW_LINE INDENT count = [ 0 for x in range ( 10 ) ] NEW_LINE string = str ( num ) NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT count [ int ( string [ i ] ) ] = count [ int ( string [ i ] ) ] + 1 NEW_LINE DEDENT result = 0 NEW_LINE multiplier = 1 NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT while count [ i ] > 0 : NEW_LINE INDENT result = result + ( i * multiplier ) NEW_LINE count [ i ] = count [ i ] - 1 NEW_LINE multiplier = multiplier * 10 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT num = 38293367 NEW_LINE print printMaximum ( num ) NEW_LINE
Binomial Random Variables | function to calculate nCr i . e . , number of ways to choose r out of n objects ; Since nCr is same as nC ( n - r ) To decrease number of iterations ; function to calculate binomial r . v . probability ; Driver code
def nCr ( n , r ) : NEW_LINE INDENT if ( r > n / 2 ) : NEW_LINE INDENT r = n - r ; NEW_LINE DEDENT answer = 1 ; NEW_LINE for i in range ( 1 , r + 1 ) : NEW_LINE INDENT answer *= ( n - r + i ) ; NEW_LINE answer /= i ; NEW_LINE DEDENT return answer ; NEW_LINE DEDENT def binomialProbability ( n , k , p ) : NEW_LINE INDENT return ( nCr ( n , k ) * pow ( p , k ) * pow ( 1 - p , n - k ) ) ; NEW_LINE DEDENT n = 10 ; NEW_LINE k = 5 ; NEW_LINE p = 1.0 / 3 ; NEW_LINE probability = binomialProbability ( n , k , p ) ; NEW_LINE print ( " Probability ▁ of " , k , " heads ▁ when ▁ a ▁ coin ▁ is ▁ tossed " , end = " ▁ " ) ; NEW_LINE print ( n , " times ▁ where ▁ probability ▁ of ▁ each ▁ head ▁ is " , round ( p , 6 ) ) ; NEW_LINE print ( " is ▁ = ▁ " , round ( probability , 6 ) ) ; NEW_LINE
Find pair with maximum GCD in an array | Python program to Find pair with maximum GCD in an array ; function to find GCD of pair with max GCD in the array ; Computing highest element ; Array to store the count of divisors i . e . Potential GCDs ; Iterating over every element ; Calculating all the divisors ; Divisor found ; Incrementing count for divisor ; Element / divisor is also a divisor Checking if both divisors are not same ; Checking the highest potential GCD ; If this divisor can divide at least 2 numbers , it is a GCD of at least 1 pair ; Array in which pair with max GCD is to be found ; Size of array
import math NEW_LINE def findMaxGCD ( arr , n ) : NEW_LINE INDENT high = 0 NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT high = max ( high , arr [ i ] ) NEW_LINE i = i + 1 NEW_LINE DEDENT divisors = [ 0 ] * ( high + 1 ) NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT j = 1 NEW_LINE while j <= math . sqrt ( arr [ i ] ) : NEW_LINE INDENT if ( arr [ i ] % j == 0 ) : NEW_LINE INDENT divisors [ j ] = divisors [ j ] + 1 NEW_LINE if ( j != arr [ i ] / j ) : NEW_LINE INDENT divisors [ arr [ i ] / j ] = divisors [ arr [ i ] / j ] NEW_LINE INDENT + 1 NEW_LINE DEDENT DEDENT DEDENT j = j + 1 NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT i = high NEW_LINE while i >= 1 : NEW_LINE INDENT if ( divisors [ i ] > 1 ) : NEW_LINE INDENT return i NEW_LINE DEDENT i = i - 1 NEW_LINE DEDENT return 1 NEW_LINE DEDENT arr = [ 1 , 2 , 4 , 8 , 8 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE print findMaxGCD ( arr , n ) NEW_LINE
Find pair with maximum GCD in an array | function to find GCD of pair with max GCD in the array ; Calculating MAX in array ; Maintaining count array ; Variable to store the multiples of a number ; Iterating from MAX to 1 GCD is always between MAX and 1 The first GCD found will be the highest as we are decrementing the potential GCD ; Iterating from current potential GCD till it is less than MAX ; A multiple found ; Incrementing potential GCD by itself To check i , 2 i , 3 i ... . ; 2 multiples found , max GCD found ; Array in which pair with max GCD is to be found ; Size of array
def findMaxGCD ( arr , n ) : NEW_LINE INDENT high = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT high = max ( high , arr [ i ] ) NEW_LINE DEDENT count = [ 0 ] * ( high + 1 ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT count [ arr [ i ] ] += 1 NEW_LINE DEDENT counter = 0 NEW_LINE for i in range ( high , 0 , - 1 ) : NEW_LINE INDENT j = i NEW_LINE while ( j <= high ) : NEW_LINE INDENT if ( count [ j ] > 0 ) : NEW_LINE INDENT counter += count [ j ] NEW_LINE DEDENT j += i NEW_LINE if ( counter == 2 ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT counter = 0 NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 4 , 8 , 8 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMaxGCD ( arr , n ) ) NEW_LINE
Evil Number | returns number of 1 s from the binary number ; Counting 1 s ; Check if number is evil or not ; Converting n to binary form ; Calculating Remainder Storing the remainders in binary form as a number ; Calling the count_one function to count and return number of 1 s in bin ; Driver Code
def count_one ( n ) : NEW_LINE INDENT c_one = 0 NEW_LINE while n != 0 : NEW_LINE INDENT rem = n % 10 NEW_LINE if rem == 1 : NEW_LINE INDENT c_one = c_one + 1 NEW_LINE DEDENT n = n / 10 NEW_LINE DEDENT return c_one NEW_LINE DEDENT def checkEvil ( n ) : NEW_LINE INDENT i = 0 NEW_LINE binary = 0 NEW_LINE while n != 0 : NEW_LINE INDENT r = n % 2 NEW_LINE binary = binary + r * ( int ( 10 ** i ) ) NEW_LINE n = n / 2 NEW_LINE DEDENT n_one = count_one ( binary ) NEW_LINE if n_one % 2 == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT num = 32 NEW_LINE check = checkEvil ( num ) NEW_LINE if check : NEW_LINE INDENT print num , " is ▁ Evil ▁ Number " NEW_LINE DEDENT else : NEW_LINE INDENT print num , " is ▁ Odious ▁ Number " NEW_LINE DEDENT
Count number of pairs ( A <= N , B <= N ) such that gcd ( A , B ) is B | returns number of valid pairs ; initialize k ; loop till imin <= n ; Initialize result ; max i with given k floor ( n / k ) ; adding k * ( number of i with floor ( n / i ) = k to ans ; set imin = imax + 1 and k = n / imin ; Driver code
def CountPairs ( n ) : NEW_LINE INDENT k = n NEW_LINE imin = 1 NEW_LINE ans = 0 NEW_LINE while ( imin <= n ) : NEW_LINE INDENT imax = n / k NEW_LINE ans += k * ( imax - imin + 1 ) NEW_LINE imin = imax + 1 NEW_LINE k = n / imin NEW_LINE DEDENT return ans NEW_LINE DEDENT print ( CountPairs ( 1 ) ) NEW_LINE print ( CountPairs ( 2 ) ) NEW_LINE print ( CountPairs ( 3 ) ) NEW_LINE
Find the last digit of given series | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Returns modulo inverse of a with respect to m using extended Euclid Algorithm ; q is quotient ; m is remainder now , process same as Euclid 's algo ; Make x1 positive ; Function to calculate the above expression ; Initialize the result ; Compute first part of expression ; Compute second part of expression i . e . , ( ( 4 ^ ( n + 1 ) - 1 ) / 3 ) mod 10 Since division of 3 in modulo can ' t ▁ ▁ ▁ be ▁ performed ▁ directly ▁ therefore ▁ we ▁ ▁ ▁ need ▁ to ▁ find ▁ it ' s modulo Inverse ; Driver code
def powermod ( x , y , p ) : NEW_LINE INDENT while ( y > 0 ) : NEW_LINE INDENT if ( ( y & 1 ) > 0 ) : NEW_LINE INDENT res = ( res * x ) % p ; NEW_LINE DEDENT x = ( x * x ) % p ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT def modInverse ( a , m ) : NEW_LINE INDENT m0 = m ; NEW_LINE x0 = 0 ; NEW_LINE x1 = 1 ; NEW_LINE if ( m == 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT while ( a > 1 ) : NEW_LINE INDENT q = int ( a / m ) ; NEW_LINE t = m ; NEW_LINE m = a % m ; NEW_LINE a = t ; NEW_LINE t = x0 ; NEW_LINE x0 = x1 - q * x0 ; NEW_LINE x1 = t ; NEW_LINE DEDENT if ( x1 < 0 ) : NEW_LINE INDENT x1 += m0 ; NEW_LINE DEDENT return x1 ; NEW_LINE DEDENT def evaluteExpression ( n ) : NEW_LINE INDENT firstsum = 0 ; NEW_LINE mod = 10 ; NEW_LINE i = 2 ; NEW_LINE j = 0 ; NEW_LINE while ( ( 1 << j ) <= n ) : NEW_LINE INDENT firstsum = ( firstsum + i ) % mod ; NEW_LINE i *= i ; NEW_LINE j += 1 ; NEW_LINE DEDENT secondsum = ( powermod ( 4 , n + 1 , mod ) - 1 ) * modInverse ( 3 , mod ) ; NEW_LINE return ( firstsum * secondsum ) % mod ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( evaluteExpression ( n ) ) ; NEW_LINE n = 10 ; NEW_LINE print ( evaluteExpression ( n ) ) ; NEW_LINE
Finding power of prime number p in n ! | Returns power of p in n ! ; initializing answer ; initializing ; loop until temp <= n ; add number of numbers divisible by n ; each time multiply temp by p ; Driver Code
def PowerOFPINnfactorial ( n , p ) : NEW_LINE INDENT ans = 0 NEW_LINE temp = p NEW_LINE while ( temp <= n ) : NEW_LINE INDENT ans += n / temp NEW_LINE temp = temp * p NEW_LINE DEDENT return int ( ans ) NEW_LINE DEDENT print ( PowerOFPINnfactorial ( 4 , 2 ) ) NEW_LINE
Program for Decimal to Binary Conversion | Function to return the binary equivalent of decimal value N ; To store the binary number ; Count used to store exponent value ; Driver code
def decimalToBinary ( N ) : NEW_LINE INDENT B_Number = 0 NEW_LINE cnt = 0 NEW_LINE while ( N != 0 ) : NEW_LINE INDENT rem = N % 2 NEW_LINE c = pow ( 10 , cnt ) NEW_LINE B_Number += rem * c NEW_LINE N //= 2 NEW_LINE cnt += 1 NEW_LINE DEDENT return B_Number NEW_LINE DEDENT N = 17 NEW_LINE print ( decimalToBinary ( N ) ) NEW_LINE
Program for Binary To Decimal Conversion | Function to convert binary to decimal ; Initializing base value to 1 , i . e 2 ^ 0 ; Driver Code
def binaryToDecimal ( n ) : NEW_LINE INDENT num = n ; NEW_LINE dec_value = 0 ; NEW_LINE base = 1 ; NEW_LINE temp = num ; NEW_LINE while ( temp ) : NEW_LINE INDENT last_digit = temp % 10 ; NEW_LINE temp = int ( temp / 10 ) ; NEW_LINE dec_value += last_digit * base ; NEW_LINE base = base * 2 ; NEW_LINE DEDENT return dec_value ; NEW_LINE DEDENT num = 10101001 ; NEW_LINE print ( binaryToDecimal ( num ) ) ; NEW_LINE
Calculating Factorials using Stirling Approximation | Python3 program for calculating factorial of a number using Stirling Approximation ; Function for calculating factorial ; value of natural e ; evaluating factorial using stirling approximation ; Driver Code
import math NEW_LINE def stirlingFactorial ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT e = 2.71 NEW_LINE z = ( math . sqrt ( 2 * 3.14 * n ) * math . pow ( ( n / e ) , n ) ) NEW_LINE return math . floor ( z ) NEW_LINE DEDENT print ( stirlingFactorial ( 1 ) ) NEW_LINE print ( stirlingFactorial ( 2 ) ) NEW_LINE print ( stirlingFactorial ( 3 ) ) NEW_LINE print ( stirlingFactorial ( 4 ) ) NEW_LINE print ( stirlingFactorial ( 5 ) ) NEW_LINE print ( stirlingFactorial ( 6 ) ) NEW_LINE print ( stirlingFactorial ( 7 ) ) NEW_LINE
Count pairs with Odd XOR | A function will return number of pair whose XOR is odd ; To store count of odd and even numbers ; Increase even if number is even otherwise increase odd ; Return number of pairs ; Driver Code
def countXorPair ( arr , n ) : NEW_LINE INDENT odd = 0 NEW_LINE even = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] % 2 == 0 : NEW_LINE INDENT even += 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT DEDENT return odd * even NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countXorPair ( arr , n ) ) NEW_LINE DEDENT
Lychrel Number Implementation | Max Iterations ; Function to check whether number is Lychrel Number ; Function to check whether the number is Palindrome ; Function to reverse the number ; Driver Code
MAX_ITERATIONS = 20 ; NEW_LINE def isLychrel ( number ) : NEW_LINE INDENT for i in range ( MAX_ITERATIONS ) : NEW_LINE INDENT number = number + reverse ( number ) ; NEW_LINE if ( isPalindrome ( number ) ) : NEW_LINE INDENT return " false " ; NEW_LINE DEDENT DEDENT return " true " ; NEW_LINE DEDENT def isPalindrome ( number ) : NEW_LINE INDENT return number == reverse ( number ) ; NEW_LINE DEDENT def reverse ( number ) : NEW_LINE INDENT reverse = 0 ; NEW_LINE while ( number > 0 ) : NEW_LINE INDENT remainder = number % 10 ; NEW_LINE reverse = ( reverse * 10 ) + remainder ; NEW_LINE number = int ( number / 10 ) ; NEW_LINE DEDENT return reverse ; NEW_LINE DEDENT number = 295 ; NEW_LINE print ( number , " ▁ is ▁ lychrel ? ▁ " , isLychrel ( number ) ) ; NEW_LINE
Rectangular ( or Pronic ) Numbers | Returns n - th rectangular number ; Driver code
def findRectNum ( n ) : NEW_LINE INDENT return n * ( n + 1 ) NEW_LINE DEDENT n = 6 NEW_LINE print ( findRectNum ( n ) ) NEW_LINE
Program for Muller Method | Python3 Program to find root of a function , f ( x ) ; Function to calculate f ( x ) ; Taking f ( x ) = x ^ 3 + 2 x ^ 2 + 10 x - 20 ; Calculating various constants required to calculate x3 ; Taking the root which is closer to x2 ; checking for resemblance of x3 with x2 till two decimal places ; Driver Code
import math ; NEW_LINE MAX_ITERATIONS = 10000 ; NEW_LINE def f ( x ) : NEW_LINE INDENT return ( 1 * pow ( x , 3 ) + 2 * x * x + 10 * x - 20 ) ; NEW_LINE DEDENT def Muller ( a , b , c ) : NEW_LINE INDENT res = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( True ) : NEW_LINE INDENT f1 = f ( a ) ; f2 = f ( b ) ; f3 = f ( c ) ; NEW_LINE d1 = f1 - f3 ; NEW_LINE d2 = f2 - f3 ; NEW_LINE h1 = a - c ; NEW_LINE h2 = b - c ; NEW_LINE a0 = f3 ; NEW_LINE a1 = ( ( ( d2 * pow ( h1 , 2 ) ) - ( d1 * pow ( h2 , 2 ) ) ) / ( ( h1 * h2 ) * ( h1 - h2 ) ) ) ; NEW_LINE a2 = ( ( ( d1 * h2 ) - ( d2 * h1 ) ) / ( ( h1 * h2 ) * ( h1 - h2 ) ) ) ; NEW_LINE x = ( ( - 2 * a0 ) / ( a1 + abs ( math . sqrt ( a1 * a1 - 4 * a0 * a2 ) ) ) ) ; NEW_LINE y = ( ( - 2 * a0 ) / ( a1 - abs ( math . sqrt ( a1 * a1 - 4 * a0 * a2 ) ) ) ) ; NEW_LINE if ( x >= y ) : NEW_LINE INDENT res = x + c ; NEW_LINE DEDENT else : NEW_LINE INDENT res = y + c ; NEW_LINE DEDENT m = res * 100 ; NEW_LINE n = c * 100 ; NEW_LINE m = math . floor ( m ) ; NEW_LINE n = math . floor ( n ) ; NEW_LINE if ( m == n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT a = b ; NEW_LINE b = c ; NEW_LINE c = res ; NEW_LINE if ( i > MAX_ITERATIONS ) : NEW_LINE INDENT print ( " Root ▁ cannot ▁ be ▁ found ▁ using " , " Muller ' s ▁ method " ) ; NEW_LINE break ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT if ( i <= MAX_ITERATIONS ) : NEW_LINE INDENT print ( " The ▁ value ▁ of ▁ the ▁ root ▁ is " , round ( res , 4 ) ) ; NEW_LINE DEDENT DEDENT a = 0 ; NEW_LINE b = 1 ; NEW_LINE c = 2 ; NEW_LINE Muller ( a , b , c ) ; NEW_LINE
Optimized Euler Totient Function for Multiple Evaluations | Python3 program to efficiently compute values of euler totient function for multiple inputs . ; Stores prime numbers upto MAX - 1 values ; Finds prime numbers upto MAX - 1 and stores them in vector p ; if prime [ i ] is not marked before ; fill vector for every newly encountered prime ; run this loop till square root of MAX , mark the index i * j as not prime ; function to find totient of n ; this loop runs sqrt ( n / ln ( n ) ) times ; subtract multiples of p [ i ] from r ; Remove all occurrences of p [ i ] in n ; when n has prime factor greater than sqrt ( n ) ; preprocess all prime numbers upto 10 ^ 5
MAX = 100001 ; NEW_LINE p = [ ] ; NEW_LINE def sieve ( ) : NEW_LINE INDENT isPrime = [ 0 ] * ( MAX + 1 ) ; NEW_LINE for i in range ( 2 , MAX + 1 ) : NEW_LINE INDENT if ( isPrime [ i ] == 0 ) : NEW_LINE INDENT p . append ( i ) ; NEW_LINE j = 2 ; NEW_LINE while ( i * j <= MAX ) : NEW_LINE INDENT isPrime [ i * j ] = 1 ; NEW_LINE j += 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def phi ( n ) : NEW_LINE INDENT res = n ; NEW_LINE i = 0 ; NEW_LINE while ( p [ i ] * p [ i ] <= n ) : NEW_LINE INDENT if ( n % p [ i ] == 0 ) : NEW_LINE INDENT res -= int ( res / p [ i ] ) ; NEW_LINE while ( n % p [ i ] == 0 ) : NEW_LINE INDENT n = int ( n / p [ i ] ) ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT if ( n > 1 ) : NEW_LINE INDENT res -= int ( res / n ) ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT sieve ( ) ; NEW_LINE print ( phi ( 11 ) ) ; NEW_LINE print ( phi ( 21 ) ) ; NEW_LINE print ( phi ( 31 ) ) ; NEW_LINE print ( phi ( 41 ) ) ; NEW_LINE print ( phi ( 51 ) ) ; NEW_LINE print ( phi ( 61 ) ) ; NEW_LINE print ( phi ( 91 ) ) ; NEW_LINE print ( phi ( 101 ) ) ; NEW_LINE
Finding n | Python3 implementation for finding nth number made of prime digits only ; Prints n - th number where each digit is a prime number ; Finding the length of n - th number ; Count of numbers with len - 1 digits ; Count of numbers with i digits ; if i is the length of such number then n < 4 * ( 4 ^ ( i - 1 ) - 1 ) / 3 and n >= 4 * ( 4 ^ i - 1 ) / 3 if a valid i is found break the loop ; check for i + 1 ; Finding ith digit at ith place ; j = 1 means 2 j = 2 means ... j = 4 means 7 ; if prev_count + 4 ^ ( len - i ) is less than n , increase prev_count by 4 ^ ( x - i ) ; else print the ith digit and break ; Driver Code
import math NEW_LINE def nthprimedigitsnumber ( n ) : NEW_LINE INDENT len = 1 ; NEW_LINE prev_count = 0 ; NEW_LINE while ( 1 ) : NEW_LINE INDENT curr_count = ( prev_count + math . pow ( 4 , len ) ) ; NEW_LINE if ( prev_count < n and curr_count >= n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT len += 1 ; NEW_LINE prev_count = curr_count ; NEW_LINE DEDENT for i in range ( 1 , len + 1 ) : NEW_LINE INDENT for j in range ( 1 , 5 ) : NEW_LINE INDENT if ( prev_count + pow ( 4 , len - i ) < n ) : NEW_LINE INDENT prev_count += pow ( 4 , len - i ) ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( j == 1 ) : NEW_LINE INDENT print ( "2" , end = " " ) ; NEW_LINE DEDENT elif ( j == 2 ) : NEW_LINE INDENT print ( "3" , end = " " ) ; NEW_LINE DEDENT elif ( j == 3 ) : NEW_LINE INDENT print ( "5" , end = " " ) ; NEW_LINE DEDENT elif ( j == 4 ) : NEW_LINE INDENT print ( "7" , end = " " ) ; NEW_LINE DEDENT break ; NEW_LINE DEDENT DEDENT DEDENT print ( ) ; NEW_LINE DEDENT nthprimedigitsnumber ( 10 ) ; NEW_LINE nthprimedigitsnumber ( 21 ) ; NEW_LINE
CassiniΓ’ €ℒ s Identity | Returns ( - 1 ) ^ n ; Driver program
def cassini ( n ) : NEW_LINE INDENT return - 1 if ( n & 1 ) else 1 NEW_LINE DEDENT n = 5 NEW_LINE print ( cassini ( n ) ) NEW_LINE
Find if a number is divisible by every number in a list | Python program which check is a number divided with every element in list or not ; Checking if a number is divided by every element or not ; Driver code
def findNoIsDivisibleOrNot ( n , l = [ ] ) : NEW_LINE INDENT for i in range ( 0 , len ( l ) ) : NEW_LINE INDENT if l [ i ] % n != 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT return 1 NEW_LINE DEDENT l = [ 14 , 12 , 4 , 18 ] NEW_LINE n = 2 NEW_LINE if findNoIsDivisibleOrNot ( n , l ) == 1 : NEW_LINE INDENT print " Yes " NEW_LINE DEDENT else : NEW_LINE INDENT print " No " NEW_LINE DEDENT
Find a range of composite numbers of given length | function to calculate factorial ; to print range of length n having all composite integers ; driver code to test above functions
def factorial ( n ) : NEW_LINE INDENT a = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT a *= i NEW_LINE DEDENT return a NEW_LINE DEDENT def printRange ( n ) : NEW_LINE INDENT a = factorial ( n + 2 ) + 2 NEW_LINE b = a + n - 1 NEW_LINE print ( " [ " + str ( a ) + " , ▁ " + str ( b ) + " ] " ) NEW_LINE DEDENT n = 3 NEW_LINE printRange ( n ) NEW_LINE
Find minimum value to assign all array elements so that array product becomes greater | Python3 program to find minimum value that can be assigned to all elements so that product becomes greater than current product . ; sort the array to apply Binary search ; using log property add every logarithmic value of element to val val = 0 where ld is long double ; set left and right extremities to find min value ; multiplying n to mid , to find the correct min value ; Driver code
import math NEW_LINE def findMinValue ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT val += ( math . log ( arr [ i ] ) ) NEW_LINE DEDENT left = arr [ 0 ] NEW_LINE right = arr [ n - 1 ] + 1 NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = ( left + right ) // 2 NEW_LINE temp = n * ( math . log ( mid ) ) NEW_LINE if ( val < temp ) : NEW_LINE INDENT ans = mid NEW_LINE right = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 2 , 1 , 10 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMinValue ( arr , n ) ) NEW_LINE DEDENT
Find the sum of all the terms in the n | Python 3 implementation to find the sum of all the terms in the nth row of the given series ; function to find the required sum ; sum = n * ( 2 * n ^ 2 + 1 ) ; Driver Code
from math import pow NEW_LINE def sumOfTermsInNthRow ( n ) : NEW_LINE INDENT sum = n * ( 2 * pow ( n , 2 ) + 1 ) NEW_LINE return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE print ( " Sum ▁ of ▁ all ▁ the ▁ terms ▁ in ▁ nth ▁ row ▁ = " , int ( sumOfTermsInNthRow ( n ) ) ) NEW_LINE DEDENT
First digit in product of an array of numbers | Python implementation to find first digit of a single number ; Keep dividing by 10 until it is greater than equal to 10 ; driver function
def firstDigit ( x ) : NEW_LINE INDENT while ( x >= 10 ) : NEW_LINE INDENT x = x // 10 NEW_LINE DEDENT return x NEW_LINE DEDENT print ( firstDigit ( 12345 ) ) NEW_LINE print ( firstDigit ( 5432 ) ) NEW_LINE
Find the occurrences of digit d in the range [ 0. . n ] | Python3 program to count appearances of a digit ' d ' in range from [ 0. . n ] ; Initialize result ; Count appearances in numbers starting from d . ; When the last digit is equal to d ; When the first digit is equal to d then ; increment result as well as number ; In case of reverse of number such as 12 and 21 ; Driver code
import math ; NEW_LINE def getOccurence ( n , d ) : NEW_LINE INDENT result = 0 ; NEW_LINE itr = d ; NEW_LINE while ( itr <= n ) : NEW_LINE INDENT if ( itr % 10 == d ) : NEW_LINE INDENT result += 1 ; NEW_LINE DEDENT if ( itr != 0 and math . floor ( itr / 10 ) == d ) : NEW_LINE INDENT result += 1 ; NEW_LINE itr += 1 ; NEW_LINE DEDENT elif ( math . floor ( itr / 10 ) == d - 1 ) : NEW_LINE INDENT itr = itr + ( 10 - d ) ; NEW_LINE DEDENT else : NEW_LINE INDENT itr = itr + 10 ; NEW_LINE DEDENT DEDENT return result ; NEW_LINE DEDENT n = 11 ; NEW_LINE d = 1 ; NEW_LINE print ( getOccurence ( n , d ) ) ; NEW_LINE
Program to calculate the value of sin ( x ) and cos ( x ) using Expansion | Python 3 code for implementing cos function ; Function for calculation ; Converting degrees to radian ; maps the sum along the series ; holds the actual value of sin ( n ) ; Driver Code
from math import fabs , cos NEW_LINE def cal_cos ( n ) : NEW_LINE INDENT accuracy = 0.0001 NEW_LINE n = n * ( 3.142 / 180.0 ) NEW_LINE x1 = 1 NEW_LINE cosx = x1 NEW_LINE cosval = cos ( n ) NEW_LINE i = 1 NEW_LINE denominator = 2 * i * ( 2 * i - 1 ) NEW_LINE x1 = - x1 * n * n / denominator NEW_LINE cosx = cosx + x1 NEW_LINE i = i + 1 NEW_LINE while ( accuracy <= fabs ( cosval - cosx ) ) : NEW_LINE INDENT denominator = 2 * i * ( 2 * i - 1 ) NEW_LINE x1 = - x1 * n * n / denominator NEW_LINE cosx = cosx + x1 NEW_LINE i = i + 1 NEW_LINE DEDENT print ( ' { 0 : . 6 } ' . format ( cosx ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 30 NEW_LINE cal_cos ( n ) NEW_LINE DEDENT
Find sum of digits in factorial of a number | Function to multiply x with large number stored in vector v . Result is stored in v . ; Calculate res + prev carry ; updation at ith position ; Returns sum of digits in n ! ; One by one multiply i to current vector and update the vector . ; Find sum of digits in vector v [ ] ; Driver code
def multiply ( v , x ) : NEW_LINE INDENT carry = 0 NEW_LINE size = len ( v ) NEW_LINE for i in range ( size ) : NEW_LINE INDENT res = carry + v [ i ] * x NEW_LINE v [ i ] = res % 10 NEW_LINE carry = res // 10 NEW_LINE DEDENT while ( carry != 0 ) : NEW_LINE INDENT v . append ( carry % 10 ) NEW_LINE carry //= 10 NEW_LINE DEDENT DEDENT def findSumOfDigits ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT multiply ( v , i ) NEW_LINE DEDENT sum = 0 NEW_LINE size = len ( v ) NEW_LINE for i in range ( size ) : NEW_LINE INDENT sum += v [ i ] NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1000 NEW_LINE print ( findSumOfDigits ( n ) ) NEW_LINE DEDENT
Find other two sides of a right angle triangle | Finds two sides of a right angle triangle if it they exist . ; if n is odd ; case of n = 1 handled separately ; case of n = 2 handled separately ; Driver Code
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 ( " b ▁ = " , b , " , ▁ 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 ( " b ▁ = " , b " , ▁ c ▁ = " , c ) NEW_LINE DEDENT DEDENT DEDENT a = 3 NEW_LINE printOtherSides ( a ) NEW_LINE
Minimum positive integer to divide a number such that the result is an odd | Python3 program to make a number odd ; Return 1 if already odd ; Check on dividing with a number when the result becomes odd Return that number ; If n is divided by i and n / i is odd then return i ; Driver code
def makeOdd ( n ) : NEW_LINE INDENT if n % 2 != 0 : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT for i in range ( 2 , n ) : NEW_LINE INDENT if ( n % i == 0 and ( int ) ( n / i ) % 2 == 1 ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT DEDENT n = 36 ; NEW_LINE print ( makeOdd ( n ) ) ; NEW_LINE
XOR of all subarray XORs | Set 2 | Returns XOR of all subarray xors ; if even number of terms are there , all numbers will appear even number of times . So result is 0. ; else initialize result by 0 as ( a xor 0 = a ) ; Driver code
def getTotalXorOfSubarrayXors ( arr , N ) : NEW_LINE INDENT if ( N % 2 == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT res = 0 NEW_LINE for i in range ( 0 , N , 2 ) : NEW_LINE INDENT res ^= arr [ i ] NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 5 , 2 , 4 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE print ( getTotalXorOfSubarrayXors ( arr , N ) ) NEW_LINE DEDENT
Fill array with 1 's using minimum iterations of filling neighbors | Returns count of iterations to fill arr [ ] with 1 s . ; Start traversing the array ; Traverse until a 0 is found ; Count contiguous 0 s ; Condition for Case 3 ; Condition to check if Case 1 satisfies : ; If count_zero is even ; If count_zero is odd ; Reset count_zero ; Case 2 ; Update res ; Driver code
def countIterations ( arr , n ) : NEW_LINE INDENT oneFound = False ; NEW_LINE res = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT oneFound = True ; NEW_LINE DEDENT while ( i < n and arr [ i ] == 1 ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT count_zero = 0 ; NEW_LINE while ( i < n and arr [ i ] == 0 ) : NEW_LINE INDENT count_zero += 1 ; NEW_LINE i += 1 ; NEW_LINE DEDENT if ( oneFound == False and i == n ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT curr_count = 0 ; NEW_LINE if ( i < n and oneFound == True ) : NEW_LINE INDENT if ( ( count_zero & 1 ) == 0 ) : NEW_LINE INDENT curr_count = count_zero // 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT curr_count = ( count_zero + 1 ) // 2 ; NEW_LINE DEDENT count_zero = 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT curr_count = count_zero ; NEW_LINE count_zero = 0 ; NEW_LINE DEDENT res = max ( res , curr_count ) ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT arr = [ 0 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countIterations ( arr , n ) ) ; NEW_LINE
Interesting facts about Fibonacci numbers | Python3 program to demonstrate that Fibonacci numbers that are divisible by their indexes have indexes as either power of 5 or multiple of 12. ; storing Fibonacci numbers
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT MAX = 100 NEW_LINE arr = [ 0 for i in range ( MAX ) ] NEW_LINE arr [ 0 ] = 0 NEW_LINE arr [ 1 ] = 1 NEW_LINE for i in range ( 2 , MAX ) : NEW_LINE INDENT arr [ i ] = arr [ i - 1 ] + arr [ i - 2 ] NEW_LINE DEDENT print ( " Fibonacci ▁ numbers ▁ divisible ▁ by ▁ their ▁ indexes ▁ are ▁ : " ) NEW_LINE for i in range ( 1 , MAX ) : NEW_LINE INDENT if ( arr [ i ] % i == 0 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT
Express a number as sum of consecutive numbers | Print consecutive numbers from last to first ; Driver code
def printConsecutive ( last , first ) : NEW_LINE INDENT print ( first , end = " " ) NEW_LINE first += 1 NEW_LINE for x in range ( first , last + 1 ) : NEW_LINE INDENT print ( " ▁ + " , x , end = " " ) NEW_LINE DEDENT DEDENT def findConsecutive ( N ) : NEW_LINE INDENT for last in range ( 1 , N ) : NEW_LINE INDENT for first in range ( 0 , last ) : NEW_LINE INDENT if 2 * N == ( last - first ) * ( last + first + 1 ) : NEW_LINE INDENT print ( N , " = ▁ " , end = " " ) NEW_LINE printConsecutive ( last , first + 1 ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT print ( " - 1" ) NEW_LINE DEDENT n = 12 NEW_LINE findConsecutive ( n ) NEW_LINE
Find n | Return n - th number in series made of 4 and 7 ; create an array of size ( n + 1 ) ; If i is odd ; Driver code
def printNthElement ( n ) : NEW_LINE INDENT arr = [ 0 ] * ( n + 1 ) ; NEW_LINE arr [ 1 ] = 4 NEW_LINE arr [ 2 ] = 7 NEW_LINE for i in range ( 3 , n + 1 ) : NEW_LINE INDENT if ( i % 2 != 0 ) : NEW_LINE INDENT arr [ i ] = arr [ i // 2 ] * 10 + 4 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = arr [ ( i // 2 ) - 1 ] * 10 + 7 NEW_LINE DEDENT DEDENT return arr [ n ] NEW_LINE DEDENT n = 6 NEW_LINE print ( printNthElement ( n ) ) NEW_LINE
Maximum sum of distinct numbers such that LCM of these numbers is N | Returns maximum sum of numbers with LCM as N ; Initialize result ; Finding a divisor of n and adding it to max_sum ; Driver code
def maxSumLCM ( n ) : NEW_LINE INDENT max_sum = 0 NEW_LINE i = 1 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT max_sum = max_sum + i NEW_LINE if ( n // i != i ) : NEW_LINE INDENT max_sum = max_sum + ( n // i ) NEW_LINE DEDENT DEDENT i = i + 1 NEW_LINE DEDENT return max_sum NEW_LINE DEDENT n = 2 NEW_LINE print ( maxSumLCM ( n ) ) NEW_LINE
Square root of a number using log | Python3 program to demonstrate finding square root of a number using sqrt ( )
import math NEW_LINE if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 12 NEW_LINE print ( math . sqrt ( n ) ) NEW_LINE DEDENT
Maximum value of an integer for which factorial can be calculated on a machine | Python3 program to find maximum value of an integer for which factorial can be calculated on your system ; when fact crosses its size it gives negative value ; Driver Code
import sys NEW_LINE def findMaxValue ( ) : NEW_LINE INDENT res = 2 ; NEW_LINE fact = 2 ; NEW_LINE while ( True ) : NEW_LINE INDENT if ( fact < 0 or fact > sys . maxsize ) : NEW_LINE INDENT break ; NEW_LINE DEDENT res += 1 ; NEW_LINE fact = fact * res ; NEW_LINE DEDENT return res - 1 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( " Maximum ▁ value ▁ of ▁ integer : " , findMaxValue ( ) ) ; NEW_LINE DEDENT