codeparrot/xlcost-text-to-code huggingface dataset:
Division without using ' / ' operator | Function to find division without using ' / ' operator ; Handling negative numbers ; if num1 is greater than equal to num2 subtract num2 from num1 and increase quotient by one . ; checking if neg equals to 1 then making quotient negative ; Driver program
def division ( num1 , num2 ) : NEW_LINE INDENT if ( num1 == 0 ) : return 0 NEW_LINE if ( num2 == 0 ) : return INT_MAX NEW_LINE negResult = 0 NEW_LINE if ( num1 < 0 ) : NEW_LINE INDENT num1 = - num1 NEW_LINE if ( num2 < 0 ) : NEW_LINE INDENT num2 = - num2 NEW_LINE DEDENT else : NEW_LINE INDENT negResult = true NEW_LINE DEDENT DEDENT elif ( num2 < 0 ) : NEW_LINE INDENT num2 = - num2 NEW_LINE negResult = true NEW_LINE DEDENT quotient = 0 NEW_LINE while ( num1 >= num2 ) : NEW_LINE INDENT num1 = num1 - num2 NEW_LINE quotient += 1 NEW_LINE DEDENT if ( negResult ) : NEW_LINE INDENT quotient = - quotient NEW_LINE DEDENT return quotient NEW_LINE DEDENT num1 = 13 ; num2 = 2 NEW_LINE print ( division ( num1 , num2 ) ) NEW_LINE
Nonagonal number | Function to find nonagonal number series . ; driver Function
def Nonagonal ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( int ( i * ( 7 * i - 5 ) / 2 ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 10 NEW_LINE Nonagonal ( n ) NEW_LINE
Find n | func for calualtion ; for summation of square of first n - natural nos . ; summation of first n natural nos . ; return result ; Driver Code
def seriesFunc ( n ) : NEW_LINE INDENT sumSquare = ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 NEW_LINE sumNatural = ( n * ( n + 1 ) / 2 ) NEW_LINE return ( sumSquare + sumNatural + 1 ) NEW_LINE DEDENT n = 8 NEW_LINE print ( int ( seriesFunc ( n ) ) ) NEW_LINE n = 13 NEW_LINE print ( int ( seriesFunc ( n ) ) ) NEW_LINE
Program to check Plus Perfect Number | Python 3 implementation to check if the number is plus perfect or not ; function to check plus perfect number ; calculating number of digits ; calculating plus perfect number ; checking whether number is plus perfect or not ; driver program
import math NEW_LINE def checkplusperfect ( x ) : NEW_LINE INDENT temp = x NEW_LINE n = 0 NEW_LINE while ( x != 0 ) : NEW_LINE INDENT x = x // 10 NEW_LINE n = n + 1 NEW_LINE DEDENT x = temp NEW_LINE sm = 0 NEW_LINE while ( x != 0 ) : NEW_LINE INDENT sm = sm + ( int ) ( math . pow ( x % 10 , n ) ) NEW_LINE x = x // 10 NEW_LINE DEDENT return ( sm == temp ) NEW_LINE DEDENT x = 9474 NEW_LINE if ( checkplusperfect ( x ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Number of distinct subsets of a set | Python3 program to count number of distinct subsets in an array of distinct numbers ; Returns 2 ^ n ; driver code
import math NEW_LINE def subsetCount ( arr , n ) : NEW_LINE INDENT return 1 << n NEW_LINE DEDENT A = [ 1 , 2 , 3 ] NEW_LINE n = len ( A ) NEW_LINE print ( subsetCount ( A , n ) ) NEW_LINE
Program to calculate GST from original and net prices | Python3 Program to compute GST from original and net prices . ; return value after calculate GST % ; Driver program to test above functions
def Calculate_GST ( org_cost , N_price ) : NEW_LINE INDENT return ( ( ( N_price - org_cost ) * 100 ) / org_cost ) ; NEW_LINE DEDENT org_cost = 100 NEW_LINE N_price = 120 NEW_LINE print ( " GST ▁ = ▁ " , end = ' ' ) NEW_LINE print ( round ( Calculate_GST ( org_cost , N_price ) ) , end = ' ' ) NEW_LINE print ( " % " ) NEW_LINE
Centered hexagonal number | Function to find centered hexagonal number ; Formula to calculate nth centered hexagonal ; Driver Code
def centeredHexagonalNumber ( n ) : NEW_LINE INDENT return 3 * n * ( n - 1 ) + 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE print ( n , " th ▁ centered ▁ hexagonal ▁ number : ▁ " , centeredHexagonalNumber ( n ) ) NEW_LINE DEDENT ' NEW_LINE
Find the distance covered to collect items at equal distances | function to calculate the distance ; main function
def find_distance ( n ) : NEW_LINE INDENT return n * ( ( 3 * n ) + 7 ) NEW_LINE DEDENT n = 5 NEW_LINE ans = find_distance ( n ) NEW_LINE print ( ans ) NEW_LINE
Twin Prime Numbers | Python3 code to check twin prime ; Function to check whether a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Returns true if n1 and n2 are twin primes ; Driver code
import math NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if n <= 1 : NEW_LINE INDENT return False NEW_LINE DEDENT if n <= 3 : NEW_LINE INDENT return True NEW_LINE DEDENT if n % 2 == 0 or n % 3 == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 5 , int ( math . sqrt ( n ) + 1 ) , 6 ) : NEW_LINE INDENT if n % i == 0 or n % ( i + 2 ) == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def twinPrime ( n1 , n2 ) : NEW_LINE INDENT return ( isPrime ( n1 ) and isPrime ( n2 ) and abs ( n1 - n2 ) == 2 ) NEW_LINE DEDENT n1 = 137 NEW_LINE n2 = 139 NEW_LINE if twinPrime ( n1 , n2 ) : NEW_LINE INDENT print ( " Twin ▁ Prime " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Twin ▁ Prime " ) NEW_LINE DEDENT
Sum of the sequence 2 , 22 , 222 , ... ... ... | Python3 code to find sum of series 2 , 22 , 222 , . . ; function which return the sum of series ; driver code
import math NEW_LINE def sumOfSeries ( n ) : NEW_LINE INDENT return 0.0246 * ( math . pow ( 10 , n ) - 1 - ( 9 * n ) ) NEW_LINE DEDENT n = 3 NEW_LINE print ( sumOfSeries ( n ) ) NEW_LINE
Find sum of even index binomial coefficients | Python program to find sum even indexed Binomial Coefficient ; Returns value of even indexed Binomial Coefficient Sum which is 2 raised to power n - 1. ; Driver method
import math NEW_LINE def evenbinomialCoeffSum ( n ) : NEW_LINE INDENT return ( 1 << ( n - 1 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE print evenbinomialCoeffSum ( n ) NEW_LINE DEDENT
Program to print triangular number series till n | Function to find triangular number ; For each iteration increase j by 1 and add it into k ; Increasing j by 1 ; Add value of j into k and update k ; Driven Code
def triangular_series ( n ) : NEW_LINE INDENT j = 1 NEW_LINE k = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( k , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT j = j + 1 NEW_LINE INDENT k = k + j NEW_LINE DEDENT n = 5 NEW_LINE triangular_series ( n ) 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 ; required sum ; Driver program to test above
def sumOfTheSeries ( n ) : NEW_LINE INDENT return int ( ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ) NEW_LINE DEDENT n = 5 NEW_LINE print ( " Sum ▁ = " , sumOfTheSeries ( n ) ) NEW_LINE
Sum of the series 1 + ( 1 + 2 ) + ( 1 + 2 + 3 ) + ( 1 + 2 + 3 + 4 ) + ... ... + ( 1 + 2 + 3 + 4 + ... + n ) | Function to find sum of series ; Driver Code
def sumOfSeries ( n ) : NEW_LINE INDENT return sum ( [ i * ( i + 1 ) / 2 for i in range ( 1 , n + 1 ) ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE print ( sumOfSeries ( n ) ) NEW_LINE DEDENT
Number of triangles after N moves | function to calculate number of triangles in Nth step ; Driver code
def numberOfTriangles ( n ) : NEW_LINE INDENT ans = 2 * ( pow ( 3 , n ) ) - 1 ; NEW_LINE return ans ; NEW_LINE DEDENT n = 2 NEW_LINE print ( numberOfTriangles ( n ) ) NEW_LINE
Motzkin number | Return the nth Motzkin Number . ; Base case ; Finding i - th Motzkin number . ; Driver code
def motzkin ( n ) : NEW_LINE INDENT dp = [ None ] * ( n + 1 ) NEW_LINE dp [ 0 ] = dp [ 1 ] = 1 ; NEW_LINE i = 2 NEW_LINE while i <= n : NEW_LINE INDENT dp [ i ] = ( ( 2 * i + 1 ) * dp [ i - 1 ] + ( 3 * i - 3 ) * dp [ i - 2 ] ) / ( i + 2 ) ; NEW_LINE i = i + 1 NEW_LINE DEDENT return dp [ n ] ; NEW_LINE DEDENT n = 8 NEW_LINE print ( motzkin ( n ) ) NEW_LINE
NicomachusÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¾¢ s Theorem ( Sum of k | Return the sum of kth group of positive odd integer . ; Driver code
def kthgroupsum ( k ) : NEW_LINE INDENT return k * k * k NEW_LINE DEDENT k = 3 NEW_LINE print ( kthgroupsum ( k ) ) NEW_LINE
Find x , y , z that satisfy 2 / n = 1 / x + 1 / y + 1 / z | function to find x y and z that satisfy given equation . ; driver code to test the above function
def printXYZ ( n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " x ▁ is ▁ " , n ) NEW_LINE print ( " y ▁ is ▁ " , n + 1 ) NEW_LINE print ( " z ▁ is ▁ " , n * ( n + 1 ) ) NEW_LINE DEDENT DEDENT n = 7 NEW_LINE printXYZ ( n ) NEW_LINE
Find n | Function to print nth term of series 1 3 6 10 ... . ; Driver code
def term ( n ) : NEW_LINE INDENT return n * ( n + 1 ) / 2 NEW_LINE DEDENT n = 4 NEW_LINE print term ( n ) NEW_LINE
Find Harmonic mean using Arithmetic mean and Geometric mean | Python 3 implementation of compution of arithmetic mean , geometric mean and harmonic mean ; Function to calculate arithmetic mean , geometric mean and harmonic mean ; Driver function
import math NEW_LINE def compute ( a , b ) : NEW_LINE INDENT AM = ( a + b ) / 2 NEW_LINE GM = math . sqrt ( a * b ) NEW_LINE HM = ( GM * GM ) / AM NEW_LINE return HM NEW_LINE DEDENT a = 5 NEW_LINE b = 15 NEW_LINE HM = compute ( a , b ) NEW_LINE print ( " Harmonic ▁ Mean ▁ between ▁ " , a , " ▁ and ▁ " , b , " ▁ is ▁ " , HM ) NEW_LINE
Find n | Returns n - th element of the series ; Driver Code
def series ( n ) : NEW_LINE INDENT print ( ( 8 * n ** 2 ) + 1 ) NEW_LINE DEDENT series ( 5 ) NEW_LINE
Check if a number is divisible by all prime divisors of another number | python program to find if all prime factors of y divide x . ; Returns true if all prime factors of y divide x . ; Driver Code
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT else : NEW_LINE INDENT return gcd ( b , a % b ) NEW_LINE DEDENT DEDENT def isDivisible ( x , y ) : NEW_LINE INDENT if ( y == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT z = gcd ( x , y ) ; NEW_LINE if ( z == 1 ) : NEW_LINE INDENT return false ; NEW_LINE DEDENT return isDivisible ( x , y / z ) ; NEW_LINE DEDENT x = 18 NEW_LINE y = 12 NEW_LINE if ( isDivisible ( x , y ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Program to find Sum of a Series a ^ 1 / 1 ! + a ^ 2 / 2 ! + a ^ 3 / 3 ! + a ^ 4 / 4 ! + Ã ¢ â ‚¬¦ Ã ¢ â ‚¬¦ . + a ^ n / n ! | Python program to print the sum of series . function to calculate sum of given series . ; multiply ( a / i ) to previous term ; store result in res ; Driver code
from __future__ import division NEW_LINE def sumOfSeries ( a , num ) : NEW_LINE INDENT res = 0 NEW_LINE prev = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT prev *= ( a / i ) NEW_LINE res = res + prev NEW_LINE DEDENT return res NEW_LINE DEDENT n = 5 NEW_LINE a = 2 NEW_LINE print ( round ( sumOfSeries ( a , n ) , 4 ) ) NEW_LINE
Program for Celsius To Fahrenheit conversion | Python code to convert Celsius scale to Fahrenheit scale ; Used the formula ; Driver Code
def Cel_To_Fah ( n ) : NEW_LINE INDENT return ( n * 1.8 ) + 32 NEW_LINE DEDENT n = 20 NEW_LINE print ( int ( Cel_To_Fah ( n ) ) ) NEW_LINE
Series with largest GCD and sum equals to n | Python3 code to find the series with largest GCD and sum equals to n ; stores the maximum gcd that can be possible of sequence . ; if maximum gcd comes out to be zero then not possible ; the smallest gcd possible is 1 ; traverse the array to find out the max gcd possible ; checks if the number is divisible or not ; x = x + 1 ; checks if x is smaller then the max gcd possible and x is greater then the resultant gcd till now , then r = x ; checks if n / x is smaller than the max gcd possible and n / x is greater then the resultant gcd till now , then r = x ; x = x + 1 ; traverses and prints d , 2d , 3d , ... , ( k - 1 ) d , ; main driver
def print_sequence ( n , k ) : NEW_LINE INDENT b = int ( n / ( k * ( k + 1 ) / 2 ) ) ; NEW_LINE if b == 0 : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT r = 1 ; NEW_LINE x = 1 NEW_LINE while x ** 2 <= n : NEW_LINE INDENT if n % x != 0 : NEW_LINE INDENT continue ; NEW_LINE DEDENT elif x <= b and x > r : NEW_LINE INDENT r = x NEW_LINE DEDENT elif n / x <= b and n / x > r : NEW_LINE INDENT r = n / x NEW_LINE DEDENT x = x + 1 NEW_LINE DEDENT i = 1 NEW_LINE while i < k : NEW_LINE INDENT print ( r * i , end = " ▁ " ) NEW_LINE i = i + 1 NEW_LINE DEDENT last_term = n - ( r * ( k * ( k - 1 ) / 2 ) ) NEW_LINE print ( last_term ) NEW_LINE DEDENT DEDENT print_sequence ( 24 , 4 ) NEW_LINE print_sequence ( 24 , 5 ) NEW_LINE print_sequence ( 6 , 4 ) NEW_LINE
Number of compositions of a natural number | Python code to find the total number of compositions of a natural number ; function to return the total number of composition of n ; Driver Code
def countCompositions ( n ) : NEW_LINE INDENT return ( 2 ** ( n - 1 ) ) NEW_LINE DEDENT print ( countCompositions ( 4 ) ) NEW_LINE
Program to count digits in an integer ( 4 Different Methods ) | Recursive Python program to count number of digits in a number ; Driver Code
def countDigit ( n ) : NEW_LINE INDENT if n / 10 == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 1 + countDigit ( n // 10 ) NEW_LINE DEDENT n = 345289467 NEW_LINE print ( " Number ▁ of ▁ digits ▁ : ▁ % ▁ d " % ( countDigit ( n ) ) ) NEW_LINE
Tribonacci Numbers | A DP based Python 3 program to print first n Tribonacci numbers . ; Driver code
def printTrib ( n ) : NEW_LINE INDENT dp = [ 0 ] * n NEW_LINE dp [ 0 ] = dp [ 1 ] = 0 ; NEW_LINE dp [ 2 ] = 1 ; NEW_LINE for i in range ( 3 , n ) : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + dp [ i - 3 ] ; NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( dp [ i ] , " ▁ " , end = " " ) NEW_LINE DEDENT DEDENT n = 10 NEW_LINE printTrib ( n ) NEW_LINE
Tribonacci Numbers | Program to print first n tribonacci numbers Matrix Multiplication function for 3 * 3 matrix ; Recursive function to raise the matrix T to the power n ; base condition . ; recursively call to square the matrix ; calculating square of the matrix T ; if n is odd multiply it one time with M ; base condition ; T [ 0 ] [ 0 ] contains the tribonacci number so return it ; Driver Code
def multiply ( T , M ) : NEW_LINE INDENT a = ( T [ 0 ] [ 0 ] * M [ 0 ] [ 0 ] + T [ 0 ] [ 1 ] * M [ 1 ] [ 0 ] + T [ 0 ] [ 2 ] * M [ 2 ] [ 0 ] ) NEW_LINE b = ( T [ 0 ] [ 0 ] * M [ 0 ] [ 1 ] + T [ 0 ] [ 1 ] * M [ 1 ] [ 1 ] + T [ 0 ] [ 2 ] * M [ 2 ] [ 1 ] ) NEW_LINE c = ( T [ 0 ] [ 0 ] * M [ 0 ] [ 2 ] + T [ 0 ] [ 1 ] * M [ 1 ] [ 2 ] + T [ 0 ] [ 2 ] * M [ 2 ] [ 2 ] ) NEW_LINE d = ( T [ 1 ] [ 0 ] * M [ 0 ] [ 0 ] + T [ 1 ] [ 1 ] * M [ 1 ] [ 0 ] + T [ 1 ] [ 2 ] * M [ 2 ] [ 0 ] ) NEW_LINE e = ( T [ 1 ] [ 0 ] * M [ 0 ] [ 1 ] + T [ 1 ] [ 1 ] * M [ 1 ] [ 1 ] + T [ 1 ] [ 2 ] * M [ 2 ] [ 1 ] ) NEW_LINE f = ( T [ 1 ] [ 0 ] * M [ 0 ] [ 2 ] + T [ 1 ] [ 1 ] * M [ 1 ] [ 2 ] + T [ 1 ] [ 2 ] * M [ 2 ] [ 2 ] ) NEW_LINE g = ( T [ 2 ] [ 0 ] * M [ 0 ] [ 0 ] + T [ 2 ] [ 1 ] * M [ 1 ] [ 0 ] + T [ 2 ] [ 2 ] * M [ 2 ] [ 0 ] ) NEW_LINE h = ( T [ 2 ] [ 0 ] * M [ 0 ] [ 1 ] + T [ 2 ] [ 1 ] * M [ 1 ] [ 1 ] + T [ 2 ] [ 2 ] * M [ 2 ] [ 1 ] ) NEW_LINE i = ( T [ 2 ] [ 0 ] * M [ 0 ] [ 2 ] + T [ 2 ] [ 1 ] * M [ 1 ] [ 2 ] + T [ 2 ] [ 2 ] * M [ 2 ] [ 2 ] ) NEW_LINE T [ 0 ] [ 0 ] = a NEW_LINE T [ 0 ] [ 1 ] = b NEW_LINE T [ 0 ] [ 2 ] = c NEW_LINE T [ 1 ] [ 0 ] = d NEW_LINE T [ 1 ] [ 1 ] = e NEW_LINE T [ 1 ] [ 2 ] = f NEW_LINE T [ 2 ] [ 0 ] = g NEW_LINE T [ 2 ] [ 1 ] = h NEW_LINE T [ 2 ] [ 2 ] = i NEW_LINE DEDENT def power ( T , n ) : NEW_LINE INDENT if ( n == 0 or n == 1 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT M = [ [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] ] NEW_LINE power ( T , n // 2 ) NEW_LINE multiply ( T , T ) NEW_LINE if ( n % 2 ) : NEW_LINE INDENT multiply ( T , M ) NEW_LINE DEDENT DEDENT def tribonacci ( n ) : NEW_LINE INDENT T = [ [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] ] NEW_LINE if ( n == 0 or n == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT power ( T , n - 2 ) NEW_LINE DEDENT return T [ 0 ] [ 0 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( tribonacci ( i ) , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT
Geometric mean ( Two Methods ) | Program to calculate the geometric mean of the given array elements . ; function to calculate geometric mean and return float value . ; declare sum variable and initialize it to 1. ; Compute the sum of all the elements in the array . ; compute geometric mean through formula antilog ( ( ( log ( 1 ) + log ( 2 ) + . . ... + log ( n ) ) / n ) and return the value to main function . ; Driver Code ; function call
import math NEW_LINE def geometricMean ( arr , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + math . log ( arr [ i ] ) ; NEW_LINE DEDENT sum = sum / n ; NEW_LINE return math . exp ( sum ) ; NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( geometricMean ( arr , n ) ) ; NEW_LINE
Smallest number k such that the product of digits of k is equal to n | Python3 implementation to find smallest number k such that the product of digits of k is equal to n ; function to find smallest number k such that the product of digits of k is equal to n ; if ' n ' is a single digit number , then it is the required number ; stack the store the digits ; repeatedly divide ' n ' by the numbers from 9 to 2 until all the numbers are used or ' n ' > 1 ; save the digit ' i ' that divides ' n ' onto the stack ; if true , then no number ' k ' can be formed ; pop digits from the stack ' digits ' and add them to 'k ; required smallest number ; Driver Code
import math as mt NEW_LINE def smallestNumber ( n ) : NEW_LINE INDENT if ( n >= 0 and n <= 9 ) : NEW_LINE INDENT return n NEW_LINE DEDENT digits = list ( ) NEW_LINE for i in range ( 9 , 1 , - 1 ) : NEW_LINE INDENT while ( n % i == 0 ) : NEW_LINE INDENT digits . append ( i ) NEW_LINE n = n // i NEW_LINE DEDENT DEDENT if ( n != 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT k = 0 NEW_LINE while ( len ( digits ) != 0 ) : NEW_LINE INDENT k = k * 10 + digits [ - 1 ] NEW_LINE digits . pop ( ) NEW_LINE DEDENT return k NEW_LINE DEDENT n = 100 NEW_LINE print ( smallestNumber ( n ) ) NEW_LINE
Check if a number is magic ( Recursive sum of digits is 1 ) | Python3 program to check if a number is Magic number . ; Note that the loop continues if n is 0 and sum is non - zero . It stops when n becomes 0 and sum becomes single digit . ; Return true if sum becomes 1. ; Driver code
def isMagic ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE while ( n > 0 or sum > 9 ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT n = sum ; NEW_LINE sum = 0 ; NEW_LINE DEDENT sum = sum + n % 10 ; NEW_LINE n = int ( n / 10 ) ; NEW_LINE DEDENT return True if ( sum == 1 ) else False ; NEW_LINE DEDENT n = 1234 ; NEW_LINE if ( isMagic ( n ) ) : NEW_LINE INDENT print ( " Magic ▁ Number " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ a ▁ magic ▁ Number " ) ; NEW_LINE DEDENT
Check if a number is magic ( Recursive sum of digits is 1 ) | Accepting sample input ; Condition to check Magic number
x = 1234 NEW_LINE if ( x % 9 == 1 ) : NEW_LINE INDENT print ( " Magic ▁ Number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ a ▁ Magic ▁ Number " ) NEW_LINE DEDENT
Rearrangement of a number which is also divisible by it | Perform hashing for given n ; perform hashing ; check whether any arrangement exists ; Create a hash for given number n The hash is of size 10 and stores count of each digit in n . ; check for all possible multipliers ; check hash table for both . ; Driver Code
def storeDigitCounts ( n , Hash ) : NEW_LINE INDENT while n > 0 : NEW_LINE INDENT Hash [ n % 10 ] += 1 NEW_LINE n //= 10 NEW_LINE DEDENT DEDENT def rearrange ( n ) : NEW_LINE INDENT hash_n = [ 0 ] * 10 NEW_LINE storeDigitCounts ( n , hash_n ) NEW_LINE for mult in range ( 2 , 10 ) : NEW_LINE INDENT curr = n * mult NEW_LINE hash_curr = [ 0 ] * 10 NEW_LINE storeDigitCounts ( curr , hash_curr ) NEW_LINE if hash_n == hash_curr : NEW_LINE INDENT return curr NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10035 NEW_LINE print ( rearrange ( n ) ) NEW_LINE DEDENT
Sylvester 's sequence | Python Code for Sylvester sequence ; To store the product . ; To store the current number . ; Loop till n . ; Driver program to test above function
def printSequence ( n ) : NEW_LINE a = 1 NEW_LINE ans = 2 NEW_LINE INDENT N = 1000000007 NEW_LINE i = 1 NEW_LINE while i <= n : NEW_LINE INDENT print ans , NEW_LINE ans = ( ( a % N ) * ( ans % N ) ) % N NEW_LINE a = ans NEW_LINE ans = ( ans + 1 ) % N NEW_LINE i = i + 1 NEW_LINE DEDENT DEDENT n = 6 NEW_LINE printSequence ( n ) NEW_LINE
Program to find sum of first n natural numbers | Returns sum of first n natural numbers ; Driver code
def findSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE x = 1 NEW_LINE while x <= n : NEW_LINE INDENT sum = sum + x NEW_LINE x = x + 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 5 NEW_LINE print findSum ( n ) NEW_LINE
Hailstone Numbers | function to print hailstone numbers and to calculate the number of steps required ; N is initially 1. ; N is reduced to 1. ; If N is Even . ; N is Odd . ; Driver Code ; Function to generate Hailstone Numbers ; Output : Number of Steps
def HailstoneNumbers ( N , c ) : NEW_LINE INDENT print ( N , end = " ▁ " ) NEW_LINE if ( N == 1 and c == 0 ) : NEW_LINE INDENT return c NEW_LINE DEDENT elif ( N == 1 and c != 0 ) : NEW_LINE INDENT c = c + 1 NEW_LINE DEDENT elif ( N % 2 == 0 ) : NEW_LINE INDENT c = c + 1 NEW_LINE c = HailstoneNumbers ( int ( N / 2 ) , c ) NEW_LINE DEDENT elif ( N % 2 != 0 ) : NEW_LINE INDENT c = c + 1 NEW_LINE c = HailstoneNumbers ( 3 * N + 1 , c ) NEW_LINE DEDENT return c NEW_LINE DEDENT N = 7 NEW_LINE x = HailstoneNumbers ( N , 0 ) NEW_LINE print ( " Number of Steps :   " , x ) NEW_LINE
Count number of digits after decimal on dividing a number | Python3 program to count digits after dot when a number is divided by another . ; ans = 0 Initialize result ; calculating remainder ; if this remainder appeared before then the numbers are irrational and would not converge to a solution the digits after decimal will be infinite ; Driver Code
def count ( x , y ) : NEW_LINE INDENT m = dict ( ) NEW_LINE while x % y != 0 : NEW_LINE INDENT x %= y NEW_LINE ans += 1 NEW_LINE if x in m : NEW_LINE INDENT return - 1 NEW_LINE DEDENT m [ x ] = 1 NEW_LINE x *= 10 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT res = count ( 1 , 2 ) NEW_LINE print ( " INF " ) if res == - 1 else print ( res ) NEW_LINE res = count ( 5 , 3 ) NEW_LINE print ( " INF " ) if res == - 1 else print ( res ) NEW_LINE res = count ( 3 , 5 ) NEW_LINE print ( " INF " ) if res == - 1 else print ( res ) NEW_LINE DEDENT
Find smallest number n such that n XOR n + 1 equals to given k . | function to return the required n ; if k is of form 2 ^ i - 1 ; driver program
def xorCalc ( k ) : NEW_LINE INDENT if ( k == 1 ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT if ( ( ( k + 1 ) & k ) == 0 ) : NEW_LINE INDENT return k / 2 NEW_LINE DEDENT return 1 ; NEW_LINE DEDENT k = 31 NEW_LINE print ( int ( xorCalc ( k ) ) ) NEW_LINE
Find n | Python3 program to find n - th number containing only 4 and 7. ; If n is odd , append 4 and move to parent ; If n is even , append7 and move to parent ; Reverse res and return . ; Driver code
def reverse ( s ) : NEW_LINE INDENT if len ( s ) == 0 : NEW_LINE INDENT return s NEW_LINE DEDENT else : NEW_LINE INDENT return reverse ( s [ 1 : ] ) + s [ 0 ] NEW_LINE DEDENT DEDENT def findNthNo ( n ) : NEW_LINE INDENT res = " " ; NEW_LINE while ( n >= 1 ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT res = res + "4" ; NEW_LINE n = ( int ) ( ( n - 1 ) / 2 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT res = res + "7" ; NEW_LINE n = ( int ) ( ( n - 2 ) / 2 ) ; NEW_LINE DEDENT DEDENT return reverse ( res ) ; NEW_LINE DEDENT n = 13 ; NEW_LINE print ( findNthNo ( n ) ) ; NEW_LINE