codeparrot/xlcost-text-to-code huggingface dataset:
Aspiring Number | Function to calculate sum of all proper divisors ; 1 is a proper divisor ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; Otherwise take both ; Calculate sum of all proper divisors only ; Function to get last number of Aliquot Sequence . ; Calculate next term from previous term ; Returns true if n is perfect ; To store sum of divisors ; Find all divisors and add them ; If sum of divisors is equal to n , then n is a perfect number ; Returns true if n is aspiring else returns false ; Checking condition for aspiring ; Driver Code
def getSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , int ( ( n ) ** ( 1 / 2 ) ) + 1 ) : NEW_LINE INDENT if not n % i : NEW_LINE if n // i == i : NEW_LINE INDENT sum += i NEW_LINE DEDENT else : NEW_LINE INDENT sum += i NEW_LINE sum += ( n // i ) NEW_LINE DEDENT DEDENT return sum - n NEW_LINE DEDENT def getAliquot ( n ) : NEW_LINE INDENT s = set ( ) NEW_LINE s . add ( n ) NEW_LINE next = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT n = getSum ( n ) NEW_LINE if n not in s : NEW_LINE return n NEW_LINE s . add ( n ) NEW_LINE DEDENT return 0 NEW_LINE DEDENT def isPerfect ( n ) : NEW_LINE INDENT sum = 1 NEW_LINE for i in range ( 2 , int ( ( n ** ( 1 / 2 ) ) ) + 1 ) : NEW_LINE INDENT if not n % i : NEW_LINE sum += ( i + n // i ) NEW_LINE DEDENT if sum == n and n != 1 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def isAspiring ( n ) : NEW_LINE INDENT alq = getAliquot ( n ) NEW_LINE if ( isPerfect ( alq ) and not isPerfect ( n ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT n = 25 NEW_LINE if ( isAspiring ( n ) ) : NEW_LINE INDENT print ( " Aspiring " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Aspiring " ) NEW_LINE DEDENT
Forming smallest array with given constraints | Return the size of smallest array with given constraint . ; Drivers code
def minimumLength ( x , y , z ) : NEW_LINE INDENT return ( 1 + abs ( x - y ) + abs ( y - z ) ) NEW_LINE DEDENT x = 3 NEW_LINE y = 1 NEW_LINE z = 2 NEW_LINE print ( minimumLength ( x , y , z ) ) NEW_LINE
Find the other | function to find the other - end point of diameter ; find end point for x coordinates ; find end point for y coordinates ; Driven Program
def endPointOfDiameterofCircle ( x1 , y1 , c1 , c2 ) : NEW_LINE INDENT print ( " x2 ▁ = " , ( 2 * c1 - x1 ) , end = " ▁ " ) NEW_LINE print ( " y2 ▁ = " , ( 2 * c2 - y1 ) ) NEW_LINE DEDENT x1 = - 4 NEW_LINE y1 = - 1 NEW_LINE c1 = 3 NEW_LINE c2 = 5 NEW_LINE endPointOfDiameterofCircle ( x1 , y1 , c1 , c2 ) NEW_LINE
Newton 's Divided Difference Interpolation Formula | Function to find the product term ; Function for calculating divided difference table ; Function for applying Newton 's  divided difference formula  ; Function for displaying divided difference table ; number of inputs given ; y [ ] [ ] is used for divided difference table where y [ ] [ 0 ] is used for input ; calculating divided difference table ; displaying divided difference table ; value to be interpolated ; printing the value
def proterm ( i , value , x ) : NEW_LINE INDENT pro = 1 ; NEW_LINE for j in range ( i ) : NEW_LINE INDENT pro = pro * ( value - x [ j ] ) ; NEW_LINE DEDENT return pro ; NEW_LINE DEDENT def dividedDiffTable ( x , y , n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( n - i ) : NEW_LINE INDENT y [ j ] [ i ] = ( ( y [ j ] [ i - 1 ] - y [ j + 1 ] [ i - 1 ] ) / ( x [ j ] - x [ i + j ] ) ) ; NEW_LINE DEDENT DEDENT return y ; NEW_LINE DEDENT def applyFormula ( value , x , y , n ) : NEW_LINE INDENT sum = y [ 0 ] [ 0 ] ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT sum = sum + ( proterm ( i , value , x ) * y [ 0 ] [ i ] ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def printDiffTable ( y , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n - i ) : NEW_LINE INDENT print ( round ( y [ i ] [ j ] , 4 ) , " TABSYMBOL " , end = " ▁ " ) ; NEW_LINE DEDENT print ( " " ) ; NEW_LINE DEDENT DEDENT n = 4 ; NEW_LINE y = [ [ 0 for i in range ( 10 ) ] for j in range ( 10 ) ] ; NEW_LINE x = [ 5 , 6 , 9 , 11 ] ; NEW_LINE y [ 0 ] [ 0 ] = 12 ; NEW_LINE y [ 1 ] [ 0 ] = 13 ; NEW_LINE y [ 2 ] [ 0 ] = 14 ; NEW_LINE y [ 3 ] [ 0 ] = 16 ; NEW_LINE y = dividedDiffTable ( x , y , n ) ; NEW_LINE printDiffTable ( y , n ) ; NEW_LINE value = 7 ; NEW_LINE print ( " Value at " , ▁ value , ▁ " is " , round ( applyFormula ( value , x , y , n ) , 2 ) ) NEW_LINE
Centered heptagonal number | Function to find Centered heptagonal number ; Formula to calculate nth Centered heptagonal number ; Driver Code
def centered_heptagonal_num ( n ) : NEW_LINE INDENT return ( 7 * n * n - 7 * n + 2 ) // 2 NEW_LINE DEDENT n = 5 NEW_LINE print ( " % sth ▁ Centered ▁ heptagonal ▁ number ▁ : ▁ " % n , centered_heptagonal_num ( n ) ) NEW_LINE
Sum of square | Function to find sum of sum of square of first n natural number ; Driven Program
def findSum ( n ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ = ( summ + ( ( i * ( i + 1 ) * ( 2 * i + 1 ) ) / 6 ) ) NEW_LINE DEDENT return summ NEW_LINE DEDENT n = 3 NEW_LINE print ( int ( findSum ( n ) ) ) NEW_LINE
Check if a given matrix is Hankel or not | Python 3 Program to check if given matrix is Hankel Matrix or not . ; Function to check if given matrix is Hankel Matrix or not . ; for each row ; for each column ; checking if i + j is less than n ; checking if the element is equal to the corresponding diagonal constant ; checking if the element is equal to the corresponding diagonal constant ; Drivers code
N = 4 NEW_LINE def checkHankelMatrix ( n , m ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT if ( i + j < n ) : NEW_LINE INDENT if ( m [ i ] [ j ] != m [ i + j ] [ 0 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( m [ i ] [ j ] != m [ i + j - n + 1 ] [ n - 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT DEDENT return True NEW_LINE DEDENT n = 4 NEW_LINE m = [ [ 1 , 2 , 3 , 5 , ] , [ 2 , 3 , 5 , 8 , ] , [ 3 , 5 , 8 , 0 , ] , [ 5 , 8 , 0 , 9 ] ] NEW_LINE ( print ( " Yes " ) if checkHankelMatrix ( n , m ) else print ( " No " ) ) NEW_LINE
Check if a number can be expressed as power | Set 2 ( Using Log ) | Python3 program to find if a number can be expressed as x raised to power y . ; Find Log n in different bases and check if the value is an integer ; Driver code
import math NEW_LINE def isPower ( n ) : NEW_LINE INDENT for x in range ( 2 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT f = math . log ( n ) / math . log ( x ) ; NEW_LINE if ( ( f - int ( f ) ) == 0.0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT for i in range ( 2 , 100 ) : NEW_LINE INDENT if ( isPower ( i ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT
Queries on sum of odd number digit sums of all the factors of a number | Python Program to answer queries on sum of sum of odd number digits of all the factors of a number ; finding sum of odd digit number in each integer . ; for each number ; using previous number sum , finding the current number num of odd digit also , adding last digit if it is odd . ; finding sum of sum of odd digit of all the factors of a number . ; for each possible factor ; adding the contribution . ; Wrapper def ; Driver Code
N = 100 NEW_LINE digitSum = [ 0 ] * N NEW_LINE factorDigitSum = [ 0 ] * N NEW_LINE def sumOddDigit ( ) : NEW_LINE INDENT global N , digitSum , factorDigitSum NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT digitSum [ i ] = ( digitSum [ int ( i / 10 ) ] + int ( i & 1 ) * ( i % 10 ) ) NEW_LINE DEDENT DEDENT def sumFactor ( ) : NEW_LINE INDENT global N , digitSum , factorDigitSum NEW_LINE j = 0 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT j = i NEW_LINE while ( j < N ) : NEW_LINE INDENT factorDigitSum [ j ] = ( factorDigitSum [ j ] + digitSum [ i ] ) NEW_LINE j = j + i NEW_LINE DEDENT DEDENT DEDENT def wrapper ( q , n ) : NEW_LINE INDENT global N , digitSum , factorDigitSum NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT digitSum [ i ] = 0 NEW_LINE factorDigitSum [ i ] = 0 NEW_LINE DEDENT sumOddDigit ( ) NEW_LINE sumFactor ( ) NEW_LINE for i in range ( 0 , q ) : NEW_LINE INDENT print ( " { } ▁ " . format ( factorDigitSum [ n [ i ] ] ) , end = " " ) NEW_LINE DEDENT DEDENT q = 2 NEW_LINE n = [ 10 , 36 ] NEW_LINE wrapper ( q , n ) NEW_LINE
Number of digits in the nth number made of given four digits | Efficient function to calculate number of digits in the nth number constructed by using 6 , 1 , 4 and 9 as digits in the ascending order . ; Number of digits increase after every i - th number where i increases in powers of 4. ; Driver Code
def number_of_digits ( n ) : NEW_LINE INDENT i = 4 NEW_LINE res = 1 NEW_LINE sum = 0 NEW_LINE while ( True ) : NEW_LINE INDENT i *= 4 NEW_LINE res += 1 NEW_LINE sum += i NEW_LINE if ( sum >= n ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT n = 21 NEW_LINE print ( number_of_digits ( n ) ) NEW_LINE
Print prime numbers from 1 to N in reverse order | Python3 program to print all primes between 1 to N in reverse order using Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers in reverse order ; static input ; to display
def Reverseorder ( n ) : NEW_LINE INDENT prime = [ True ] * ( n + 1 ) ; NEW_LINE p = 2 ; NEW_LINE while ( p * p <= n ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( ( p * 2 ) , ( n + 1 ) , p ) : NEW_LINE INDENT prime [ i ] = False ; NEW_LINE DEDENT DEDENT p += 1 ; NEW_LINE DEDENT for p in range ( n , 1 , - 1 ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT print ( p , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT N = 25 ; NEW_LINE print ( " Prime ▁ number ▁ in ▁ reverse ▁ order " ) ; NEW_LINE if ( N == 1 ) : NEW_LINE INDENT print ( " No ▁ prime ▁ no ▁ exist ▁ in ▁ this ▁ range " ) ; NEW_LINE DEDENT else : NEW_LINE
Vantieghems Theorem for Primality Test | Python3 code to verify Vantieghem 's Theorem ; Check if above condition is satisfied ; Product of previous powers of 2 ; Driver code
def checkVantieghemsTheorem ( limit ) : NEW_LINE INDENT prod = 1 NEW_LINE for n in range ( 2 , limit ) : NEW_LINE INDENT if n == 2 : NEW_LINE INDENT print ( 2 , " is ▁ prime " ) NEW_LINE DEDENT if ( ( ( prod - n ) % ( ( 1 << n ) - 1 ) ) == 0 ) : NEW_LINE INDENT print ( n , " is ▁ prime " ) NEW_LINE DEDENT prod *= ( ( 1 << n ) - 1 ) NEW_LINE DEDENT DEDENT checkVantieghemsTheorem ( 10 ) NEW_LINE
Count numbers formed by given two digit with sum having given digits | Python 3 program to count the number of numbers formed by digits a and b exactly of a length N such that the sum of the digits of the number thus formed is of digits a and b . ; function to check if sum of digits is made of a and b ; sum of digits is 0 ; if any of digits in sum is other than a and b ; calculate the modInverse V of a number in O ( log n ) ; q is quotient ; m is remainder now , process same as Euclid 's algo ; Update y and x ; Make x positive ; function to pregenerate factorials ; function to pre calculate the modInverse of factorials ; calculates the modInverse of the last factorial ; precalculates the modInverse of all factorials by formulae ; function that returns the value of nCi ; function that returns the count of numbers ; function call to pre - calculate the factorials and modInverse of factorials ; if a and b are same ; Driver Code
mod = 1000000007 NEW_LINE N = 1000005 NEW_LINE fact = [ 0 ] * N NEW_LINE invfact = [ 0 ] * N NEW_LINE def check ( x , a , b ) : NEW_LINE INDENT if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( x ) : NEW_LINE INDENT if ( x % 10 != a and x % 10 != b ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT x //= 10 NEW_LINE DEDENT return 1 NEW_LINE DEDENT def modInverse ( a , m ) : NEW_LINE INDENT m0 = m NEW_LINE y = 0 NEW_LINE x = 1 NEW_LINE if ( m == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( a > 1 ) : NEW_LINE INDENT q = a // m NEW_LINE t = m NEW_LINE m = a % m NEW_LINE a = t NEW_LINE t = y NEW_LINE y = x - q * y NEW_LINE x = t NEW_LINE DEDENT if ( x < 0 ) : NEW_LINE INDENT x += m0 NEW_LINE DEDENT return x NEW_LINE DEDENT def pregenFact ( ) : NEW_LINE INDENT fact [ 0 ] = fact [ 1 ] = 1 NEW_LINE for i in range ( 1 , 1000001 ) : NEW_LINE INDENT fact [ i ] = fact [ i - 1 ] * i % mod NEW_LINE DEDENT DEDENT def pregenInverse ( ) : NEW_LINE INDENT invfact [ 0 ] = invfact [ 1 ] = 1 NEW_LINE invfact [ 1000000 ] = modInverse ( fact [ 1000000 ] , mod ) NEW_LINE for i in range ( 999999 , 0 , - 1 ) : NEW_LINE INDENT invfact [ i ] = ( ( invfact [ i + 1 ] * ( i + 1 ) ) % mod ) NEW_LINE DEDENT DEDENT def comb ( big , small ) : NEW_LINE INDENT return ( fact [ big ] * invfact [ small ] % mod * invfact [ big - small ] % mod ) NEW_LINE DEDENT def count ( a , b , n ) : NEW_LINE INDENT pregenFact ( ) NEW_LINE pregenInverse ( ) NEW_LINE if ( a == b ) : NEW_LINE INDENT return ( check ( a * n , a , b ) ) NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT if ( check ( i * a + ( n - i ) * b , a , b ) ) : NEW_LINE INDENT ans = ( ans + comb ( n , i ) ) % mod NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 3 NEW_LINE b = 4 NEW_LINE n = 11028 NEW_LINE print ( count ( a , b , n ) ) NEW_LINE DEDENT
Finding n | Function to generate a fixed number ; Driver Code
def magicOfSequence ( N ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT sum += ( i * i * i + i * 2 ) NEW_LINE DEDENT return sum ; NEW_LINE DEDENT N = 4 NEW_LINE print ( magicOfSequence ( N ) ) NEW_LINE
Expressing a number as sum of consecutive | Set 2 ( Using odd factors ) | returns the number of odd factors ; If i is an odd factor and n is a perfect square ; If n is not perfect square ; N as sum of consecutive numbers
def countOddFactors ( n ) : NEW_LINE INDENT odd_factors = 0 NEW_LINE i = 1 NEW_LINE while ( ( 1 * i * i ) <= n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( 1 * i * i == n ) : NEW_LINE INDENT if ( i & 1 ) : NEW_LINE INDENT odd_factors = odd_factors + 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( ( i & 1 ) ) : NEW_LINE INDENT odd_factors = odd_factors + 1 NEW_LINE DEDENT factor = int ( n / i ) NEW_LINE if ( factor & 1 ) : NEW_LINE INDENT odd_factors = odd_factors + 1 NEW_LINE DEDENT DEDENT DEDENT i = i + 1 NEW_LINE DEDENT return odd_factors - 1 NEW_LINE DEDENT N = 15 NEW_LINE print ( countOddFactors ( N ) ) NEW_LINE N = 10 NEW_LINE print ( countOddFactors ( N ) ) NEW_LINE
Making zero array by decrementing pairs of adjacent | Python3 program to find if it is possible to make all array elements 0 by decrement operations . ; used for storing the sum of even and odd position element in array . ; if position is odd , store sum value of odd position in odd ; if position is even , store sum value of even position in even ; Driver Code
def isPossibleToZero ( a , n ) : NEW_LINE INDENT even = 0 ; NEW_LINE odd = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i & 1 ) : NEW_LINE INDENT odd += a [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT even += a [ i ] ; NEW_LINE DEDENT DEDENT return ( odd == even ) ; NEW_LINE DEDENT arr = [ 0 , 1 , 1 , 0 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE if ( isPossibleToZero ( arr , n ) ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT
Program for sum of cos ( x ) series | Python3 program to find the sum of cos ( x ) series ; here x is in degree . we have to convert it to radian for using it with series formula , as in series expansion angle is in radian ; Driver Code
PI = 3.142 ; NEW_LINE def cosXSertiesSum ( x , n ) : NEW_LINE INDENT x = x * ( PI / 180.0 ) ; NEW_LINE res = 1 ; NEW_LINE sign = 1 ; NEW_LINE fact = 1 ; NEW_LINE pow = 1 ; NEW_LINE for i in range ( 1 , 5 ) : NEW_LINE INDENT sign = sign * ( - 1 ) ; NEW_LINE fact = fact * ( 2 * i - 1 ) * ( 2 * i ) ; NEW_LINE pow = pow * x * x ; NEW_LINE res = res + sign * pow / fact ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT x = 50 ; NEW_LINE n = 5 ; NEW_LINE print ( round ( cosXSertiesSum ( x , 5 ) , 6 ) ) ; NEW_LINE
Sum of digits written in different bases from 2 to n | def to calculate sum of digit for a given base ; Sum of digits ; Calculating the number ( n ) by taking mod with the base and adding remainder to the result and parallelly reducing the num value . ; returning the result ; def calling for multiple bases ; Driver code
def solve ( n , base ) : NEW_LINE INDENT result = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT remainder = n % base NEW_LINE result = result + remainder NEW_LINE n = int ( n / base ) NEW_LINE DEDENT return result NEW_LINE DEDENT def printSumsOfDigits ( n ) : NEW_LINE INDENT for base in range ( 2 , n ) : NEW_LINE INDENT print ( solve ( n , base ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 8 NEW_LINE printSumsOfDigits ( n ) NEW_LINE
Possible two sets from first N natural numbers difference of sums as D | Function returns true if it is possible to split into two sets otherwise returns false ; Driver code
def check ( N , D ) : NEW_LINE INDENT temp = N * ( N + 1 ) // 2 + D NEW_LINE return ( bool ( temp % 2 == 0 ) ) NEW_LINE DEDENT N = 5 NEW_LINE M = 7 NEW_LINE if check ( N , M ) : NEW_LINE INDENT print ( " yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " no " ) NEW_LINE DEDENT
Minimum digits to remove to make a number Perfect Square | C ++ program to find required minimum digits need to remove to make a number perfect square ; function to check minimum number of digits should be removed to make this number a perfect square ; size of the string ; our final answer ; to store string which is perfect square . ; We make all possible subsequences ; to check jth bit is set or not . ; we do not consider a number with leading zeros ; convert our temporary string into integer ; checking temp is perfect square or not . ; taking maximum sized string ; print PerfectSquare ; Driver code
import math NEW_LINE def perfectSquare ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE ans = - 1 NEW_LINE num = " " NEW_LINE for i in range ( 1 , ( 1 << n ) ) : NEW_LINE INDENT str = " " NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT if ( ( i >> j ) & 1 ) : NEW_LINE INDENT str = str + s [ j ] NEW_LINE DEDENT DEDENT if ( str [ 0 ] != '0' ) : NEW_LINE INDENT temp = 0 ; NEW_LINE for j in range ( 0 , len ( str ) ) : NEW_LINE INDENT temp = ( temp * 10 + ( ord ( str [ j ] ) - ord ( '0' ) ) ) NEW_LINE DEDENT k = int ( math . sqrt ( temp ) ) NEW_LINE if ( k * k == temp ) : NEW_LINE INDENT if ( ans < len ( str ) ) : NEW_LINE INDENT ans = len ( str ) NEW_LINE num = str NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( ans == - 1 ) : NEW_LINE INDENT return ans NEW_LINE DEDENT else : NEW_LINE INDENT print ( " { } ▁ " . format ( num ) , end = " " ) NEW_LINE return n - ans NEW_LINE DEDENT DEDENT print ( perfectSquare ( "8314" ) ) NEW_LINE print ( perfectSquare ( "753" ) ) ; NEW_LINE
Lagrange 's four square theorem | Prints all the possible combinations 4 numbers whose sum of squares is equal to the given no . ; loops checking the sum of squares ; if sum of four squares equals the given no . ; printing the numbers ; Driver Code ; 74 = 0 * 0 + 0 * 0 + 5 * 5 + 7 * 7 74 = 0 * 0 + 1 * 1 + 3 * 3 + 8 * 8 74 = 0 * 0 + 3 * 3 + 4 * 4 + 7 * 7 74 = 1 * 1 + 1 * 1 + 6 * 6 + 6 * 6 74 = 2 * 2 + 3 * 3 + 5 * 5 + 6 * 6
def printFourSquares ( a ) : NEW_LINE INDENT i = 0 NEW_LINE while ( i * i <= a ) : NEW_LINE INDENT j = i NEW_LINE while ( j * j <= a ) : NEW_LINE INDENT k = j NEW_LINE while ( k * k <= a ) : NEW_LINE INDENT l = k NEW_LINE while ( l * l <= a ) : NEW_LINE INDENT if ( i * i + j * j + k * k + l * l == a ) : NEW_LINE INDENT print ( " { } ▁ = ▁ { } * { } ▁ + ▁ { } * { } ▁ + " . format ( a , i , i , j , j ) , end = " ▁ " ) NEW_LINE print ( " { } * { } ▁ + ▁ { } * { } " . format ( k , k , l , l ) , end = " " ) NEW_LINE DEDENT l = l + 1 NEW_LINE DEDENT k = k + 1 NEW_LINE DEDENT j = j + 1 NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT DEDENT a = 74 NEW_LINE printFourSquares ( a ) NEW_LINE
Hardy | Python3 program to count all prime factors ; A function to count prime factors of a given number n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; This condition is to handle the case when n is a prime number greater than 2 ; Driver Code
import math NEW_LINE def exactPrimeFactorCount ( n ) : NEW_LINE INDENT count = 0 NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT count = count + 1 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT n = int ( n / 2 ) NEW_LINE DEDENT DEDENT i = 3 NEW_LINE while ( i <= int ( math . sqrt ( n ) ) ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT count = count + 1 NEW_LINE while ( n % i == 0 ) : NEW_LINE INDENT n = int ( n / i ) NEW_LINE DEDENT DEDENT i = i + 2 NEW_LINE DEDENT if ( n > 2 ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT return count NEW_LINE DEDENT n = 51242183 NEW_LINE print ( " The ▁ number ▁ of ▁ distinct ▁ prime ▁ factors ▁ is / are ▁ { } " . format ( exactPrimeFactorCount ( n ) , end =   " " ) ) NEW_LINE print ( " The ▁ value ▁ of ▁ log ( log ( n ) ) ▁ is ▁ { 0 : . 4f } " . format ( math . log ( math . log ( n ) ) ) ) NEW_LINE
Number of Digits in a ^ b | Python Program to calculate no . of digits in a ^ b ; function to calculate number of digits in a ^ b ; Driver Program
import math NEW_LINE def no_of_digit ( a , b ) : NEW_LINE INDENT return ( ( int ) ( b * math . log10 ( a ) ) + 1 ) NEW_LINE DEDENT a = 2 NEW_LINE b = 100 NEW_LINE print ( " no ▁ of ▁ digits ▁ = ▁ " , no_of_digit ( a , b ) ) NEW_LINE
Check whether a number is Emirpimes or not | Checking whether a number is semi - prime or not ; Increment count of prime numbers ; If number is still greater than 1 , after exiting the add it to the count variable as it indicates the number is a prime number ; Return '1' if count is equal to '2' else return '0 ; Checking whether a number is emirpimes or not ; Number itself is not semiprime . ; Finding reverse of n . ; The definition of emirpimes excludes palindromes , hence we do not check further , if the number entered is a palindrome ; Checking whether the reverse of the semi prime number entered is also a semi prime number or not ; Driver Code
def checkSemiprime ( 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 DEDENT ' NEW_LINE INDENT return cnt == 2 ; NEW_LINE DEDENT def isEmirpimes ( n ) : NEW_LINE INDENT if ( checkSemiprime ( n ) == False ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT r = 0 ; NEW_LINE t = n ; NEW_LINE while ( t != 0 ) : NEW_LINE INDENT r = r * 10 + t % 10 ; NEW_LINE t = t / n ; NEW_LINE DEDENT if ( r == n ) : NEW_LINE INDENT return false ; NEW_LINE DEDENT return ( checkSemiprime ( r ) ) ; NEW_LINE DEDENT n = 15 ; NEW_LINE if ( isEmirpimes ( n ) ) : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT