codeparrot/xlcost-text-to-code huggingface dataset:
Super | Python3 implementation to check if N is a super Poulet number ; Function to find the divisors ; Loop to iterate over the square root of the N ; Check if divisors are equal ; Function to check if N is a super Poulet number ; Loop to check that every divisor divides 2 ^ D - 2 ; Driver Code
import math NEW_LINE def findDivisors ( n ) : NEW_LINE INDENT divisors = [ ] NEW_LINE for i in range ( 1 , int ( math . sqrt ( n ) + 1 ) ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( n / i == i ) : NEW_LINE INDENT divisors . append ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT divisors . append ( i ) NEW_LINE divisors . append ( int ( n / i ) ) NEW_LINE DEDENT DEDENT DEDENT return sorted ( divisors ) NEW_LINE DEDENT def isSuperdNum ( n ) : NEW_LINE INDENT d = findDivisors ( n ) NEW_LINE for i in d : NEW_LINE INDENT x = ( 2 ** i - 2 ) / i NEW_LINE if int ( x ) != x : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 341 NEW_LINE if isSuperdNum ( n ) == True : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Beatty sequence | Python3 implementation of the above approach ; Function to print the first N terms of the Beatty sequence ; Driver code
import math NEW_LINE def BeattySequence ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ans = math . floor ( i * math . sqrt ( 2 ) ) NEW_LINE print ( ans , end = ' , ▁ ' ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE BeattySequence ( n ) NEW_LINE
Sum of the first N Pronic Numbers | Function to calculate the sum ; Driver code
def calculateSum ( N ) : NEW_LINE INDENT return ( N * ( N - 1 ) // 2 + N * ( N - 1 ) * ( 2 * N - 1 ) // 6 ) ; NEW_LINE DEDENT N = 3 ; NEW_LINE print ( calculateSum ( N ) ) ; NEW_LINE
Self Numbers | Function to find the sum of digits of a number N ; Function to check for Self number ; Driver code
def getSum ( n ) : NEW_LINE INDENT sum1 = 0 ; NEW_LINE while ( n != 0 ) : NEW_LINE INDENT sum1 = sum1 + n % 10 ; NEW_LINE n = n // 10 ; NEW_LINE DEDENT return sum1 ; NEW_LINE DEDENT def isSelfNum ( n ) : NEW_LINE INDENT for m in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( m + getSum ( m ) == n ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT n = 20 ; NEW_LINE if ( isSelfNum ( n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Central binomial coefficient | Function to find the value of Nth Central Binomial Coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Driver code
def binomialCoeff ( n , k ) : NEW_LINE INDENT C = [ [ 0 for j in range ( k + 1 ) ] for i in range ( n + 1 ) ] NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , k ) + 1 ) : NEW_LINE INDENT if j == 0 or j == i : NEW_LINE INDENT C [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT return C [ n ] [ k ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE k = n NEW_LINE n = 2 * n NEW_LINE print ( binomialCoeff ( n , k ) ) NEW_LINE DEDENT
Multiply perfect number | Python3 implementation of the above approach ; Function to find the sum of divisors ; Note that this loop runs till square root of N ; If divisors are equal , take only one of them ; Otherwise take both ; Function to check Multiply - perfect number ; Driver code
import math NEW_LINE def getSum ( n ) : NEW_LINE INDENT sum1 = 0 ; NEW_LINE for i in range ( 1 , int ( math . sqrt ( n ) ) ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( n // i == i ) : NEW_LINE INDENT sum1 = sum1 + i ; NEW_LINE DEDENT else : NEW_LINE INDENT sum1 = sum1 + i ; NEW_LINE sum1 = sum1 + ( n // i ) ; NEW_LINE DEDENT DEDENT DEDENT return sum1 ; NEW_LINE DEDENT def MultiplyPerfectNumber ( n ) : NEW_LINE INDENT if ( getSum ( n ) % n == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT n = 28 ; NEW_LINE if ( MultiplyPerfectNumber ( n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Perfect totient number | Function to find the Totient number of the given value ; Initialize result as n ; Consider all prime factors of n and subtract their multiples from result ; Check if p is a prime factor . ; If yes , then update N and result ; If n has a prime factor greater than sqrt ( n ) ( There can be at - most one such prime factor ) ; Function to check if the number is a perfect totient number ; Store original value of n ; Loop to calculate sum of iterated totients ; Condition for Perfect Totient Number ; Driver Code
def phi ( n ) : NEW_LINE INDENT result = n NEW_LINE for p in range ( 2 , n ) : NEW_LINE INDENT if p * p > n : NEW_LINE INDENT break NEW_LINE DEDENT if ( n % p == 0 ) : NEW_LINE INDENT while ( n % p == 0 ) : NEW_LINE INDENT n //= p NEW_LINE DEDENT result -= result // p NEW_LINE DEDENT DEDENT if ( n > 1 ) : NEW_LINE INDENT result -= result // n NEW_LINE DEDENT return result NEW_LINE DEDENT def isPerfectTotientNum ( n ) : NEW_LINE INDENT temp = n NEW_LINE sum = 0 NEW_LINE while ( n > 1 ) : NEW_LINE INDENT sum = sum + phi ( n ) NEW_LINE n = phi ( n ) NEW_LINE DEDENT if ( sum == temp ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 9 NEW_LINE if ( isPerfectTotientNum ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Cunningham Numbers | Python3 implementation for the above approach ; Function to check if a number can be expressed as a ^ b . ; Function to check if N is a Cunningham number ; Given Number ; Function Call
import math NEW_LINE def isPower ( a ) : NEW_LINE INDENT if ( a == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT i = 2 NEW_LINE while ( i * i <= a ) : NEW_LINE INDENT val = math . log ( a ) / math . log ( i ) NEW_LINE if ( ( val - int ( val ) ) < 0.00000001 ) : NEW_LINE INDENT return True NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return False NEW_LINE DEDENT def isCunningham ( n ) : NEW_LINE INDENT return isPower ( n - 1 ) or isPower ( n + 1 ) NEW_LINE DEDENT n = 126 NEW_LINE if ( isCunningham ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Product of all Subarrays of an Array | Set 2 | Function to find the product of elements of all subarray ; Initialize the result ; Computing the product of subarray using formula ; Return the product of all elements of each subarray ; Given array arr [ ] ; Function Call
def SubArrayProdct ( arr , n ) : NEW_LINE INDENT result = 1 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT result *= pow ( arr [ i ] , ( i + 1 ) * ( n - i ) ) ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT arr = [ 2 , 4 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( SubArrayProdct ( arr , N ) ) NEW_LINE
Number of ways to reach ( M , N ) in a matrix starting from the origin without visiting ( X , Y ) | Function for computing nCr ; Function to find factorial of a number ; Function for counting the number of ways to reach ( m , n ) without visiting ( x , y ) ; Given dimensions of Matrix ; Cell not to be visited ; Function call
def nCr ( n , r ) : NEW_LINE INDENT return ( fact ( n ) // ( fact ( r ) * fact ( n - r ) ) ) NEW_LINE DEDENT def fact ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT return res NEW_LINE DEDENT def countWays ( m , n , x , y ) : NEW_LINE INDENT return ( nCr ( m + n , m ) - nCr ( x + y , x ) * nCr ( m + n - x - y , m - x ) ) NEW_LINE DEDENT m = 5 NEW_LINE n = 4 NEW_LINE x = 3 NEW_LINE y = 2 NEW_LINE print ( countWays ( m , n , x , y ) ) NEW_LINE
Smallest number greater than or equal to N using only digits 1 to K | Function to count the digits greater than K ; Function to print the list ; Function to find the number greater than or equal to n , which is only made of first k digits ; If the number itself satisfy the conditions ; Check digit from back ; If digit > K is present previously and current digit is less than K ; If current digit is greater than K ; If an extra digit needs to be added ; Print the number ; Driver Code
def CountGreater ( n , k ) : NEW_LINE INDENT a = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT if ( ( n % 10 ) > k ) : NEW_LINE INDENT a += 1 NEW_LINE DEDENT n = n // 10 NEW_LINE DEDENT return a NEW_LINE DEDENT def PrintList ( ans ) : NEW_LINE INDENT for i in ans : NEW_LINE INDENT print ( i , end = ' ' ) NEW_LINE DEDENT DEDENT def getNumber ( n , k ) : NEW_LINE INDENT count = CountGreater ( n , k ) NEW_LINE if ( count == 0 ) : NEW_LINE INDENT print ( n ) NEW_LINE return NEW_LINE DEDENT ans = [ ] NEW_LINE changed = False NEW_LINE while ( n > 0 ) : NEW_LINE INDENT digit = n % 10 NEW_LINE if ( changed == True ) : NEW_LINE INDENT ans . insert ( 0 , digit ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( count == 0 and digit < k ) : NEW_LINE INDENT ans . insert ( 0 , digit + 1 ) NEW_LINE changed = True NEW_LINE DEDENT else : NEW_LINE INDENT ans . insert ( 0 , 1 ) NEW_LINE if ( digit > k ) : NEW_LINE INDENT count -= 1 NEW_LINE DEDENT DEDENT DEDENT n = n // 10 NEW_LINE DEDENT if ( changed == False ) : NEW_LINE INDENT ans . insert ( 0 , 1 ) NEW_LINE DEDENT PrintList ( ans ) NEW_LINE return NEW_LINE DEDENT N = 51234 NEW_LINE K = 4 NEW_LINE getNumber ( N , K ) NEW_LINE
Find the Batting Average of a batsman | Function to find the average of a batsman ; Calculate number of dismissals ; check for 0 times out ; Calculate batting average ; Driver Program
def averageRuns ( runs , matches , notout ) : NEW_LINE INDENT out = matches - notout ; NEW_LINE if ( out == 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT avg = runs // out ; NEW_LINE return avg ; NEW_LINE DEDENT runs = 10000 ; NEW_LINE matches = 250 ; NEW_LINE notout = 50 ; NEW_LINE avg = averageRuns ( runs , matches , notout ) ; NEW_LINE if ( avg == - 1 ) : NEW_LINE INDENT print ( " NA " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( avg ) ; NEW_LINE DEDENT
Sum of series formed by difference between product and sum of N natural numbers | Recursive Function to calculate the sum upto Nth term ; If N - th term is calculated ; Update multi to store product upto K ; Update add to store sum upto K ; Update prevSum to store sum upto K ; Proceed to next K ; Function to calculate and return the Sum upto Nth term ; Recursive Function ; Driver code
def seriesSumUtil ( k , n , prevSum , multi , add ) : NEW_LINE INDENT if ( k == n + 1 ) : NEW_LINE INDENT return prevSum ; NEW_LINE DEDENT multi = multi * k ; NEW_LINE add = add + k ; NEW_LINE prevSum = prevSum + multi - add ; NEW_LINE return seriesSumUtil ( k + 1 , n , prevSum , multi , add ) ; NEW_LINE DEDENT def seriesSum ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT prevSum = 0 ; NEW_LINE multi = 1 ; NEW_LINE add = 1 ; NEW_LINE return seriesSumUtil ( 2 , n , prevSum , multi , add ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 ; NEW_LINE print ( seriesSum ( N ) ) ; NEW_LINE DEDENT
Count of total bits toggled / flipped in binary representation of 0 to N | Function to count and pr the required number of toggles ; Store the count of toggles ; Add the contribution of the current LSB ; Update N ; Print the result ; Driver code
def solve ( N ) : NEW_LINE INDENT ans = 0 NEW_LINE while ( N != 0 ) : NEW_LINE INDENT ans += N NEW_LINE N //= 2 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT N = 5 NEW_LINE solve ( N ) NEW_LINE
Maximum Bitwise AND pair ( X , Y ) from given range such that X and Y can be same | Function to return the maximum bitwise AND ; Driver code
def maximumAND ( L , R ) : NEW_LINE INDENT return R NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT l = 3 NEW_LINE r = 7 NEW_LINE print ( maximumAND ( l , r ) ) NEW_LINE DEDENT
Check if K distinct array elements form an odd sum | Function to return if odd sum is possible or not ; Stores distinct odd elements ; Stores distinct even elements ; Iterating through given array ; If element is even ; If element is odd ; If atleast K elements in the array are odd ; Check for all odd frequencies of odd elements whether sufficient even numbers are present or not ; Count of even numbers required ; If required even numbers are present in the array ; Driver Program
def oddSum ( A , N , K ) : NEW_LINE INDENT Odd = set ( [ ] ) NEW_LINE Even = set ( [ ] ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] % 2 == 0 ) : NEW_LINE INDENT Even . add ( A [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT Odd . add ( A [ i ] ) NEW_LINE DEDENT DEDENT if ( len ( Odd ) >= K ) : NEW_LINE INDENT return True NEW_LINE DEDENT flag = False NEW_LINE for i in range ( 1 , K , 2 ) : NEW_LINE INDENT needed = K - i NEW_LINE if ( needed <= len ( Even ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return flag NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT K = 5 NEW_LINE A = [ 12 , 1 , 7 , 7 , 26 , 18 ] NEW_LINE N = 3 NEW_LINE if ( oddSum ( A , N , K ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Count of elements not divisible by any other elements of Array | Function to count the number of elements of array which are not divisible by any other element of same array ; Length for boolean array ; Hash map for storing the element and it 's frequency ; Update the maximum element ; Boolean array of size of the max element + 1 ; Marking the multiples as false ; To store the final count ; Traverse boolean array ; Check if i is not divisible by any other array elements and appears in the array only once ; Return the final Count ; Driver Code ; Given array ; Function Call
def countEle ( a , n ) : NEW_LINE INDENT len = 0 NEW_LINE hmap = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT len = max ( len , a [ i ] ) NEW_LINE hmap [ a [ i ] ] = hmap . get ( a [ i ] , 0 ) + 1 NEW_LINE DEDENT v = [ True for i in range ( len + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( v [ a [ i ] ] == False ) : NEW_LINE INDENT continue NEW_LINE DEDENT for j in range ( 2 * a [ i ] , len + 1 , a [ i ] ) : NEW_LINE INDENT v [ j ] = False NEW_LINE DEDENT DEDENT count = 0 NEW_LINE for i in range ( 1 , len + 1 ) : NEW_LINE INDENT if ( v [ i ] == True and ( i in hmap ) and hmap [ i ] == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 86 , 45 , 18 , 4 , 8 , 28 , 19 , 33 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countEle ( arr , n ) ) NEW_LINE DEDENT
Smallest N digit number divisible by N | Function to find the smallest N - digit number divisible by N ; Find largest n digit number ; Find smallest n digit number ; If i is divisible by N , then print i and return ; ; Driver Code ; Given number ; Function call
def smallestNumber ( N ) : NEW_LINE INDENT L = pow ( 10 , N ) - 1 ; NEW_LINE S = pow ( 10 , N - 1 ) ; NEW_LINE for i in range ( S , L ) : NEW_LINE INDENT if ( i % N == 0 ) : NEW_LINE INDENT print ( i ) ; NEW_LINE return ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 ; NEW_LINE smallestNumber ( N ) NEW_LINE DEDENT
Number less than equals to N with maximum product of prime factors | Function to find the smallest number having a maximum product of prime factors ; Declare the list arr ; Initialise list with 1 ; Iterate from [ 2 , N ] ; If value at index i is 1 , then i is prime and make update list at index for all multiples of i ; Initialise maxValue ; Find number having maximum product of prime factor <= N ; Find the maximum value ; Given number ; Function call
def maxPrimefactorNum ( N ) : NEW_LINE INDENT arr = [ ] NEW_LINE for i in range ( N + 1 ) : NEW_LINE INDENT arr . append ( 1 ) NEW_LINE DEDENT for i in range ( 2 , N + 1 ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT for j in range ( i , N + 1 , i ) : NEW_LINE INDENT arr [ j ] *= i NEW_LINE DEDENT DEDENT DEDENT maxValue = 1 NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT if ( arr [ i ] > maxValue ) : NEW_LINE INDENT maxValue = i NEW_LINE DEDENT DEDENT return maxValue NEW_LINE DEDENT N = 20 ; NEW_LINE print ( maxPrimefactorNum ( N ) ) NEW_LINE
Zuckerman Numbers | Function to get product of digits ; Function to check if N is an Zuckerman number ; Driver code
def getProduct ( n ) : NEW_LINE INDENT product = 1 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT product = product * ( n % 10 ) NEW_LINE n = n // 10 NEW_LINE DEDENT return product NEW_LINE DEDENT def isZuckerman ( n ) : NEW_LINE INDENT return n % getProduct ( n ) == 0 NEW_LINE DEDENT N = 115 NEW_LINE if ( isZuckerman ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Additive Prime Number | Check if N is prime or not ; Corner Cases ; This is checked to skip middle five numbers ; Function to get sum of digits ; Return the sum of digits ; Function to check whether the given number is Additive Prime number or not ; If number is not prime ; Check if sum of digits is prime or not ; Given Number N ; Function Call
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 def getSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT sum = sum + n % 10 NEW_LINE n = n / 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def isAdditivePrime ( n ) : NEW_LINE INDENT if ( not isPrime ( n ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT return isPrime ( getSum ( n ) ) NEW_LINE DEDENT N = 23 NEW_LINE if ( isAdditivePrime ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Straight | Function to check if N is a Straight Line number or not ; N must be > 99 ; Difference between consecutive digits must be same ; Given Number N ; Function Call
def isStraighLineNum ( N ) : NEW_LINE INDENT if ( N <= 99 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT str1 = str ( N ) ; NEW_LINE d = int ( str1 [ 1 ] ) - int ( str1 [ 0 ] ) ; NEW_LINE for i in range ( 2 , len ( str1 ) ) : NEW_LINE INDENT if ( int ( str1 [ i ] ) - int ( str1 [ i - 1 ] ) != d ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT N = 135 ; NEW_LINE if ( isStraighLineNum ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Second | Function to find N - th term in the series ; Driver Code
def findNthTerm ( n ) : NEW_LINE INDENT print ( pow ( 2 , n ) - 2 * n ) ; NEW_LINE DEDENT N = 4 ; NEW_LINE findNthTerm ( N ) ; NEW_LINE
Alternating Numbers | Function to check if a string is of the form even odd even odd ... ; Function to check if a string is of the form odd even odd even ... ; Function to check if n is an alternating number ; Given Number N ; Function Call
def isEvenOddForm ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i % 2 == 0 and int ( s [ i ] ) % 2 != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( i % 2 == 1 and int ( s [ i ] ) % 2 != 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isOddEvenForm ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i % 2 == 0 and int ( s [ i ] ) % 2 != 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( i % 2 == 1 and int ( s [ i ] ) % 2 != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isAlternating ( n ) : NEW_LINE INDENT s = str ( n ) NEW_LINE return ( isEvenOddForm ( s ) or isOddEvenForm ( s ) ) NEW_LINE DEDENT N = 129 NEW_LINE if ( isAlternating ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Ormiston prime Pairs | Function to check if the number is a prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to update the frequency array such that freq [ i ] stores the frequency of digit i in n ; While there are digits left to process ; Update the frequency of the current digit ; Remove the last digit ; Function that returns true if a and b are anagrams of each other ; To store the frequencies of the digits in a and b ; Update the frequency of the digits in a ; Update the frequency of the digits in b ; Match the frequencies of the common digits ; If frequency differs for any digit then the numbers are not anagrams of each other ; Returns true if n1 and n2 are Ormiston primes ; Driver code
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 TEN = 10 NEW_LINE def updateFreq ( n , freq ) : NEW_LINE INDENT while ( n ) : NEW_LINE INDENT digit = n % TEN NEW_LINE freq [ digit ] += 1 NEW_LINE n = n // TEN NEW_LINE DEDENT DEDENT def areAnagrams ( a , b ) : NEW_LINE INDENT freqA = [ 0 ] * TEN NEW_LINE freqB = [ 0 ] * TEN NEW_LINE updateFreq ( a , freqA ) NEW_LINE updateFreq ( b , freqB ) NEW_LINE for i in range ( TEN ) : NEW_LINE INDENT if ( freqA [ i ] != freqB [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def OrmistonPrime ( n1 , n2 ) : NEW_LINE INDENT return ( isPrime ( n1 ) and isPrime ( n2 ) and areAnagrams ( n1 , n2 ) ) NEW_LINE DEDENT n1 , n2 = 1913 , 1931 NEW_LINE if ( OrmistonPrime ( n1 , n2 ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Decakismyriagon Number | Function to find the N - th Decakismyriagon Number ; Given Number N ; Function Call
def DecakismyriagonNum ( N ) : NEW_LINE INDENT return ( 99998 * N * N - 99996 * N ) // 2 ; NEW_LINE DEDENT N = 3 ; NEW_LINE print ( DecakismyriagonNum ( N ) ) ; NEW_LINE
Zygodrome Number | Function to check if N is an zygodrome number ; Convert N to string ; Adding a space at the beginning and end of the string ; Traverse the string ; If any character is not same as prev and next then return false ; Driver code
def iszygodromeNum ( N ) : NEW_LINE INDENT s = str ( N ) ; NEW_LINE s = ' ▁ ' + s + ' ▁ ' ; NEW_LINE i = 1 NEW_LINE while i < len ( s ) - 1 : NEW_LINE INDENT if ( ( s [ i ] != s [ i - 1 ] ) and ( s [ i ] != s [ i + 1 ] ) ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return True ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 1122 ; NEW_LINE if iszygodromeNum ( n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Loeschian Number | Python3 program for the above approach ; Function to check if N is a Loeschian Number ; Iterate [ 0 , sqrt ( N ) ] for x ; Iterate [ 0 , sqrt ( N ) ] for y ; Check the given criteria ; If no such pair found then return false ; Given Number N ; Function Call
import math NEW_LINE def isLoeschian ( n ) : NEW_LINE INDENT for x in range ( 1 , ( int ) ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT for y in range ( 1 , ( int ) ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( x * x + x * y + y * y == n ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT N = 19 NEW_LINE if ( isLoeschian ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT