codeparrot/xlcost-text-to-code huggingface dataset:
Program to convert a Binary Number to Hexa | Function to convert BCD to hexadecimal ; Iterating through the bits backwards ; Computing the hexadecimal number formed so far and storing it in a vector . ; Reinitializing all variables for next group . ; Printing the hexadecimal number formed so far . ; Driver Code ; Function Call
def bcdToHexaDecimal ( s ) : NEW_LINE INDENT len1 = len ( s ) NEW_LINE check = 0 NEW_LINE num = 0 NEW_LINE sum = 0 NEW_LINE mul = 1 NEW_LINE ans = [ ] NEW_LINE i = len1 - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT sum += ( ord ( s [ i ] ) - ord ( '0' ) ) * mul NEW_LINE mul *= 2 NEW_LINE check += 1 NEW_LINE if ( check == 4 or i == 0 ) : NEW_LINE INDENT if ( sum <= 9 ) : NEW_LINE INDENT ans . append ( chr ( sum + ord ( '0' ) ) ) NEW_LINE DEDENT else : NEW_LINE INDENT ans . append ( chr ( sum + 55 ) ) ; NEW_LINE DEDENT check = 0 NEW_LINE sum = 0 NEW_LINE mul = 1 NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT len1 = len ( ans ) NEW_LINE i = len1 - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT print ( ans [ i ] , end = " " ) NEW_LINE i -= 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "100000101111" NEW_LINE bcdToHexaDecimal ( s ) NEW_LINE DEDENT
Program to check if N is a Myriagon Number | Python3 implementation to check that a number is a myriagon number or not ; Function to check that the number is a myriagon number ; Condition to check if the number is a myriagon number ; Given Number ; Function call
import math NEW_LINE def isMyriagon ( N ) : NEW_LINE INDENT n = ( 9996 + math . sqrt ( 79984 * N + 99920016 ) ) / 19996 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT n = 10000 NEW_LINE if ( isMyriagon ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Program to check if N is a Heptagonal Number | Python3 program for the above approach ; Function to check if N is a heptagonal number ; Condition to check if the number is a heptagonal number ; Given Number ; Function call
import math NEW_LINE def isheptagonal ( N ) : NEW_LINE INDENT n = ( 3 + math . sqrt ( 40 * N + 9 ) ) / 10 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT N = 7 NEW_LINE if ( isheptagonal ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Program to check if N is a Enneadecagonal Number | Python3 program for the above approach ; Function to check if number N is a enneadecagonal number ; Condition to check if N is a enneadecagonal number ; Given Number ; Function call
import math NEW_LINE def isenneadecagonal ( N ) : NEW_LINE INDENT n = ( 15 + math . sqrt ( 136 * N + 225 ) ) / 34 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT N = 19 NEW_LINE if ( isenneadecagonal ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Program to check if N is a Centered Tridecagonal Number | Python3 program for the above approach ; Function to check if the number N is a centered tridecagonal number ; Condition to check if N is centered tridecagonal number ; Given Number ; Function call
import numpy as np NEW_LINE def isCenteredtridecagonal ( N ) : NEW_LINE INDENT n = ( 13 + np . sqrt ( 104 * N + 65 ) ) / 26 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT N = 14 NEW_LINE if ( isCenteredtridecagonal ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find the Nth natural number which is not divisible by A | Python3 code to Find the Nth number which is not divisible by A from the series ; Find the quotient and the remainder when k is divided by n - 1 ; if remainder is 0 then multiply n by q and add the remainder ; print the answer ; driver code
def findNum ( n , k ) : NEW_LINE INDENT q = k // ( n - 1 ) NEW_LINE r = k % ( n - 1 ) NEW_LINE if ( r != 0 ) : NEW_LINE INDENT a = ( n * q ) + r NEW_LINE DEDENT else : NEW_LINE INDENT a = ( n * q ) - 1 NEW_LINE DEDENT print ( a ) NEW_LINE DEDENT A = 4 NEW_LINE N = 6 NEW_LINE findNum ( A , N ) NEW_LINE
Count the nodes in the given Tree whose weight is a Perfect Number | Python3 implementation to Count the Nodes in the given tree whose weight is a Perfect Number ; Function that returns True if n is perfect ; Variable to store sum of divisors ; Find all divisors and add them ; Check if sum of divisors is equal to n , then n is a perfect number ; Function to perform dfs ; If weight of the current Node is a perfect number ; Weights of the Node ; Edges of the tree
graph = [ [ ] for i in range ( 100 ) ] NEW_LINE weight = [ 0 ] * 100 NEW_LINE ans = 0 NEW_LINE def isPerfect ( n ) : NEW_LINE INDENT sum = 1 ; NEW_LINE i = 2 ; NEW_LINE while ( i * i < n ) : 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 i += 1 ; NEW_LINE DEDENT if ( sum == n and n != 1 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT def dfs ( Node , parent ) : NEW_LINE INDENT global ans ; NEW_LINE if ( isPerfect ( weight [ Node ] ) ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT for to in graph [ Node ] : NEW_LINE INDENT if ( to == parent ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT dfs ( to , Node ) ; NEW_LINE DEDENT DEDENT weight [ 1 ] = 5 ; NEW_LINE weight [ 2 ] = 10 ; NEW_LINE weight [ 3 ] = 11 ; NEW_LINE weight [ 4 ] = 8 ; NEW_LINE weight [ 5 ] = 6 ; NEW_LINE graph [ 1 ] . append ( 2 ) ; NEW_LINE graph [ 2 ] . append ( 3 ) ; NEW_LINE graph [ 2 ] . append ( 4 ) ; NEW_LINE graph [ 1 ] . append ( 5 ) ; NEW_LINE dfs ( 1 , 1 ) ; NEW_LINE print ( ans ) ; NEW_LINE
Minimum cost to merge numbers from 1 to N | Function returns the minimum cost ; Min Heap representation ; Add all elements to heap ; First minimum ; Second minimum ; Multiply them ; Add to the cost ; Push the product into the heap again ; Return the optimal cost ; Driver code
def GetMinCost ( N ) : NEW_LINE INDENT pq = [ ] NEW_LINE for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT pq . append ( i ) NEW_LINE DEDENT pq . sort ( reverse = False ) NEW_LINE cost = 0 NEW_LINE while ( len ( pq ) > 1 ) : NEW_LINE INDENT mini = pq [ 0 ] NEW_LINE pq . remove ( pq [ 0 ] ) NEW_LINE secondmini = pq [ 0 ] NEW_LINE pq . remove ( pq [ 0 ] ) NEW_LINE current = mini * secondmini NEW_LINE cost += current NEW_LINE pq . append ( current ) NEW_LINE pq . sort ( reverse = False ) NEW_LINE DEDENT return cost NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE print ( GetMinCost ( N ) ) NEW_LINE DEDENT
Permutation of first N positive integers such that prime numbers are at prime indices | Set 2 | Python3 program to count permutations from 1 to N such that prime numbers occur at prime indices ;  ; Computing count of prime numbers using sieve ; Computing permutations for primes ; Computing permutations for non - primes ; Driver code
import math ; NEW_LINE def numPrimeArrangements ( n ) : NEW_LINE INDENT prime = [ True for i in range ( n + 1 ) ] NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for i in range ( 2 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if prime [ i ] : NEW_LINE INDENT factor = 2 NEW_LINE while factor * i <= n : NEW_LINE INDENT prime [ factor * i ] = False NEW_LINE factor += 1 NEW_LINE DEDENT DEDENT DEDENT primeIndices = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if prime [ i ] : NEW_LINE INDENT primeIndices += 1 NEW_LINE DEDENT DEDENT mod = 1000000007 NEW_LINE res = 1 NEW_LINE for i in range ( 1 , primeIndices + 1 ) : NEW_LINE INDENT res = ( res * i ) % mod NEW_LINE DEDENT for i in range ( 1 , n - primeIndices + 1 ) : NEW_LINE INDENT res = ( res * i ) % mod NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE print ( numPrimeArrangements ( N ) ) NEW_LINE DEDENT
Probability of getting K heads in N coin tosses | Function to calculate factorial ; Applying the formula ; Driver code ; Call count_heads with n and r
def fact ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT return res NEW_LINE DEDENT def count_heads ( n , r ) : NEW_LINE INDENT output = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) NEW_LINE output = output / ( pow ( 2 , n ) ) NEW_LINE return output NEW_LINE DEDENT n = 4 NEW_LINE r = 3 NEW_LINE print ( count_heads ( n , r ) ) NEW_LINE
Largest component size in a graph formed by connecting non |  ; mark this node as visited ; apply dfs and add nodes belonging to this component ; create graph and store in adjacency list form ; iterate over all pair of nodes ; if not co - prime ; build undirected graph ; visited array for dfs ; Driver Code
from math import gcd NEW_LINE def dfs ( u , adj , vis ) : NEW_LINE INDENT vis [ u ] = 1 NEW_LINE componentSize = 1 NEW_LINE for x in adj [ u ] : NEW_LINE INDENT if ( vis [ x ] == 0 ) : NEW_LINE INDENT componentSize += dfs ( x , adj , vis ) NEW_LINE DEDENT DEDENT return componentSize NEW_LINE DEDENT def maximumComponentSize ( a , n ) : NEW_LINE INDENT adj = [ [ ] for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( gcd ( a [ i ] , a [ j ] ) > 1 ) : NEW_LINE INDENT adj [ i ] . append ( j ) NEW_LINE DEDENT adj [ j ] . append ( i ) NEW_LINE DEDENT DEDENT answer = 0 NEW_LINE vis = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( vis [ i ] == False ) : NEW_LINE INDENT answer = max ( answer , dfs ( i , adj , vis ) ) NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 8 NEW_LINE A = [ 2 , 3 , 6 , 7 , 4 , 12 , 21 , 39 ] NEW_LINE print ( maximumComponentSize ( A , n ) ) NEW_LINE DEDENT
Largest component size in a graph formed by connecting non | smallest prime factor ; Sieve of Eratosthenes ; is spf [ i ] = 0 , then it 's prime ; smallest prime factor of all multiples is i ; Prime factorise n , and store prime factors in set s ; for implementing DSU ; root of component of node i ; finding root as well as applying path compression ; merging two components ; find roots of both components ; if already belonging to the same compenent ; Union by rank , the rank in this case is sizeContainer of the component . Smaller sizeContainer will be merged into larger , so the larger 's root will be  final root ; Function to find the maximum sized container ; intitalise the parents , and component sizeContainer ; intitally all component sizeContainers are 1 ans each node it parent of itself ; store prime factors of a [ i ] in s ; if this prime is seen as a factor for the first time ; if not then merge with that component in which this prime was previously seen ; maximum of sizeContainer of all components ; Driver Code
spf = [ 0 for i in range ( 100005 ) ] NEW_LINE def sieve ( ) : NEW_LINE INDENT for i in range ( 2 , 100005 ) : NEW_LINE INDENT if ( spf [ i ] == 0 ) : NEW_LINE INDENT spf [ i ] = i ; NEW_LINE for j in range ( 2 * i , 100005 , i ) : NEW_LINE INDENT if ( spf [ j ] == 0 ) : NEW_LINE INDENT spf [ j ] = i ; NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def factorize ( n , s ) : NEW_LINE INDENT while ( n > 1 ) : NEW_LINE INDENT z = spf [ n ] ; NEW_LINE s . add ( z ) ; NEW_LINE while ( n % z == 0 ) : NEW_LINE INDENT n //= z ; NEW_LINE DEDENT DEDENT DEDENT id = [ 0 for i in range ( 100005 ) ] NEW_LINE par = [ 0 for i in range ( 100005 ) ] NEW_LINE sizeContainer = [ 0 for i in range ( 100005 ) ] NEW_LINE def root ( i ) : NEW_LINE INDENT if ( par [ i ] == i ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT else : NEW_LINE INDENT return root ( par [ i ] ) ; NEW_LINE return par [ i ] NEW_LINE DEDENT DEDENT def merge ( a , b ) : NEW_LINE INDENT p = root ( a ) ; NEW_LINE q = root ( b ) ; NEW_LINE if ( p == q ) : NEW_LINE INDENT return ; NEW_LINE DEDENT if ( sizeContainer [ p ] > sizeContainer [ q ] ) : NEW_LINE INDENT p = p + q ; NEW_LINE q = p - q ; NEW_LINE p = p - q ; NEW_LINE DEDENT par [ p ] = q ; NEW_LINE sizeContainer [ q ] += sizeContainer [ p ] ; NEW_LINE DEDENT def maximumComponentsizeContainer ( a , n ) : NEW_LINE INDENT for i in range ( 100005 ) : NEW_LINE INDENT par [ i ] = i ; NEW_LINE sizeContainer [ i ] = 1 ; NEW_LINE DEDENT sieve ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT s = set ( ) NEW_LINE factorize ( a [ i ] , s ) ; NEW_LINE for it in s : NEW_LINE INDENT if ( id [ it ] == 0 ) : NEW_LINE INDENT id [ it ] = i + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT merge ( i + 1 , id [ it ] ) ; NEW_LINE DEDENT DEDENT DEDENT answer = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT answer = max ( answer , sizeContainer [ i ] ) ; NEW_LINE DEDENT return answer ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 8 ; NEW_LINE A = [ 2 , 3 , 6 , 7 , 4 , 12 , 21 , 39 ] NEW_LINE print ( maximumComponentsizeContainer ( A , n ) ) ; NEW_LINE DEDENT
Check if original Array is retained after performing XOR with M exactly K times | Function to check if original Array can be retained by performing XOR with M exactly K times ; Check if O is present or not ; If K is odd and 0 is not present then the answer will always be No . ; Else it will be Yes ; Driver Code
def check ( Arr , n , M , K ) : NEW_LINE INDENT flag = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( Arr [ i ] == 0 ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT DEDENT if ( K % 2 != 0 and flag == 0 ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT else : NEW_LINE INDENT return " Yes " ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Arr = [ 1 , 1 , 2 , 4 , 7 , 8 ] NEW_LINE M = 5 ; NEW_LINE K = 6 ; NEW_LINE n = len ( Arr ) ; NEW_LINE print ( check ( Arr , n , M , K ) ) NEW_LINE DEDENT
Dixon 's Factorization Method with implementation | Python 3 implementation of Dixon factorization algo ; Function to find the factors of a number using the Dixon Factorization Algorithm ; Factor base for the given number ; Starting from the ceil of the root of the given number N ; Storing the related squares ; For every number from the square root Till N ; Finding the related squares ; If the two numbers are the related squares , then append them to the array ; For every pair in the array , compute the GCD such that ; If we find a factor other than 1 , then appending it to the final factor array ; Driver Code
from math import sqrt , gcd NEW_LINE import numpy as np NEW_LINE def factor ( n ) : NEW_LINE INDENT base = [ 2 , 3 , 5 , 7 ] NEW_LINE start = int ( sqrt ( n ) ) NEW_LINE pairs = [ ] NEW_LINE for i in range ( start , n ) : NEW_LINE INDENT for j in range ( len ( base ) ) : NEW_LINE INDENT lhs = i ** 2 % n NEW_LINE rhs = base [ j ] ** 2 % n NEW_LINE if ( lhs == rhs ) : NEW_LINE INDENT pairs . append ( [ i , base [ j ] ] ) NEW_LINE DEDENT DEDENT DEDENT new = [ ] NEW_LINE for i in range ( len ( pairs ) ) : NEW_LINE INDENT factor = gcd ( pairs [ i ] [ 0 ] - pairs [ i ] [ 1 ] , n ) NEW_LINE if ( factor != 1 ) : NEW_LINE INDENT new . append ( factor ) NEW_LINE DEDENT DEDENT x = np . array ( new ) NEW_LINE return ( np . unique ( x ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( factor ( 23449 ) ) NEW_LINE DEDENT
Longest sub | Python3 program to find longest subarray of prime numbers ; To store the prime numbers ; Function that find prime numbers till limit ; Find primes using Sieve of Eratosthenes ; Function that finds all prime numbers in given range using Segmented Sieve ; Find the limit ; To store the prime numbers ; Compute all primes less than or equals to sqrt ( high ) using Simple Sieve ; Count the elements in the range [ low , high ] ; Declaring and initializing boolean for the range [ low , high ] to False ; Traverse the prime numbers till limit ; Find the minimum number in [ low . . high ] that is a multiple of prime [ i ] ; Mark the multiples of prime [ i ] in [ low , high ] as true ; Element which are not marked in range are Prime ; Function that finds longest subarray of prime numbers ; If element is Non - prime then updated current_max to 0 ; If element is prime , then update current_max and max_so_far ; Return the count of longest subarray ; Driver Code ; Find minimum and maximum element ; Find prime in the range [ min_el , max_el ] ; Function call
import math NEW_LINE allPrimes = set ( ) NEW_LINE def simpleSieve ( limit , prime ) : NEW_LINE INDENT mark = [ False ] * ( limit + 1 ) NEW_LINE for i in range ( 2 , limit + 1 ) : NEW_LINE INDENT if mark [ i ] == False : NEW_LINE INDENT prime . append ( i ) NEW_LINE for j in range ( i , limit + 1 , i ) : NEW_LINE INDENT mark [ j ] = True NEW_LINE DEDENT DEDENT DEDENT DEDENT def primesInRange ( low , high ) : NEW_LINE INDENT limit = math . floor ( math . sqrt ( high ) ) + 1 NEW_LINE prime = [ ] NEW_LINE simpleSieve ( limit , prime ) NEW_LINE n = high - low + 1 NEW_LINE mark = [ False ] * ( n + 1 ) NEW_LINE for i in range ( len ( prime ) ) : NEW_LINE INDENT loLim = low // prime [ i ] NEW_LINE loLim *= prime [ i ] NEW_LINE if loLim < low : NEW_LINE INDENT loLim += prime [ i ] NEW_LINE DEDENT if loLim == prime [ i ] : NEW_LINE INDENT loLim += prime [ i ] NEW_LINE DEDENT for j in range ( loLim , high + 1 , prime [ i ] ) : NEW_LINE INDENT mark [ j - low ] = True NEW_LINE DEDENT DEDENT for i in range ( low , high + 1 ) : NEW_LINE INDENT if not mark [ i - low ] : NEW_LINE INDENT allPrimes . add ( i ) NEW_LINE DEDENT DEDENT DEDENT def maxPrimeSubarray ( arr , n ) : NEW_LINE INDENT current_max = 0 NEW_LINE max_so_far = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] not in allPrimes : NEW_LINE INDENT current_max = 0 NEW_LINE DEDENT else : NEW_LINE INDENT current_max += 1 NEW_LINE max_so_far = max ( current_max , max_so_far ) NEW_LINE DEDENT DEDENT return max_so_far NEW_LINE DEDENT arr = [ 1 , 2 , 4 , 3 , 29 , 11 , 7 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE max_el = max ( arr ) NEW_LINE min_el = min ( arr ) NEW_LINE primesInRange ( min_el , max_el ) NEW_LINE print ( maxPrimeSubarray ( arr , n ) ) NEW_LINE
Sum of all armstrong numbers lying in the range [ L , R ] for Q queries | pref [ ] array to precompute the sum of all armstrong number ; Function that return number num if num is armstrong else return 0 ; Function to precompute the sum of all armstrong numbers upto 100000 ; checkarmstrong ( ) return the number i if i is armstrong else return 0 ; Function to print the sum for each query ; Function to prsum of all armstrong numbers between [ L , R ] ; Function that pre computes the sum of all armstrong numbers ; Iterate over all Queries to print the sum ; Queries ; Function that print the the sum of all armstrong number in Range [ L , R ]
pref = [ 0 ] * 100001 NEW_LINE def checkArmstrong ( x ) : NEW_LINE INDENT n = len ( str ( x ) ) NEW_LINE sum1 = 0 NEW_LINE temp = x NEW_LINE while temp > 0 : NEW_LINE INDENT digit = temp % 10 NEW_LINE sum1 += digit ** n NEW_LINE temp //= 10 NEW_LINE DEDENT if sum1 == x : NEW_LINE INDENT return x NEW_LINE DEDENT return 0 NEW_LINE DEDENT def preCompute ( ) : NEW_LINE INDENT for i in range ( 1 , 100001 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + checkArmstrong ( i ) NEW_LINE DEDENT DEDENT def printSum ( L , R ) : NEW_LINE INDENT print ( pref [ R ] - pref [ L - 1 ] ) NEW_LINE DEDENT def printSumarmstrong ( arr , Q ) : NEW_LINE INDENT preCompute ( ) NEW_LINE for i in range ( Q ) : NEW_LINE INDENT printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) NEW_LINE DEDENT DEDENT Q = 2 NEW_LINE arr = [ [ 1 , 13 ] , [ 121 , 211 ] ] NEW_LINE printSumarmstrong ( arr , Q ) NEW_LINE
Count of pairs in an array whose product is a perfect square | prime [ ] array to calculate Prime Number ; Array to store the value of k for each element in arr [ ] ; For value of k , Sieve implemented ; Initialize k [ i ] to i ; Prime sieve ; If i is prime then remove all factors of prime from it ; Update that j is not prime ; Remove all square divisors i . e if k [ j ] is divisible by i * i then divide it by i * i ; Function that return total count of pairs with perfect square product ; Map used to store the frequency of k ; Store the frequency of k ; The total number of pairs is the summation of ( fi * ( fi - 1 ) ) / 2 ; Driver code ; Length of arr ; To pre - compute the value of k ; Function that return total count of pairs with perfect square product
prime = [ 0 ] * 100001 NEW_LINE k = [ 0 ] * 100001 NEW_LINE def Sieve ( ) : NEW_LINE INDENT for i in range ( 1 , 100001 ) : NEW_LINE INDENT k [ i ] = i NEW_LINE DEDENT for i in range ( 2 , 100001 ) : NEW_LINE INDENT if ( prime [ i ] == 0 ) : NEW_LINE INDENT for j in range ( i , 100001 , i ) : NEW_LINE INDENT prime [ j ] = 1 NEW_LINE while ( k [ j ] % ( i * i ) == 0 ) : NEW_LINE INDENT k [ j ] /= ( i * i ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def countPairs ( arr , n ) : NEW_LINE INDENT freq = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if k [ arr [ i ] ] in freq . keys ( ) : NEW_LINE INDENT freq [ k [ arr [ i ] ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT freq [ k [ arr [ i ] ] ] = 1 NEW_LINE DEDENT DEDENT Sum = 0 NEW_LINE for i in freq : NEW_LINE INDENT Sum += ( freq [ i ] * ( freq [ i ] - 1 ) ) / 2 NEW_LINE DEDENT return Sum NEW_LINE DEDENT arr = [ 1 , 2 , 4 , 8 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE Sieve ( ) NEW_LINE print ( int ( countPairs ( arr , n ) ) ) NEW_LINE
Sum of all Perfect Cubes lying in the range [ L , R ] for Q queries | Array to precompute the sum of cubes from 1 to 100010 so that for every query , the answer can be returned in O ( 1 ) . ; Function to check if a number is a perfect Cube or not ; Find floating point value of cube root of x . ; If cube root of x is cr return the x , else 0 ; Function to precompute the perfect Cubes upto 100000. ; Function to print the sum for each query ; Driver code ; To calculate the precompute function ; Calling the printSum function for every query
pref = [ 0 ] * 100010 ; NEW_LINE def isPerfectCube ( x ) : NEW_LINE INDENT cr = round ( x ** ( 1 / 3 ) ) ; NEW_LINE rslt = x if ( cr * cr * cr == x ) else 0 ; NEW_LINE return rslt ; NEW_LINE DEDENT def compute ( ) : NEW_LINE INDENT for i in range ( 1 , 100001 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + isPerfectCube ( i ) ; NEW_LINE DEDENT DEDENT def printSum ( L , R ) : NEW_LINE INDENT sum = pref [ R ] - pref [ L - 1 ] ; NEW_LINE print ( sum , end = " ▁ " ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT compute ( ) ; NEW_LINE Q = 4 ; NEW_LINE arr = [ [ 1 , 10 ] , [ 1 , 100 ] , [ 2 , 25 ] , [ 4 , 50 ] ] ; NEW_LINE for i in range ( Q ) : NEW_LINE INDENT printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; NEW_LINE DEDENT DEDENT
Total number of Subsets of size at most K | Function to compute the value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to calculate sum of nCj from j = 1 to k ; Calling the nCr function for each value of j ; Driver code
def binomialCoeff ( n , k ) : NEW_LINE INDENT C = [ [ 0 for i in range ( k + 1 ) ] for j in range ( n + 1 ) ] ; NEW_LINE i , j = 0 , 0 ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , k ) + 1 ) : NEW_LINE INDENT if ( j == 0 or j == i ) : NEW_LINE INDENT C [ i ] [ j ] = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; NEW_LINE DEDENT DEDENT DEDENT return C [ n ] [ k ] ; NEW_LINE DEDENT def count ( n , k ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for j in range ( 1 , k + 1 ) : NEW_LINE INDENT sum = sum + binomialCoeff ( n , j ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 ; NEW_LINE k = 2 ; NEW_LINE print ( count ( n , k ) , end = " " ) ; NEW_LINE n1 = 5 ; NEW_LINE k1 = 2 ; NEW_LINE print ( count ( n1 , k1 ) ) ; NEW_LINE DEDENT