codeparrot/xlcost-text-to-code huggingface dataset:
Count the number of ways to fill K boxes with N distinct items | Python3 program to calculate the above formula ; To store the factorials of all numbers ; Function to calculate factorial of all numbers ; Calculate x to the power y in O ( log n ) time ; Function to find inverse mod of a number x ; Calculate ( n C r ) ; Loop to compute the formula evaluated ; Add even power terms ; Subtract odd power terms ; Choose the k boxes which were used ; Driver code
mod = 1000000007 NEW_LINE factorial = [ 0 for i in range ( 100005 ) ] NEW_LINE def StoreFactorials ( n ) : NEW_LINE INDENT factorial [ 0 ] = 1 NEW_LINE for i in range ( 1 , n + 1 , 1 ) : NEW_LINE INDENT factorial [ i ] = ( i * factorial [ i - 1 ] ) % mod NEW_LINE DEDENT DEDENT def Power ( x , y ) : NEW_LINE INDENT ans = 1 NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y % 2 == 1 ) : NEW_LINE INDENT ans = ( ans * x ) % mod NEW_LINE DEDENT x = ( x * x ) % mod NEW_LINE y //= 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT def invmod ( x ) : NEW_LINE INDENT return Power ( x , mod - 2 ) NEW_LINE DEDENT def nCr ( n , r ) : NEW_LINE INDENT return ( ( factorial [ n ] * invmod ( ( factorial [ r ] * factorial [ n - r ] ) % mod ) ) % mod ) NEW_LINE DEDENT def CountWays ( n , k ) : NEW_LINE INDENT StoreFactorials ( n ) NEW_LINE ans = 0 NEW_LINE i = k NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( i % 2 == k % 2 ) : NEW_LINE INDENT ans = ( ( ans + ( Power ( i , n ) * nCr ( k , i ) ) % mod ) % mod ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = ( ( ans + mod - ( Power ( i , n ) * nCr ( k , i ) ) % mod ) % mod ) NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT ans = ( ans * nCr ( n , k ) ) % mod NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE K = 5 NEW_LINE print ( CountWays ( N , K ) ) NEW_LINE DEDENT
Program to print numbers from N to 1 in reverse order | Recursive function to print from N to 1 ; if N is less than 1 then return void function ; recursive call of the function ; Driver Code
def PrintReverseOrder ( N ) : NEW_LINE INDENT if ( N <= 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( N , end = " ▁ " ) ; NEW_LINE PrintReverseOrder ( N - 1 ) ; NEW_LINE DEDENT DEDENT N = 5 ; NEW_LINE PrintReverseOrder ( N ) ; NEW_LINE
Find the XOR of first half and second half elements of an array | Function to find the xor of the first half elements and second half elements of an array ; xor of elements in FirstHalfXOR ; xor of elements in SecondHalfXOR ; Driver Code ; Function call
def XOROfElements ( arr , n ) : NEW_LINE INDENT FirstHalfXOR = 0 ; NEW_LINE SecondHalfXOR = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i < n // 2 ) : NEW_LINE INDENT FirstHalfXOR ^= arr [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT SecondHalfXOR ^= arr [ i ] ; NEW_LINE DEDENT DEDENT print ( FirstHalfXOR , " , " , SecondHalfXOR ) ; NEW_LINE DEDENT arr = [ 20 , 30 , 50 , 10 , 55 , 15 , 42 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE XOROfElements ( arr , N ) ; NEW_LINE
Minimum volume of cone that can be circumscribed about a sphere of radius R | Python3 program to find the minimum Volume of the cone that can be circumscribed about a sphere of radius R ; Function to find the volume of the cone ; r = radius of cone h = height of cone Volume of cone = ( 1 / 3 ) * ( 3.14 ) * ( r * * 2 ) * ( h ) we get radius of cone from the derivation is root ( 2 ) times multiple of R we get height of cone from the derivation is 4 times multiple of R ; Driver code
import math NEW_LINE def Volume_of_cone ( R ) : NEW_LINE INDENT V = ( 1 / 3 ) * ( 3.14 ) * ( 2 * ( R ** 2 ) ) * ( 4 * R ) NEW_LINE return V NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT R = 10 NEW_LINE print ( Volume_of_cone ( R ) ) NEW_LINE DEDENT
Program to find Surface Area and Volume of Octagonal Prism | Python3 program to find the Surface area and volume of octagonal prism ; Function to find the Volume of octagonal prism ; Formula to calculate volume = ( area * h ) ; Display volume ; Function to find the surface area of octagonal prism ; Formula to calculate Surface area ; Driver Code
import math NEW_LINE def find_volume ( area , h ) : NEW_LINE INDENT Volume = ( area * h ) NEW_LINE print ( " Volume : ▁ " , end = " ▁ " ) NEW_LINE print ( Volume ) NEW_LINE DEDENT def find_Surface_area ( area , a , h ) : NEW_LINE INDENT Surface_area = ( 2 * area ) + ( 8 * a * h ) NEW_LINE print ( " Surface ▁ area : ▁ " , end = " ▁ " ) NEW_LINE print ( Surface_area ) NEW_LINE DEDENT h = 1 NEW_LINE a = 6 NEW_LINE d = 2 NEW_LINE area = 2 * a * d NEW_LINE find_Surface_area ( area , a , h ) NEW_LINE find_volume ( area , h ) NEW_LINE
Count of pairs upto N such whose LCM is not equal to their product for Q queries | Python 3 program to find the count of pairs from 1 to N such that their LCM is not equal to their product ; To store Euler 's Totient Function ; To store prefix sum table ; Compute Totients of all numbers smaller than or equal to N ; Make phi [ 1 ] = 0 since 1 cannot form any pair ; Initialise all remaining phi [ ] with i ; Compute remaining phi ; If phi [ p ] is not computed already , then number p is prime ; phi of prime number is p - 1 ; Update phi of all multiples of p ; Add the contribution of p to its multiple i by multiplying it with ( 1 - 1 / p ) ; Function to store prefix sum table ; Prefix Sum of all Euler 's Totient Values ; Total number of pairs that can be formed ; Driver Code ; Function call to compute all phi ; Function call to store all prefix sum
N = 100005 NEW_LINE phi = [ 0 for i in range ( N ) ] NEW_LINE pref = [ 0 for i in range ( N ) ] NEW_LINE def precompute ( ) : NEW_LINE INDENT phi [ 1 ] = 0 NEW_LINE for i in range ( 2 , N , 1 ) : NEW_LINE INDENT phi [ i ] = i NEW_LINE DEDENT for p in range ( 2 , N ) : NEW_LINE INDENT if ( phi [ p ] == p ) : NEW_LINE INDENT phi [ p ] = p - 1 NEW_LINE for i in range ( 2 * p , N , p ) : NEW_LINE INDENT phi [ i ] = ( phi [ i ] // p ) * ( p - 1 ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def prefix ( ) : NEW_LINE INDENT for i in range ( 1 , N , 1 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + phi [ i ] NEW_LINE DEDENT DEDENT def find_pairs ( n ) : NEW_LINE INDENT total = ( n * ( n - 1 ) ) // 2 NEW_LINE ans = total - pref [ n ] NEW_LINE print ( " Number ▁ of ▁ pairs ▁ from ▁ 1 ▁ to " , n , " are " , ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT precompute ( ) NEW_LINE prefix ( ) NEW_LINE q = [ 5 , 7 ] NEW_LINE n = len ( q ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT find_pairs ( q [ i ] ) NEW_LINE DEDENT DEDENT
Program to balance the given Chemical Equation | Function to calculate GCD ; Function to calculate b1 , b2 and b3 ; Variable declaration ; temp variable to store gcd ; Computing GCD ; Driver code
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def balance ( x , y , p , q ) : NEW_LINE INDENT if ( p % x == 0 and q % y == 0 ) : NEW_LINE INDENT b1 = p // x NEW_LINE b2 = q // y NEW_LINE b3 = 1 NEW_LINE DEDENT else : NEW_LINE INDENT p = p * y NEW_LINE q = q * x NEW_LINE b3 = x * y NEW_LINE temp = gcd ( p , gcd ( q , b3 ) ) NEW_LINE b1 = p // temp NEW_LINE b2 = q // temp NEW_LINE b3 = b3 // temp NEW_LINE DEDENT print ( b1 , b2 , b3 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 2 NEW_LINE y = 3 NEW_LINE p = 4 NEW_LINE q = 5 NEW_LINE balance ( x , y , p , q ) NEW_LINE DEDENT
Find the Maximum Alternate Subsequence Sum from a given array | Function to find maximum alternating subsequence sum ; initialize sum to 0 ; calculate the sum ; return the final result ; array initiaisation ; length of array
def maxAlternatingSum ( arr , n ) : NEW_LINE INDENT max_sum = 0 NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT current_max = arr [ i ] NEW_LINE k = i NEW_LINE while k < n and ( ( arr [ i ] > 0 and arr [ k ] > 0 ) or ( arr [ i ] < 0 and arr [ k ] < 0 ) ) : NEW_LINE INDENT current_max = max ( current_max , arr [ k ] ) NEW_LINE k += 1 NEW_LINE DEDENT max_sum += current_max NEW_LINE i = k NEW_LINE DEDENT return max_sum NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , - 1 , - 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxAlternatingSum ( arr , n ) ) NEW_LINE
Minimum length subarray containing all unique elements after Q operations | Function to find minimum size subarray of all array elements ; Updating the array after processing each query ; Making it to 0 - indexing ; Prefix sum array concept is used to obtain the count array ; Iterating over the array to get the final array ; Variable to get count of all unique elements ; Hash to maintain perviously occurred elements ; Loop to find the all unique elements ; Array to maintain counter of encountered elements ; Variable to store answer ; Using two pointers approach ; Increment counter if occurred once ; When all unique elements are found ; update answer with minimum size ; Decrement count of elements from left ; Decrement counter ; Driver code
def subarrayLength ( A , R , N , M ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT l = R [ i ] [ 0 ] NEW_LINE r = R [ i ] [ 1 ] + 1 NEW_LINE l -= 1 NEW_LINE r -= 1 NEW_LINE A [ l ] += 1 NEW_LINE if ( r < N ) : NEW_LINE INDENT A [ r ] -= 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT A [ i ] += A [ i - 1 ] NEW_LINE DEDENT count = 0 NEW_LINE s = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] not in s ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT s . append ( A [ i ] ) NEW_LINE DEDENT repeat = [ 0 ] * ( count + 1 ) NEW_LINE ans = N NEW_LINE counter , left , right = 0 , 0 , 0 NEW_LINE while ( right < N ) : NEW_LINE INDENT cur_element = A [ right ] NEW_LINE repeat [ cur_element ] += 1 NEW_LINE if ( repeat [ cur_element ] == 1 ) : NEW_LINE INDENT counter += 1 NEW_LINE DEDENT while ( counter == count ) : NEW_LINE INDENT ans = min ( ans , right - left + 1 ) NEW_LINE cur_element = A [ left ] NEW_LINE repeat [ cur_element ] -= 1 NEW_LINE left += 1 NEW_LINE if ( repeat [ cur_element ] == 0 ) : NEW_LINE INDENT counter -= 1 NEW_LINE DEDENT DEDENT right += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , queries = 8 , 6 NEW_LINE Q = [ [ 1 , 4 ] , [ 3 , 4 ] , [ 4 , 5 ] , [ 5 , 5 ] , [ 7 , 8 ] , [ 8 , 8 ] ] NEW_LINE A = [ 0 ] * N NEW_LINE print ( subarrayLength ( A , Q , N , queries ) ) NEW_LINE DEDENT
Elements of Array which can be expressed as power of prime numbers | Function to mark all the exponent of prime numbers ; If number is prime then marking all of its exponent true ; Function to display all required elements ; Function to print the required numbers ; To find the largest number ; Function call to mark all the Exponential prime nos . ; Function call ; Driver code
def ModifiedSieveOfEratosthenes ( N , Expo_Prime ) : NEW_LINE INDENT primes = [ True ] * N ; NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT if ( primes [ i ] ) : NEW_LINE INDENT no = i ; NEW_LINE while ( no <= N ) : NEW_LINE INDENT Expo_Prime [ no ] = True ; NEW_LINE no *= i ; NEW_LINE DEDENT for j in range ( i * i , N , i ) : NEW_LINE INDENT primes [ j ] = False ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def Display ( arr , Expo_Prime , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( Expo_Prime [ arr [ i ] ] ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT def FindExpoPrime ( arr , n ) : NEW_LINE INDENT max = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( max < arr [ i ] ) : NEW_LINE INDENT max = arr [ i ] ; NEW_LINE DEDENT DEDENT Expo_Prime = [ False ] * ( max + 1 ) ; NEW_LINE ModifiedSieveOfEratosthenes ( max + 1 , Expo_Prime ) ; NEW_LINE Display ( arr , Expo_Prime , n ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 6 , 9 , 16 , 1 , 3 , 12 , 36 , 625 , 1000 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE FindExpoPrime ( arr , n ) ; NEW_LINE DEDENT
Elements of Array which can be expressed as power of some integer to given exponent K | Method returns Nth power of A ; Smaller eps , denotes more accuracy ; Initializing difference between two roots by INT_MAX ; x ^ K denotes current value of x ; loop untiwe reach desired accuracy ; calculating current value from previous value by newton 's method ; Function to check whether its k root is an integer or not ; Function to find the numbers ; Driver code
def nthRoot ( A , N ) : NEW_LINE INDENT xPre = 7 NEW_LINE eps = 1e-3 NEW_LINE delX = 10 ** 9 NEW_LINE xK = 0 NEW_LINE while ( delX > eps ) : NEW_LINE INDENT xK = ( ( N - 1.0 ) * xPre + A / pow ( xPre , N - 1 ) ) / N NEW_LINE delX = abs ( xK - xPre ) NEW_LINE xPre = xK NEW_LINE DEDENT return xK NEW_LINE DEDENT def check ( no , k ) : NEW_LINE INDENT kth_root = nthRoot ( no , k ) NEW_LINE num = int ( kth_root ) NEW_LINE if ( abs ( num - kth_root ) < 1e-4 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def printExpo ( arr , n , k ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( check ( arr [ i ] , k ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 6 NEW_LINE arr = [ 46656 , 64 , 256 , 729 , 16 , 1000 ] NEW_LINE n = len ( arr ) NEW_LINE printExpo ( arr , n , K ) NEW_LINE DEDENT
Count of subsequences whose product is a difference of square of two integers | Function to count the number of contiguous subsequences whose product can be expressed as square of difference of two integers ; Iterating through the array ; Check if that number can be expressed as the square of difference of two numbers ; Variable to compute the product ; Finding the remaining subsequences ; Check if that number can be expressed as the square of difference of two numbers ; Return the number of subsequences ; Driver code
def CntcontSubs ( a , n ) : NEW_LINE INDENT c = 0 NEW_LINE d = 0 NEW_LINE sum = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 2 != 0 or a [ i ] % 4 == 0 ) : NEW_LINE INDENT d += 1 NEW_LINE DEDENT sum = a [ i ] NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT sum = sum * a [ j ] NEW_LINE if ( sum % 2 != 0 or sum % 4 == 0 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT sum = 1 NEW_LINE DEDENT return c + d NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 4 , 2 , 9 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( CntcontSubs ( arr , n ) ) NEW_LINE DEDENT
Count of subsequences whose product is a difference of square of two integers | Function to count all the contiguous subsequences whose product is expressed as the square of the difference of two integers ; Creating vectors to store the remainders and the subsequences ; Iterating through the array ; Finding the remainder when the element is divided by 4 ; Bringing all the elements in the range [ 0 , 3 ] ; If the remainder is 2 , store the index of the ; If the remainder is 2 , store the index of the ; Finding the total number of subsequences ; If there are no numbers which yield the remainder 2 ; Iterating through the vector ; If the element is 2 , find the nearest 2 or 0 and find the number of elements between them ; Returning the count ; Driver Code
def CntcontSubs ( a , n ) : NEW_LINE INDENT prod = 1 NEW_LINE vect = [ ] NEW_LINE vect . append ( ( 0 , 2 ) ) NEW_LINE two , zero = [ ] , [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] = a [ i ] % 4 NEW_LINE if ( a [ i ] < 0 ) : NEW_LINE INDENT a [ i ] = a [ i ] + 4 NEW_LINE DEDENT if ( a [ i ] == 2 ) : NEW_LINE INDENT two . append ( i + 1 ) NEW_LINE DEDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT zero . append ( i + 1 ) NEW_LINE DEDENT if ( a [ i ] == 0 or a [ i ] == 2 ) : NEW_LINE INDENT vect . append ( ( i + 1 , a [ i ] ) ) NEW_LINE DEDENT DEDENT vect . append ( ( n + 1 , 2 ) ) NEW_LINE total = ( n * ( n + 1 ) ) // 2 NEW_LINE if ( len ( two ) == 0 ) : NEW_LINE INDENT return total NEW_LINE DEDENT else : NEW_LINE INDENT Sum = 0 NEW_LINE pos1 , pos2 , pos3 = - 1 , - 1 , - 1 NEW_LINE sz = len ( vect ) NEW_LINE for i in range ( 1 , sz - 1 ) : NEW_LINE INDENT if ( vect [ i ] [ 1 ] == 2 ) : NEW_LINE INDENT Sum += ( ( vect [ i ] [ 0 ] - vect [ i - 1 ] [ 0 ] ) * ( vect [ i + 1 ] [ 0 ] - vect [ i ] [ 0 ] ) - 1 ) NEW_LINE DEDENT DEDENT return ( total - Sum - len ( two ) ) NEW_LINE DEDENT DEDENT a = [ 5 , 4 , 2 , 9 , 8 ] NEW_LINE n = len ( a ) NEW_LINE print ( CntcontSubs ( a , n ) ) NEW_LINE
Count of possible subarrays and subsequences using given length of Array | Function to count the subarray for the given array ; Function to count the subsequence for the given array length ; Driver Code
def countSubarray ( n ) : NEW_LINE INDENT return ( ( n ) * ( n + 1 ) ) // 2 NEW_LINE DEDENT def countSubsequence ( n ) : NEW_LINE INDENT return ( 2 ** n ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE print ( countSubarray ( n ) ) NEW_LINE print ( countSubsequence ( n ) ) NEW_LINE DEDENT
Program for finding the Integral of a given function using Boole 's Rule | Function to return the value of f ( x ) for the given value of x ; Function to computes the integrand of y at the given intervals of x with step size h and the initial limit a and final limit b ; Number of intervals ; Computing the step size ; Substituing a = 0 , b = 4 and h = 1 ; Driver code
def y ( x ) : NEW_LINE INDENT return ( 1 / ( 1 + x ) ) NEW_LINE DEDENT def BooleRule ( a , b ) : NEW_LINE INDENT n = 4 NEW_LINE h = ( ( b - a ) / n ) NEW_LINE sum = 0 NEW_LINE bl = ( 7 * y ( a ) + 32 * y ( a + h ) + 12 * y ( a + 2 * h ) + 32 * y ( a + 3 * h ) + 7 * y ( a + 4 * h ) ) * 2 * h / 45 NEW_LINE sum = sum + bl NEW_LINE return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT lowlimit = 0 NEW_LINE upplimit = 4 NEW_LINE print ( " f ( x ) ▁ = " , round ( BooleRule ( 0 , 4 ) , 4 ) ) NEW_LINE DEDENT
Number of subsets with same AND , OR and XOR values in an Array | Python3 program to find the number of subsets with equal bitwise AND , OR and XOR values ; Function to find the number of subsets with equal bitwise AND , OR and XOR values ; Precompute the modded powers of two for subset counting ; Loop to iterate and find the modded powers of two for subset counting ; Map to store the frequency of each element ; Loop to compute the frequency ; For every element > 0 , the number of subsets formed using this element only is equal to 2 ^ ( frequency [ element ] - 1 ) . And for 0 , we have to find all the subsets , so 2 ^ ( frequency [ element ] ) - 1 ; If element is greater than 0 ; Driver code
mod = 1000000007 NEW_LINE def countSubsets ( a , n ) : NEW_LINE INDENT answer = 0 NEW_LINE powerOfTwo = [ 0 for x in range ( 100005 ) ] NEW_LINE powerOfTwo [ 0 ] = 1 NEW_LINE for i in range ( 1 , 100005 ) : NEW_LINE INDENT powerOfTwo [ i ] = ( powerOfTwo [ i - 1 ] * 2 ) % mod NEW_LINE DEDENT frequency = { } NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if a [ i ] in frequency : NEW_LINE INDENT frequency [ a [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT frequency [ a [ i ] ] = 1 NEW_LINE DEDENT DEDENT for key , value in frequency . items ( ) : NEW_LINE INDENT if ( key != 0 ) : NEW_LINE INDENT answer = ( answer % mod + powerOfTwo [ value - 1 ] ) % mod NEW_LINE DEDENT else : NEW_LINE INDENT answer = ( answer % mod + powerOfTwo [ value ] - 1 + mod ) % mod NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT N = 6 NEW_LINE A = [ 1 , 3 , 2 , 1 , 2 , 1 ] NEW_LINE print ( countSubsets ( A , N ) ) NEW_LINE
Sum of all Perfect numbers lying in the range [ L , R ] | Python3 implementation to find the sum of all perfect numbers lying in the range [ L , R ] ; Array to store the sum ; Function to check if a number is a perfect number or not ; Iterating till the square root of the number and checking if the sum of divisors is equal to the number or not ; If it is a perfect number , then return the number ; Else , return 0 ; Function to precompute the sum of perfect squares and store then in an array
from math import sqrt NEW_LINE pref = [ 0 ] * 10000 ; NEW_LINE def isPerfect ( n ) : NEW_LINE INDENT sum = 1 ; NEW_LINE for i in range ( 2 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( i * i != n ) : NEW_LINE INDENT sum = sum + i + n // i ; NEW_LINE DEDENT else : NEW_LINE INDENT sum = sum + i ; NEW_LINE DEDENT DEDENT DEDENT if ( sum == n and n != 1 ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT return 0 ; NEW_LINE DEDENT def precomputation ( ) : NEW_LINE INDENT for i in range ( 1 , 10000 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + isPerfect ( i ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L = 6 ; R = 28 ; NEW_LINE precomputation ( ) ; NEW_LINE print ( pref [ R ] - pref [ L - 1 ] ) ; NEW_LINE DEDENT
Ternary number system or Base 3 numbers | Python3 program to convert a ternary number to decimal number ; Function to convert a ternary number to a decimal number ; If the number is greater than 0 , compute the decimal representation of the number ; Loop to iterate through the number ; Computing the decimal digit ; Driver code
import math ; NEW_LINE def convertToDecimal ( N ) : NEW_LINE INDENT print ( " Decimal ▁ number ▁ of " , N , " is : " , end = " ▁ " ) ; NEW_LINE if ( N != 0 ) : NEW_LINE INDENT decimalNumber = 0 ; NEW_LINE i = 0 ; NEW_LINE remainder = 0 ; NEW_LINE while ( N != 0 ) : NEW_LINE INDENT remainder = N % 10 ; NEW_LINE N = N // 10 ; NEW_LINE decimalNumber += remainder * math . pow ( 3 , i ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT print ( decimalNumber ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( "0" ) ; NEW_LINE DEDENT DEDENT Ternary = 10202202 ; NEW_LINE convertToDecimal ( Ternary ) ; NEW_LINE
Check if a given pair of Numbers are Betrothed numbers or not | Python3 program for the above approach ; Function to check whether N is Perfect Square or not ; Find sqrt ; Function to check whether the given pairs of numbers is Betrothed Numbers or not ; For finding the sum of all the divisors of first number n ; For finding the sum of all the divisors of second number m ; Driver Code ; Function Call
from math import sqrt , floor NEW_LINE def isPerfectSquare ( N ) : NEW_LINE INDENT sr = sqrt ( N ) NEW_LINE return ( sr - floor ( sr ) ) == 0 NEW_LINE DEDENT def BetrothedNumbers ( n , m ) : NEW_LINE INDENT Sum1 = 1 NEW_LINE Sum2 = 1 NEW_LINE for i in range ( 2 , int ( sqrt ( n ) ) + 1 , 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( isPerfectSquare ( n ) ) : NEW_LINE INDENT Sum1 += i NEW_LINE DEDENT else : NEW_LINE INDENT Sum1 += i + n / i NEW_LINE DEDENT DEDENT DEDENT for i in range ( 2 , int ( sqrt ( m ) ) + 1 , 1 ) : NEW_LINE INDENT if ( m % i == 0 ) : NEW_LINE INDENT if ( isPerfectSquare ( m ) ) : NEW_LINE INDENT Sum2 += i NEW_LINE DEDENT else : NEW_LINE INDENT Sum2 += i + ( m / i ) NEW_LINE DEDENT DEDENT DEDENT if ( ( n + 1 == Sum2 ) and ( m + 1 == Sum1 ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 9504 NEW_LINE M = 20734 NEW_LINE BetrothedNumbers ( N , M ) NEW_LINE DEDENT