codeparrot/xlcost-text-to-code huggingface dataset:
Count all perfect divisors of a number | Below is Python3 code to count total perfect divisors ; Pre - compute counts of all perfect divisors of all numbers upto MAX . ; Iterate through all the multiples of i * i ; Increment all such multiples by 1 ; Returns count of perfect divisors of n . ; Driver code
MAX = 100001 NEW_LINE perfectDiv = [ 0 ] * MAX NEW_LINE def precomputeCounts ( ) : NEW_LINE INDENT i = 1 NEW_LINE while i * i < MAX : NEW_LINE INDENT for j in range ( i * i , MAX , i * i ) : NEW_LINE INDENT perfectDiv [ j ] += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT def countPerfectDivisors ( n ) : NEW_LINE INDENT return perfectDiv [ n ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT precomputeCounts ( ) NEW_LINE n = 16 NEW_LINE print ( " Total ▁ perfect ▁ divisors ▁ of ▁ " , n , " ▁ = ▁ " , countPerfectDivisors ( n ) ) NEW_LINE n = 12 NEW_LINE print ( " Total ▁ perfect ▁ divisors ▁ of ▁ " , n , " ▁ = ▁ " , countPerfectDivisors ( n ) ) NEW_LINE DEDENT
Prime Factorization using Sieve O ( log n ) for multiple queries | Python3 program to find prime factorization of a number n in O ( Log n ) time with precomputation allowed . ; stores smallest prime factor for every number ; Calculating SPF ( Smallest Prime Factor ) for every number till MAXN . Time Complexity : O ( nloglogn ) ; marking smallest prime factor for every number to be itself . ; separately marking spf for every even number as 2 ; checking if i is prime ; marking SPF for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; A O ( log n ) function returning prime factorization by dividing by smallest prime factor at every step ; precalculating Smallest Prime Factor ; calling getFactorization function
import math as mt NEW_LINE MAXN = 100001 NEW_LINE spf = [ 0 for i in range ( 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 , mt . ceil ( mt . sqrt ( MAXN ) ) ) : 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 getFactorization ( x ) : NEW_LINE INDENT ret = list ( ) NEW_LINE while ( x != 1 ) : NEW_LINE INDENT ret . append ( spf [ x ] ) NEW_LINE x = x // spf [ x ] NEW_LINE DEDENT return ret NEW_LINE DEDENT sieve ( ) NEW_LINE x = 12246 NEW_LINE print ( " prime ▁ factorization ▁ for " , x , " : ▁ " , end = " " ) NEW_LINE p = getFactorization ( x ) NEW_LINE for i in range ( len ( p ) ) : NEW_LINE INDENT print ( p [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Nearest element with at | Python3 program to print nearest element with at least one common prime factor . ; Pre - computation of smallest prime divisor of all numbers ; Prime number will have same divisor ; Function to calculate all divisors of input array ; Pre - compute all the divisors of array element by using prime factors ; Traverse all elements , ; For every divisor of current element , find closest element . ; Visit divisor if not visited ; Fetch the index of visited divisor ; Update the divisor index to current index ; Set the minimum distance ; Set the min distance of current index ' i ' to nearest one ; Add 1 as indexing starts from 0 ; Set the min distance of found index 'ind ; Add 1 as indexing starts from 0 ; Simple sieve to find smallest prime divisor of number from 2 to MAX ; function to calculate nearest distance of every array elements ; Print the nearest distance having GDC > 1
MAX = 100001 NEW_LINE INF = 10 ** 9 NEW_LINE primedivisor = [ 0 for i in range ( MAX ) ] NEW_LINE dist = [ 0 for i in range ( MAX ) ] NEW_LINE pos = [ 0 for i in range ( MAX ) ] NEW_LINE divInd = [ 0 for i in range ( MAX ) ] NEW_LINE divisors = [ [ ] for i in range ( MAX ) ] NEW_LINE def sieveOfEratosthenes ( ) : NEW_LINE INDENT for i in range ( 2 , MAX ) : NEW_LINE INDENT if i * i > MAX : NEW_LINE INDENT break NEW_LINE DEDENT if ( primedivisor [ i ] == 0 ) : NEW_LINE INDENT for j in range ( 2 * i , MAX , i ) : NEW_LINE INDENT primedivisor [ j ] = i NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , MAX ) : NEW_LINE INDENT if ( primedivisor [ i ] == 0 ) : NEW_LINE INDENT primedivisor [ i ] = i NEW_LINE DEDENT DEDENT DEDENT def findDivisors ( arr , n ) : NEW_LINE INDENT for i in range ( MAX ) : NEW_LINE INDENT pos [ i ] = divInd [ i ] = - 1 NEW_LINE dist [ i ] = 10 ** 9 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT num = arr [ i ] NEW_LINE while ( num > 1 ) : NEW_LINE INDENT div = primedivisor [ num ] NEW_LINE divisors [ i ] . append ( div ) NEW_LINE while ( num % div == 0 ) : NEW_LINE INDENT num //= div NEW_LINE DEDENT DEDENT DEDENT DEDENT def nearestGCD ( arr , n ) : NEW_LINE INDENT findDivisors ( arr , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT for div in divisors [ i ] : NEW_LINE INDENT if ( divInd [ div ] == - 1 ) : NEW_LINE INDENT divInd [ div ] = i NEW_LINE DEDENT else : NEW_LINE INDENT ind = divInd [ div ] NEW_LINE divInd [ div ] = i NEW_LINE if ( dist [ i ] > abs ( ind - i ) ) : NEW_LINE INDENT dist [ i ] = abs ( ind - i ) NEW_LINE pos [ i ] = ind + 1 NEW_LINE DEDENT if ( dist [ ind ] > abs ( ind - i ) ) : NEW_LINE DEDENT DEDENT DEDENT DEDENT ' NEW_LINE INDENT dist [ ind ] = abs ( ind - i ) NEW_LINE pos [ ind ] = i + 1 NEW_LINE DEDENT sieveOfEratosthenes ( ) NEW_LINE arr = [ 2 , 9 , 4 , 3 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE nearestGCD ( arr , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( pos [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Largest subsequence having GCD greater than 1 | Returns length of the largest subsequence with GCD more than 1. ; Finding the Maximum value in arr [ ] ; Iterate from 2 to maximum possible divisor of all give values ; If we found divisor , increment count ; Driver code
def largestGCDSubsequence ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE maxele = max ( arr ) NEW_LINE for i in range ( 2 , maxele + 1 ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( arr [ j ] % i == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT ans = max ( ans , count ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 6 , 2 , 5 , 4 ] NEW_LINE size = len ( arr ) NEW_LINE print ( largestGCDSubsequence ( arr , size ) ) NEW_LINE DEDENT
Queries on the sum of prime factor counts in a range | Python3 program to find sum prime factors in given range . ; using sieve method to evaluating the prime factor of numbers ; if i is prime ; setting number of prime factor of a prime number . ; Returns sum of counts of prime factors in range from l to r . This function mainly uses count [ ] which is filled by Sieve ( ) ; finding the sum of number of prime factor of numbers in a range . ; Driver Code
MAX = 100006 ; NEW_LINE count = [ 0 ] * MAX ; NEW_LINE def sieve ( ) : NEW_LINE INDENT i = 2 ; NEW_LINE while ( i * i <= MAX ) : NEW_LINE INDENT if ( count [ i ] == 0 ) : NEW_LINE INDENT for j in range ( 2 * i , MAX , i ) : NEW_LINE INDENT count [ j ] += 1 ; NEW_LINE DEDENT count [ i ] = 1 ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT DEDENT def query ( l , r ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT sum += count [ i ] ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT sieve ( ) ; NEW_LINE print ( query ( 6 , 10 ) , query ( 1 , 5 ) ) ; NEW_LINE
Generation of n numbers with given set of factors | Generate n numbers with factors in factor [ ] ; array of k to store next multiples of given factors ; Prints n numbers output = 0 ; Next number to print as output ; Find the next smallest multiple ; Printing minimum in each iteration print the value if output is not equal to current value ( to avoid the duplicates ) ; incrementing the current value by the respective factor ; Driver code
def generateNumbers ( factor , n , k ) : NEW_LINE INDENT next = [ 0 ] * k ; NEW_LINE i = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT toincrement = 0 ; NEW_LINE for j in range ( k ) : NEW_LINE INDENT if ( next [ j ] < next [ toincrement ] ) : NEW_LINE INDENT toincrement = j ; NEW_LINE DEDENT DEDENT if ( output != next [ toincrement ] ) : NEW_LINE INDENT output = next [ toincrement ] ; NEW_LINE print ( next [ toincrement ] , end = " ▁ " ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT next [ toincrement ] += factor [ toincrement ] ; NEW_LINE DEDENT DEDENT factor = [ 3 , 5 , 7 ] ; NEW_LINE n = 10 ; NEW_LINE k = len ( factor ) ; NEW_LINE generateNumbers ( factor , n , k ) ; NEW_LINE
Reversible numbers | Function to check reversible number ; Calculate reverse of n ; Calculate sum of number and its reverse ; Check for reverse number reach digit must be odd ; Driver Code
def checkReversible ( n ) : NEW_LINE INDENT rev = 0 NEW_LINE flag = n NEW_LINE while ( flag != 0 ) : NEW_LINE INDENT rem = flag % 10 NEW_LINE rev *= 10 NEW_LINE rev += rem NEW_LINE flag //= 10 NEW_LINE DEDENT sum = rev + n NEW_LINE while ( sum and ( ( rem % 2 ) != 0 ) ) : NEW_LINE INDENT rem = sum % 10 NEW_LINE sum //= 10 NEW_LINE DEDENT if ( sum == 0 ) : NEW_LINE INDENT print ( " Reversible ▁ Number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Non - Reversible ▁ Number " ) NEW_LINE DEDENT DEDENT n = 36 NEW_LINE checkReversible ( n ) NEW_LINE
Multiplicative order | function for GCD ; Function return smallest + ve integer that holds condition A ^ k ( mod N ) = 1 ; result store power of A that rised to the power N - 1 ; modular arithmetic ; return smallest + ve integer ; increment power ; Driver program
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 multiplicativeOrder ( A , N ) : NEW_LINE INDENT if ( GCD ( A , N ) != 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT result = 1 NEW_LINE K = 1 NEW_LINE while ( K < N ) : NEW_LINE INDENT result = ( result * A ) % N NEW_LINE if ( result == 1 ) : NEW_LINE INDENT return K NEW_LINE DEDENT K = K + 1 NEW_LINE DEDENT return - 1 NEW_LINE DEDENT A = 4 NEW_LINE N = 7 NEW_LINE print ( multiplicativeOrder ( A , N ) ) NEW_LINE
Sum of product of x and y such that floor ( n / x ) = y | Python3 program to find sum of product of x and y such that n / x = y ( Integer Division ) ; Return the sum of natural number in a range . ; n * ( n + 1 ) / 2. ; Return the sum of product x * y . ; Iterating i from 1 to sqrt ( n ) ; Finding the upper limit . ; Finding the lower limit . ; Driven Code
import math NEW_LINE def sumOfRange ( a , b ) : NEW_LINE INDENT i = ( a * ( a + 1 ) ) >> 1 ; NEW_LINE j = ( b * ( b + 1 ) ) >> 1 ; NEW_LINE return ( i - j ) ; NEW_LINE DEDENT def sumofproduct ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE root = int ( math . sqrt ( n ) ) ; NEW_LINE for i in range ( 1 , root + 1 ) : NEW_LINE INDENT up = int ( n / i ) ; NEW_LINE low = max ( int ( n / ( i + 1 ) ) , root ) ; NEW_LINE sum += ( i * sumOfRange ( up , low ) ) ; NEW_LINE sum += ( i * int ( n / i ) ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT n = 10 ; NEW_LINE print ( sumofproduct ( n ) ) ; NEW_LINE
Primitive root of a prime number n modulo n | Python3 program to find primitive root of a given number n ; Returns True if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Iterative Function to calculate ( x ^ n ) % p in O ( logy ) ; 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 ; Utility function to store prime factors of a number ; Print the number of 2 s that divide n ; n must be odd at this po . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Function to find smallest primitive root of n ; Check if n is prime or not ; Find value of Euler Totient function of n . Since n is a prime number , the value of Euler Totient function is n - 1 as there are n - 1 relatively prime numbers . ; Find prime factors of phi and store in a set ; Check for every number from 2 to phi ; Iterate through all prime factors of phi . and check if we found a power with value 1 ; Check if r ^ ( ( phi ) / primefactors ) mod n is 1 or not ; If there was no power with value 1. ; If no primitive root found ; Driver Code
from math import sqrt NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT 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 = i + 6 NEW_LINE DEDENT return True NEW_LINE DEDENT def power ( x , y , p ) : NEW_LINE INDENT while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT def findPrimefactors ( s , n ) : NEW_LINE INDENT while ( n % 2 == 0 ) : NEW_LINE INDENT s . add ( 2 ) NEW_LINE n = n // 2 NEW_LINE DEDENT for i in range ( 3 , int ( sqrt ( n ) ) , 2 ) : NEW_LINE INDENT while ( n % i == 0 ) : NEW_LINE INDENT s . add ( i ) NEW_LINE n = n // i NEW_LINE DEDENT DEDENT if ( n > 2 ) : NEW_LINE INDENT s . add ( n ) NEW_LINE DEDENT DEDENT def findPrimitive ( n ) : NEW_LINE INDENT s = set ( ) NEW_LINE if ( isPrime ( n ) == False ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT phi = n - 1 NEW_LINE findPrimefactors ( s , phi ) NEW_LINE for r in range ( 2 , phi + 1 ) : NEW_LINE INDENT flag = False NEW_LINE for it in s : NEW_LINE INDENT if ( power ( r , phi // it , n ) == 1 ) : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == False ) : NEW_LINE INDENT return r NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT n = 761 NEW_LINE print ( " Smallest ▁ primitive ▁ root ▁ of " , n , " is " , findPrimitive ( n ) ) NEW_LINE
Minimum number of power terms with sum equal to n | Return minimum power terms of x required ; if x is 1 , return n since any power of 1 is 1 only . ; Consider n = a * x + b where a = n / x and b = n % x . ; Update count of powers for 1 's added ; Repeat the process for reduced n ; Driver code
def minPower ( n , x ) : NEW_LINE INDENT if ( x == 1 ) : NEW_LINE INDENT return n NEW_LINE DEDENT ans = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT ans += ( n % x ) NEW_LINE n //= x NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 5 NEW_LINE x = 3 NEW_LINE print ( minPower ( n , x ) ) NEW_LINE
Sum of Perrin Numbers | function for sum of first n Perrin number . ; if ( n == 0 ) : n = 0 ; if ( n == 1 ) : n = 1 ; if ( n == 2 ) : n = 2 ; calculate k = 5 sum of three previous step . ; Sum remaining numbers ; calculate next term ; Driver code
def calSum ( n ) : NEW_LINE INDENT a = 3 NEW_LINE b = 0 NEW_LINE c = 2 NEW_LINE INDENT return 3 NEW_LINE return 3 NEW_LINE return 5 NEW_LINE DEDENT sum = 5 NEW_LINE while ( n > 2 ) : NEW_LINE INDENT d = a + b NEW_LINE sum = sum + d NEW_LINE a = b NEW_LINE b = c NEW_LINE c = d NEW_LINE n = n - 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 9 NEW_LINE print ( calSum ( n ) ) NEW_LINE
Print the kth common factor of two numbers | Returns k 'th common factor of x and y. ; Find smaller of two numbers ; Count common factors until we either reach small or count becomes k . ; If we reached small ; Driver code
def findKHCF ( x , y , k ) : NEW_LINE INDENT small = min ( x , y ) NEW_LINE count = 1 NEW_LINE for i in range ( 2 , small + 1 ) : NEW_LINE INDENT if ( x % i == 0 and y % i == 0 ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT if ( count == k ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT x = 4 NEW_LINE y = 24 NEW_LINE k = 3 NEW_LINE print ( findKHCF ( x , y , k ) ) NEW_LINE
Find minimum number to be divided to make a number a perfect square | Python program to find minimum number which divide n to make it a perfect square . ; Return the minimum number to be divided to make n a perfect square . ; Since 2 is only even prime , compute its power separately . ; If count is odd , it must be removed by dividing n by prime number . ; If count is odd , it must be removed by dividing n by prime number . ; Driver Code
import math NEW_LINE def findMinNumber ( n ) : NEW_LINE INDENT count = 0 NEW_LINE ans = 1 NEW_LINE while n % 2 == 0 : NEW_LINE INDENT count += 1 NEW_LINE n //= 2 NEW_LINE DEDENT if count % 2 is not 0 : NEW_LINE INDENT ans *= 2 NEW_LINE DEDENT for i in range ( 3 , ( int ) ( math . sqrt ( n ) ) + 1 , 2 ) : NEW_LINE INDENT count = 0 NEW_LINE while n % i == 0 : NEW_LINE INDENT count += 1 NEW_LINE n //= i NEW_LINE DEDENT if count % 2 is not 0 : NEW_LINE INDENT ans *= i NEW_LINE DEDENT DEDENT if n > 2 : NEW_LINE INDENT ans *= n NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 72 NEW_LINE print ( findMinNumber ( n ) ) NEW_LINE
Program to implement Collatz Conjecture | Function to find if n reaches to 1 or not . ; If there is a cycle formed , we can 't reach 1. ; If n is odd then pass n = 3 n + 1 else n = n / 2 ; Wrapper over isToOneRec ( ) ; To store numbers visited using recursive calls . ; Driver Code
def isToOneRec ( n : int , s : set ) -> bool : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return True NEW_LINE DEDENT if n in s : NEW_LINE INDENT return False NEW_LINE DEDENT if n % 2 : NEW_LINE INDENT return isToOneRec ( 3 * n + 1 , s ) NEW_LINE DEDENT else : NEW_LINE INDENT return isToOneRec ( n // 2 , s ) NEW_LINE DEDENT DEDENT def isToOne ( n : int ) -> bool : NEW_LINE INDENT s = set ( ) NEW_LINE return isToOneRec ( n , s ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE if isToOne ( n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Program to implement Collatz Conjecture | Function to find if n reaches to 1 or not . ; Return true if n is positive ; Drivers code
def isToOne ( n ) : NEW_LINE INDENT return ( n > 0 ) NEW_LINE DEDENT n = 5 NEW_LINE if isToOne ( n ) == True : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
GCD of two numbers formed by n repeating x and y times | Return the Greatest common Divisor of two numbers . ; Prints Greatest Common Divisor of number formed by n repeating x times and y times . ; Finding GCD of x and y . ; Print n , g times . ; 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 findgcd ( n , x , y ) : NEW_LINE INDENT g = gcd ( x , y ) NEW_LINE for i in range ( g ) : NEW_LINE INDENT print ( n ) NEW_LINE DEDENT DEDENT n = 123 NEW_LINE x = 5 NEW_LINE y = 2 NEW_LINE findgcd ( n , x , y ) NEW_LINE
Count natural numbers whose factorials are divisible by x but not y | GCD function to compute the greatest divisor among a and b ; Returns first number whose factorial is divisible by x . ; Result ; Remove common factors ; We found first i . ; Count of natural numbers whose factorials are divisible by x but not y . ; Return difference between first natural number whose factorial is divisible by y and first natural number whose factorial is divisible by x . ; Driver code
def gcd ( a , b ) : NEW_LINE INDENT if ( ( a % b ) == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def firstFactorialDivisibleNumber ( x ) : NEW_LINE INDENT i = 1 NEW_LINE new_x = x NEW_LINE for i in range ( 1 , x ) : NEW_LINE INDENT new_x /= gcd ( i , new_x ) NEW_LINE if ( new_x == 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return i NEW_LINE DEDENT def countFactorialXNotY ( x , y ) : NEW_LINE INDENT return ( firstFactorialDivisibleNumber ( y ) - firstFactorialDivisibleNumber ( x ) ) NEW_LINE DEDENT x = 15 NEW_LINE y = 25 NEW_LINE print ( countFactorialXNotY ( x , y ) ) NEW_LINE
Find the first natural number whose factorial is divisible by x | Returns first number whose factorial divides x . ; i = 1 ; Result ; Driver code
def firstFactorialDivisibleNumber ( x ) : NEW_LINE INDENT fact = 1 ; NEW_LINE for i in range ( 1 , x ) : NEW_LINE INDENT fact = fact * i NEW_LINE if ( fact % x == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return i NEW_LINE DEDENT x = 16 NEW_LINE print ( firstFactorialDivisibleNumber ( x ) ) NEW_LINE
Find two prime numbers with given sum | Generate all prime numbers less than n . ; Initialize all entries of boolean array as True . A value in isPrime [ i ] will finally be False if i is Not a prime , else True bool isPrime [ n + 1 ] ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Prints a prime pair with given sum ; Generating primes using Sieve ; Traversing all numbers to find first pair ; Driven program
def SieveOfEratosthenes ( n , isPrime ) : NEW_LINE INDENT isPrime [ 0 ] = isPrime [ 1 ] = False NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT isPrime [ i ] = True NEW_LINE DEDENT p = 2 NEW_LINE while ( p * p <= n ) : NEW_LINE INDENT if ( isPrime [ p ] == True ) : NEW_LINE INDENT i = p * p NEW_LINE while ( i <= n ) : NEW_LINE INDENT isPrime [ i ] = False NEW_LINE i += p NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def findPrimePair ( n ) : NEW_LINE INDENT isPrime = [ 0 ] * ( n + 1 ) NEW_LINE SieveOfEratosthenes ( n , isPrime ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( isPrime [ i ] and isPrime [ n - i ] ) : NEW_LINE INDENT print ( i , ( n - i ) ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT n = 74 NEW_LINE findPrimePair ( n ) NEW_LINE
Count numbers with same first and last digits | Utility method to get first digit of x ; method to return count of numbers with same starting and ending digit from 1 upto x ; get ten - spans from 1 to x ; add 9 to consider all 1 digit numbers ; Find first and last digits ; If last digit is greater than first digit then decrease count by 1 ; Method to return count of numbers with same starting and ending digit between start and end ; Driver Code
def getFirstDigit ( x ) : NEW_LINE INDENT while ( x >= 10 ) : NEW_LINE INDENT x //= 10 NEW_LINE DEDENT return x NEW_LINE DEDENT def getCountWithSameStartAndEndFrom1 ( x ) : NEW_LINE INDENT if ( x < 10 ) : NEW_LINE INDENT return x NEW_LINE DEDENT tens = x // 10 NEW_LINE res = tens + 9 NEW_LINE firstDigit = getFirstDigit ( x ) NEW_LINE lastDigit = x % 10 NEW_LINE if ( lastDigit < firstDigit ) : NEW_LINE INDENT res = res - 1 NEW_LINE DEDENT return res NEW_LINE DEDENT def getCountWithSameStartAndEnd ( start , end ) : NEW_LINE INDENT return ( getCountWithSameStartAndEndFrom1 ( end ) - getCountWithSameStartAndEndFrom1 ( start - 1 ) ) NEW_LINE DEDENT start = 5 NEW_LINE end = 40 NEW_LINE print ( getCountWithSameStartAndEnd ( start , end ) ) NEW_LINE
Right | Generate all prime numbers less than n . ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime , else true bool isPrime [ n + 1 ] ; ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Returns true if n is right - truncatable , else false ; Generating primes using Sieve ; Checking whether the number remains prime when the last ( " right " ) digit is successively removed ; Driven program
def sieveOfEratosthenes ( n , isPrime ) : NEW_LINE INDENT isPrime [ 0 ] = isPrime [ 1 ] = False NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT isPrime [ i ] = True NEW_LINE DEDENT p = 2 NEW_LINE while ( p * p <= n ) : NEW_LINE INDENT if ( isPrime [ p ] == True ) : NEW_LINE INDENT i = p * 2 NEW_LINE while ( i <= n ) : NEW_LINE INDENT isPrime [ i ] = False NEW_LINE i = i + p NEW_LINE DEDENT DEDENT p = p + 1 NEW_LINE DEDENT DEDENT def rightTruPrime ( n ) : NEW_LINE INDENT isPrime = [ None ] * ( n + 1 ) NEW_LINE sieveOfEratosthenes ( n , isPrime ) NEW_LINE while ( n != 0 ) : NEW_LINE INDENT if ( isPrime [ n ] ) : NEW_LINE INDENT n = n // 10 NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT n = 59399 NEW_LINE if ( rightTruPrime ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT