codeparrot/xlcost-text-to-code huggingface dataset:
Program to find sum of the given sequence | Python3 program to find the sum of the given sequence ; function to find moudulo inverse under 10 ^ 9 + 7 ; Function to find the sum of the given sequence ; Driver code
MOD = 1000000007 ; NEW_LINE def modInv ( x ) : NEW_LINE INDENT n = MOD - 2 ; NEW_LINE result = 1 ; NEW_LINE while ( n ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT result = result * x % MOD ; NEW_LINE DEDENT x = x * x % MOD ; NEW_LINE n = int ( n / 2 ) ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT def getSum ( n , k ) : NEW_LINE INDENT ans = 1 ; NEW_LINE for i in range ( n + 1 , n - k , - 1 ) : NEW_LINE INDENT ans = ans * i % MOD ; NEW_LINE DEDENT ans = ans * modInv ( k + 1 ) % MOD ; NEW_LINE return ans ; NEW_LINE DEDENT n = 3 ; NEW_LINE k = 2 ; NEW_LINE print ( getSum ( n , k ) ) ; NEW_LINE
Triplet with no element divisible by 3 and sum N | Function to print a , b and c ; check if n - 2 is divisible by 3 or not ; Driver code
def printCombination ( n ) : NEW_LINE INDENT print ( "1 ▁ " , end = " " ) ; NEW_LINE if ( ( n - 2 ) % 3 == 0 ) : NEW_LINE INDENT print ( "2" , n - 3 , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( "1" , ( n - 2 ) , end = " " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 233 ; NEW_LINE printCombination ( n ) ; NEW_LINE DEDENT
Pairs with GCD equal to one in the given range | Function to print all pairs ; check if even ; we can print all adjacent pairs for i in range ( l , r , 2 ) : print ( " { " , i , " , " , i + 1 , " } , " ) ; Driver Code
def checkPairs ( l , r ) : NEW_LINE INDENT if ( l - r ) % 2 == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l , r = 1 , 8 NEW_LINE if checkPairs ( l , r ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Check if a number with even number of digits is palindrome or not | Function to check if the number is palindrome ; if divisible by 11 then return True ; if not divisible by 11 then return False ; Driver code
def isPalindrome ( n ) : NEW_LINE INDENT if n % 11 == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 123321 NEW_LINE if isPalindrome ( n ) : NEW_LINE INDENT print ( " Palindrome " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Palindrome " ) NEW_LINE DEDENT DEDENT
Count number of triplets with product equal to given number with duplicates allowed | The target value for which we have to find the solution ; This variable contains the total count of triplets found ; Loop from the first to the third last integer in the list ; Check if arr [ i ] is a factor of target or not . If not , skip to the next element ; Check if the pair ( arr [ i ] , arr [ j ] ) can be a part of triplet whose product is equal to the target ; Find the remaining element of the triplet ; If element is found . increment the total count of the triplets
target = 93 NEW_LINE arr = [ 1 , 31 , 3 , 1 , 93 , 3 , 31 , 1 , 93 ] NEW_LINE length = len ( arr ) NEW_LINE totalCount = 0 NEW_LINE for i in range ( length - 2 ) : NEW_LINE INDENT if target % arr [ i ] == 0 : NEW_LINE INDENT for j in range ( i + 1 , length - 1 ) : NEW_LINE INDENT if target % ( arr [ i ] * arr [ j ] ) == 0 : NEW_LINE INDENT toFind = target // ( arr [ i ] * arr [ j ] ) NEW_LINE for k in range ( j + 1 , length ) : NEW_LINE INDENT if arr [ k ] == toFind : NEW_LINE INDENT totalCount += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT DEDENT print ( ' Total ▁ number ▁ of ▁ triplets ▁ found : ▁ ' , totalCount ) NEW_LINE
Number of Permutations such that no Three Terms forms Increasing Subsequence | Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver code
def binomialCoeff ( n , k ) : NEW_LINE INDENT res = 1 NEW_LINE if k > n - k : NEW_LINE INDENT k = n - k NEW_LINE DEDENT for i in range ( k ) : NEW_LINE INDENT res = res * ( n - i ) NEW_LINE res = res // ( i + 1 ) NEW_LINE DEDENT return res NEW_LINE DEDENT def catalan ( n ) : NEW_LINE INDENT c = binomialCoeff ( 2 * n , n ) NEW_LINE return c // ( n + 1 ) NEW_LINE DEDENT n = 3 NEW_LINE print ( catalan ( n ) ) NEW_LINE
Fascinating Number | function to check if number is fascinating or not ; frequency count array using 1 indexing ; obtaining the resultant number using string concatenation ; Traversing the string character by character ; gives integer value of a character digit ; To check if any digit has appeared multiple times ; Traversing through freq array to check if any digit was missing ; Driver Code ; Input number ; Not a valid number ; Calling the function to check if input number is fascinating or not
def isFascinating ( num ) : NEW_LINE INDENT freq = [ 0 ] * 10 NEW_LINE val = ( str ( num ) + str ( num * 2 ) + str ( num * 3 ) ) NEW_LINE for i in range ( len ( val ) ) : NEW_LINE INDENT digit = int ( val [ i ] ) NEW_LINE if freq [ digit ] and digit != 0 > 0 : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT freq [ digit ] += 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , 10 ) : NEW_LINE INDENT if freq [ i ] == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT num = 192 NEW_LINE if num < 100 : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = isFascinating ( num ) NEW_LINE if ans : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT DEDENT
Count ways to distribute m items among n people | function used to generate binomial coefficient time complexity O ( m ) ; helper function for generating no of ways to distribute m mangoes amongst n people ; not enough mangoes to be distributed ; ways -> ( n + m - 1 ) C ( n - 1 ) ; Driver function ; m represents number of mangoes n represents number of people
def binomial_coefficient ( n , m ) : NEW_LINE INDENT res = 1 NEW_LINE if m > n - m : NEW_LINE INDENT m = n - m NEW_LINE DEDENT for i in range ( 0 , m ) : NEW_LINE INDENT res *= ( n - i ) NEW_LINE res /= ( i + 1 ) NEW_LINE DEDENT return res NEW_LINE DEDENT def calculate_ways ( m , n ) : NEW_LINE INDENT if m < n : NEW_LINE INDENT return 0 NEW_LINE DEDENT ways = binomial_coefficient ( n + m - 1 , n - 1 ) NEW_LINE return int ( ways ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT m = 7 ; n = 5 NEW_LINE result = calculate_ways ( m , n ) NEW_LINE print ( result ) NEW_LINE DEDENT
Queries to count the number of unordered co | Python3 program to find number of unordered coprime pairs of integers from 1 to N ; to store euler 's totient function ; to store required answer ; Computes and prints totient of all numbers smaller than or equal to N . ; Initialise the phi [ ] with 1 ; Compute other Phi values ; If phi [ p ] is not computed already , then number p is prime ; Phi of a prime number p is always equal to p - 1. ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; function to compute number coprime pairs ; function call to compute euler totient function ; prefix sum of all euler totient function values ; Driver code ; function call
N = 100005 NEW_LINE phi = [ 0 ] * N NEW_LINE S = [ 0 ] * N NEW_LINE def computeTotient ( ) : NEW_LINE INDENT for i in range ( 1 , N ) : NEW_LINE INDENT phi [ i ] = i NEW_LINE DEDENT for p in range ( 2 , N ) : NEW_LINE INDENT if ( phi [ p ] == p ) : NEW_LINE INDENT phi [ p ] = p - 1 NEW_LINE for i in range ( 2 * p , N , p ) : NEW_LINE INDENT phi [ i ] = ( phi [ i ] // p ) * ( p - 1 ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def CoPrimes ( ) : NEW_LINE INDENT computeTotient ( ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT S [ i ] = S [ i - 1 ] + phi [ i ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT CoPrimes ( ) NEW_LINE q = [ 3 , 4 ] NEW_LINE n = len ( q ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( " Number of unordered coprime " ▁ + ▁ " pairs of integers from 1 to " , ▁ q [ i ] , ▁ " are   " , S [ q [ i ] ] ) NEW_LINE DEDENT DEDENT
N | Function to return the decimal value of a binary number ; Initializing base value to 1 , i . e 2 ^ 0 ; find the binary representation of the N - th number in sequence ; base case ; answer string ; add n - 1 1 's ; add 0 ; add n 1 's at end ; Driver Code
def binaryToDecimal ( n ) : NEW_LINE INDENT num = n NEW_LINE dec_value = 0 NEW_LINE base = 1 NEW_LINE l = len ( num ) NEW_LINE for i in range ( l - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( num [ i ] == '1' ) : NEW_LINE INDENT dec_value += base NEW_LINE DEDENT base = base * 2 NEW_LINE DEDENT return dec_value NEW_LINE DEDENT def numberSequence ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT s = " " NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT s += '1' NEW_LINE DEDENT s += '0' NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT s += '1' NEW_LINE DEDENT num = binaryToDecimal ( s ) NEW_LINE return num NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE print ( numberSequence ( n ) ) NEW_LINE DEDENT
N | calculate Nth term of series ; calculates the N - th term ; Driver Code
def numberSequence ( n ) : NEW_LINE INDENT num = pow ( 4 , n ) - pow ( 2 , n ) - 1 NEW_LINE return num NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE print ( numberSequence ( n ) ) NEW_LINE DEDENT
Alternate Primes till N | Function for checking number is prime or not ; if flag = 0 then number is prime and return 1 otherwise return 0 ; Function for printing alternate prime number ; counter is initialize with 0 ; looping through 2 to n - 1 ; function calling along with if condition ; if counter is multiple of 2 then only print prime number ; Driver code ; Function calling
def prime ( num ) : NEW_LINE INDENT flag = 0 NEW_LINE for i in range ( 2 , num // 2 + 1 ) : NEW_LINE INDENT if num % i == 0 : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if flag == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def print_alternate_prime ( n ) : NEW_LINE INDENT counter = 0 NEW_LINE for num in range ( 2 , n ) : NEW_LINE INDENT if prime ( num ) == 1 : NEW_LINE INDENT if counter % 2 == 0 : NEW_LINE INDENT print ( num , end = " ▁ " ) NEW_LINE DEDENT counter += 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 15 NEW_LINE print ( " Following ▁ are ▁ the ▁ alternate ▁ prime " + " number ▁ smaller ▁ than ▁ or ▁ equal ▁ to " , n ) NEW_LINE print_alternate_prime ( n ) NEW_LINE DEDENT
Alternate Primes till N | Python 3 program to print all equal to n using Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers ; for next prime to get printed ; Driver Code
def SieveOfEratosthenes ( n ) : NEW_LINE INDENT prime = [ None ] * ( n + 1 ) NEW_LINE for i in range ( len ( prime ) ) : NEW_LINE INDENT prime [ i ] = True NEW_LINE DEDENT p = 2 NEW_LINE while p * p <= n : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT flag = True NEW_LINE for p in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT if ( flag ) : NEW_LINE INDENT print ( str ( p ) , end = " ▁ " ) NEW_LINE flag = False NEW_LINE DEDENT else : NEW_LINE INDENT flag = True NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 15 NEW_LINE print ( " Following ▁ are ▁ the ▁ alternate " + " ▁ prime ▁ numbers ▁ smaller ▁ " + " than ▁ or ▁ equal ▁ to ▁ " + str ( n ) ) NEW_LINE SieveOfEratosthenes ( n ) NEW_LINE DEDENT
Find maximum among x ^ ( y ^ 2 ) or y ^ ( x ^ 2 ) where x and y are given | Function to find maximum ; Case 1 ; Case 2 ; Driver Code
def findGreater ( x , y ) : NEW_LINE INDENT if ( x > y ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT else : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT x = 4 ; NEW_LINE y = 9 ; NEW_LINE if ( findGreater ( x , y ) ) : NEW_LINE INDENT print ( "1" ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( "2" ) ; NEW_LINE DEDENT
Maximum profit after buying and selling stocks with transaction fees | Python3 implementation of above approach ; b [ 0 ] will contain the maximum profit ; b [ 1 ] will contain the day on which we are getting the maximum profit ; here finding the max profit ; if we get less then or equal to zero it means we are not getting the profit ; check if Sum is greater then maximum then store the new maximum ; Driver code
def max_profit ( a , b , n , fee ) : NEW_LINE INDENT i , j , profit = 1 , n - 1 , 0 NEW_LINE l , r , diff_day = 0 , 0 , 1 NEW_LINE b [ 0 ] = 0 NEW_LINE b [ 1 ] = diff_day NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT l = 0 NEW_LINE r = diff_day NEW_LINE Sum = 0 NEW_LINE for j in range ( n - 1 , i - 1 , - 1 ) : NEW_LINE INDENT profit = ( a [ r ] - a [ l ] ) - fee NEW_LINE if ( profit > 0 ) : NEW_LINE INDENT Sum = Sum + profit NEW_LINE DEDENT l += 1 NEW_LINE r += 1 NEW_LINE DEDENT if ( b [ 0 ] < Sum ) : NEW_LINE INDENT b [ 0 ] = Sum NEW_LINE b [ 1 ] = diff_day NEW_LINE DEDENT DEDENT diff_day += 1 NEW_LINE return 0 NEW_LINE DEDENT arr = [ 6 , 1 , 7 , 2 , 8 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE b = [ 0 for i in range ( 2 ) ] NEW_LINE tranFee = 2 NEW_LINE max_profit ( arr , b , n , tranFee ) NEW_LINE print ( b [ 0 ] , " , " , b [ 1 ] ) NEW_LINE
Eggs dropping puzzle ( Binomial Coefficient and Binary Search Solution ) | Find sum of binomial coefficients xCi ( where i varies from 1 to n ) . If the sum becomes more than K ; Do binary search to find minimum number of trials in worst case . ; Initialize low and high as 1 st and last floors ; Do binary search , for every mid , find sum of binomial coefficients and check if the sum is greater than k or not . ; Driver Code
def binomialCoeff ( x , n , k ) : NEW_LINE INDENT sum = 0 ; NEW_LINE term = 1 ; NEW_LINE i = 1 ; NEW_LINE while ( i <= n and sum < k ) : NEW_LINE INDENT term *= x - i + 1 ; NEW_LINE term /= i ; NEW_LINE sum += term ; NEW_LINE i += 1 ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def minTrials ( n , k ) : NEW_LINE INDENT low = 1 ; NEW_LINE high = k ; NEW_LINE while ( low < high ) : NEW_LINE INDENT mid = int ( ( low + high ) / 2 ) ; NEW_LINE if ( binomialCoeff ( mid , n , k ) < k ) : NEW_LINE INDENT low = mid + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT high = mid ; NEW_LINE DEDENT DEDENT return int ( low ) ; NEW_LINE DEDENT print ( minTrials ( 2 , 10 ) ) ; NEW_LINE
Find next palindrome prime | Python3 program to find next palindromic prime for a given number . ; if ( 8 <= N <= 11 ) return 11 ; generate odd length palindrome number which will cover given constraint . ; if y >= N and it is a prime number then return it . ; Driver code
import math as mt NEW_LINE def isPrime ( num ) : NEW_LINE INDENT if ( num < 2 or num % 2 == 0 ) : NEW_LINE INDENT return num == 2 NEW_LINE DEDENT for i in range ( 3 , mt . ceil ( mt . sqrt ( num + 1 ) ) ) : NEW_LINE INDENT if ( num % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def primePalindrome ( N ) : NEW_LINE INDENT if ( 8 <= N and N <= 11 ) : NEW_LINE INDENT return 11 NEW_LINE DEDENT for x in range ( 1 , 100000 ) : NEW_LINE INDENT s = str ( x ) NEW_LINE d = s [ : : - 1 ] NEW_LINE y = int ( s + d [ 1 : ] ) NEW_LINE if ( y >= N and isPrime ( y ) ) : NEW_LINE INDENT return y NEW_LINE DEDENT DEDENT DEDENT print ( primePalindrome ( 112 ) ) NEW_LINE
Number of integral solutions for equation x = b * ( sumofdigits ( x ) ^ a ) + c | This function returns the sum of the digits of a number ; This function creates the array of valid numbers ; this computes s ( x ) ^ a ; this gives the result of equation ; checking if the sum same as i ; counter to keep track of numbers ; resultant array ; prints the number ; Driver Code ; calculate which value of x are possible
def getsum ( a ) : NEW_LINE INDENT r = 0 NEW_LINE sum = 0 NEW_LINE while ( a > 0 ) : NEW_LINE INDENT r = a % 10 NEW_LINE sum = sum + r NEW_LINE a = a // 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def value ( a , b , c ) : NEW_LINE INDENT x = 0 NEW_LINE q = 0 NEW_LINE w = 0 NEW_LINE v = [ ] NEW_LINE for i in range ( 1 , 82 ) : NEW_LINE INDENT no = pow ( i , a ) NEW_LINE no = b * no + c NEW_LINE if ( no > 0 and no < 1000000000 ) : NEW_LINE INDENT x = getsum ( no ) NEW_LINE if ( x == i ) : NEW_LINE INDENT q += 1 NEW_LINE v . append ( no ) NEW_LINE w += 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( len ( v ) ) : NEW_LINE INDENT print ( v [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 2 NEW_LINE b = 2 NEW_LINE c = - 1 NEW_LINE value ( a , b , c ) NEW_LINE DEDENT
Cunningham chain | Function to print Cunningham chain of the second kind ; Iterate till all elements are printed ; check prime or not ; Driver Code
def print_t ( p0 ) : NEW_LINE INDENT i = 0 ; NEW_LINE while ( True ) : NEW_LINE INDENT flag = 1 ; NEW_LINE x = pow ( 2 , i ) ; NEW_LINE p1 = x * p0 - ( x - 1 ) ; NEW_LINE for k in range ( 2 , p1 ) : NEW_LINE INDENT if ( p1 % k == 0 ) : NEW_LINE INDENT flag = 0 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT print ( p1 , end = " ▁ " ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT DEDENT p0 = 19 ; NEW_LINE print_t ( p0 ) ; NEW_LINE
Count number of right triangles possible with a given perimeter | Function to return the count ; making a list to store ( a , b ) pairs ; no triangle if p is odd ; make ( a , b ) pair in sorted order ; check to avoid duplicates ; store the new pair ; Driver Code
def countTriangles ( p ) : NEW_LINE INDENT store = [ ] NEW_LINE if p % 2 != 0 : return 0 NEW_LINE else : NEW_LINE INDENT count = 0 NEW_LINE for b in range ( 1 , p // 2 ) : NEW_LINE INDENT a = p / 2 * ( ( p - 2 * b ) / ( p - b ) ) NEW_LINE inta = int ( a ) NEW_LINE if ( a == inta ) : NEW_LINE INDENT ab = tuple ( sorted ( ( inta , b ) ) ) NEW_LINE if ab not in store : NEW_LINE INDENT count += 1 NEW_LINE store . append ( ab ) NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT DEDENT p = 840 NEW_LINE print ( " number ▁ of ▁ right ▁ triangles ▁ = ▁ " + str ( countTriangles ( p ) ) ) NEW_LINE
Count pairs with Bitwise AND as ODD number | Python program to count pairs with Odd AND ; Count total odd numbers ; return count of even pair ; Driver Code ; calling function findOddPair and print number of odd pair
def findOddPair ( A , N ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( 0 , N - 1 ) : NEW_LINE INDENT if ( ( A [ i ] % 2 == 1 ) ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT return count * ( count - 1 ) / 2 NEW_LINE DEDENT a = [ 5 , 1 , 3 , 2 ] NEW_LINE n = len ( a ) NEW_LINE print ( int ( findOddPair ( a , n ) ) ) NEW_LINE
Surd number | define a isSurd function which Returns true if x is Surd number . ; Try all powers of i ; Driver code
def isSurd ( n ) : NEW_LINE INDENT i = 2 NEW_LINE for i in range ( 2 , ( i * i ) + 1 ) : NEW_LINE INDENT j = i NEW_LINE while ( j < n ) : NEW_LINE INDENT j = j * i NEW_LINE DEDENT if ( j == n ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 15 NEW_LINE if ( isSurd ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Program to find last two digits of 2 ^ n | Find the first digit ; Get the last digit from the number ; Remove last digit from number ; Get the last digit from the number ( last second of num ) ; Take last digit to ten 's position  i.e. last second digit ; Add the value of ones and tens to make it complete 2 digit number ; return the first digit ; Driver Code ; pow function used
def LastTwoDigit ( num ) : NEW_LINE INDENT one = num % 10 NEW_LINE num //= 10 NEW_LINE tens = num % 10 NEW_LINE tens *= 10 NEW_LINE num = tens + one NEW_LINE return num NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE num = 1 NEW_LINE num = pow ( 2 , n ) ; NEW_LINE print ( " Last ▁ " + str ( 2 ) + " ▁ digits ▁ of ▁ " + str ( 2 ) + " ^ " + str ( n ) + " ▁ = ▁ " , end = " " ) NEW_LINE print ( LastTwoDigit ( num ) ) NEW_LINE DEDENT
Program to find last two digits of 2 ^ n | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; 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 ; function to calculate number of digits in x ; function to print last 2 digits of 2 ^ n ; Generating 10 ^ 2 ; Calling modular exponentiation ; Printing leftmost zeros . Since ( 2 ^ n ) % 2 can have digits less then 2. In that case we need to print zeros ; If temp is not zero then print temp If temp is zero then already printed ; Driver Code
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 numberOfDigits ( x ) : NEW_LINE INDENT i = 0 NEW_LINE while ( x ) : NEW_LINE INDENT x //= 10 NEW_LINE i += 1 NEW_LINE DEDENT return i NEW_LINE DEDENT def LastTwoDigit ( n ) : NEW_LINE INDENT print ( " Last ▁ " + str ( 2 ) + " ▁ digits ▁ of ▁ " + str ( 2 ) , end = " " ) NEW_LINE print ( " ^ " + str ( n ) + " ▁ = ▁ " , end = " " ) NEW_LINE temp = 1 NEW_LINE for i in range ( 1 , 3 ) : NEW_LINE INDENT temp *= 10 NEW_LINE DEDENT temp = power ( 2 , n , temp ) NEW_LINE for i in range ( 2 - numberOfDigits ( temp ) ) : NEW_LINE INDENT print ( 0 , end = " " ) NEW_LINE DEDENT if temp : NEW_LINE INDENT print ( temp ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 72 NEW_LINE LastTwoDigit ( n ) NEW_LINE DEDENT
Find gcd ( a ^ n , c ) where a , n and c can vary from 1 to 10 ^ 9 | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; 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 ; Finds GCD of a and b ; Finds GCD of a ^ n and c ; check if c is a divisor of a ; First compute ( a ^ n ) % c ; Now simply return GCD of modulo power and c . ; Driver code
def modPower ( 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 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 gcdPow ( a , n , c ) : NEW_LINE INDENT if ( a % c == 0 ) : NEW_LINE INDENT return c NEW_LINE DEDENT modexpo = modPower ( a , n , c ) NEW_LINE return gcd ( modexpo , c ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 10248585 NEW_LINE n = 1000000 NEW_LINE c = 12564 NEW_LINE print ( gcdPow ( a , n , c ) ) NEW_LINE DEDENT
Number of sub arrays with odd sum | Python 3 code to find count of sub - arrays with odd sum ; Find sum of all subarrays and increment result if sum is odd ; Driver code
def countOddSum ( ar , n ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT val = 0 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT val = val + ar [ j ] NEW_LINE if ( val % 2 != 0 ) : NEW_LINE INDENT result += 1 NEW_LINE DEDENT DEDENT DEDENT return ( result ) NEW_LINE DEDENT ar = [ 5 , 4 , 4 , 5 , 1 , 3 ] NEW_LINE print ( " The ▁ Number ▁ of ▁ Subarrays " , " with ▁ odd " , end = " " ) NEW_LINE print ( " ▁ sum ▁ is ▁ " + str ( countOddSum ( ar , 6 ) ) ) NEW_LINE