codeparrot/xlcost-text-to-code huggingface dataset:
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