codeparrot/xlcost-text-to-code huggingface dataset:
Fibonomial coefficient and Fibonomial triangle | Python3 Program to print Fibonomial Triangle of height n . ; Function to produce Fibonacci Series . ; 0 th and 1 st number of the series are 0 and 1 ; Add the previous 2 numbers in the series and store it ; Function to produce fibonomial coefficient ; Function to print Fibonomial Triangle . ; Finding the fibonacci series . ; to store triangle value . ; initialising the 0 th element of each row and diagonal element equal to 0. ; for each row . ; for each column . ; finding each element using recurrence relation . ; printing the Fibonomial Triangle . ; Driver Code
N = 6 ; NEW_LINE def fib ( f , n ) : NEW_LINE INDENT f [ 0 ] = 0 ; NEW_LINE f [ 1 ] = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; NEW_LINE DEDENT DEDENT def fibcoef ( fc , f , n ) : NEW_LINE INDENT for i in range ( n + 1 ) : NEW_LINE INDENT fc [ i ] [ 0 ] = 1 ; NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT k = j ; NEW_LINE while ( k > 0 ) : NEW_LINE INDENT k -= 1 ; NEW_LINE fc [ i ] [ j ] *= f [ k ] ; NEW_LINE DEDENT k = 1 ; NEW_LINE while ( ( j + 1 ) != k ) : NEW_LINE INDENT fc [ i ] [ j ] /= f [ k ] ; NEW_LINE k += 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def printFibonomialTriangle ( n ) : NEW_LINE INDENT f = [ 0 ] * ( N + 1 ) ; NEW_LINE fib ( f , n ) ; NEW_LINE dp = [ [ 0 for x in range ( N + 1 ) ] for y in range ( N + 1 ) ] ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = 1 ; NEW_LINE dp [ i ] [ i ] = 1 ; NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , i ) : NEW_LINE INDENT dp [ i ] [ j ] = ( f [ i - j + 1 ] * dp [ i - 1 ] [ j - 1 ] + f [ j - 1 ] * dp [ i - 1 ] [ j ] ) ; NEW_LINE DEDENT DEDENT for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( i + 1 ) : NEW_LINE INDENT print ( dp [ i ] [ j ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( " " ) ; NEW_LINE DEDENT DEDENT n = 6 ; NEW_LINE printFibonomialTriangle ( n ) ; NEW_LINE
Sum of Arithmetic Geometric Sequence | Python3 code to find the sum of first n terms . ; Return the sum of first n term of AGP ; finding the each term of AGP and adding it to sum . ; Driven Code
import math NEW_LINE def sumofNterm ( a , d , b , r , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sum += ( ( a + ( i - 1 ) * d ) * ( b * math . pow ( r , i - 1 ) ) ) NEW_LINE DEDENT return int ( sum ) NEW_LINE DEDENT a = 1 NEW_LINE d = 1 NEW_LINE b = 2 NEW_LINE r = 2 NEW_LINE n = 3 NEW_LINE print ( sumofNterm ( a , d , b , r , n ) ) NEW_LINE
Sum of the series 2 + ( 2 + 4 ) + ( 2 + 4 + 6 ) + ( 2 + 4 + 6 + 8 ) + …… + ( 2 + 4 + 6 + 8 + … . + 2 n ) | function to find the sum of the given series ; first term of each i - th term ; next term ; required sum ; Driver program to test above
def sumOfTheSeries ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT k = 2 NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT sum = sum + k ; NEW_LINE k = k + 2 NEW_LINE DEDENT DEDENT return sum ; NEW_LINE DEDENT n = 5 NEW_LINE ans = sumOfTheSeries ( n ) ; NEW_LINE print ( ans ) NEW_LINE
Program to find sum of series 1 * 2 * 3 + 2 * 3 * 4 + 3 * 4 * 5 + . . . + n * ( n + 1 ) * ( n + 2 ) | Function to calculate sum of series . ; Driver code
def sumOfSeries ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE i = 1 ; NEW_LINE while i <= n : NEW_LINE INDENT sum = sum + i * ( i + 1 ) * ( i + 2 ) NEW_LINE i = i + 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 10 NEW_LINE print ( sumOfSeries ( n ) ) NEW_LINE
Program to get the Sum of series : 1 | Python3 code to get the sum of the series ; Function to get the series ; Sum of n - 1 terms starting from 2 nd term ; Driver Code
import math NEW_LINE def Series ( x , n ) : NEW_LINE INDENT sum = 1 NEW_LINE term = 1 NEW_LINE y = 2 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT fct = 1 NEW_LINE for j in range ( 1 , y + 1 ) : NEW_LINE INDENT fct = fct * j NEW_LINE DEDENT term = term * ( - 1 ) NEW_LINE m = term * math . pow ( x , y ) / fct NEW_LINE sum = sum + m NEW_LINE y += 2 NEW_LINE DEDENT return sum NEW_LINE DEDENT x = 9 NEW_LINE n = 10 NEW_LINE print ( ' % .4f ' % Series ( x , n ) ) NEW_LINE
Program to get the Sum of series : 1 | Function to get the series ; Computing sum of remaining n - 1 terms . ; Driver Code
def Series ( x , n ) : NEW_LINE INDENT sum = 1 NEW_LINE term = 1 NEW_LINE fct = 1 NEW_LINE p = 1 NEW_LINE multi = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT fct = fct * multi * ( multi + 1 ) NEW_LINE p = p * x * x NEW_LINE term = ( - 1 ) * term NEW_LINE multi += 2 NEW_LINE sum = sum + ( term * p ) / fct NEW_LINE DEDENT return sum NEW_LINE DEDENT x = 9 NEW_LINE n = 10 NEW_LINE print ( ' % .4f ' % Series ( x , n ) ) NEW_LINE
Find the number of consecutive zero at the end after multiplying n numbers | Function to count two 's factor ; Count number of 2 s present in n ; Function to count five 's factor ; Function to count number of zeros ; Count the two 's factor of n number ; Count the five 's factor of n number ; Return the minimum ; Driver Code
def two_factor ( n ) : NEW_LINE INDENT twocount = 0 NEW_LINE while n % 2 == 0 : NEW_LINE INDENT twocount += 1 NEW_LINE n = int ( n / 2 ) NEW_LINE DEDENT return twocount NEW_LINE DEDENT def five_factor ( n ) : NEW_LINE INDENT fivecount = 0 NEW_LINE while n % 5 == 0 : NEW_LINE INDENT fivecount += 1 NEW_LINE n = int ( n / 5 ) NEW_LINE DEDENT return fivecount NEW_LINE DEDENT def find_con_zero ( arr , n ) : NEW_LINE INDENT twocount = 0 NEW_LINE fivecount = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT twocount += two_factor ( arr [ i ] ) NEW_LINE fivecount += five_factor ( arr [ i ] ) NEW_LINE DEDENT if twocount < fivecount : NEW_LINE INDENT return twocount NEW_LINE DEDENT else : NEW_LINE INDENT return fivecount NEW_LINE DEDENT DEDENT arr = [ 100 , 10 , 5 , 25 , 35 , 14 ] NEW_LINE n = 6 NEW_LINE print ( find_con_zero ( arr , n ) ) NEW_LINE
First occurrence of a digit in a given fraction | function to print the first digit ; reduce the number to its mod ; traverse for every decimal places ; get every fraction places when ( a * 10 / b ) / c ; check if it is equal to the required integer ; mod the number ; driver code to test the above function
def first ( a , b , c ) : NEW_LINE INDENT a %= b NEW_LINE for i in range ( 1 , b + 1 ) : NEW_LINE INDENT a = a * 10 NEW_LINE if int ( a / b ) == c : NEW_LINE INDENT return i NEW_LINE DEDENT a %= b NEW_LINE DEDENT return - 1 NEW_LINE DEDENT a = 1 NEW_LINE b = 4 NEW_LINE c = 5 NEW_LINE print ( first ( a , b , c ) ) NEW_LINE
Minimize the absolute difference of sum of two subsets | function to print difference ; summation of n elements ; if divisible by 4 ; if remainder 1 or 2. In case of remainder 2 , we divide elements from 3 to n in groups of size 4 and put 1 in one group and 2 in group . This also makes difference 1. ; We put elements from 4 to n in groups of size 4. Remaining elements 1 , 2 and 3 can be divided as ( 1 , 2 ) and ( 3 ) . ; driver code to test the above function
def subsetDifference ( n ) : NEW_LINE INDENT s = int ( n * ( n + 1 ) / 2 ) NEW_LINE if n % 4 == 0 : NEW_LINE INDENT print ( " First ▁ subset ▁ sum ▁ = ▁ " , int ( s / 2 ) ) NEW_LINE print ( " Second ▁ subset ▁ sum ▁ = ▁ " , int ( s / 2 ) ) NEW_LINE print ( " Difference ▁ = ▁ " , 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT if n % 4 == 1 or n % 4 == 2 : NEW_LINE INDENT print ( " First ▁ subset ▁ sum ▁ = ▁ " , int ( s / 2 ) ) NEW_LINE print ( " Second ▁ subset ▁ sum ▁ = ▁ " , int ( s / 2 ) + 1 ) NEW_LINE print ( " Difference ▁ = ▁ " , 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " First ▁ subset ▁ sum ▁ = ▁ " , int ( s / 2 ) ) NEW_LINE print ( " Second ▁ subset ▁ sum ▁ = ▁ " , int ( s / 2 ) ) NEW_LINE print ( " Difference ▁ = ▁ " , 0 ) NEW_LINE DEDENT DEDENT DEDENT n = 6 NEW_LINE subsetDifference ( n ) NEW_LINE
Time required to meet in equilateral triangle | function to calculate time to meet ; Driver Code
def timeToMeet ( s , v ) : NEW_LINE INDENT V = 3 * v / 2 ; NEW_LINE time = s / V ; NEW_LINE print ( time ) ; NEW_LINE DEDENT s = 25 ; NEW_LINE v = 56 ; NEW_LINE timeToMeet ( s , v ) ; NEW_LINE
Sum of the series 1 + ( 1 + 3 ) + ( 1 + 3 + 5 ) + ( 1 + 3 + 5 + 7 ) + â €¦ â €¦ + ( 1 + 3 + 5 + 7 + â €¦ + ( 2 n | functionn to find the sum of the given series ; first term of each i - th term ; next term ; required sum ; Driver program
def sumOfTheSeries ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT k = 1 NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT sum += k NEW_LINE k += 2 NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT n = 5 NEW_LINE print ( " Sum ▁ = " , sumOfTheSeries ( n ) ) NEW_LINE
Check if a number can be written as sum of three consecutive integers | function to check if a number can be written as sum of three consecutive integer . ; if n is 0 ; if n is positive , increment loop by 1. ; if n is negative , decrement loop by 1. ; Running loop from 0 to n - 2 ; check if sum of three consecutive integer is equal to n . ; Driver Code
def checksum ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT print ( " - 1 ▁ 0 ▁ 1" ) NEW_LINE return 0 NEW_LINE DEDENT inc = 0 NEW_LINE if n > 0 : NEW_LINE INDENT inc = 1 NEW_LINE DEDENT else : NEW_LINE INDENT inc = - 1 NEW_LINE DEDENT for i in range ( 0 , n - 1 , inc ) : NEW_LINE INDENT if i + i + 1 + i + 2 == n : NEW_LINE INDENT print ( i , " ▁ " , i + 1 , " ▁ " , i + 2 ) NEW_LINE return 0 NEW_LINE DEDENT DEDENT print ( " - 1" ) NEW_LINE DEDENT n = 6 NEW_LINE checksum ( n ) NEW_LINE
Sum of all divisors from 1 to n | Python3 code to find sum of all divisor of number up to 'n ; Utility function to find sum of all divisor of number up to 'n ; Find all divisors of i and add them ; Driver code
' NEW_LINE ' NEW_LINE def divisorSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT j = 1 NEW_LINE while j * j <= i : NEW_LINE INDENT 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 j = j + 1 NEW_LINE DEDENT DEDENT return int ( sum ) NEW_LINE DEDENT n = 4 NEW_LINE print ( divisorSum ( n ) ) NEW_LINE n = 5 NEW_LINE print ( divisorSum ( n ) ) NEW_LINE
Program for Binomial Coefficients table | Function to print binomial table ; B ( m , x ) is 1 if either m or x is is 0. ; Otherwise using recursive formula B ( m , x ) = B ( m , x - 1 ) * ( m - x + 1 ) / x ; Driver Function
def printbinomial ( max ) : NEW_LINE INDENT for m in range ( max + 1 ) : NEW_LINE INDENT print ( ' % ▁ 2d ' % m , end = ' ▁ ' ) NEW_LINE binom = 1 NEW_LINE for x in range ( m + 1 ) : NEW_LINE INDENT if m != 0 and x != 0 : NEW_LINE INDENT binom = binom * ( m - x + 1 ) / x NEW_LINE DEDENT print ( ' % ▁ 4d ' % binom , end = ' ▁ ' ) NEW_LINE DEDENT print ( " " , end = ' ' ) NEW_LINE DEDENT DEDENT max = 10 NEW_LINE printbinomial ( max ) NEW_LINE
Find largest prime factor of a number | Python3 code to find largest prime factor of number ; A function to find largest prime factor ; Initialize the maximum prime factor variable with the lowest one ; Print the number of 2 s that divide n ; n must be odd at this point ; now we have to iterate only for integers who does not have prime factor 2 and 3 ; This condition is to handle the case when n is a prime number greater than 4 ; Driver code to test above function
import math NEW_LINE def maxPrimeFactors ( n ) : NEW_LINE INDENT maxPrime = - 1 NEW_LINE while n % 2 == 0 : NEW_LINE INDENT maxPrime = 2 NEW_LINE DEDENT while n % 3 == 0 : NEW_LINE INDENT maxPrime = 3 NEW_LINE n = n / 3 NEW_LINE DEDENT for i in range ( 5 , int ( math . sqrt ( n ) ) + 1 , 6 ) : NEW_LINE INDENT while n % i == 0 : NEW_LINE INDENT maxPrime = i NEW_LINE n = n / i NEW_LINE DEDENT while n % ( i + 2 ) == 0 : NEW_LINE INDENT maxPrime = i + 2 NEW_LINE n = n / ( i + 2 ) NEW_LINE DEDENT DEDENT if n > 4 : NEW_LINE INDENT maxPrime = n NEW_LINE DEDENT return int ( maxPrime ) NEW_LINE DEDENT n = 15 NEW_LINE print ( maxPrimeFactors ( n ) ) NEW_LINE n = 25698751364526 NEW_LINE print ( maxPrimeFactors ( n ) ) NEW_LINE
Count unset bits in a range | Function to get no of set bits in the binary representation of 'n ; function to count unset bits in the given range ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; returns number of unset bits in the range ' l ' to ' r ' in 'n ; Driver code to test above
' NEW_LINE def countSetBits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while n : NEW_LINE INDENT n &= ( n - 1 ) NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def countUnsetBitsInGivenRange ( n , l , r ) : NEW_LINE INDENT num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) NEW_LINE DEDENT ' NEW_LINE INDENT return ( r - l + 1 ) - countSetBits ( n & num ) NEW_LINE DEDENT n = 80 NEW_LINE l = 1 NEW_LINE r = 4 NEW_LINE print ( countUnsetBitsInGivenRange ( n , l , r ) ) NEW_LINE
Sum of fourth power of first n even natural numbers | calculate the sum of fourth power of first n even natural numbers ; made even number ; Driver Code
def evenPowerSum ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT j = 2 * i ; NEW_LINE sum = sum + ( j * j * j * j ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT n = 5 ; NEW_LINE print ( evenPowerSum ( n ) ) ; NEW_LINE
Sum of fourth power of first n even natural numbers | calculate the sum of fourth power of first n even natural numbers ; Driver Code
def evenPowerSum ( n ) : NEW_LINE INDENT return ( 8 * n * ( n + 1 ) * ( 2 * n + 1 ) * ( 3 * n * n + 3 * n - 1 ) ) / 15 ; NEW_LINE DEDENT n = 4 ; NEW_LINE print ( int ( evenPowerSum ( n ) ) ) ; NEW_LINE
Balanced Prime | Python3 code to find Nth Balanced Prime ; Return the Nth balanced prime . ; Sieve of Eratosthenes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; storing all primes ; Finding the Nth balanced Prime ; Driven Program
MAX = 501 NEW_LINE def balancedprime ( n ) : NEW_LINE INDENT prime = [ True ] * ( MAX + 1 ) NEW_LINE p = 2 NEW_LINE while p * p <= MAX : NEW_LINE INDENT if prime [ p ] == True : NEW_LINE INDENT i = p * 2 NEW_LINE while i <= MAX : NEW_LINE INDENT prime [ i ] = False NEW_LINE i = i + p NEW_LINE DEDENT DEDENT p = p + 1 NEW_LINE DEDENT v = list ( ) NEW_LINE p = 3 NEW_LINE while p <= MAX : NEW_LINE INDENT if prime [ p ] : NEW_LINE INDENT v . append ( p ) NEW_LINE DEDENT p = p + 2 NEW_LINE DEDENT count = 0 NEW_LINE i = 1 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT if v [ i ] == ( v [ i + 1 ] + v [ i - 1 ] ) / 2 : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if count == n : NEW_LINE INDENT return v [ i ] NEW_LINE DEDENT DEDENT DEDENT n = 4 NEW_LINE print ( balancedprime ( n ) ) NEW_LINE
Smallest integer which has n factors or more | Python3 program to print the smallest integer with n factors or more ; array to store prime factors ; function to generate all prime factors of numbers from 1 to 10 ^ 6 ; Initializes all the positions with their value . ; Initializes all multiples of 2 with 2 ; A modified version of Sieve of Eratosthenes to store the smallest prime factor that divides every number . ; check if it has no prime factor . ; Initializes of j starting from i * i ; if it has no prime factor before , then stores the smallest prime divisor ; function to calculate number of factors ; stores the smallest prime number that divides n ; stores the count of number of times a prime number divides n . ; reduces to the next number after prime factorization of n ; false when prime factorization is done ; if the same prime number is dividing n , then we increase the count ; if its a new prime factor that is factorizing n , then we again set c = 1 and change dup to the new prime factor , and apply the formula explained above . ; prime factorizes a number ; for the last prime factor ; function to find the smallest integer with n factors or more . ; check if no of factors is more than n or not ; generate prime factors of number upto 10 ^ 6
MAX = 100001 ; NEW_LINE factor = [ 0 ] * MAX ; NEW_LINE def generatePrimeFactors ( ) : NEW_LINE INDENT factor [ 1 ] = 1 ; NEW_LINE for i in range ( 2 , MAX ) : NEW_LINE INDENT factor [ i ] = i ; NEW_LINE DEDENT i = 4 NEW_LINE while ( i < MAX ) : NEW_LINE INDENT factor [ i ] = 2 ; NEW_LINE i += 2 ; NEW_LINE DEDENT i = 3 ; NEW_LINE while ( i * i < MAX ) : NEW_LINE INDENT if ( factor [ i ] == i ) : NEW_LINE INDENT j = i * i ; NEW_LINE while ( j < MAX ) : NEW_LINE INDENT if ( factor [ j ] == j ) : NEW_LINE INDENT factor [ j ] = i ; NEW_LINE DEDENT j += i ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT DEDENT def calculateNoOFactors ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT ans = 1 ; NEW_LINE dup = factor [ n ] ; NEW_LINE c = 1 ; NEW_LINE j = int ( n / factor [ n ] ) ; NEW_LINE while ( j != 1 ) : NEW_LINE INDENT if ( factor [ j ] == dup ) : NEW_LINE INDENT c += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT dup = factor [ j ] ; NEW_LINE ans = ans * ( c + 1 ) ; NEW_LINE c = 1 ; NEW_LINE DEDENT j = int ( j / factor [ j ] ) ; NEW_LINE DEDENT ans = ans * ( c + 1 ) ; NEW_LINE return ans ; NEW_LINE DEDENT def smallest ( n ) : NEW_LINE INDENT i = 1 ; NEW_LINE while ( True ) : NEW_LINE INDENT if ( calculateNoOFactors ( i ) >= n ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT DEDENT generatePrimeFactors ( ) ; NEW_LINE n = 4 ; NEW_LINE print ( smallest ( n ) ) ; NEW_LINE
Sum of squares of first n natural numbers | Return the sum of square of first n natural numbers ; Iterate i from 1 and n finding square of i and add to sum . ; Driven Program
def squaresum ( n ) : NEW_LINE INDENT sm = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sm = sm + ( i * i ) NEW_LINE DEDENT return sm NEW_LINE DEDENT n = 4 NEW_LINE print ( squaresum ( n ) ) NEW_LINE
Break a number such that sum of maximum divisors of all parts is minimum | Function to check if a number is prime or not . ; If n is an even number ( we can write it as sum of two primes ) ; If n is odd and n - 2 is prime . ; If n is odd , n - 3 must be even . ; Driver code
def isPrime ( n ) : NEW_LINE INDENT i = 2 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def minimumSum ( n ) : NEW_LINE INDENT if ( isPrime ( n ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n % 2 == 0 ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT if ( isPrime ( n - 2 ) ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT return 3 NEW_LINE DEDENT n = 27 NEW_LINE print ( minimumSum ( n ) ) NEW_LINE
Find first and last digits of a number | Find the first digit ; Remove last digit from number till only one digit is left ; return the first digit ; Find the last digit ; return the last digit ; Driver Code
def firstDigit ( n ) : NEW_LINE INDENT while n >= 10 : NEW_LINE INDENT n = n / 10 ; NEW_LINE DEDENT return int ( n ) NEW_LINE DEDENT def lastDigit ( n ) : NEW_LINE INDENT return ( n % 10 ) NEW_LINE DEDENT n = 98562 ; NEW_LINE print ( firstDigit ( n ) , end = " ▁ " ) NEW_LINE print ( lastDigit ( n ) ) NEW_LINE
Find first and last digits of a number | Python3 program to find first and last digits of a number ; Find the first digit ; Find total number of digits - 1 ; Find first digit ; Return first digit ; Find the last digit ; return the last digit ; Driver Code
import math NEW_LINE def firstDigit ( n ) : NEW_LINE INDENT digits = ( int ) ( math . log10 ( n ) ) NEW_LINE n = ( int ) ( n / pow ( 10 , digits ) ) NEW_LINE return n ; NEW_LINE DEDENT def lastDigit ( n ) : NEW_LINE INDENT return ( n % 10 ) NEW_LINE DEDENT n = 98562 ; NEW_LINE print ( firstDigit ( n ) , end = " ▁ " ) NEW_LINE print ( lastDigit ( n ) ) NEW_LINE
Express an odd number as sum of prime numbers | Function to check if a number is prime or not . ; Prints at most three prime numbers whose sum is n . ; CASE - I ; CASE - II ; CASE - III ; Driver Code
def isPrime ( x ) : NEW_LINE INDENT if ( x == 0 or x == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT i = 2 NEW_LINE while i * i <= x : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT return 1 NEW_LINE DEDENT def findPrimes ( n ) : NEW_LINE INDENT if ( isPrime ( n ) ) : NEW_LINE INDENT print ( n , end = " ▁ " ) NEW_LINE DEDENT elif ( isPrime ( n - 2 ) ) : NEW_LINE INDENT print ( "2" , end = " ▁ " ) NEW_LINE print ( n - 2 , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( "3" , end = " ▁ " ) NEW_LINE n = n - 3 NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT if ( isPrime ( i ) and isPrime ( n - i ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE print ( ( n - i ) , end = " ▁ " ) NEW_LINE break NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT DEDENT DEDENT n = 27 ; NEW_LINE findPrimes ( n ) ; NEW_LINE
AKS Primality Test | array used to store coefficients . ; function to calculate the coefficients of ( x - 1 ) ^ n - ( x ^ n - 1 ) with the help of Pascal 's triangle . ; function to check whether the number is prime or not ; Calculating all the coefficients by the function coef and storing all the coefficients in c array . ; subtracting c [ n ] and adding c [ 0 ] by 1 as ( x - 1 ) ^ n - ( x ^ n - 1 ) , here we are subtracting c [ n ] by 1 and adding 1 in expression . ; checking all the coefficients whether they are divisible by n or not . if n is not prime , then loop breaks and ( i > 0 ) . ; Return true if all coefficients are divisible by n . ; Driver Code
c = [ 0 ] * 100 ; NEW_LINE def coef ( n ) : NEW_LINE INDENT c [ 0 ] = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT c [ 1 + i ] = 1 ; NEW_LINE for j in range ( i , 0 , - 1 ) : NEW_LINE INDENT c [ j ] = c [ j - 1 ] - c [ j ] ; NEW_LINE DEDENT c [ 0 ] = - c [ 0 ] ; NEW_LINE DEDENT DEDENT def isPrime ( n ) : NEW_LINE INDENT coef ( n ) ; NEW_LINE c [ 0 ] = c [ 0 ] + 1 ; NEW_LINE c [ n ] = c [ n ] - 1 ; NEW_LINE i = n ; NEW_LINE while ( i > - 1 and c [ i ] % n == 0 ) : NEW_LINE INDENT i = i - 1 ; NEW_LINE DEDENT return True if i < 0 else False ; NEW_LINE DEDENT n = 37 ; NEW_LINE if ( isPrime ( n ) ) : NEW_LINE INDENT print ( " Prime " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Prime " ) ; NEW_LINE DEDENT
Motzkin number | Return the nth Motzkin Number . ; Base Case ; Recursive step ; Driver code
def motzkin ( n ) : NEW_LINE INDENT if ( n == 0 or n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return ( ( 2 * n + 1 ) * motzkin ( n - 1 ) + ( 3 * n - 3 ) * motzkin ( n - 2 ) ) / ( n + 2 ) NEW_LINE DEDENT n = 8 NEW_LINE print ( motzkin ( n ) ) NEW_LINE
Sum of the series 0.6 , 0.06 , 0.006 , 0.0006 , ... to n terms | Python3 program to find sum of 0.6 , 0.06 , 0.006 , 0.0006 , ... to n terms ; function which return the sum of series ; Driver code
import math NEW_LINE def sumOfSeries ( n ) : NEW_LINE INDENT return ( ( 0.666 ) * ( 1 - 1 / pow ( 10 , n ) ) ) ; NEW_LINE DEDENT n = 2 ; NEW_LINE print ( sumOfSeries ( n ) ) ; NEW_LINE
Narcissistic number | function to count digits ; Returns true if n is Narcissistic number ; Count the number of digits ; Calculates the sum of digits raised to power ; Driver code
def countDigit ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( 1 + countDigit ( n // 10 ) ) NEW_LINE DEDENT def check ( n ) : NEW_LINE INDENT l = countDigit ( n ) NEW_LINE dup = n ; sm = 0 NEW_LINE while ( dup ) : NEW_LINE INDENT sm = sm + pow ( dup % 10 , l ) NEW_LINE dup = dup // 10 NEW_LINE DEDENT return ( n == sm ) NEW_LINE DEDENT n = 1634 NEW_LINE if ( check ( n ) ) : NEW_LINE INDENT print ( " yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " no " ) NEW_LINE DEDENT
Sum of squares of first n natural numbers | Function to calculate series ; Driver Code
def summation ( n ) : NEW_LINE INDENT return sum ( [ i ** 2 for i in range ( 1 , n + 1 ) ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 NEW_LINE print ( summation ( n ) ) NEW_LINE DEDENT