codeparrot/xlcost-text-to-code huggingface dataset:
Numbers less than N which are product of exactly two distinct prime numbers | Python 3 program to find numbers that are product of exactly two distinct prime numbers ; Function to check whether a number is a PerfectSquare or not ; Function to check if a number is a product of exactly two distinct primes ; Function to find numbers that are product of exactly two distinct prime numbers . ; Vector to store such numbers ; insert in the vector ; Print all numbers till n from the vector ; Driver function
import math NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT sr = math . sqrt ( x ) NEW_LINE return ( ( sr - math . floor ( sr ) ) == 0 ) NEW_LINE DEDENT def isProduct ( num ) : NEW_LINE INDENT cnt = 0 NEW_LINE i = 2 NEW_LINE while cnt < 2 and i * i <= num : NEW_LINE INDENT while ( num % i == 0 ) : NEW_LINE INDENT num //= i NEW_LINE cnt += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if ( num > 1 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT return cnt == 2 NEW_LINE DEDENT def findNumbers ( N ) : NEW_LINE INDENT vec = [ ] NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( isProduct ( i ) and not isPerfectSquare ( i ) ) : NEW_LINE INDENT vec . append ( i ) NEW_LINE DEDENT DEDENT for i in range ( len ( vec ) ) : NEW_LINE INDENT print ( vec [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 30 NEW_LINE findNumbers ( N ) NEW_LINE DEDENT
Program to find the Nth term of the series 3 , 20 , 63 , 144 , 230 , â €¦ â €¦ | calculate Nth term of series ; return final sum ; Driver code
def nthTerm ( n ) : NEW_LINE INDENT return 2 * pow ( n , 3 ) + pow ( n , 2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE DEDENT
Find Nth number of the series 1 , 6 , 15 , 28 , 45 , ... . . | Function for calculating Nth term of series ; Driver code ; Taking n as 4 ; Printing the nth term
def NthTerm ( N ) : NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE print ( NthTerm ( N ) ) NEW_LINE DEDENT
Program to print the Sum of series | calculate Nth term of series ; Driver Function ; Get the value of N ; Get the sum of the series
def findSum ( N ) : NEW_LINE INDENT return ( ( N * ( N + 1 ) * ( 2 * N - 5 ) + 4 * N ) / 2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE print ( findSum ( N ) ) NEW_LINE DEDENT
Program to find the Nth term of series | Python3 program to find N - th term of the series : 9 , 23 , 45 , 75 , 113 , 159. . ... . ; calculate Nth term of series ; Driver Code ; Get the value of N ; Find the Nth term and print it
def nthTerm ( N ) : NEW_LINE INDENT return ( ( 3 * N * N ) - ( 6 * N ) + 2 ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE print ( nthTerm ( n ) ) NEW_LINE DEDENT
Program to find the value of tan ( nÎ ˜ ) | Python3 program to find the value of cos ( n - theta ) ; Function to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of cos ( n - theta ) ; store required answer ; use to toggle sign in sequence . ; calculate numerator ; calculate denominator ; Driver code
import math NEW_LINE MAX = 16 NEW_LINE nCr = [ [ 0 for i in range ( MAX ) ] for i in range ( MAX ) ] NEW_LINE def binomial ( ) : NEW_LINE INDENT for i in range ( MAX ) : NEW_LINE INDENT for j in range ( 0 , i + 1 ) : NEW_LINE INDENT if j == 0 or j == i : NEW_LINE INDENT nCr [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT nCr [ i ] [ j ] = nCr [ i - 1 ] [ j ] + nCr [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT DEDENT DEDENT DEDENT def findTanNTheta ( tanTheta , n ) : NEW_LINE INDENT numerator = 0 NEW_LINE denominator = 1 NEW_LINE ans = 0 NEW_LINE toggle = 1 NEW_LINE for i in range ( 1 , n + 1 , 2 ) : NEW_LINE INDENT numerator = ( numerator + nCr [ n ] [ i ] * ( tanTheta ** ( i ) ) * toggle ) NEW_LINE toggle = toggle * - 1 NEW_LINE DEDENT toggle = - 1 NEW_LINE for i in range ( 2 , n + 1 , 2 ) : NEW_LINE INDENT numerator = ( numerator + nCr [ n ] [ i ] * ( tanTheta ** i ) * toggle ) NEW_LINE toggle = toggle * - 1 NEW_LINE DEDENT ans = numerator / denominator NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT binomial ( ) NEW_LINE tanTheta = 0.3 NEW_LINE n = 10 NEW_LINE print ( findTanNTheta ( tanTheta , n ) ) NEW_LINE DEDENT
Program to find Sum of the series 1 * 3 + 3 * 5 + ... . | Python program to find sum of first n terms ; Sn = n * ( 4 * n * n + 6 * n - 1 ) / 3 ; number of terms to be included in the sum ; find the Sn
def calculateSum ( n ) : NEW_LINE INDENT return ( n * ( 4 * n * n + 6 * n - 1 ) / 3 ) ; NEW_LINE DEDENT n = 4 NEW_LINE print ( " Sum ▁ = " , calculateSum ( n ) ) NEW_LINE
Triplet with no element divisible by 3 and sum N | Function to print a , b and c ; first loop ; check for 1 st number ; second loop ; check for 2 nd number ; third loop ; Check for 3 rd number ; Driver Code
def printCombination ( n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( i % 3 != 0 ) : NEW_LINE INDENT for j in range ( 1 , n ) : NEW_LINE INDENT if ( j % 3 != 0 ) : NEW_LINE INDENT for k in range ( 1 , n ) : NEW_LINE INDENT if ( k % 3 != 0 and ( i + j + k ) == n ) : NEW_LINE INDENT print ( i , j , k ) ; NEW_LINE return ; NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT DEDENT DEDENT n = 233 ; NEW_LINE printCombination ( n ) ; NEW_LINE
Program to find the percentage of difference between two numbers | Function to calculate the percentage ; Driver code ; Function calling
def percent ( a , b ) : NEW_LINE INDENT result = int ( ( ( b - a ) * 100 ) / a ) NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b = 20 , 25 NEW_LINE print ( percent ( a , b ) , " % " ) NEW_LINE DEDENT
Numbers with sum of digits equal to the sum of digits of its all prime factor | maximum size of number ; array to store smallest prime factor of number ; array to store sum of digits of a number ; boolean array to check given number is countable for required answer or not . ; prefix array to store answer ; Calculating SPF ( Smallest Prime Factor ) for every number till MAXN . ; marking smallest prime factor for every number to be itself . ; separately marking spf for every even number as 2 ; checking if i is prime ; marking SPF for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; Function to find sum of digits in a number ; find sum of digits of all numbers up to MAXN ; add sum of digits of least prime factor and n / spf [ n ] ; if it is valid make isValid true ; prefix sum to compute answer ; Driver code ; print answer for required range ; print answer for required range
MAXN = 100005 NEW_LINE spf = [ 0 ] * MAXN NEW_LINE sum_digits = [ 0 ] * MAXN NEW_LINE isValid = [ 0 ] * MAXN NEW_LINE ans = [ 0 ] * MAXN NEW_LINE def Smallest_prime_factor ( ) : NEW_LINE INDENT for i in range ( 1 , MAXN ) : NEW_LINE INDENT spf [ i ] = i NEW_LINE DEDENT for i in range ( 4 , MAXN , 2 ) : NEW_LINE INDENT spf [ i ] = 2 NEW_LINE DEDENT i = 3 NEW_LINE while i * i <= MAXN : NEW_LINE INDENT if ( spf [ i ] == i ) : NEW_LINE INDENT for j in range ( i * i , MAXN , i ) : NEW_LINE INDENT if ( spf [ j ] == j ) : NEW_LINE INDENT spf [ j ] = i NEW_LINE DEDENT DEDENT DEDENT i += 2 NEW_LINE DEDENT DEDENT def Digit_Sum ( copy ) : NEW_LINE INDENT d = 0 NEW_LINE while ( copy ) : NEW_LINE INDENT d += copy % 10 NEW_LINE copy //= 10 NEW_LINE DEDENT return d NEW_LINE DEDENT def Sum_Of_All_Digits ( ) : NEW_LINE INDENT for n in range ( 2 , MAXN ) : NEW_LINE INDENT sum_digits [ n ] = ( sum_digits [ n // spf [ n ] ] + Digit_Sum ( spf [ n ] ) ) NEW_LINE if ( Digit_Sum ( n ) == sum_digits [ n ] ) : NEW_LINE INDENT isValid [ n ] = True NEW_LINE DEDENT DEDENT for n in range ( 2 , MAXN ) : NEW_LINE INDENT if ( isValid [ n ] ) : NEW_LINE INDENT ans [ n ] = 1 NEW_LINE DEDENT ans [ n ] += ans [ n - 1 ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT Smallest_prime_factor ( ) NEW_LINE Sum_Of_All_Digits ( ) NEW_LINE l = 2 NEW_LINE r = 3 NEW_LINE print ( " Valid ▁ numbers ▁ in ▁ the ▁ range " , l , r , " are " , ans [ r ] - ans [ l - 1 ] ) NEW_LINE l = 2 NEW_LINE r = 10 NEW_LINE print ( " Valid ▁ numbers ▁ in ▁ the ▁ range " , l , r , " are " , ans [ r ] - ans [ l - 1 ] ) NEW_LINE DEDENT
Count numbers which can be represented as sum of same parity primes |  ; Function to calculate count ; Driver Code
/ * Python program to Count numbers NEW_LINE which can be represented as NEW_LINE sum of same parity primes * / NEW_LINE def calculate ( array , size ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( size ) : NEW_LINE INDENT if ( array [ i ] % 2 == 0 and array [ i ] != 0 and array [ i ] != 2 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 3 , 4 , 6 ] NEW_LINE size = len ( a ) NEW_LINE print ( calculate ( a , size ) ) NEW_LINE DEDENT
N | Python3 program to answer queries for N - th prime factor of a number ; 2 - D vector that stores prime factors ; Function to pre - store prime factors of all numbers till 10 ^ 6 ; calculate unique prime factors for every number till 10 ^ 6 ; find prime factors ; store if prime factor ; Function that returns answer for every query ; Function to pre - store unique prime factors ; 1 st query ; 2 nd query ; 3 rd query ; 4 th query
from math import sqrt , ceil NEW_LINE N = 10001 NEW_LINE v = [ [ ] for i in range ( N ) ] NEW_LINE def preprocess ( ) : NEW_LINE INDENT for i in range ( 1 , N ) : NEW_LINE INDENT num = i NEW_LINE for j in range ( 2 , ceil ( sqrt ( num ) ) + 1 ) : NEW_LINE INDENT if ( num % j == 0 ) : NEW_LINE INDENT v [ i ] . append ( j ) NEW_LINE while ( num % j == 0 ) : NEW_LINE INDENT num = num // j NEW_LINE DEDENT DEDENT DEDENT if ( num > 2 ) : NEW_LINE INDENT v [ i ] . append ( num ) NEW_LINE DEDENT DEDENT DEDENT def query ( number , n ) : NEW_LINE INDENT return v [ number ] [ n - 1 ] NEW_LINE DEDENT preprocess ( ) NEW_LINE number = 6 NEW_LINE n = 1 NEW_LINE print ( query ( number , n ) ) NEW_LINE number = 210 NEW_LINE n = 3 NEW_LINE print ( query ( number , n ) ) NEW_LINE number = 210 NEW_LINE n = 2 NEW_LINE print ( query ( number , n ) ) NEW_LINE number = 60 NEW_LINE n = 2 NEW_LINE print ( query ( number , n ) ) NEW_LINE
Program to find HCF ( Highest Common Factor ) of 2 Numbers | Recursive function to return gcd of a and b ; Driver program to test above function
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 a = 98 NEW_LINE b = 56 NEW_LINE if ( gcd ( a , b ) ) : NEW_LINE INDENT print ( ' GCD ▁ of ' , a , ' and ' , b , ' is ' , gcd ( a , b ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' not ▁ found ' ) NEW_LINE DEDENT
Largest number with maximum trailing nines which is less than N and greater than N | function to count no of digits ; function to implement above approach ; if difference between power and n doesn 't exceed d ; loop to build a number from the appropriate no of digits containing only 9 ; if the build number is same as original number ( n ) ; observation ; Driver Code ; variable that stores no of digits in n
def dig ( a ) : NEW_LINE INDENT count = 0 ; NEW_LINE while ( a > 0 ) : NEW_LINE INDENT a /= 10 NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def required_number ( num , n , d ) : NEW_LINE INDENT flag = 0 NEW_LINE power = 0 NEW_LINE a = 0 NEW_LINE for i in range ( num , 0 , - 1 ) : NEW_LINE INDENT power = pow ( 10 , i ) NEW_LINE a = n % power NEW_LINE if ( d > a ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT t = 0 NEW_LINE for j in range ( 0 , i ) : NEW_LINE INDENT t += 9 * pow ( 10 , j ) NEW_LINE DEDENT if ( n % power == t ) : NEW_LINE INDENT print ( n , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ( n - ( n % power ) - 1 ) , end = " " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( n , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1029 NEW_LINE d = 102 NEW_LINE num = dig ( n ) NEW_LINE required_number ( num , n , d ) NEW_LINE DEDENT
Egg Dropping Puzzle with 2 Eggs and K Floors | Python3 program to find optimal number of trials for k floors and 2 eggs . ; Driver Code
import math as mt NEW_LINE def twoEggDrop ( k ) : NEW_LINE INDENT return mt . ceil ( ( - 1.0 + mt . sqrt ( 1 + 8 * k ) ) / 2 ) NEW_LINE DEDENT k = 100 NEW_LINE print ( twoEggDrop ( k ) ) NEW_LINE
Program to find the Area and Volume of Icosahedron | Python3 program to find the Area and volume of Icosahedron ; Function to find area of Icosahedron ; Formula to calculate area ; Function to find volume of Icosahedron ; Formula to calculate volume ; Driver Code ; Function call to find area of Icosahedron . ; Function call to find volume of Icosahedron .
from math import sqrt NEW_LINE def findArea ( a ) : NEW_LINE INDENT area = 5 * sqrt ( 3 ) * a * a NEW_LINE return area NEW_LINE DEDENT def findVolume ( a ) : NEW_LINE INDENT volume = ( ( 5 / 12 ) * ( 3 + sqrt ( 5 ) ) * a * a * a ) NEW_LINE return volume NEW_LINE DEDENT a = 5 NEW_LINE print ( " Area : ▁ " , findArea ( a ) ) NEW_LINE print ( " Volume : ▁ " , findVolume ( a ) ) NEW_LINE
Total number of ways to place X and Y at n places such that no two X are together | Function to return number of ways ; for n = 1 ; for n = 2 ; iterate to find Fibonacci term ; total number of places
def ways ( n ) : NEW_LINE INDENT first = 2 ; NEW_LINE second = 3 ; NEW_LINE res = 0 ; NEW_LINE for i in range ( 3 , n + 1 ) : NEW_LINE INDENT res = first + second ; NEW_LINE first = second ; NEW_LINE second = res ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT n = 7 ; NEW_LINE print ( " Total ▁ ways ▁ are : ▁ " , ways ( n ) ) ; NEW_LINE
Number of digits in N factorial to the power N | python3 program to find count of digits in N factorial raised to N ; we take sum of logarithms as explained in the approach ; multiply the result with n ; Driver Code
import math NEW_LINE def countDigits ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ans += math . log10 ( i ) NEW_LINE DEDENT ans = ans * n NEW_LINE return 1 + math . floor ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE print ( countDigits ( n ) ) NEW_LINE DEDENT
Program to convert centimeter into meter and kilometer | Python3 program to convert centimeter into meter and kilometer ; Converting centimeter into meter and kilometer ; Driver Code
cm = 1000 ; NEW_LINE meter = cm / 100.0 ; NEW_LINE kilometer = cm / 100000.0 ; NEW_LINE print ( " Length ▁ in ▁ meter ▁ = ▁ " , meter , " m " ) ; NEW_LINE print ( " Length ▁ in ▁ Kilometer ▁ = ▁ " , kilometer , " km " ) ; NEW_LINE
Check if two people starting from different points ever meet | Python3 program to find if two people starting from different positions ever meet or not . ; If speed of a person at a position before other person is smaller , then return false . ; Making sure that x1 is greater ; checking if relative speed is a factor of relative distance or not ;  ; Driver code
def everMeet ( x1 , x2 , v1 , v2 ) : NEW_LINE INDENT if ( x1 < x2 and v1 <= v2 ) : NEW_LINE INDENT return False ; NEW_LINE if ( x1 > x2 and v1 >= v2 ) : NEW_LINE INDENT return False ; NEW_LINE if ( x1 < x2 ) : NEW_LINE INDENT swap ( x1 , x2 ) NEW_LINE swap ( v1 , v2 ) NEW_LINE return ( ( x1 - x2 ) % ( v1 - v2 ) == 0 ) NEW_LINE def swap ( a , b ) : NEW_LINE INDENT t = a NEW_LINE a = b NEW_LINE b = t NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def main ( ) : NEW_LINE INDENT x1 , v1 , x2 , v2 = 5 , 8 , 4 , 7 NEW_LINE if ( everMeet ( x1 , x2 , v1 , v2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
Find ceil of a / b without using ceil ( ) function | Python3 program to find math . ceil ( a / b ) without using math . ceil ( ) function ; taking input 1 ; example of perfect division taking input 2
import math NEW_LINE a = 4 ; NEW_LINE b = 3 ; NEW_LINE val = ( a + b - 1 ) / b ; NEW_LINE print ( " The ▁ ceiling ▁ value ▁ of ▁ 4/3 ▁ is ▁ " , math . floor ( val ) ) ; NEW_LINE a = 6 ; NEW_LINE b = 3 ; NEW_LINE val = ( a + b - 1 ) / b ; NEW_LINE print ( " The ▁ ceiling ▁ value ▁ of ▁ 6/3 ▁ is ▁ " , math . floor ( val ) ) ; NEW_LINE
Sum of range in a series of first odd then even natural numbers | Python 3 program to find sum in the given range in the sequence 1 3 5 7 ... . . N 2 4 6. . . N - 1 ; Function that returns sum in the range 1 to x in the sequence 1 3 5 7. ... . N 2 4 6. . . N - 1 ; number of odd numbers ; number of extra even numbers required ; Driver code
import math NEW_LINE def sumTillX ( x , n ) : NEW_LINE INDENT odd = math . ceil ( n / 2.0 ) NEW_LINE if ( x <= odd ) : NEW_LINE INDENT return x * x ; NEW_LINE DEDENT even = x - odd ; NEW_LINE return ( ( odd * odd ) + ( even * even ) + even ) ; NEW_LINE DEDENT def rangeSum ( N , L , R ) : NEW_LINE INDENT return ( sumTillX ( R , N ) - sumTillX ( L - 1 , N ) ) ; NEW_LINE DEDENT N = 10 NEW_LINE L = 1 NEW_LINE R = 6 NEW_LINE print ( rangeSum ( N , L , R ) ) NEW_LINE
Twin Prime Numbers between 1 and n | Python program to illustrate ... To print total number of twin prime 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 ; check twin prime numbers display the twin prime numbers ; driver program ; Calling the function
def printTwinPrime ( n ) : NEW_LINE INDENT prime = [ True for i in range ( n + 2 ) ] NEW_LINE p = 2 NEW_LINE while ( p * p <= n + 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , n + 2 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT for p in range ( 2 , n - 1 ) : NEW_LINE INDENT if prime [ p ] and prime [ p + 2 ] : NEW_LINE INDENT print ( " ( " , p , " , " , ( p + 2 ) , " ) " , end = ' ' ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 25 NEW_LINE printTwinPrime ( n ) NEW_LINE DEDENT
Cube Free Numbers smaller than n | Python3 Program to print all cube free numbers smaller than or equal to n . ; Returns true if n is a cube free number , else returns false . ; check for all possible divisible cubes ; Print all cube free numbers smaller than n . ; Driver program
import math NEW_LINE def isCubeFree ( n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , int ( n ** ( 1 / 3 ) + 1 ) ) : NEW_LINE INDENT if ( n % ( i * i * i ) == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def printCubeFree ( n ) : NEW_LINE INDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( isCubeFree ( i ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT n = 20 NEW_LINE printCubeFree ( n ) NEW_LINE
Decimal Equivalent of Gray Code and its Inverse | Function to convert given decimal number of gray code into its inverse in decimal form ; Taking xor until n becomes zero ; Driver Code
def inversegrayCode ( n ) : NEW_LINE INDENT inv = 0 ; NEW_LINE while ( n ) : NEW_LINE INDENT inv = inv ^ n ; NEW_LINE n = n >> 1 ; NEW_LINE DEDENT return inv ; NEW_LINE DEDENT n = 15 ; NEW_LINE print ( inversegrayCode ( n ) ) ; NEW_LINE
Product of unique prime factors of a number | Python program to find product of unique prime factors of a number ; A function to print all prime factors of a given number n ; Handle prime factor 2 explicitly so that can optimally handle other prime factors . ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Driver Code
import math NEW_LINE def productPrimeFactors ( n ) : NEW_LINE INDENT product = 1 NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT product *= 2 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT n = n / 2 NEW_LINE DEDENT DEDENT for i in range ( 3 , int ( math . sqrt ( n ) ) + 1 , 2 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT product = product * i NEW_LINE while ( n % i == 0 ) : NEW_LINE INDENT n = n / i NEW_LINE DEDENT DEDENT DEDENT if ( n > 2 ) : NEW_LINE INDENT product = product * n NEW_LINE DEDENT return product NEW_LINE DEDENT n = 44 NEW_LINE print ( int ( productPrimeFactors ( n ) ) ) NEW_LINE
Maximizing Probability of one type from N containers | Returns the Maximum probability for Drawing 1 copy of number A from N containers with N copies each of numbers A and B ; Pmax = N / ( N + 1 ) ; 1. N = 1 ; 2. N = 2 ; 3. N = 10
def calculateProbability ( N ) : NEW_LINE INDENT probability = N / ( N + 1 ) NEW_LINE return probability NEW_LINE DEDENT N = 1 NEW_LINE probabilityMax = calculateProbability ( N ) NEW_LINE print ( " Maximum ▁ Probability ▁ for ▁ N ▁ = ▁ " , N , " is , ▁ % .4f " % probabilityMax ) NEW_LINE N = 2 NEW_LINE probabilityMax = calculateProbability ( N ) ; NEW_LINE print ( " Maximum ▁ Probability ▁ for ▁ N ▁ = " , N , " is , ▁ % .4f " % probabilityMax ) NEW_LINE N = 10 NEW_LINE probabilityMax = calculateProbability ( N ) ; NEW_LINE print ( " Maximum ▁ Probability ▁ for ▁ N ▁ = " , N , " is , ▁ % .4f " % probabilityMax ) NEW_LINE
Program to implement standard deviation of grouped data | Python Program to implement standard deviation of grouped data . ; Function to find mean of grouped data . ; Function to find standard deviation of grouped data . ; Formula to find standard deviation of grouped data . ; Declare and initialize the lower limit of interval . ; Declare and initialize the upper limit of interval . ; Calculating the size of array .
import math NEW_LINE def mean ( mid , freq , n ) : NEW_LINE INDENT sum = 0 NEW_LINE freqSum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum = sum + mid [ i ] * freq [ i ] NEW_LINE freqSum = freqSum + freq [ i ] NEW_LINE DEDENT return sum / freqSum NEW_LINE DEDENT def groupedSD ( lower_limit , upper_limit , freq , n ) : NEW_LINE INDENT mid = [ [ 0 ] for i in range ( 0 , n ) ] NEW_LINE sum = 0 NEW_LINE freqSum = 0 NEW_LINE sd = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT mid [ i ] = ( lower_limit [ i ] + upper_limit [ i ] ) / 2 NEW_LINE sum = sum + freq [ i ] * mid [ i ] * mid [ i ] NEW_LINE freqSum = freqSum + freq [ i ] NEW_LINE DEDENT sd = math . sqrt ( ( sum - freqSum * mean ( mid , freq , n ) * mean ( mid , freq , n ) ) / ( freqSum - 1 ) ) NEW_LINE return sd NEW_LINE DEDENT lower_limit = [ 50 , 61 , 71 , 86 , 96 ] NEW_LINE upper_limit = [ 60 , 70 , 85 , 95 , 100 ] NEW_LINE freq = [ 9 , 7 , 9 , 12 , 8 ] NEW_LINE n = len ( lower_limit ) NEW_LINE print ( groupedSD ( lower_limit , upper_limit , freq , n ) ) NEW_LINE
Average of first n even natural numbers | Function to find average of sum of first n even numbers ; sum of first n even numbers ; calculating Average ; driver code
def avg_of_even_num ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sum = sum + 2 * i NEW_LINE DEDENT return sum / n NEW_LINE DEDENT n = 9 NEW_LINE print ( avg_of_even_num ( n ) ) NEW_LINE
Average of first n even natural numbers | Return the average of sum of first n even numbers ; Driven Program
def avg_of_even_num ( n ) : NEW_LINE INDENT return n + 1 NEW_LINE DEDENT n = 8 NEW_LINE print ( avg_of_even_num ( n ) ) NEW_LINE
Sum of square of first n odd numbers | Python3 code to find sum of square of first n odd numbers ; driver code
def squareSum ( n ) : NEW_LINE INDENT return int ( n * ( 4 * n * n - 1 ) / 3 ) NEW_LINE DEDENT ans = squareSum ( 8 ) NEW_LINE print ( ans ) NEW_LINE