codeparrot/xlcost-text-to-code huggingface dataset:
Print all safe primes below N | Python 3 implementation of the approach ; Function to print first n safe primes ; Initialize all entries of integer array as 1. A value in prime [ i ] will finally be 0 if i is Not a prime , else 1 ; 0 and 1 are not primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; If i is prime ; 2 p + 1 ; If 2 p + 1 is also a prime then set prime [ 2 p + 1 ] = 2 ; i is a safe prime ; Driver code
from math import sqrt NEW_LINE def printSafePrimes ( n ) : NEW_LINE INDENT prime = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT prime [ i ] = 1 NEW_LINE DEDENT prime [ 0 ] = prime [ 1 ] = 0 NEW_LINE for p in range ( 2 , int ( sqrt ( n ) ) + 1 , 1 ) : NEW_LINE INDENT if ( prime [ p ] == 1 ) : NEW_LINE INDENT for i in range ( p * 2 , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = 0 NEW_LINE DEDENT DEDENT DEDENT for i in range ( 2 , n + 1 , 1 ) : NEW_LINE INDENT if ( prime [ i ] != 0 ) : NEW_LINE INDENT temp = ( 2 * i ) + 1 NEW_LINE if ( temp <= n and prime [ temp ] != 0 ) : NEW_LINE INDENT prime [ temp ] = 2 NEW_LINE DEDENT DEDENT DEDENT for i in range ( 5 , n + 1 ) : NEW_LINE INDENT if ( prime [ i ] == 2 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 20 NEW_LINE printSafePrimes ( n ) NEW_LINE DEDENT
Minimum multiplications with { 2 , 3 , 7 } to make two numbers equal | Function to find powers of 2 , 3 and 7 in x ; To keep count of each divisor ; To store the result ; Count powers of 2 in x ; Count powers of 3 in x ; Count powers of 7 in x ; Remaining number which is not divisible by 2 , 3 or 7 ; Function to return the minimum number of given operations required to make a and b equal ; a = x * 2 ^ a1 * 3 ^ a2 * 7 ^ a3 va [ 0 ] = a1 va [ 1 ] = a2 va [ 2 ] = a3 va [ 3 ] = x ; Similarly for b ; If a and b cannot be made equal with the given operation . Note that va [ 3 ] and vb [ 3 ] contain remaining numbers after repeated divisions with 2 , 3 and 7. If remaining numbers are not same then we cannot make them equal . ; Minimum number of operations required ; Driver code
def Divisors ( x ) : NEW_LINE INDENT c = 0 NEW_LINE v = [ ] NEW_LINE while ( x % 2 == 0 ) : NEW_LINE INDENT c += 1 NEW_LINE x /= 2 NEW_LINE DEDENT v . append ( c ) NEW_LINE c = 0 NEW_LINE while ( x % 3 == 0 ) : NEW_LINE INDENT c += 1 NEW_LINE x /= 3 NEW_LINE DEDENT v . append ( c ) NEW_LINE c = 0 NEW_LINE while ( x % 7 == 0 ) : NEW_LINE INDENT c += 1 NEW_LINE x /= 7 NEW_LINE DEDENT v . append ( c ) NEW_LINE v . append ( x ) NEW_LINE return v NEW_LINE DEDENT def MinOperations ( a , b ) : NEW_LINE INDENT va = Divisors ( a ) NEW_LINE vb = Divisors ( b ) NEW_LINE if ( va [ 3 ] != vb [ 3 ] ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT minOperations = abs ( va [ 0 ] - vb [ 0 ] ) + abs ( va [ 1 ] - vb [ 1 ] ) + abs ( va [ 2 ] - vb [ 2 ] ) NEW_LINE return minOperations NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 14 NEW_LINE b = 28 NEW_LINE print ( MinOperations ( a , b ) ) NEW_LINE DEDENT
Product of N with its largest odd digit | Function to return the largest odd digit in n ; If all digits are even then - 1 will be returned ; Last digit from n ; If current digit is odd and > maxOdd ; Remove last digit ; Return the maximum odd digit ; Function to return the product of n with its largest odd digit ; If there are no odd digits in n ; Product of n with its largest odd digit ; Driver code
def largestOddDigit ( n ) : NEW_LINE INDENT maxOdd = - 1 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT digit = n % 10 NEW_LINE if ( digit % 2 == 1 and digit > maxOdd ) : NEW_LINE INDENT maxOdd = digit NEW_LINE DEDENT n = n // 10 NEW_LINE DEDENT return maxOdd NEW_LINE DEDENT def getProduct ( n ) : NEW_LINE INDENT maxOdd = largestOddDigit ( n ) NEW_LINE if ( maxOdd == - 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return ( n * maxOdd ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 12345 NEW_LINE print ( getProduct ( n ) ) NEW_LINE DEDENT
Check if a number is perfect square without finding square root | Program to find if x is a perfect square . ; Check if mid is perfect square ; Mid is small -> go right to increase mid ; Mid is large -> to left to decrease mid ; Driver code ; Function Call
def isPerfectSquare ( x ) : NEW_LINE INDENT left = 1 NEW_LINE right = x NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = ( left + right ) >> 1 NEW_LINE if ( ( mid * mid ) == x ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( mid * mid < x ) : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT right = mid - 1 NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 2500 NEW_LINE if ( isPerfectSquare ( x ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Find k numbers which are powers of 2 and have sum N | Set 1 | function to prnumbers ; Count the number of set bits ; Not - possible condition ; Stores the number ; Get the set bits ; Iterate till we get K elements ; Get the topmost element ; append the elements / 2 into priority queue ; Prall elements ; Driver Code
def printNum ( n , k ) : NEW_LINE INDENT x = 0 NEW_LINE m = n NEW_LINE while ( m ) : NEW_LINE INDENT x += m & 1 NEW_LINE m >>= 1 NEW_LINE DEDENT if k < x or k > n : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT pq = [ ] NEW_LINE two = 1 NEW_LINE while ( n ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT pq . append ( two ) NEW_LINE DEDENT two = two * 2 NEW_LINE n = n >> 1 NEW_LINE DEDENT while ( len ( pq ) < k ) : NEW_LINE INDENT el = pq [ - 1 ] NEW_LINE pq . pop ( ) NEW_LINE pq . append ( el // 2 ) NEW_LINE pq . append ( el // 2 ) NEW_LINE DEDENT ind = 0 NEW_LINE pq . sort ( ) NEW_LINE while ( ind < k ) : NEW_LINE INDENT print ( pq [ - 1 ] , end = " ▁ " ) NEW_LINE pq . pop ( ) NEW_LINE ind += 1 NEW_LINE DEDENT DEDENT n = 9 NEW_LINE k = 4 NEW_LINE printNum ( n , k ) NEW_LINE
Sum of LCM ( 1 , n ) , LCM ( 2 , n ) , LCM ( 3 , n ) , ... , LCM ( n , n ) | Python3 implementation of the approach ; Euler totient Function ; Function to return the required LCM sum ; Summation of d * ETF ( d ) where d belongs to set of divisors of n ; Driver code
n = 100002 ; NEW_LINE phi = [ 0 ] * ( n + 2 ) ; NEW_LINE ans = [ 0 ] * ( n + 2 ) ; NEW_LINE def ETF ( ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT phi [ i ] = i ; NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( phi [ i ] == i ) : NEW_LINE INDENT phi [ i ] = i - 1 ; NEW_LINE for j in range ( 2 * i , n + 1 , i ) : NEW_LINE INDENT phi [ j ] = ( phi [ j ] * ( i - 1 ) ) // i ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def LcmSum ( m ) : NEW_LINE INDENT ETF ( ) ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( i , n + 1 , i ) : NEW_LINE INDENT ans [ j ] += ( i * phi [ i ] ) ; NEW_LINE DEDENT DEDENT answer = ans [ m ] ; NEW_LINE answer = ( answer + 1 ) * m ; NEW_LINE answer = answer // 2 ; NEW_LINE return answer ; NEW_LINE DEDENT m = 5 ; NEW_LINE print ( LcmSum ( m ) ) ; NEW_LINE
Permutations of string such that no two vowels are adjacent | Factorial of a number ; Function to find c ( n , r ) ; Function to count permutations of string such that no two vowels are adjacent ; Finding the frequencies of the characters ; finding the no . of vowels and consonants in given word ; finding places for the vowels ; ways to fill consonants 6 ! / 2 ! ; ways to put vowels 7 C5 x 5 ! ; Driver code
def factorial ( n ) : NEW_LINE INDENT fact = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT fact = fact * i NEW_LINE DEDENT return fact NEW_LINE DEDENT def ncr ( n , r ) : NEW_LINE INDENT return factorial ( n ) // ( factorial ( r ) * factorial ( n - r ) ) NEW_LINE DEDENT def countWays ( string ) : NEW_LINE INDENT freq = [ 0 ] * 26 NEW_LINE nvowels , nconsonants = 0 , 0 NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT freq [ ord ( string [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( i == 0 or i == 4 or i == 8 or i == 14 or i == 20 ) : NEW_LINE INDENT nvowels += freq [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT nconsonants += freq [ i ] NEW_LINE DEDENT DEDENT vplaces = nconsonants + 1 NEW_LINE cways = factorial ( nconsonants ) NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( i != 0 and i != 4 and i != 8 and i != 14 and i != 20 and freq [ i ] > 1 ) : NEW_LINE INDENT cways = cways // factorial ( freq [ i ] ) NEW_LINE DEDENT DEDENT vways = ncr ( vplaces , nvowels ) * factorial ( nvowels ) NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( i == 0 or i == 4 or i == 8 or i == 14 or i == 20 and freq [ i ] > 1 ) : NEW_LINE INDENT vways = vways // factorial ( freq [ i ] ) NEW_LINE DEDENT DEDENT return cways * vways ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " permutation " NEW_LINE print ( countWays ( string ) ) NEW_LINE DEDENT
Pairs from an array that satisfy the given condition | Function to return the number of set bits in n ; Function to return the count of required pairs ; Set bits for first element of the pair ; Set bits for second element of the pair ; Set bits of the resultant number which is the XOR of both the elements of the pair ; If the condition is satisfied ; Increment the count ; Return the total count ; Driver code
def setBits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n ) : NEW_LINE INDENT n = n & ( n - 1 ) NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def countPairs ( a , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n - 1 , 1 ) : NEW_LINE INDENT setbits_x = setBits ( a [ i ] ) NEW_LINE for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT setbits_y = setBits ( a [ j ] ) NEW_LINE setbits_xor_xy = setBits ( a [ i ] ^ a [ j ] ) ; NEW_LINE if ( setbits_x + setbits_y == setbits_xor_xy ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( a ) NEW_LINE print ( countPairs ( a , n ) ) NEW_LINE DEDENT
Number of array elements derivable from D after performing certain operations | Function to return gcd of a and b ; Function to Return the number of elements of arr [ ] which can be derived from D by performing ( + A , - A , + B , - B ) ; find the gcd of A and B ; counter stores the number of array elements which can be derived from D ; arr [ i ] can be derived from D only if | arr [ i ] - D | is divisible by gcd of A and B ; Driver Code
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) ; NEW_LINE DEDENT def findPossibleDerivables ( arr , n , D , A , B ) : NEW_LINE INDENT gcdAB = gcd ( A , B ) NEW_LINE counter = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( abs ( arr [ i ] - D ) % gcdAB ) == 0 ) : NEW_LINE INDENT counter += 1 NEW_LINE DEDENT DEDENT return counter NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 7 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE D , A , B = 5 , 4 , 2 NEW_LINE print ( findPossibleDerivables ( arr , n , D , A , B ) ) NEW_LINE a = [ 1 , 2 , 3 ] NEW_LINE n = len ( a ) NEW_LINE D , A , B = 6 , 3 , 2 NEW_LINE print ( findPossibleDerivables ( a , n , D , A , B ) ) NEW_LINE DEDENT
Find the sum of first N terms of the series 2 * 3 * 5 , 3 * 5 * 7 , 4 * 7 * 9 , ... | Function to return the sum of the first n terms of the given series ; As described in the approach ; Driver Code
def calSum ( n ) : NEW_LINE INDENT return ( n * ( 2 * n * n * n + 12 * n * n + 25 * n + 21 ) ) / 2 ; NEW_LINE DEDENT n = 3 NEW_LINE print ( calSum ( n ) ) NEW_LINE
Find elements of array using XOR of consecutive elements | Function to find the array elements using XOR of consecutive elements ; array to store the original elements ; first elements a i . e elements [ 0 ] = a ; To get the next elements we have to calculate xor of previous elements with given xor of 2 consecutive elements . e . g . if a ^ b = k1 so to get b xor a both side . b = k1 ^ a ; Printing the original array elements ; Driver code
def getElements ( a , arr , n ) : NEW_LINE INDENT elements = [ 1 for i in range ( n + 1 ) ] NEW_LINE elements [ 0 ] = a NEW_LINE for i in range ( n ) : NEW_LINE INDENT elements [ i + 1 ] = arr [ i ] ^ elements [ i ] NEW_LINE DEDENT for i in range ( n + 1 ) : NEW_LINE INDENT print ( elements [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 13 , 2 , 6 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE a = 5 NEW_LINE getElements ( a , arr , n ) NEW_LINE
Maximum GCD of N integers with given product | Python3 implementation of above approach ; Function to find maximum GCD of N integers with product P ; map to store prime factors of P ; prime factorization of P ; traverse all prime factors and multiply its 1 / N power to the result ; Driver code
from math import sqrt NEW_LINE def maxGCD ( N , P ) : NEW_LINE INDENT ans = 1 NEW_LINE prime_factors = { } NEW_LINE for i in range ( 2 , int ( sqrt ( P ) + 1 ) ) : NEW_LINE INDENT while ( P % i == 0 ) : NEW_LINE INDENT if i not in prime_factors : NEW_LINE INDENT prime_factors [ i ] = 0 NEW_LINE DEDENT prime_factors [ i ] += 1 NEW_LINE P //= i NEW_LINE DEDENT DEDENT if ( P != 1 ) : NEW_LINE INDENT prime_factors [ P ] += 1 NEW_LINE DEDENT for key , value in prime_factors . items ( ) : NEW_LINE INDENT ans *= pow ( key , value // N ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , P = 3 , 24 NEW_LINE print ( maxGCD ( N , P ) ) NEW_LINE DEDENT
Check if the sum of distinct digits of two integers are equal | Function to return the sum of distinct digits of a number ; Take last digit ; If digit has not been used before ; Set digit as used ; Remove last digit ; Function to check whether the sum of distinct digits of two numbers are equal ; Driver code
def distinctDigitSum ( n ) : NEW_LINE INDENT used = [ False ] * 10 NEW_LINE sum = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT digit = n % 10 NEW_LINE if ( not used [ digit ] ) : NEW_LINE INDENT used [ digit ] = True NEW_LINE sum += digit NEW_LINE DEDENT n = n // 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def checkSum ( m , n ) : NEW_LINE INDENT sumM = distinctDigitSum ( m ) NEW_LINE sumN = distinctDigitSum ( n ) NEW_LINE if ( sumM == sumN ) : NEW_LINE INDENT return " YES " NEW_LINE DEDENT return " NO " NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT m = 2452 NEW_LINE n = 9222 NEW_LINE print ( checkSum ( m , n ) ) NEW_LINE DEDENT
All pairs whose xor gives unique prime | Function that returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to return the count of valid pairs ; If xor ( a [ i ] , a [ j ] ) is prime and unique ; Driver code
def isPrime ( n ) : NEW_LINE INDENT if n <= 1 : NEW_LINE INDENT return False NEW_LINE DEDENT if n <= 3 : NEW_LINE INDENT return True NEW_LINE DEDENT if n % 2 == 0 or n % 3 == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT i = 5 NEW_LINE while i * i <= n : NEW_LINE INDENT if n % i == 0 or n % ( i + 2 ) == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT i += 6 NEW_LINE DEDENT return True NEW_LINE DEDENT def countPairs ( a , n ) : NEW_LINE INDENT count = 0 NEW_LINE m = dict ( ) NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if isPrime ( a [ i ] ^ a [ j ] ) and m . get ( a [ i ] ^ a [ j ] , 0 ) == 0 : NEW_LINE INDENT m [ ( a [ i ] ^ a [ j ] ) ] = 1 NEW_LINE count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT a = [ 10 , 12 , 23 , 45 , 5 , 6 ] NEW_LINE n = len ( a ) NEW_LINE print ( countPairs ( a , n ) ) NEW_LINE
Sum of element whose prime factors are present in array | Python program to find the sum of the elements of an array whose prime factors are present in the same array ; Stores smallest prime factor for every number ; Function to calculate SPF ( Smallest Prime Factor ) for every number till MAXN ; Marking smallest prime factor for every number to be itself . ; Separately marking spf for every even number as 2 ; If i is prime ; Marking SPF for all numbers divisible by i ; Marking spf [ j ] if it is not previously marked ; Function to return the sum of the elements of an array whose prime factors are present in the same array ; Function call to calculate smallest prime factors of all the numbers upto MAXN ; Create map for each element ; If smallest prime factor of num is present in array ; Each factor of arr [ i ] is present in the array ; Driver Code ; Function call to print required answer
from collections import defaultdict NEW_LINE MAXN = 1000001 NEW_LINE MAXN_sqrt = int ( MAXN ** ( 0.5 ) ) NEW_LINE spf = [ None ] * ( MAXN ) NEW_LINE def sieve ( ) : NEW_LINE INDENT spf [ 1 ] = 1 NEW_LINE for i in range ( 2 , MAXN ) : NEW_LINE INDENT spf [ i ] = i NEW_LINE DEDENT for i in range ( 4 , MAXN , 2 ) : NEW_LINE INDENT spf [ i ] = 2 NEW_LINE DEDENT for i in range ( 3 , MAXN_sqrt ) : NEW_LINE INDENT if spf [ i ] == i : NEW_LINE INDENT for j in range ( i * i , MAXN , i ) : NEW_LINE INDENT if spf [ j ] == j : NEW_LINE INDENT spf [ j ] = i NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def sumFactors ( arr , n ) : NEW_LINE INDENT sieve ( ) NEW_LINE Map = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT Map [ arr [ i ] ] = 1 NEW_LINE DEDENT Sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT num = arr [ i ] NEW_LINE while num != 1 and Map [ spf [ num ] ] == 1 : NEW_LINE INDENT num = num // spf [ num ] NEW_LINE DEDENT if num == 1 : NEW_LINE INDENT Sum += arr [ i ] NEW_LINE DEDENT DEDENT return Sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 11 , 55 , 25 , 100 ] NEW_LINE n = len ( arr ) NEW_LINE print ( sumFactors ( arr , n ) ) NEW_LINE DEDENT
Find nth Hermite number | Python 3 program to find nth Hermite number ; Utility function to calculate double factorial of a number ; Function to return nth Hermite number ; If n is even then return 0 ; If n is odd ; Calculate double factorial of ( n - 1 ) and multiply it with 2 ^ ( n / 2 ) ; If n / 2 is odd then nth Hermite number will be negative ; Return nth Hermite number ; Driver Code ; Print nth Hermite number
from math import pow NEW_LINE def doubleFactorial ( n ) : NEW_LINE INDENT fact = 1 NEW_LINE for i in range ( 1 , n + 1 , 2 ) : NEW_LINE INDENT fact = fact * i NEW_LINE DEDENT return fact NEW_LINE DEDENT def hermiteNumber ( n ) : NEW_LINE INDENT if ( n % 2 == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT number = ( ( pow ( 2 , n / 2 ) ) * doubleFactorial ( n - 1 ) ) NEW_LINE if ( ( n / 2 ) % 2 == 1 ) : NEW_LINE INDENT number = number * - 1 NEW_LINE DEDENT return number NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 6 NEW_LINE print ( int ( hermiteNumber ( n ) ) ) NEW_LINE DEDENT
Count numbers in a range having GCD of powers of prime factors equal to 1 | Python3 implementation of the approach ; Vector to store powers greater than 3 ; Set to store perfect squares ; Set to store powers other than perfect squares ; Pushing squares ; if the values is already a perfect square means present in the set ; Run loop until some power of current number doesn 't exceed MAX ; Pushing only odd powers as even power of a number can always be expressed as a perfect square which is already present in set squares ; Inserting those sorted values of set into a vector ; Precompute the powers ; Calculate perfect squares in range using sqrtl function ; Calculate upper value of R in vector using binary search ; Calculate lower value of L in vector using binary search ; Calculate perfect powers ; Compute final answer ; Driver Code
from bisect import bisect as upper_bound NEW_LINE from bisect import bisect_left as lower_bound NEW_LINE from math import floor NEW_LINE N = 1000005 NEW_LINE MAX = 10 ** 18 NEW_LINE powers = [ ] NEW_LINE squares = dict ( ) NEW_LINE s = dict ( ) NEW_LINE def powersPrecomputation ( ) : NEW_LINE INDENT for i in range ( 2 , N ) : NEW_LINE INDENT squares [ i * i ] = 1 NEW_LINE if ( i not in squares . keys ( ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT temp = i NEW_LINE while ( i * i <= ( MAX // temp ) ) : NEW_LINE INDENT temp *= ( i * i ) NEW_LINE s [ temp ] = 1 NEW_LINE DEDENT DEDENT for x in s : NEW_LINE INDENT powers . append ( x ) NEW_LINE DEDENT DEDENT def calculateAnswer ( L , R ) : NEW_LINE INDENT powersPrecomputation ( ) NEW_LINE perfectSquares = floor ( ( R ) ** ( .5 ) ) - floor ( ( L - 1 ) ** ( .5 ) ) NEW_LINE high = upper_bound ( powers , R ) NEW_LINE low = lower_bound ( powers , L ) NEW_LINE perfectPowers = perfectSquares + ( high - low ) NEW_LINE ans = ( R - L + 1 ) - perfectPowers NEW_LINE return ans NEW_LINE DEDENT L = 13 NEW_LINE R = 20 NEW_LINE print ( calculateAnswer ( L , R ) ) NEW_LINE
Sum of integers upto N with given unit digit | Function to return the required sum ; If the unit digit is d ; Driver code
def getSum ( n , d ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT if ( i % 10 == d ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , d = 30 , 3 NEW_LINE print ( getSum ( n , d ) ) NEW_LINE DEDENT
Split a number into 3 parts such that none of the parts is divisible by 3 | Python3 program to split a number into three parts such than none of them is divisible by 3. ; Print x = 1 , y = 1 and z = N - 2 ; Otherwise , print x = 1 , y = 2 and z = N - 3 ; Driver code
def printThreeParts ( N ) : NEW_LINE INDENT if ( N % 3 == 0 ) : NEW_LINE INDENT print ( " ▁ x ▁ = ▁ 1 , ▁ y ▁ = ▁ 1 , ▁ z ▁ = ▁ " , N - 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ▁ x ▁ = ▁ 1 , ▁ y ▁ = ▁ 2 , ▁ z ▁ = ▁ " , N - 3 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 10 NEW_LINE printThreeParts ( N ) NEW_LINE DEDENT
Minimum absolute difference of a number and its closest prime | Python 3 program to find the minimum absolute difference between a number and its closest prime ; Function to check if a number is prime or not ; Function to find the minimum absolute difference between a number and its closest prime ; Variables to store first prime above and below N ; Finding first prime number greater than N ; Finding first prime number less than N ; Variables to store the differences ; Driver code
from math import sqrt NEW_LINE def isPrime ( N ) : NEW_LINE INDENT k = int ( sqrt ( N ) ) + 1 NEW_LINE for i in range ( 2 , k , 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def getDifference ( N ) : NEW_LINE INDENT if ( N == 0 ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT elif ( N == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif ( isPrime ( N ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT aboveN = - 1 NEW_LINE belowN = - 1 NEW_LINE n1 = N + 1 NEW_LINE while ( True ) : NEW_LINE INDENT if ( isPrime ( n1 ) ) : NEW_LINE INDENT aboveN = n1 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT n1 += 1 NEW_LINE DEDENT DEDENT n1 = N - 1 NEW_LINE while ( True ) : NEW_LINE INDENT if ( isPrime ( n1 ) ) : NEW_LINE INDENT belowN = n1 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT n1 -= 1 NEW_LINE DEDENT DEDENT diff1 = aboveN - N NEW_LINE diff2 = N - belowN NEW_LINE return min ( diff1 , diff2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 25 NEW_LINE print ( getDifference ( N ) ) NEW_LINE DEDENT
Check if the sum of perfect squares in an array is divisible by x | Python3 implementation of the approach ; Function that returns true if the the sum of all the perfect squares of the given array is divisible by x ; If a [ i ] is a perfect square ; Driver code
import math NEW_LINE def check ( a , y ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT x = math . sqrt ( a [ i ] ) NEW_LINE if ( math . floor ( x ) == math . ceil ( x ) ) : NEW_LINE INDENT sum = sum + a [ i ] NEW_LINE DEDENT DEDENT if ( sum % y == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT a = [ 2 , 3 , 4 , 9 , 10 ] NEW_LINE x = 13 NEW_LINE if check ( a , x ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT