codeparrot/xlcost-text-to-code huggingface dataset:
Sum of N terms in the expansion of Arcsin ( x ) | Function to find the arcsin ( x ) ; The power to which ' x ' is raised ; Numerator value ; Denominator value ; Driver code
def find_Solution ( x , n ) : NEW_LINE INDENT Sum = x NEW_LINE e = 2 NEW_LINE o = 1 NEW_LINE p = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT p += 2 NEW_LINE Sum += ( o / e ) * ( pow ( x , p ) / p ) NEW_LINE o = o * ( o + 2 ) NEW_LINE e = e * ( e + 2 ) NEW_LINE DEDENT print ( round ( Sum , 10 ) ) NEW_LINE DEDENT x = - 0.5 NEW_LINE if ( abs ( x ) >= 1 ) : NEW_LINE INDENT print ( " Invalid Input " ) NEW_LINE DEDENT n = 8 NEW_LINE find_Solution ( x , n ) NEW_LINE
Minimize the cost of buying the Objects | Function that will calculate the price ; Calculate the number of items we can get for free ; Calculate the number of items we will have to pay the price for ; Calculate the price ; Driver code ; Calling function
def totalPay ( totalItems , priceOfOneItem , N , M ) : NEW_LINE INDENT freeItems = 0 NEW_LINE actual = 0 NEW_LINE freeItems = totalItems // ( N + M ) NEW_LINE actual = totalItems - freeItems NEW_LINE amount = actual * priceOfOneItem NEW_LINE return amount NEW_LINE DEDENT T = 12 NEW_LINE P = 8 NEW_LINE N = 2 NEW_LINE M = 1 NEW_LINE print ( " Amount ▁ = ▁ " , totalPay ( T , P , N , M ) ) NEW_LINE
Count of all possible pairs of disjoint subsets of integers from 1 to N | Python3 implementation of the approach ; Modulo exponentiation function ; Function to calculate ( x ^ y ) % p in O ( log ( y ) ) ; Driver Code ; Evaluating ( ( 3 ^ n - 2 ^ ( n + 1 ) + 1 ) / 2 ) % p ; From Fermatss little theorem a ^ - 1 ? a ^ ( m - 2 ) ( mod m )
p = 1000000007 NEW_LINE def power ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p ; NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % p NEW_LINE DEDENT return res % p NEW_LINE DEDENT n = 3 NEW_LINE x = ( power ( 3 , n ) % p + 1 ) % p NEW_LINE x = ( x - power ( 2 , n + 1 ) + p ) % p NEW_LINE x = ( x * power ( 2 , p - 2 ) ) % p NEW_LINE print ( x ) NEW_LINE
Find the remainder when First digit of a number is divided by its Last digit | Function to find the remainder ; Get the last digit ; Get the first digit ; Compute the remainder ; Driver code
def findRemainder ( n ) : NEW_LINE INDENT l = n % 10 NEW_LINE while ( n >= 10 ) : NEW_LINE INDENT n //= 10 NEW_LINE DEDENT f = n NEW_LINE remainder = f % l NEW_LINE print ( remainder ) NEW_LINE DEDENT n = 5223 NEW_LINE findRemainder ( n ) NEW_LINE
Percentage increase in the volume of cuboid if length , breadth and height are increased by fixed percentages | Function to return the percentage increase in the volume of the cuboid ; Driver code
def increaseInVol ( l , b , h ) : NEW_LINE INDENT percentInc = ( ( 1 + ( l / 100 ) ) * ( 1 + ( b / 100 ) ) * ( 1 + ( h / 100 ) ) ) NEW_LINE percentInc -= 1 NEW_LINE percentInc *= 100 NEW_LINE return percentInc NEW_LINE DEDENT l = 50 NEW_LINE b = 20 NEW_LINE h = 10 NEW_LINE print ( increaseInVol ( l , b , h ) , " % " ) NEW_LINE
Count the number of occurrences of a particular digit in a number | Function to count the occurrences of the digit D in N ; Loop to find the digits of N ; check if the digit is D ; return the count of the occurrences of D in N ; Driver code
def countOccurrances ( n , d ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT if ( n % 10 == d ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT n = n // 10 NEW_LINE DEDENT return count NEW_LINE DEDENT d = 2 NEW_LINE n = 214215421 NEW_LINE print ( countOccurrances ( n , d ) ) NEW_LINE
Find number of factors of N when location of its two factors whose product is N is given | Function to find the number of factors ; print the number of factors ; Driver code ; initialize the factors position
def findFactors ( a , b ) : NEW_LINE INDENT c = a + b - 1 NEW_LINE print ( c ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 13 NEW_LINE b = 36 NEW_LINE findFactors ( a , b ) NEW_LINE DEDENT
21 Matchsticks Problem | Function to return the optimal strategy ; Removing matchsticks in blocks of five ; Driver code
def TwentyoneMatchstick ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( 5 - arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 3 , 4 , 2 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE TwentyoneMatchstick ( arr , N ) NEW_LINE
Find the smallest positive number which can not be represented by given digits | Function to find the smallest positive number which can not be represented by given digits ; Storing the count of 0 digit or store the value at 0 th index ; Calculates the min value in the array starting from 1 st index and also store its index . ; If its value at 0 th index is less than min value than either 10 , 100 , 1000 ... can 't be expressed ; If its value is greater than min value then iterate the loop upto first min value index and simply print its index value . ; Driver code ; Value of N is always 10 as we take digit from 0 to 9 ; Calling function
def expressDigit ( arr , n ) : NEW_LINE INDENT min = 9 NEW_LINE index = 0 NEW_LINE temp = 0 NEW_LINE temp = arr [ 0 ] NEW_LINE for i in range ( 1 , 10 ) : NEW_LINE INDENT if ( arr [ i ] < min ) : NEW_LINE INDENT min = arr [ i ] NEW_LINE index = i NEW_LINE DEDENT DEDENT if ( temp < min ) : NEW_LINE INDENT print ( 1 , end = " " ) NEW_LINE for i in range ( 1 , temp + 1 ) : NEW_LINE INDENT print ( 0 , end = " " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for i in range ( min ) : NEW_LINE INDENT print ( index , end = " " ) NEW_LINE DEDENT print ( index ) NEW_LINE DEDENT DEDENT arr = [ 2 , 2 , 1 , 2 , 1 , 1 , 3 , 1 , 1 , 1 ] NEW_LINE N = 10 NEW_LINE expressDigit ( arr , N ) NEW_LINE
Find the average of k digits from the beginning and l digits from the end of the given number | implementation of the approach ; Function to return the count of digits in num ; Function to return the sum of first n digits of num ; Remove the unnecessary digits ; Function to return the sum of the last n digits of num ; If the average can 't be calculated without  using the same digit more than once ; Sum of the last l digits of n ; Sum of the first k digits of n ( totalDigits - k ) must be removed from the end of the number to get the remaining k digits from the beginning ; Return the average ; Driver code
from math import pow NEW_LINE def countDigits ( num ) : NEW_LINE INDENT cnt = 0 NEW_LINE while ( num > 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE num //= 10 NEW_LINE DEDENT return cnt NEW_LINE DEDENT def sumFromStart ( num , n , rem ) : NEW_LINE INDENT num //= pow ( 10 , rem ) NEW_LINE sum = 0 NEW_LINE while ( num > 0 ) : NEW_LINE INDENT sum += ( num % 10 ) NEW_LINE num //= 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def sumFromEnd ( num , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += ( num % 10 ) NEW_LINE num //= 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def getAverage ( n , k , l ) : NEW_LINE INDENT totalDigits = countDigits ( n ) NEW_LINE if ( totalDigits < ( k + l ) ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT sum1 = sumFromEnd ( n , l ) NEW_LINE sum2 = sumFromStart ( n , k , totalDigits - k ) NEW_LINE return ( sum1 + sum2 ) / ( k + l ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 123456 NEW_LINE k = 2 NEW_LINE l = 3 NEW_LINE print ( getAverage ( n , k , l ) ) NEW_LINE DEDENT
Print N terms of Withoff Sequence | Python3 program to find Wythoff array ; Function to find the n , k term of Wythoff array ; tau = ( sqrt ( 5 ) + 1 ) / 2 ; Already_stored ; T ( n , - 1 ) = n - 1. ; T ( n , 0 ) = floor ( n * tau ) . ; T ( n , k ) = T ( n , k - 1 ) + T ( n , k - 2 ) for k >= 1. ; Store ; Return the ans ; Function to find first n terms of Wythoff array by traversing in anti - diagonal ; Map to store the Wythoff array ; Anti diagonal ; Driver code ; Function call
import math NEW_LINE def Wythoff ( mp , n , k ) : NEW_LINE INDENT tau = ( math . sqrt ( 5 ) + 1 ) / 2 NEW_LINE t_n_k = 0 NEW_LINE if ( ( n in mp ) and ( k in mp [ n ] ) ) : NEW_LINE INDENT return mp [ n ] [ k ] ; NEW_LINE DEDENT if ( k == - 1 ) : NEW_LINE INDENT return n - 1 ; NEW_LINE DEDENT elif ( k == 0 ) : NEW_LINE INDENT t_n_k = math . floor ( n * tau ) ; NEW_LINE DEDENT else : NEW_LINE INDENT t_n_k = Wythoff ( mp , n , k - 1 ) + Wythoff ( mp , n , k - 2 ) NEW_LINE DEDENT if n not in mp : NEW_LINE INDENT mp [ n ] = dict ( ) NEW_LINE DEDENT mp [ n ] [ k ] = t_n_k ; NEW_LINE return int ( t_n_k ) NEW_LINE DEDENT def Wythoff_Array ( n ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE count = 0 ; NEW_LINE mp = dict ( ) NEW_LINE while ( count < n ) : NEW_LINE INDENT print ( Wythoff ( mp , i + 1 , j + 1 ) , end = ' ' ) NEW_LINE count += 1 NEW_LINE if ( count != n ) : NEW_LINE INDENT print ( " , ▁ " , end = ' ' ) NEW_LINE DEDENT i += 1 NEW_LINE j -= 1 NEW_LINE if ( j < 0 ) : NEW_LINE INDENT j = i ; NEW_LINE i = 0 ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 15 ; NEW_LINE Wythoff_Array ( n ) ; NEW_LINE DEDENT
Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given | Function to return the sum of numbers which are in the ration a : b and after adding x to both the numbers the new ratio becomes c : d ; Driver code
def sum ( a , b , c , d , x ) : NEW_LINE INDENT ans = ( ( x * ( a + b ) * ( c - d ) ) / ( ( a * d ) - ( b * c ) ) ) ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a , b , c , d , x = 1 , 2 , 9 , 13 , 5 ; NEW_LINE print ( sum ( a , b , c , d , x ) ) ; NEW_LINE DEDENT
Triangle of numbers arising from Gilbreath 's conjecture | Python3 code for printing the Triangle of numbers arising from Gilbreath 's conjecture ; Check whether the number is prime or not ; Set the 0 th row of the matrix with c primes from 0 , 0 to 0 , c - 1 ; Find the n , k term of matrix of Gilbreath 's conjecture ; recursively find ; store the ans ; Print first n terms of Gilbreath sequence successive absolute differences of primes read by antidiagonals upwards . ; map to store the matrix and hash to check if the element is present or not ; set the primes of first row ; print the Gilbreath number ; increase the count ; anti diagonal upwards ; Driver code
import math NEW_LINE def is_Prime ( n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT for i in range ( 2 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def set_primes ( mp , hash , c ) : NEW_LINE INDENT count = 0 ; NEW_LINE i = 2 NEW_LINE while ( count < c ) : NEW_LINE INDENT if ( is_Prime ( i ) ) : NEW_LINE INDENT if 0 not in mp : NEW_LINE INDENT mp [ 0 ] = dict ( ) NEW_LINE DEDENT mp [ 0 ] [ count ] = i ; NEW_LINE count += 1 NEW_LINE if 0 not in hash : NEW_LINE INDENT hash [ 0 ] = dict ( ) NEW_LINE DEDENT hash [ 0 ] [ count - 1 ] = 1 ; NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT def Gilbreath ( mp , hash , n , k ) : NEW_LINE INDENT if ( n in hash and k in hash [ n ] and hash [ n ] [ k ] != 0 ) : NEW_LINE INDENT return mp [ n ] [ k ] ; NEW_LINE DEDENT ans = abs ( Gilbreath ( mp , hash , n - 1 , k + 1 ) - Gilbreath ( mp , hash , n - 1 , k ) ) ; NEW_LINE if n not in mp : NEW_LINE INDENT mp [ n ] = dict ( ) NEW_LINE DEDENT mp [ n ] [ k ] = ans ; NEW_LINE return ans ; NEW_LINE DEDENT def solve ( n ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE count = 0 ; NEW_LINE mp = dict ( ) NEW_LINE hash = dict ( ) NEW_LINE set_primes ( mp , hash , 100 ) ; NEW_LINE while ( count < n ) : NEW_LINE INDENT print ( Gilbreath ( mp , hash , i , j ) , end = ' , ▁ ' ) NEW_LINE count += 1 NEW_LINE i -= 1 NEW_LINE j += 1 NEW_LINE if ( i < 0 ) : NEW_LINE INDENT i = j ; NEW_LINE j = 0 ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 15 ; NEW_LINE solve ( n ) ; NEW_LINE DEDENT
Roots of the quadratic equation when a + b + c = 0 without using Shridharacharya formula | Function to print the roots of the quadratic equation when a + b + c = 0 ; Driver code
def printRoots ( a , b , c ) : NEW_LINE INDENT print ( 1 , " , " , c / ( a * 1.0 ) ) NEW_LINE DEDENT a = 2 NEW_LINE b = 3 NEW_LINE c = - 5 NEW_LINE printRoots ( a , b , c ) NEW_LINE
Longest alternative parity subsequence | Function to find the longest ; Marks the starting of odd number as sequence and alternatively changes ; Finding the longest odd / even sequence ; Find odd number ; Find even number ; Length of the longest even / odd sequence ; Find odd number ; Find even number ; Answer is maximum of both odd / even or even / odd subsequence ; Driver Code
def longestAlternativeSequence ( a , n ) : NEW_LINE INDENT maxi1 = 0 NEW_LINE f1 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( f1 == 0 ) : NEW_LINE INDENT if ( a [ i ] % 2 ) : NEW_LINE INDENT f1 = 1 NEW_LINE maxi1 += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( a [ i ] % 2 == 0 ) : NEW_LINE INDENT maxi1 += 1 NEW_LINE f1 = 0 NEW_LINE DEDENT DEDENT DEDENT maxi2 = 0 NEW_LINE f2 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( f2 ) : NEW_LINE INDENT if ( a [ i ] % 2 ) : NEW_LINE INDENT f2 = 1 NEW_LINE maxi2 += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( a [ i ] % 2 == 0 ) : NEW_LINE INDENT maxi2 += 1 NEW_LINE f2 = 0 NEW_LINE DEDENT DEDENT DEDENT return max ( maxi1 , maxi2 ) NEW_LINE DEDENT a = [ 13 , 16 , 8 , 9 , 32 , 10 ] NEW_LINE n = len ( a ) NEW_LINE print ( longestAlternativeSequence ( a , n ) ) NEW_LINE
Nambiar Number Generator | Function to return the Nambiar number of the given number ; If there is no digit to choose ; Choose the first digit ; Chosen digit 's parity ; To store the sum of the consecutive digits starting from the chosen digit ; While there are digits to choose ; Update the sum ; If the parity differs ; Return the current sum concatenated with the Numbiar number for the rest of the String ; Driver code
def nambiarNumber ( Str , i ) : NEW_LINE INDENT if ( i >= len ( Str ) ) : NEW_LINE INDENT return " " NEW_LINE DEDENT firstDigit = ord ( Str [ i ] ) - ord ( '0' ) NEW_LINE digitParity = firstDigit % 2 NEW_LINE sumDigits = 0 NEW_LINE while ( i < len ( Str ) ) : NEW_LINE INDENT sumDigits += ( ord ( Str [ i ] ) - ord ( '0' ) ) NEW_LINE sumParity = sumDigits % 2 NEW_LINE if ( digitParity != sumParity ) : NEW_LINE INDENT break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return ( " " + str ( sumDigits ) + nambiarNumber ( Str , i + 1 ) ) NEW_LINE DEDENT Str = "9880127431" NEW_LINE print ( nambiarNumber ( Str , 0 ) ) NEW_LINE
Program to find the Depreciation of Value | Python 3 program to find depreciation of the value initial value , rate and time are given ; Function to return the depreciation of value ; Driver Code
from math import pow NEW_LINE def Depreciation ( v , r , t ) : NEW_LINE INDENT D = v * pow ( ( 1 - r / 100 ) , t ) NEW_LINE return D NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT V1 = 200 NEW_LINE R = 10 NEW_LINE T = 2 NEW_LINE print ( int ( Depreciation ( V1 , R , T ) ) ) NEW_LINE DEDENT
Program to find first N Fermat Numbers | Iterative Function to calculate ( x ^ y ) in O ( log y ) ; res = 1 Initialize result ; If y is odd , multiply x with the result ; n must be even now y = y >> 1 y = y / 2 x = x * x Change x to x ^ 2 ; Function to find nth fermat number ; 2 to the power i ; 2 to the power 2 ^ i ; Function to find first n Fermat numbers ; Calculate 2 ^ 2 ^ i ; Driver code ; Function call
def power ( x , y ) : NEW_LINE INDENT while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = res * x NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT def Fermat ( i ) : NEW_LINE INDENT power2_i = power ( 2 , i ) NEW_LINE power2_2_i = power ( 2 , power2_i ) NEW_LINE return power2_2_i + 1 NEW_LINE DEDENT def Fermat_Number ( n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( Fermat ( i ) , end = " " ) NEW_LINE if ( i != n - 1 ) : NEW_LINE INDENT print ( end = " , ▁ " ) NEW_LINE DEDENT DEDENT DEDENT n = 7 NEW_LINE Fermat_Number ( n ) NEW_LINE
Highly Totient Number | Function to find euler totient number ; Consider all prime factors of n and subtract their multiples from result ; Check if p is a prime factor . ; If yes , then update n and result ; If n has a prime factor greater than sqrt ( n ) ( There can be at - most one such prime factor ) ; Function to find first n highly totient numbers ; Count of Highly totient numbers and value of count of phi of previous numbers ; Store all the values of phi ( x ) upto 10 ^ 5 with frequencies ; If count is greater than count of previous element ; Display the number ; Store the value of phi ; Driver code ; Function call
def phi ( n ) : NEW_LINE INDENT p = 2 NEW_LINE while ( p * p <= n ) : NEW_LINE INDENT if ( n % p == 0 ) : NEW_LINE INDENT while ( n % p == 0 ) : NEW_LINE INDENT n //= p ; NEW_LINE DEDENT result -= ( result // p ) ; NEW_LINE DEDENT p += 1 NEW_LINE DEDENT if ( n > 1 ) : NEW_LINE INDENT result -= ( result // n ) ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT def Highly_Totient ( n ) : NEW_LINE INDENT count = 0 NEW_LINE p_count = - 1 NEW_LINE mp = dict ( ) NEW_LINE i = 1 NEW_LINE while i < 100000 : NEW_LINE INDENT tmp = phi ( i ) NEW_LINE if tmp not in mp : NEW_LINE INDENT mp [ tmp ] = 0 NEW_LINE DEDENT mp [ tmp ] += 1 ; NEW_LINE i += 1 NEW_LINE DEDENT i = 1 NEW_LINE while ( count < n ) : NEW_LINE INDENT if ( ( i in mp ) and mp [ i ] > p_count ) : NEW_LINE INDENT print ( i , end = ' ' ) ; NEW_LINE if ( count < n - 1 ) : NEW_LINE INDENT print ( " , ▁ " , end = ' ' ) ; NEW_LINE DEDENT p_count = mp [ i ] ; NEW_LINE count += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 20 ; NEW_LINE Highly_Totient ( n ) ; NEW_LINE DEDENT
Program to find the Speed of train as per speed of sound | Python3 implementation of the approach ; Function to find the Speed of train ; Driver code ; calling Function
from math import ceil NEW_LINE def speedOfTrain ( X , Y ) : NEW_LINE INDENT Speed = 0 NEW_LINE Speed = 1188 * ( ( X - Y ) / Y ) NEW_LINE return Speed NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 8 NEW_LINE Y = 7.2 NEW_LINE print ( ceil ( speedOfTrain ( X , Y ) ) , end = " ▁ km / hr " ) NEW_LINE DEDENT
Maximum value of division of two numbers in an Array | Function to maximum value of division of two numbers in an array ; Traverse through the array ; Return the required answer ; Driver code
def Division ( a , n ) : NEW_LINE INDENT maxi = - 10 ** 9 NEW_LINE mini = 10 ** 9 NEW_LINE for i in a : NEW_LINE INDENT maxi = max ( i , maxi ) NEW_LINE mini = min ( i , mini ) NEW_LINE DEDENT return maxi // mini NEW_LINE DEDENT a = [ 3 , 7 , 9 , 3 , 11 ] NEW_LINE n = len ( a ) NEW_LINE print ( Division ( a , n ) ) NEW_LINE
Ramanujan Prime | Python3 program to find Ramanujan numbers ; FUnction to return a vector of primes ; 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 greater than or equal to the square of it . numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Print all prime numbers ; Function to find number of primes less than or equal to x ; Binary search to find out number of primes less than or equal to x ; Function to find the nth ramanujan prime ; For n >= 1 , a ( n ) < 4 * n * log ( 4 n ) ; We start from upperbound and find where pi ( i ) - pi ( i / 2 ) < n the previous number being the nth ramanujan prime ; Function to find first n Ramanujan numbers ; Get the prime numbers ; Driver code
from math import log , ceil NEW_LINE MAX = 1000000 NEW_LINE def addPrimes ( ) : NEW_LINE INDENT n = MAX NEW_LINE prime = [ True for i in range ( n + 1 ) ] NEW_LINE for p in range ( 2 , n + 1 ) : NEW_LINE INDENT if p * p > n : NEW_LINE INDENT break NEW_LINE DEDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( 2 * p , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT ans = [ ] NEW_LINE for p in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT ans . append ( p ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def pi ( x , v ) : NEW_LINE INDENT l , r = 0 , len ( v ) - 1 NEW_LINE m , i = 0 , - 1 NEW_LINE while ( l <= r ) : NEW_LINE INDENT m = ( l + r ) // 2 NEW_LINE if ( v [ m ] <= x ) : NEW_LINE INDENT i = m NEW_LINE l = m + 1 NEW_LINE DEDENT else : NEW_LINE INDENT r = m - 1 NEW_LINE DEDENT DEDENT return i + 1 NEW_LINE DEDENT def Ramanujan ( n , v ) : NEW_LINE INDENT upperbound = ceil ( 4 * n * ( log ( 4 * n ) / log ( 2 ) ) ) NEW_LINE for i in range ( upperbound , - 1 , - 1 ) : NEW_LINE INDENT if ( pi ( i , v ) - pi ( i / 2 , v ) < n ) : NEW_LINE INDENT return 1 + i NEW_LINE DEDENT DEDENT DEDENT def Ramanujan_Numbers ( n ) : NEW_LINE INDENT c = 1 NEW_LINE v = addPrimes ( ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( Ramanujan ( i , v ) , end = " " ) NEW_LINE if ( i != n ) : NEW_LINE INDENT print ( end = " , ▁ " ) NEW_LINE DEDENT DEDENT DEDENT n = 10 NEW_LINE Ramanujan_Numbers ( n ) NEW_LINE
Quadruplet pair with XOR zero in the given Array | Python3 implementation of the approach ; Function that returns true if the array contains a valid quadruplet pair ; We can always find a valid quadruplet pair for array size greater than MAX ; For smaller size arrays , perform brute force ; Driver code
MAX = 130 NEW_LINE def validQuadruple ( arr , n ) : NEW_LINE INDENT if ( n >= MAX ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT for l in range ( k + 1 , n ) : NEW_LINE INDENT if ( ( arr [ i ] ^ arr [ j ] ^ arr [ k ] ^ arr [ l ] ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return False NEW_LINE DEDENT arr = [ 1 , 0 , 2 , 3 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE if ( validQuadruple ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants | Function to returns factorial of n ; Function to find nCr ; Function to find the number of words of X vowels and Y consonants can be formed from M vowels and N consonants ; Driver code ; Function call
def fact ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , n + 1 , 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT return res NEW_LINE DEDENT def nCr ( n , r ) : NEW_LINE INDENT return fact ( n ) // ( fact ( r ) * fact ( n - r ) ) NEW_LINE DEDENT def NumberOfWays ( X , Y , M , N ) : NEW_LINE INDENT return fact ( X + Y ) * nCr ( M , X ) * nCr ( N , Y ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 2 NEW_LINE Y = 2 NEW_LINE M = 3 NEW_LINE N = 3 NEW_LINE print ( NumberOfWays ( X , Y , M , N ) ) NEW_LINE DEDENT
Program for sum of cosh ( x ) series upto Nth term | function to return the factorial of a number ; function to return the Sum of the series ; Driver code
def fact ( n ) : NEW_LINE INDENT i , fac = 1 , 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT fac = fac * i NEW_LINE DEDENT return fac NEW_LINE DEDENT def log_Expansion ( x , n ) : NEW_LINE INDENT Sum = 0 NEW_LINE i = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT Sum = Sum + pow ( x , 2 * i ) / fact ( 2 * i ) NEW_LINE DEDENT return Sum NEW_LINE DEDENT x = 1 NEW_LINE n = 10 NEW_LINE print ( log_Expansion ( x , n ) ) NEW_LINE