codeparrot/xlcost-text-to-code huggingface dataset:
Ulam Number Sequence | Python3 code to print nth Ulam number ; Array to store Ulam Number ; function to compute ulam Number ; Set to search specific Ulam number efficiently ; push First 2 two term of the sequence in the array and set for further calculation ; loop to generate Ulam number ; traverse the array and check if i can be represented as sum of two distinct element of the array ; Check if i - arr [ j ] exist in the array or not using set . If yes , Then i can be represented as sum of arr [ j ] + ( i - arr [ j ] ) ; if Count is greater than 2 break the loop ; If count is 2 that means i can be represented as sum of two distinct terms of the sequence ; i is ulam number ; Driver Code ; pre compute Ulam Number sequence ; print nth Ulam number
MAX = 10000 NEW_LINE arr = [ ] NEW_LINE def ulam ( ) : NEW_LINE INDENT s = set ( ) NEW_LINE arr . append ( 1 ) NEW_LINE s . add ( 1 ) NEW_LINE arr . append ( 2 ) NEW_LINE s . add ( 2 ) NEW_LINE for i in range ( 3 , MAX ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( 0 , len ( arr ) ) : NEW_LINE INDENT if ( i - arr [ j ] ) in s and arr [ j ] != ( i - arr [ j ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if count > 2 : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if count == 2 : NEW_LINE INDENT arr . append ( i ) NEW_LINE s . add ( i ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT ulam ( ) NEW_LINE n = 9 NEW_LINE print ( arr [ n - 1 ] ) NEW_LINE DEDENT
Hilbert Number | Utility function to return Nth Hilbert Number ; Driver code
def nthHilbertNumber ( n ) : NEW_LINE INDENT return 4 * ( n - 1 ) + 1 NEW_LINE DEDENT n = 5 NEW_LINE print ( nthHilbertNumber ( n ) ) NEW_LINE
Program to find the nth Kynea number | Function to calculate nth kynea number ; Firstly calculate 2 ^ n + 1 ; Now calculate ( 2 ^ n + 1 ) ^ 2 ; Now calculate ( ( 2 ^ n + 1 ) ^ 2 ) - 2 ; return nth Kynea number ; Driver Code ; print nth kynea number
def nthKyneaNumber ( n ) : NEW_LINE INDENT n = ( 1 << n ) + 1 NEW_LINE n = n * n NEW_LINE n = n - 2 NEW_LINE return n NEW_LINE DEDENT n = 2 NEW_LINE print ( nthKyneaNumber ( n ) ) NEW_LINE
Program to find the nth Kynea number | Function to calculate nth kynea number ; Calculate nth kynea number ; Driver Code ; print nth kynea number
def nthKyneaNumber ( n ) : NEW_LINE INDENT return ( ( 1 << ( 2 * n ) ) + ( 1 << ( n + 1 ) ) - 1 ) NEW_LINE DEDENT n = 2 NEW_LINE print ( nthKyneaNumber ( n ) ) NEW_LINE
Program to check whether a number is Proth number or not | Utility function to Check power of two ; Function to check if the Given number is Proth number or not ; check if k divides n or not ; Check if n / k is power of 2 or not ; update k to next odd number ; If we reach here means there exists no value of K Such that k is odd number and n / k is a power of 2 greater than k ; Get n ; Check n for Proth Number
def isPowerOfTwo ( n ) : NEW_LINE INDENT return ( n and ( not ( n & ( n - 1 ) ) ) ) NEW_LINE DEDENT def isProthNumber ( n ) : NEW_LINE INDENT k = 1 NEW_LINE while ( k < ( n // k ) ) : NEW_LINE INDENT if ( n % k == 0 ) : NEW_LINE INDENT if ( isPowerOfTwo ( n // k ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT k = k + 2 NEW_LINE DEDENT return False NEW_LINE int n = 25 ; NEW_LINE DEDENT if ( isProthNumber ( n - 1 ) ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT
Find last two digits of sum of N factorials | Function to find the unit ' s ▁ and ▁ ten ' s place digit ; Let us write for cases when N is smaller than or equal to 10 ; We know following ( 1 ! + 2 ! + 3 ! + 4 ! ... + 10 ! ) % 100 = 13 ( N >= 10 ) ; Driver Code
def get_last_two_digit ( N ) : NEW_LINE INDENT if N <= 10 : NEW_LINE INDENT ans = 0 NEW_LINE fac = 1 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT fac = fac * i NEW_LINE ans += fac NEW_LINE DEDENT ans = ans % 100 NEW_LINE return ans NEW_LINE DEDENT else : NEW_LINE INDENT return 13 NEW_LINE DEDENT DEDENT N = 1 NEW_LINE for N in range ( 1 , 11 ) : NEW_LINE INDENT print ( " For ▁ N ▁ = ▁ " , N , " : ▁ " , get_last_two_digit ( N ) , sep = ' ▁ ' ) NEW_LINE DEDENT
Check whether product of ' n ' numbers is even or odd | function to check whether product of ' n ' numbers is even or odd ; if a single even number is found , then final product will be an even number ; product is an odd number ; Driver Code
def isProductEven ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( ( arr [ i ] & 1 ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT arr = [ 2 , 4 , 3 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE if ( isProductEven ( arr , n ) ) : NEW_LINE INDENT print ( " Even " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Odd " ) NEW_LINE DEDENT
Sum of squares of Fibonacci numbers | Function to calculate sum of squares of Fibonacci numbers ; Initialize result ; Add remaining terms ; Driver Code
def calculateSquareSum ( n ) : NEW_LINE INDENT fibo = [ 0 ] * ( n + 1 ) ; NEW_LINE if ( n <= 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT fibo [ 0 ] = 0 ; NEW_LINE fibo [ 1 ] = 1 ; NEW_LINE sum = ( ( fibo [ 0 ] * fibo [ 0 ] ) + ( fibo [ 1 ] * fibo [ 1 ] ) ) ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT fibo [ i ] = ( fibo [ i - 1 ] + fibo [ i - 2 ] ) ; NEW_LINE sum += ( fibo [ i ] * fibo [ i ] ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT n = 6 ; NEW_LINE print ( " Sum ▁ of ▁ squares ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : " , calculateSquareSum ( n ) ) ; NEW_LINE
Value of the series ( 1 ^ 3 + 2 ^ 3 + 3 ^ 3 + ... + n ^ 3 ) mod 4 for a given n | function for obtaining the value of f ( n ) mod 4 ; Find the remainder of n when divided by 4 ; If n is of the form 4 k or 4 k + 3 ; If n is of the form 4 k + 1 or 4 k + 2 ; Driver code
def fnMod ( n ) : NEW_LINE INDENT rem = n % 4 NEW_LINE if ( rem == 0 or rem == 3 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( rem == 1 or rem == 2 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 6 NEW_LINE print ( fnMod ( n ) ) NEW_LINE DEDENT
Minimum increment operations to make the array in increasing order | function to find minimum moves required to make the array in increasing order ; to store answer ; iterate over an array ; non - increasing order ; add moves to answer ; increase the element ; return required answer ; Driver code
def MinimumMoves ( a , n , x ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if a [ i ] <= a [ i - 1 ] : NEW_LINE INDENT p = ( a [ i - 1 ] - a [ i ] ) // x + 1 NEW_LINE ans += p NEW_LINE a [ i ] += p * x NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 3 , 2 ] NEW_LINE x = 2 NEW_LINE n = len ( arr ) NEW_LINE print ( MinimumMoves ( arr , n , x ) ) NEW_LINE DEDENT
Check if a large number is divisible by 2 , 3 and 5 or not | function to return sum of digits of a number ; function to Check if a large number is divisible by 2 , 3 and 5 or not ; Driver code
def SumOfDigits ( str , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum += int ( ord ( str [ i ] ) - ord ( '0' ) ) NEW_LINE DEDENT return sum NEW_LINE DEDENT def Divisible ( str , n ) : NEW_LINE INDENT if ( ( SumOfDigits ( str , n ) % 3 == 0 and str [ n - 1 ] == '0' ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = "263730746028908374890" NEW_LINE n = len ( str ) NEW_LINE if ( Divisible ( str , n ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Count all the numbers in a range with smallest factor as K | Function to check if k is a prime number or not ; Corner case ; Check from 2 to n - 1 ; Function to check if a number is not divisible by any number between 2 and K - 1 ; to check if the num is divisible by any numbers between 2 and k - 1 ; if not divisible by any number between 2 and k - 1 but divisible by k ; Function to find count of numbers in range [ a , b ] with smallest factor as K ; a number can be divisible only by k and not by any number less than k only if k is a prime ; to check if a number has smallest factor as K ; Driver code
def isPrime ( k ) : NEW_LINE INDENT if ( k <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , k ) : NEW_LINE INDENT if ( k % i == 0 ) : NEW_LINE INDENT return false NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def check ( num , k ) : NEW_LINE INDENT flag = 1 NEW_LINE for i in range ( 2 , k ) : NEW_LINE INDENT if ( num % i == 0 ) : NEW_LINE INDENT flag = 0 NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT if ( num % k == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def findCount ( a , b , k ) : NEW_LINE INDENT count = 0 NEW_LINE if ( not isPrime ( k ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( a , b + 1 ) : NEW_LINE INDENT ans = check ( i , k ) NEW_LINE if ( ans == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT continue NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 2020 NEW_LINE b = 6300 NEW_LINE k = 29 NEW_LINE print ( findCount ( a , b , k ) ) NEW_LINE DEDENT
Largest number with the given set of N digits that is divisible by 2 , 3 and 5 | Function to find the largest integer with the given set ; find sum of all the digits look if any 0 is present or not ; if 0 is not present , the resultant number won 't be divisible by 5 ; sort all the elements in a non - decreasing manner ; if there is just one element 0 ; find the remainder of the sum of digits when divided by 3 ; there can a remainder as 1 or 2 ; traverse from the end of the digits ; first element which has the same remainder , remove it ; if there is no element which has a same remainder as y ; subtract it by 3 ( could be one or two ) ; delete two minimal digits which has a remainder as y ; print all the digits as a single integer ; Driver code ; initialize the number of set of digits ; initialize all the set of digits in a vector
def findLargest ( n , v ) : NEW_LINE INDENT flag = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( v [ i ] == 0 ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT sum += v [ i ] NEW_LINE DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( " Not ▁ possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT v . sort ( reverse = True ) NEW_LINE if ( v [ 0 ] == 0 ) : NEW_LINE INDENT print ( "0" ) NEW_LINE return 0 NEW_LINE DEDENT else : NEW_LINE INDENT flag = 0 NEW_LINE y = sum % 3 NEW_LINE if ( y != 0 ) : NEW_LINE INDENT i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( v [ i ] % 3 == y ) : NEW_LINE INDENT v . remove ( v [ i ] ) NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT if ( flag == 0 ) : NEW_LINE INDENT y = 3 - y NEW_LINE cnt = 0 NEW_LINE i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( v [ i ] % 3 == y ) : NEW_LINE INDENT v . remove ( v [ i ] ) NEW_LINE cnt += 1 NEW_LINE if ( cnt >= 2 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT i -= 1 NEW_LINE DEDENT DEDENT DEDENT for i in ( v ) : NEW_LINE print ( i , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 11 NEW_LINE v = [ 3 , 9 , 9 , 6 , 4 , 3 , 6 , 4 , 9 , 6 , 0 ] NEW_LINE findLargest ( n , v ) NEW_LINE DEDENT
Find the k | Python3 program to find the K - th smallest factor ; Function to find the k 'th divisor ; initialize vectors v1 and v2 ; store all the divisors in the two vectors accordingly ; reverse the vector v2 to sort it in increasing order ; if k is greater than the size of vectors then no divisor can be possible ; else print the ( k - 1 ) th value of vector ; If K is lying in first vector ; If K is lying in second vector ; Driver code
import math as mt NEW_LINE def findkth ( n , k ) : NEW_LINE INDENT v1 = list ( ) NEW_LINE v2 = list ( ) NEW_LINE for i in range ( 1 , mt . ceil ( n ** ( .5 ) ) ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT v1 . append ( i ) NEW_LINE if ( i != mt . ceil ( mt . sqrt ( n ) ) ) : NEW_LINE INDENT v2 . append ( n // i ) NEW_LINE DEDENT DEDENT DEDENT v2 [ : : - 1 ] NEW_LINE if ( k > ( len ( v1 ) + len ( v2 ) ) ) : NEW_LINE INDENT print ( " Doesn ' t ▁ Exist " , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( k <= len ( v1 ) ) : NEW_LINE INDENT print ( v1 [ k - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( v2 [ k - len ( v1 ) - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT n = 15 NEW_LINE k = 2 NEW_LINE findkth ( n , k ) NEW_LINE
Number of solutions for x < y , where a <= x <= b and c <= y <= d and x , y are integers | function to Find the number of solutions for x < y , where a <= x <= b and c <= y <= d and x , y integers . ; to store answer ; iterate explicitly over all possible values of x ; return answer ; Driver code ; function call
def NumberOfSolutions ( a , b , c , d ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( a , b + 1 ) : NEW_LINE INDENT if d >= max ( c , i + 1 ) : NEW_LINE INDENT ans += d - max ( c , i + 1 ) + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b , c , d = 2 , 3 , 3 , 4 NEW_LINE print ( NumberOfSolutions ( a , b , c , d ) ) NEW_LINE DEDENT
Minimum value possible of a given function from the given set | Function to find the value of F ( n ) ; Driver code
def findF_N ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = ans + ( i + 1 ) * ( n - i - 1 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 3 NEW_LINE print ( findF_N ( n ) ) NEW_LINE
Find N digits number which is divisible by D | Function to return N digits number which is divisible by D ; to store answer ; Driver code
def findNumber ( n , d ) : NEW_LINE INDENT ans = " " NEW_LINE if ( d != 10 ) : NEW_LINE INDENT ans += str ( d ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT ans += '0' NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT ans += " Impossible " NEW_LINE DEDENT else : NEW_LINE INDENT ans += '1' NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT ans += '0' NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 12 NEW_LINE d = 3 NEW_LINE print ( findNumber ( n , d ) ) NEW_LINE DEDENT
Count all the numbers less than 10 ^ 6 whose minimum prime factor is N | Python3 implementation of above approach ; the sieve of prime number and count of minimum prime factor ; form the prime sieve ; 1 is not a prime number ; form the sieve ; if i is prime ; if i is the least prime factor ; mark the number j as non prime ; count the numbers whose least prime factor is i ; form the sieve ; display ; display
MAX = 1000000 NEW_LINE sieve_Prime = [ 0 for i in range ( MAX + 4 ) ] NEW_LINE sieve_count = [ 0 for i in range ( MAX + 4 ) ] NEW_LINE def form_sieve ( ) : NEW_LINE INDENT sieve_Prime [ 1 ] = 1 NEW_LINE for i in range ( 2 , MAX + 1 ) : NEW_LINE INDENT if sieve_Prime [ i ] == 0 : NEW_LINE INDENT for j in range ( i * 2 , MAX + 1 , i ) : NEW_LINE INDENT if sieve_Prime [ j ] == 0 : NEW_LINE INDENT sieve_Prime [ j ] = 1 NEW_LINE sieve_count [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT form_sieve ( ) NEW_LINE n = 2 NEW_LINE print ( " Count ▁ = " , sieve_count [ n ] + 1 ) NEW_LINE n = 3 NEW_LINE print ( " Count ▁ = " , sieve_count [ n ] + 1 ) NEW_LINE
How to access elements of a Square Matrix | Python3 Program to read a square matrix and print the elements above secondary diagonal ; Get the square matrix ; Display the matrix ; Print the elements above secondary diagonal ; check for elements above secondary diagonal
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT matrix = [ [ 0 for i in range ( 5 ) ] for j in range ( 5 ) ] ; NEW_LINE row_index , column_index , x , size = 0 , 0 , 0 , 5 ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT x += 1 ; NEW_LINE matrix [ row_index ] [ column_index ] += x ; NEW_LINE DEDENT DEDENT print ( " The ▁ matrix ▁ is " ) ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT print ( matrix [ row_index ] [ column_index ] , end = " TABSYMBOL " ) ; NEW_LINE DEDENT print ( " " ) ; NEW_LINE DEDENT print ( " Elements above Secondary diagonal are : " ) ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT if ( ( row_index + column_index ) < size - 1 ) : NEW_LINE INDENT print ( matrix [ row_index ] [ column_index ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT
How to access elements of a Square Matrix | Python3 Program to read a square matrix and print the Corner Elements ; Get the square matrix ; Display the matrix ; Print the Corner elements ; check for corner elements
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT matrix = [ [ 0 for i in range ( 5 ) ] for j in range ( 5 ) ] NEW_LINE row_index , column_index , x , size = 0 , 0 , 0 , 5 ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT x += 1 ; NEW_LINE matrix [ row_index ] [ column_index ] = x ; NEW_LINE DEDENT DEDENT print ( " The ▁ matrix ▁ is " ) ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT print ( matrix [ row_index ] [ column_index ] , end = " TABSYMBOL " ) ; NEW_LINE DEDENT print ( " " ) ; NEW_LINE DEDENT print ( " Corner Elements are : " ) ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT if ( ( row_index == 0 or row_index == size - 1 ) and ( column_index == 0 or column_index == size - 1 ) ) : NEW_LINE INDENT print ( matrix [ row_index ] [ column_index ] , end = " TABSYMBOL " ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT
Find the largest good number in the divisors of given number N | function to return distinct prime factors ; to store distinct prime factors ; run a loop upto sqrt ( n ) ; place this prime factor in vector ; This condition is to handle the case when n is a prime number greater than 1 ; function that returns good number ; distinct prime factors ; to store answer ; product of all distinct prime factors is required answer ; Driver code ; function call
def PrimeFactors ( n ) : NEW_LINE INDENT v = [ ] NEW_LINE x = n NEW_LINE i = 2 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT v . append ( i ) NEW_LINE while ( x % i == 0 ) : NEW_LINE INDENT x //= i NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT if ( x > 1 ) : NEW_LINE INDENT v . append ( x ) NEW_LINE DEDENT return v NEW_LINE DEDENT def GoodNumber ( n ) : NEW_LINE INDENT v = PrimeFactors ( n ) NEW_LINE ans = 1 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT ans *= v [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 12 NEW_LINE print ( GoodNumber ( n ) ) NEW_LINE DEDENT
Find Largest Special Prime which is less than or equal to a given number | Function to check whether the number is a special prime or not ; While number is not equal to zero ; If the number is not prime return false . ; Else remove the last digit by dividing the number by 10. ; If the number has become zero then the number is special prime , hence return true ; Function to find the Largest Special Prime which is less than or equal to a given number ; Initially all numbers are considered Primes . ; There is always an answer possible ; Checking if the number is a special prime or not ; If yes print the number and break the loop . ; Else decrement the number . ; Driver code
def checkSpecialPrime ( sieve , num ) : NEW_LINE INDENT while ( num ) : NEW_LINE INDENT if ( not sieve [ num ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT num //= 10 NEW_LINE DEDENT return True NEW_LINE DEDENT def findSpecialPrime ( N ) : NEW_LINE INDENT sieve = [ True ] * ( N + 10 ) NEW_LINE sieve [ 0 ] = sieve [ 1 ] = False ; NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT if ( sieve [ i ] ) : NEW_LINE INDENT for j in range ( i * i , N + 1 , i ) : NEW_LINE INDENT sieve [ j ] = False NEW_LINE DEDENT DEDENT DEDENT while ( True ) : NEW_LINE INDENT if ( checkSpecialPrime ( sieve , N ) ) : NEW_LINE INDENT print ( N ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT N -= 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT findSpecialPrime ( 379 ) NEW_LINE findSpecialPrime ( 100 ) NEW_LINE DEDENT
Check if an integer can be expressed as a sum of two semi | Python3 Code to check if an integer can be expressed as sum of two semi - primes ; Utility function to compute semi - primes in a range ; Increment count of prime numbers ; If number is greater than 1 , add it to the count variable as it indicates the number remain is prime number ; if count is equal to '2' then number is semi - prime ; Utility function to check if a number sum of two semi - primes ; arr [ i ] is already a semi - prime if n - arr [ i ] is also a semi - prime then a number can be expressed as sum of two semi - primes ; Driver code
MAX = 10000 NEW_LINE arr = [ ] NEW_LINE sprime = [ False ] * ( MAX ) NEW_LINE def computeSemiPrime ( ) : NEW_LINE INDENT for i in range ( 2 , MAX ) : NEW_LINE INDENT cnt , num , j = 0 , i , 2 NEW_LINE while cnt < 2 and j * j <= num : NEW_LINE INDENT while num % j == 0 : NEW_LINE INDENT num /= j NEW_LINE cnt += 1 NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if num > 1 : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT if cnt == 2 : NEW_LINE INDENT sprime [ i ] = True NEW_LINE arr . append ( i ) NEW_LINE DEDENT DEDENT DEDENT def checkSemiPrime ( n ) : NEW_LINE INDENT i = 0 NEW_LINE while arr [ i ] <= n // 2 : NEW_LINE INDENT if sprime [ n - arr [ i ] ] == True : NEW_LINE INDENT return True NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT computeSemiPrime ( ) NEW_LINE n = 30 NEW_LINE if checkSemiPrime ( n ) == True : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Check if a number is a Pythagorean Prime or not | Utility function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver Code ; Check if number is prime and of the form 4 n + 1
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 i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = i + 6 NEW_LINE DEDENT return True NEW_LINE DEDENT n = 13 NEW_LINE if ( isPrime ( n ) and ( n % 4 == 1 ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Divide an isosceles triangle in two parts with ratio of areas as n : m | Python 3 program , to find height h which divide isosceles triangle into ratio n : m ; Function to return the height ; type cast the n , m into float ; calculate the height for cut ; Driver code
from math import sqrt NEW_LINE def heightCalculate ( H , n , m ) : NEW_LINE INDENT N = n * 1.0 NEW_LINE M = m * 1.0 NEW_LINE h = H * sqrt ( N / ( N + M ) ) NEW_LINE return h NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT H = 10 NEW_LINE n = 3 NEW_LINE m = 4 NEW_LINE print ( " { 0 : . 6 } " . format ( heightCalculate ( H , n , m ) ) ) ; NEW_LINE DEDENT