codeparrot/xlcost-text-to-code huggingface dataset:
Runge | A sample differential equation " dy / dx ▁ = ▁ ( x ▁ - ▁ y ) /2" ; Finds value of y for a given x using step size h and initial value y0 at x0 . ; Count number of iterations using step size or step height h ; Iterate for number of iterations ; Apply Runge Kutta Formulas to find next value of y ; Update next value of y ; Update next value of x ; Driver Code
def dydx ( x , y ) : NEW_LINE INDENT return ( x + y - 2 ) ; NEW_LINE DEDENT def rungeKutta ( x0 , y0 , x , h ) : NEW_LINE INDENT n = round ( ( x - x0 ) / h ) ; NEW_LINE y = y0 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT k1 = h * dydx ( x0 , y ) ; NEW_LINE k2 = h * dydx ( x0 + 0.5 * h , y + 0.5 * k1 ) ; NEW_LINE y = y + ( 1.0 / 6.0 ) * ( k1 + 2 * k2 ) ; NEW_LINE x0 = x0 + h ; NEW_LINE DEDENT return y ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x0 = 0 ; y = 1 ; NEW_LINE x = 2 ; h = 0.2 ; NEW_LINE print ( " y ( x ) ▁ = " , rungeKutta ( x0 , y , x , h ) ) ; NEW_LINE DEDENT
Count of Fibonacci pairs which satisfy the given equation | Python program to find the count of Fibonacci pairs ( x , y ) which satisfy the equation Ax + By = N ; Array to store the Fibonacci numbers ; Array to store the number of ordered pairs ; Function to find if a number is a perfect square ; Function that returns 1 if N is non - fibonacci number else 0 ; N is Fibinacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both are perferct square ; Function to store the fibonacci numbers and their frequency in form a * x + b * y ; Storing the Fibonacci numbers ; For loop to find all the possible combinations of the Fibonacci numbers ; Finding the number of ordered pairs ; Driver code ; Find the ordered pair for every query
import math NEW_LINE size = 101 NEW_LINE fib = [ 0 ] * 100010 NEW_LINE freq = [ 0 ] * ( 100010 ) NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT s = int ( math . sqrt ( x ) ) NEW_LINE return ( s * s ) == x NEW_LINE DEDENT def isFibonacci ( n ) : NEW_LINE INDENT if ( isPerfectSquare ( 5 * n * n + 4 ) or isPerfectSquare ( 5 * n * n - 4 ) ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT return 0 ; NEW_LINE DEDENT def compute ( a , b ) : NEW_LINE INDENT for i in range ( 1 , 100010 ) : NEW_LINE INDENT fib [ i ] = isFibonacci ( i ) NEW_LINE DEDENT for x in range ( 1 , 100010 ) : NEW_LINE INDENT for y in range ( 1 , size ) : NEW_LINE INDENT if ( fib [ x ] == 1 and fib [ y ] == 1 and a * x + b * y < 100010 ) : NEW_LINE INDENT freq [ a * x + b * y ] += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT Q = 2 NEW_LINE A = 5 NEW_LINE B = 10 NEW_LINE compute ( A , B ) ; NEW_LINE arr = [ 50 , 150 ] NEW_LINE for i in range ( Q ) : NEW_LINE INDENT print ( freq [ arr [ i ] ] , end = " ▁ " ) NEW_LINE DEDENT
Maximise the sum of two Numbers using at most one swap between them | Python program to maximise the sum of two Numbers using at most one swap between them ; Function to maximize the sum by swapping only one digit ; Store digits of max ( n1 , n2 ) ; Store digits of min ( n1 , n2 ) ; If length of the two numbers are unequal ; Find the most significant number and the most significant index for swapping ; If both numbers are of equal length ; Fetch the index of current maximum digit present in both the arrays ; Compute the difference ; Find the most significant index and the most significant digit to be swapped ; Restore the new numbers ; Display the maximized sum ; Driver function
MAX = 100 NEW_LINE def findMaxSum ( n1 , n2 ) : NEW_LINE INDENT arr1 = [ 0 ] * ( MAX ) NEW_LINE arr2 = [ 0 ] * ( MAX ) NEW_LINE l1 = 0 NEW_LINE l2 = 0 NEW_LINE max1 = max ( n1 , n2 ) ; NEW_LINE min1 = min ( n1 , n2 ) ; NEW_LINE i = max1 NEW_LINE while i > 0 : NEW_LINE INDENT arr1 [ l1 ] = ( i % 10 ) NEW_LINE l1 += 1 NEW_LINE i //= 10 NEW_LINE DEDENT i = min1 NEW_LINE while i > 0 : NEW_LINE INDENT arr2 [ l2 ] = ( i % 10 ) NEW_LINE l2 += 1 NEW_LINE i //= 10 NEW_LINE DEDENT f = 0 NEW_LINE if ( l1 != l2 ) : NEW_LINE INDENT index = arr2 . index ( max ( arr2 ) ) NEW_LINE for i in range ( l1 - 1 , ( l2 - 1 ) , - 1 ) : NEW_LINE INDENT if ( arr1 [ i ] < arr2 [ index ] ) : NEW_LINE INDENT ( arr1 [ i ] , arr2 [ index ] ) = ( arr2 [ index ] , arr1 [ i ] ) NEW_LINE f = 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if ( f != 1 ) : NEW_LINE INDENT index1 = 0 NEW_LINE index2 = 0 NEW_LINE diff1 = 0 NEW_LINE diff2 = 0 NEW_LINE for i in range ( l2 - 1 , - 1 , - 1 ) : NEW_LINE INDENT index1 = arr1 . index ( max ( arr1 [ : i ] ) ) NEW_LINE index2 = arr2 . index ( max ( arr2 [ : i ] ) ) NEW_LINE diff1 = ( arr2 [ index2 ] - arr1 [ i ] ) ; NEW_LINE diff2 = ( arr1 [ index1 ] - arr2 [ i ] ) ; NEW_LINE if ( diff1 > 0 or diff2 > 0 ) : NEW_LINE INDENT if ( diff1 > diff2 ) : NEW_LINE INDENT arr1 [ i ] , arr2 [ index2 ] = arr2 [ index2 ] , arr1 [ i ] NEW_LINE break NEW_LINE DEDENT elif ( diff2 > diff1 ) : NEW_LINE INDENT arr2 [ i ] , arr1 [ index1 ] = arr1 [ index1 ] , arr2 [ i ] NEW_LINE break NEW_LINE DEDENT elif ( diff1 == diff2 ) : NEW_LINE INDENT if ( index1 <= index2 ) : NEW_LINE INDENT arr2 [ i ] , arr1 [ index1 ] = arr1 [ index1 ] , arr2 [ i ] NEW_LINE break NEW_LINE DEDENT elif ( index2 <= index1 ) : NEW_LINE INDENT arr1 [ i ] , arr2 [ index2 ] = arr2 [ index2 ] , arr1 [ i ] NEW_LINE break ; NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT f_n1 = 0 NEW_LINE f_n2 = 0 NEW_LINE for i in range ( l1 - 1 , - 1 , - 1 ) : NEW_LINE INDENT f_n1 = ( f_n1 * 10 ) + arr1 [ i ] NEW_LINE f_n2 = ( f_n2 * 10 ) + arr2 [ i ] NEW_LINE DEDENT print ( f_n1 + f_n2 ) NEW_LINE DEDENT N1 = 9987 NEW_LINE N2 = 123 NEW_LINE findMaxSum ( N1 , N2 ) NEW_LINE
Check if a number is divisible by 47 or not | Function to check if the number is divisible by 47 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Subtracting fourteen times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 43 or not ; Driver Code
def isDivisible ( n ) : NEW_LINE INDENT while n // 100 : NEW_LINE INDENT d = n % 10 NEW_LINE n //= 10 NEW_LINE n = abs ( n - ( d * 14 ) ) NEW_LINE DEDENT return ( n % 47 == 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 59173 NEW_LINE if ( isDivisible ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Check if a HexaDecimal number is Even or Odd | Check if the number is odd or even ; check if the last digit is either '0' , '2' , '4' , '6' , '8' , ' A ' ( = 10 ) , ' C ' ( = 12 ) or ' E ' ( = 14 ) ; Driver code
def even_or_odd ( N ) : NEW_LINE INDENT l = len ( N ) NEW_LINE if ( N [ l - 1 ] == '0' or N [ l - 1 ] == '2' or N [ l - 1 ] == '4' or N [ l - 1 ] == '6' or N [ l - 1 ] == '8' or N [ l - 1 ] == ' A ' or N [ l - 1 ] == ' C ' or N [ l - 1 ] == ' E ' ) : NEW_LINE INDENT return ( " Even " ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( " Odd " ) NEW_LINE DEDENT DEDENT N = " AB3454D " NEW_LINE print ( even_or_odd ( N ) ) NEW_LINE
Check if a number is divisible by 31 or not | Function to check if the number is divisible by 31 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Subtracting three times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 31 or not ; Driver Code
def isDivisible ( n ) : NEW_LINE INDENT while n // 100 : NEW_LINE INDENT d = n % 10 NEW_LINE n //= 10 NEW_LINE n = abs ( n - ( d * 3 ) ) NEW_LINE DEDENT return ( n % 31 == 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1922 NEW_LINE if ( isDivisible ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Check if the number is divisible 43 or not | Function to check if the number is divisible by 43 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Adding thirteen times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 43 or not ; Driver Code
def isDivisible ( n ) : NEW_LINE INDENT while n // 100 : NEW_LINE INDENT d = n % 10 NEW_LINE n //= 10 NEW_LINE n = abs ( n + ( d * 13 ) ) NEW_LINE DEDENT return ( n % 43 == 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2795 NEW_LINE if ( isDivisible ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Find the next Non | Python 3 implementation of the approach ; Function to check if a number is perfect square ; Function to check if a number is Fibinacci Number ; N is Fibinacci if either ( 5 * N * N + 4 ) , ( 5 * N * N - 4 ) or both is a perferct square ; Function to find the next Non - Fibinacci Number ; Case 1 If N <= 3 , then 4 will be next Non - Fibinacci Number ; Case 2 If N + 1 is Fibinacci , then N + 2 will be next Non - Fibinacci Number ; If N + 1 is Non - Fibinacci , then N + 2 will be next Non - Fibinacci Number ; Driver code
from math import sqrt NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT s = sqrt ( x ) NEW_LINE return ( s * s == x ) NEW_LINE DEDENT def isFibonacci ( N ) : NEW_LINE INDENT return isPerfectSquare ( 5 * N * N + 4 ) or isPerfectSquare ( 5 * N * N - 4 ) NEW_LINE DEDENT def nextNonFibonacci ( N ) : NEW_LINE INDENT if ( N <= 3 ) : NEW_LINE INDENT return 4 NEW_LINE DEDENT if ( isFibonacci ( N + 1 ) ) : NEW_LINE INDENT return N + 2 NEW_LINE DEDENT else : NEW_LINE INDENT return N NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE print ( nextNonFibonacci ( N ) ) NEW_LINE N = 4 NEW_LINE print ( nextNonFibonacci ( N ) ) NEW_LINE N = 7 NEW_LINE print ( nextNonFibonacci ( N ) ) NEW_LINE DEDENT
Represent K ^ N as the sum of exactly N numbers | Python 3 program to represent K ^ N as the sum of exactly N numbers ; Function to print N numbers whose sum is a power of K ; Printing K ^ 1 ; Loop to print the difference of powers from K ^ 2 ; Driver code
from math import pow NEW_LINE def printf ( n , k ) : NEW_LINE INDENT print ( int ( k ) , end = " ▁ " ) NEW_LINE for i in range ( 2 , n + 1 , 1 ) : NEW_LINE INDENT x = pow ( k , i ) - pow ( k , i - 1 ) NEW_LINE print ( int ( x ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE K = 4 NEW_LINE printf ( N , K ) NEW_LINE DEDENT
Check if the given number is divisible by 71 or not | Function to check if the number is divisible by 71 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Subtracting seven times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 71 or not ; Driver Code
def isDivisible ( n ) : NEW_LINE INDENT while n // 100 : NEW_LINE INDENT d = n % 10 NEW_LINE n //= 10 NEW_LINE n = abs ( n - ( d * 7 ) ) NEW_LINE DEDENT return ( n % 71 == 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5041 NEW_LINE if ( isDivisible ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Count of prime digits of a Number which divides the number | Function to find the number of digits in number which divides the number and is also a prime number ; Only 2 , 3 , 5 and 7 are prime one - digit number ; Loop to compute all the digits of the number untill it is not equal to the zero ; Fetching each digit of the number ; Checking if digit is greater than 0 and can divides n and is prime too ; Driver Code
def countDigit ( n ) : NEW_LINE INDENT prime = [ False ] * 10 NEW_LINE prime [ 2 ] = True NEW_LINE prime [ 3 ] = True ; NEW_LINE prime [ 5 ] = True NEW_LINE prime [ 7 ] = True ; NEW_LINE temp = n NEW_LINE count = 0 ; NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT d = temp % 10 ; NEW_LINE temp //= 10 ; NEW_LINE if ( d > 0 and n % d == 0 and prime [ d ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT n = 1032 NEW_LINE print ( countDigit ( n ) ) NEW_LINE
Bitwise Operations on Digits of a Number | Python 3 implementation of the approach ; Function to find the digits ; Function to Find OR of all digits of a number ; Find OR of all digits ; return OR of digits ; Function to Find AND of all digits of a number ; Find AND of all digits ; return AND of digits ; Function to Find XOR of all digits of a number ; Find XOR of all digits ; return XOR of digits ; Driver code ; Find and store all digits ; Find XOR of digits ; Find OR of digits ; Find AND of digits ; Driver code
digit = [ 0 ] * ( 100000 ) NEW_LINE def findDigits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT digit [ count ] = n % 10 ; NEW_LINE n = n // 10 ; NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def OR_of_Digits ( n , count ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( count ) : NEW_LINE INDENT ans = ans | digit [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT def AND_of_Digits ( n , count ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( count ) : NEW_LINE INDENT ans = ans & digit [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT def XOR_of_Digits ( n , count ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( count ) : NEW_LINE INDENT ans = ans ^ digit [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT def bitwise_operation ( N ) : NEW_LINE INDENT countOfDigit = findDigits ( N ) NEW_LINE print ( " XOR ▁ = ▁ " , XOR_of_Digits ( N , countOfDigit ) ) NEW_LINE print ( " OR ▁ = ▁ " , OR_of_Digits ( N , countOfDigit ) ) NEW_LINE print ( " AND ▁ = ▁ " , AND_of_Digits ( N , countOfDigit ) ) NEW_LINE DEDENT N = 123456 ; NEW_LINE bitwise_operation ( N ) NEW_LINE
Length of longest subarray with product greater than or equal to 0 | Function that count the Length of longest subarray with product greater than or equals to zero ; If product is greater than zero , return array size ; Traverse the array and if any negative element found then update the Length of longest subarray with the Length of left and right subarray ; Driver Code
def maxLength ( arr , N ) : NEW_LINE INDENT product = 1 NEW_LINE Len = 0 NEW_LINE for i in arr : NEW_LINE INDENT product *= i NEW_LINE DEDENT if ( product >= 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT Len = max ( Len , max ( N - i - 1 , i ) ) NEW_LINE DEDENT DEDENT return Len NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 1 , 1 , 1 , - 2 , 3 , 2 , - 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maxLength ( arr , N ) ) NEW_LINE DEDENT
Print a pair of numbers with the given Sum and Product | Python3 program to find any pair which has sum S and product P . ; Prints roots of quadratic equation ax * 2 + bx + c = 0 ; calculating the sq root value for b * b - 4 * a * c ; Finding the roots ; Check if the roots are valid or not ; Finding the roots ; Check if the roots are valid or not ; when d < 0 ; No such pair exists in this case ; Driver code
from math import sqrt NEW_LINE def findRoots ( b , c ) : NEW_LINE INDENT a = 1 NEW_LINE d = b * b - 4 * a * c NEW_LINE sqrt_val = sqrt ( abs ( d ) ) NEW_LINE if ( d > 0 ) : NEW_LINE INDENT x = - b + sqrt_val NEW_LINE y = - b - sqrt_val NEW_LINE root1 = ( x ) // ( 2 * a ) NEW_LINE root2 = ( y ) // ( 2 * a ) NEW_LINE if ( root1 + root2 == - 1 * b and root1 * root2 == c ) : NEW_LINE INDENT print ( int ( root1 ) , " , " , int ( root2 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT elif ( d == 0 ) : NEW_LINE INDENT root = - b // ( 2 * a ) NEW_LINE if ( root + root == - 1 * b and root * root == c ) : NEW_LINE INDENT print ( root , " , " , root ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = 5 NEW_LINE P = 6 NEW_LINE findRoots ( - S , P ) NEW_LINE S = 5 NEW_LINE P = 9 NEW_LINE findRoots ( - S , P ) NEW_LINE DEDENT
Print N numbers such that their product is a Perfect Cube | Function to find the N numbers such that their product is a perfect cube ; Loop to traverse each number from 1 to N ; Print the cube of i as the ith term of the output ; Driver Code ; Function Call
def findNumbers ( N ) : NEW_LINE INDENT i = 1 NEW_LINE while ( i <= N ) : NEW_LINE INDENT print ( ( i * i * i ) , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE findNumbers ( N ) NEW_LINE DEDENT
Sum of all proper divisors from 1 to N | Utility function to find sum of all proper divisor of number up to N ; Loop to iterate over all the numbers from 1 to N ; Find all divisors of i and add them ; Subtracting ' i ' so that the number itself is not included ; Driver Code
def properDivisorSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT if j * j > i : NEW_LINE INDENT break NEW_LINE DEDENT if ( i % j == 0 ) : NEW_LINE INDENT if ( i // j == j ) : NEW_LINE INDENT sum += j NEW_LINE DEDENT else : NEW_LINE INDENT sum += j + i // j NEW_LINE DEDENT DEDENT DEDENT sum = sum - i NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE print ( properDivisorSum ( n ) ) NEW_LINE n = 5 NEW_LINE print ( properDivisorSum ( n ) ) NEW_LINE DEDENT
Sum of all proper divisors from 1 to N | Utility function to find sum of all proper divisor of number up to N ; Loop to find the proper divisor of every number from 1 to N ; Driver Code
def properDivisorSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sum += ( n // i ) * i NEW_LINE DEDENT return sum - n * ( n + 1 ) // 2 NEW_LINE DEDENT n = 4 NEW_LINE print ( properDivisorSum ( n ) ) NEW_LINE n = 5 NEW_LINE print ( properDivisorSum ( n ) ) NEW_LINE
Minimum change in given value so that it lies in all given Ranges | Function to find the minimum difference in the number D such that D is inside of every range ; Loop to find the common range out of the given array of ranges . ; Storing the start and end index ; Sorting the range ; Finding the maximum starting value of common segment ; Finding the minimum ending value of common segment ; If there is no common segment ; If the given number is between the computed common range . ; Finding the minimum distance ; Driver Code ; Given array of ranges
def findMinimumOperation ( n , d , arrays ) : NEW_LINE INDENT cnt = 0 NEW_LINE first = - 10 ** 9 NEW_LINE end = 10 ** 9 NEW_LINE while ( n ) : NEW_LINE INDENT arr = [ arrays [ cnt ] [ 0 ] , arrays [ cnt ] [ 1 ] ] NEW_LINE arr = sorted ( arr ) NEW_LINE first = max ( first , arr [ 0 ] ) NEW_LINE end = min ( end , arr [ 1 ] ) NEW_LINE cnt += 1 NEW_LINE n -= 1 NEW_LINE DEDENT if ( first > end ) : NEW_LINE INDENT print ( " - 1" , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( d >= first and d <= end ) : NEW_LINE INDENT print ( "0" , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( min ( abs ( first - d ) , abs ( d - end ) ) , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE d = 3 NEW_LINE arrays = [ [ 0 , 7 ] , [ 2 , 14 ] , [ 4 , 6 ] ] NEW_LINE findMinimumOperation ( n , d , arrays ) NEW_LINE DEDENT
Number of factors of very large number N modulo M where M is any prime number | Python 3 implementation to find the Number of factors of very large number N modulo M ; Function for modular multiplication ; Function to find the number of factors of large Number N ; Count the number of times 2 divides the number N ; Condition to check if 2 divides it ; Check for all the possible numbers that can divide it ; Loop to check the number of times prime number i divides it ; Condition to check if prime number i divides it ; Condition to check if N at the end is a prime number . ; Driver Code
from math import sqrt NEW_LINE mod = 1000000007 NEW_LINE def mult ( a , b ) : NEW_LINE INDENT return ( ( a % mod ) * ( b % mod ) ) % mod NEW_LINE DEDENT def calculate_factors ( n ) : NEW_LINE INDENT cnt = 0 NEW_LINE ans = 1 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE n = n // 2 NEW_LINE DEDENT if ( cnt ) : NEW_LINE INDENT ans = mult ( ans , ( cnt + 1 ) ) NEW_LINE DEDENT for i in range ( 3 , int ( sqrt ( n ) ) , 2 ) : NEW_LINE INDENT cnt = 0 NEW_LINE while ( n % i == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE n = n // i NEW_LINE DEDENT if ( cnt ) : NEW_LINE INDENT ans = mult ( ans , ( cnt + 1 ) ) NEW_LINE DEDENT DEDENT if ( n > 2 ) : NEW_LINE INDENT ans = mult ( ans , 2 ) NEW_LINE DEDENT return ans % mod NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 19374857 NEW_LINE mod = 17 NEW_LINE print ( calculate_factors ( n ) ) NEW_LINE DEDENT
Maximum value of B less than A such that A ^ B = A + B | Function to find the maximum value of B such that A ^ B = A + B ; Binary Representation of A ; Loop to find the negation of the integer A ; output ; Driver Code ; Function Call
def maxValue ( a ) : NEW_LINE INDENT a = bin ( a ) [ 2 : ] NEW_LINE b = ' ' NEW_LINE for i in list ( a ) : NEW_LINE INDENT b += str ( int ( not int ( i ) ) ) NEW_LINE DEDENT print ( int ( b , 2 ) ) NEW_LINE return int ( b , 2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 4 NEW_LINE maxValue ( a ) NEW_LINE DEDENT
Print all possible pair with prime XOR in the Array | Python implementation of the above approach ; Function for Sieve of Eratosthenes ; If i is prime , then make all multiples of i false ; Function to prall Pairs whose XOR is prime ; if A [ i ] ^ A [ j ] is prime , then prthis pair ; Driver Code ; Generate all the prime number ; Function Call
sz = 10 ** 5 NEW_LINE isPrime = [ True ] * ( sz + 1 ) NEW_LINE def generatePrime ( ) : NEW_LINE INDENT i , j = 0 , 0 NEW_LINE isPrime [ 0 ] = isPrime [ 1 ] = False NEW_LINE for i in range ( 2 , sz + 1 ) : NEW_LINE INDENT if i * i > sz : NEW_LINE INDENT break NEW_LINE DEDENT if ( isPrime [ i ] ) : NEW_LINE INDENT for j in range ( i * i , sz , i ) : NEW_LINE INDENT isPrime [ j ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def Pair_of_PrimeXor ( A , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( isPrime [ ( A [ i ] ^ A [ j ] ) ] ) : NEW_LINE INDENT print ( " ( " , A [ i ] , " , " , A [ j ] , " ) " , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 3 , 6 , 11 ] NEW_LINE n = len ( A ) NEW_LINE generatePrime ( ) NEW_LINE Pair_of_PrimeXor ( A , n ) NEW_LINE DEDENT
Check if N can be expressed as product of 3 distinct numbers | function to find 3 distinct number with given product ; Declare a vector to store divisors ; store all divisors of number in array ; store all the occurence of divisors ; check if n is not equals to - 1 then n is also a prime factor ; Initialize the variables with 1 ; check for first number a ; check for second number b ; check for third number c ; check for all unwanted condition ; Driver function
def getnumbers ( n ) : NEW_LINE INDENT divisor = [ ] NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT while ( n % i == 0 ) : NEW_LINE INDENT divisor . append ( i ) NEW_LINE n //= i NEW_LINE DEDENT DEDENT if ( n != 1 ) : NEW_LINE INDENT divisor . append ( n ) NEW_LINE DEDENT a , b , c , size = 0 , 0 , 0 , 0 NEW_LINE a = b = c = 1 NEW_LINE size = len ( divisor ) NEW_LINE for i in range ( size ) : NEW_LINE INDENT if ( a == 1 ) : NEW_LINE INDENT a = a * divisor [ i ] NEW_LINE DEDENT elif ( b == 1 or b == a ) : NEW_LINE INDENT b = b * divisor [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT c = c * divisor [ i ] NEW_LINE DEDENT DEDENT if ( a == 1 or b == 1 or c == 1 or a == b or b == c or a == c ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( a , b , c ) NEW_LINE DEDENT DEDENT n = 64 NEW_LINE getnumbers ( n ) NEW_LINE