codeparrot/xlcost-text-to-code huggingface dataset:
Count of subarrays whose sum is a perfect square | Python3 code for the above approach . ; Function to find count of subarrays whose sum is a perfect square . ; To search for index with ( current prefix sum - j * j ) ; Storing the prefix sum ; Used to track the minimum value in prefixSum ; Calculating the prefixSum and tracking the prefixMin ; Below statement is used if array contains negative numbers ; Counts the no of subarrays with perfect square sum ; As 0 is a perfect square , so we initialize 0 th index - key with value 1 ; Here we count the perfect square subarray sum by searching if there is a prefix with sum = ( current prefixSum - ( sq * sq ) ) ; Increasing our subarray count ; Increasing the current prefixSum index value in map by 1 to count the other perfect squares while traversing further ; Driver code ; Printing the result
from collections import defaultdict NEW_LINE def countSubarrays ( arr , n ) : NEW_LINE INDENT mp = defaultdict ( lambda : 0 ) NEW_LINE prefixSum = [ 0 ] * n NEW_LINE prefixMin = 0 NEW_LINE prefixSum [ 0 ] = arr [ 0 ] NEW_LINE prefixMin = min ( prefixMin , prefixSum [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] NEW_LINE prefixMin = min ( prefixMin , prefixSum [ i ] ) NEW_LINE DEDENT countSubs = 0 NEW_LINE mp [ 0 ] = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT j = 0 NEW_LINE while prefixSum [ i ] - j * j >= prefixMin : NEW_LINE INDENT if prefixSum [ i ] - j * j in mp : NEW_LINE INDENT countSubs += mp [ prefixSum [ i ] - j * j ] NEW_LINE DEDENT j += 1 NEW_LINE DEDENT mp [ prefixSum [ i ] ] += 1 NEW_LINE DEDENT return countSubs NEW_LINE DEDENT arr = [ 2 , 3 , - 5 , 6 , - 7 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE ans = countSubarrays ( arr , n ) NEW_LINE print ( ans ) NEW_LINE
Find if two given Quadratic equations have common roots or not | Function to check if 2 quadratic equations have common roots or not . ; Driver code
def checkSolution ( a1 , b1 , c1 , a2 , b2 , c2 ) : NEW_LINE INDENT return ( ( a1 / a2 ) == ( b1 / b2 ) and ( b1 / b2 ) == ( c1 / c2 ) ) NEW_LINE DEDENT a1 , b1 , c1 = 1 , - 5 , 6 NEW_LINE a2 , b2 , c2 = 2 , - 10 , 12 NEW_LINE if ( checkSolution ( a1 , b1 , c1 , a2 , b2 , c2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Check if roots of a Quadratic Equation are numerically equal but opposite in sign or not | Function to find the required answer ; Driver code
def checkSolution ( a , b , c ) : NEW_LINE INDENT if b == 0 : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT a = 2 NEW_LINE b = 0 NEW_LINE c = 2 NEW_LINE checkSolution ( a , b , c ) NEW_LINE
Count of index pairs in array whose range product is a positive integer | Python3 program to find the count of index pairs in the array positive range product ; Condition if number of negative elements is even then increase even_count ; Otherwise increase odd_count ; Condition if current element is negative ; Condition if number of negative elements is even then add even_count in answer ; Otherwise add odd_count in answer ; Driver Code
def positiveProduct ( arr , n ) : NEW_LINE INDENT even_count = 0 NEW_LINE odd_count = 0 NEW_LINE total_count = 0 NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( total_count % 2 == 0 ) : NEW_LINE INDENT even_count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd_count += 1 NEW_LINE DEDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT total_count += 1 NEW_LINE DEDENT if ( total_count % 2 == 0 ) : NEW_LINE INDENT ans += even_count NEW_LINE DEDENT else : NEW_LINE INDENT ans += odd_count NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 5 , - 3 , 3 , - 1 , 1 ] NEW_LINE size = len ( A ) NEW_LINE positiveProduct ( A , size ) NEW_LINE DEDENT
Pair of integers having difference of their fifth power as X | Python3 implementation to find a pair of integers A & B such that difference of fifth power is equal to the given number X ; Function to find a pair of integers A & B such that difference of fifth power is equal to the given number X ; Loop to choose every possible pair with in the range ; Check if equation holds ; Driver Code ; Function Call
import math NEW_LINE def findPair ( x ) : NEW_LINE INDENT lim = 120 NEW_LINE for i in range ( - lim , lim + 1 ) : NEW_LINE INDENT for j in range ( - lim , lim + 1 ) : NEW_LINE INDENT if ( math . pow ( i , 5 ) - math . pow ( j , 5 ) == x ) : NEW_LINE INDENT print ( i , end = ' ▁ ' ) NEW_LINE print ( j , end =   ' ' ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT print ( " - 1" ) NEW_LINE DEDENT X = 33 NEW_LINE findPair ( X ) NEW_LINE
Sum of GCD of all numbers upto N with N itself | Function to Find Sum of GCD of each numbers ; Consider all prime factors of no . and subtract their multiples from result ; Check if p is a prime factor ; If yes , then update no and result ; If no has a prime factor greater than Math . sqrt ( n ) then at - most one such prime factor exists ; Return the result ; Finding GCD of pairs ; Calculate the divisors ; Return count of numbers from 1 to N with GCD d with N ; Check if d1 and d2 are equal then skip this ; Driver code
def getCount ( d , n ) : NEW_LINE INDENT no = n // d ; NEW_LINE result = no ; NEW_LINE for p in range ( 2 , int ( pow ( no , 1 / 2 ) ) + 1 ) : NEW_LINE INDENT if ( no % p == 0 ) : NEW_LINE INDENT while ( no % p == 0 ) : NEW_LINE INDENT no //= p ; NEW_LINE DEDENT result -= result // p ; NEW_LINE DEDENT DEDENT if ( no > 1 ) : NEW_LINE INDENT result -= result // no ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT def sumOfGCDofPairs ( n ) : NEW_LINE INDENT res = 0 ; NEW_LINE for i in range ( 1 , int ( pow ( n , 1 / 2 ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT d1 = i ; NEW_LINE d2 = n // i ; NEW_LINE res += d1 * getCount ( d1 , n ) ; NEW_LINE if ( d1 != d2 ) : NEW_LINE INDENT res += d2 * getCount ( d2 , n ) ; NEW_LINE DEDENT DEDENT DEDENT return res ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 12 ; NEW_LINE print ( sumOfGCDofPairs ( n ) ) ; NEW_LINE DEDENT
Check whether Array represents a Fibonacci Series or not | Returns true if a permutation of arr [ 0. . n - 1 ] can form a Fibonacci Series ; Sort array ; After sorting , check if every element is equal to the sum of previous 2 elements ; Driver Code
def checkIsFibonacci ( arr , n ) : NEW_LINE INDENT if ( n == 1 or n == 2 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT arr . sort ( ) NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT if ( ( arr [ i - 1 ] + arr [ i - 2 ] ) != arr [ i ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 8 , 3 , 5 , 13 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE if ( checkIsFibonacci ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Last digit in a power of 2 | Python3 program to find last digit in a power of 2. ; Corner case ; Find the shift in current cycle and return value accordingly ; Driver code
def lastDigit2PowerN ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif n % 4 == 1 : NEW_LINE INDENT return 2 NEW_LINE DEDENT elif n % 4 == 2 : NEW_LINE INDENT return 4 NEW_LINE DEDENT elif n % 4 == 3 : NEW_LINE INDENT return 8 NEW_LINE DEDENT else : NEW_LINE DEDENT for n in range ( 20 ) : NEW_LINE INDENT print ( lastDigit2PowerN ( n ) , end = " ▁ " ) NEW_LINE DEDENT
Sum of all subsequences of length K | Function to find nCr ; Function that returns factorial of n ; Function for finding sum of all K length subsequences ; Calculate the sum of array ; Calculate nCk ; Return the final result ; Driver Code
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 sumSubsequences ( arr , n , k ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum = sum + arr [ i ] NEW_LINE DEDENT kLengthSubSequence = nCr ( n , k ) NEW_LINE ans = sum * ( ( k * kLengthSubSequence ) / n ) ; NEW_LINE return ans NEW_LINE DEDENT arr = [ 7 , 8 , 9 , 2 ] NEW_LINE k = 2 NEW_LINE n = len ( arr ) NEW_LINE print ( sumSubsequences ( arr , n , k ) ) NEW_LINE
Sum of first K numbers which are not divisible by N | Function to find the sum ; Find the last multiple of N ; Find the K - th non - multiple of N ; Calculate the sum of all elements from 1 to val ; Calculate the sum of all multiples of N between 1 to val ; Driver code
def findSum ( n , k ) : NEW_LINE INDENT val = ( k // ( n - 1 ) ) * n ; NEW_LINE rem = k % ( n - 1 ) ; NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT val = val - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT val = val + rem ; NEW_LINE DEDENT sum = ( val * ( val + 1 ) ) // 2 ; NEW_LINE x = k // ( n - 1 ) ; NEW_LINE sum_of_multiples = ( x * ( x + 1 ) * n ) // 2 ; NEW_LINE sum -= sum_of_multiples ; NEW_LINE return sum ; NEW_LINE DEDENT n = 7 ; k = 13 ; NEW_LINE print ( findSum ( n , k ) ) NEW_LINE
Count of the non | Python3 program to find count of non - prime divisors of given number ; Function to factors of the given number ; Loop to find the divisors of the number 2 ; Loop to find the divisors of the given number upto SQRT ( N ) ; Condition to check if the rest number is also a prime number ; Function to find the non - prime divisors of the given number ; Loop to count the number of the total divisors of given number ; Driver Code ; Function Call
from math import sqrt NEW_LINE def getFactorization ( x ) : NEW_LINE INDENT count = 0 NEW_LINE v = [ ] NEW_LINE while ( x % 2 == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE x = x // 2 NEW_LINE DEDENT if ( count != 0 ) : NEW_LINE INDENT v . append ( count ) NEW_LINE DEDENT for i in range ( 3 , int ( sqrt ( x ) ) + 12 ) : NEW_LINE INDENT count = 0 NEW_LINE while ( x % i == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE x //= i NEW_LINE DEDENT if ( count != 0 ) : NEW_LINE INDENT v . append ( count ) NEW_LINE DEDENT DEDENT if ( x > 1 ) : NEW_LINE INDENT v . append ( 1 ) NEW_LINE DEDENT return v NEW_LINE DEDENT def nonPrimeDivisors ( N ) : NEW_LINE INDENT v = getFactorization ( N ) NEW_LINE ret = 1 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT ret = ret * ( v [ i ] + 1 ) NEW_LINE DEDENT ret = ret - len ( v ) NEW_LINE return ret NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 NEW_LINE print ( nonPrimeDivisors ( N ) ) NEW_LINE DEDENT
Check if a number is Full Fibonacci or not | Python3 program to check if a given number is a Full Fibonacci Number or not ; A utility function that returns true if x is perfect square ; Returns true if N is a Fibonacci Number and false otherwise ; N is Fibonacci if one of 5 * N ^ 2 + 4 or 5 * N ^ 2 - 4 or both is a perferct square ; Function to check digits ; Check if all digits are fibonacci or not ; Extract digit ; Check if the current digit is not fibonacci ; Function to check and return if N is a Full Fibonacci number or not ; Driver Code
from math import * NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT s = sqrt ( x ) NEW_LINE return ( s * s == x ) NEW_LINE DEDENT def isFibonacci ( n ) : NEW_LINE INDENT return ( isPerfectSquare ( 5 * n * n + 4 ) or isPerfectSquare ( 5 * n * n - 4 ) ) NEW_LINE DEDENT def checkDigits ( n ) : NEW_LINE INDENT while ( n ) : NEW_LINE INDENT dig = n % 10 NEW_LINE if ( dig == 4 and dig == 6 and dig == 7 and dig == 9 ) : NEW_LINE INDENT return False NEW_LINE DEDENT n /= 10 NEW_LINE DEDENT return True NEW_LINE DEDENT def isFullfibonacci ( n ) : NEW_LINE INDENT return ( checkDigits ( n ) and isFibonacci ( n ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 13 NEW_LINE if ( isFullfibonacci ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Check whether a large number is divisible by 53 or not | Function to check if the number is divisible by 53 or not ; Driver Code
def isDivisible ( s ) : NEW_LINE INDENT flag = 0 NEW_LINE while ( len ( s ) > 4 ) : NEW_LINE INDENT l = len ( s ) - 1 NEW_LINE x = ( ord ( s [ l ] ) - ord ( '0' ) ) * 37 NEW_LINE s = s [ : : - 1 ] NEW_LINE s = s . replace ( '0' , ' ' , 1 ) NEW_LINE i = 0 NEW_LINE carry = 0 NEW_LINE while ( x ) : NEW_LINE INDENT d = ( ( ord ( s [ i ] ) - ord ( '0' ) ) - ( x % 10 ) - carry ) NEW_LINE if ( d < 0 ) : NEW_LINE INDENT d += 10 NEW_LINE carry = 1 NEW_LINE DEDENT else : NEW_LINE INDENT carry = 0 NEW_LINE DEDENT s = s . replace ( s [ i ] , chr ( d + ord ( '0' ) ) , 1 ) NEW_LINE x //= 10 NEW_LINE i += 1 NEW_LINE DEDENT while ( carry and i < l ) : NEW_LINE INDENT d = ( ord ( s [ i ] ) - ord ( '0' ) ) - carry NEW_LINE if ( d < 0 ) : NEW_LINE INDENT d += 10 NEW_LINE carry = 1 NEW_LINE DEDENT else : NEW_LINE INDENT carry = 0 NEW_LINE DEDENT s = s . replace ( s [ i ] , chr ( d + ord ( '0' ) ) , 1 ) NEW_LINE i += 1 NEW_LINE DEDENT s = s [ : : - 1 ] NEW_LINE DEDENT num = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT num = num * 10 + ( ord ( s [ i ] ) - ord ( '0' ) ) NEW_LINE DEDENT if ( num % 53 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = "1843246219106" NEW_LINE if ( isDivisible ( N ) ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT DEDENT
Check whether two numbers are in silver ratio | Function to check that two numbers are in silver ratio ; Swapping the numbers such that A contains the maximum number between these numbers ; First Ratio ; Second Ratio ; Condition to check that two numbers are in silver ratio ; Driver Code ; Function Call
def checksilverRatio ( a , b ) : NEW_LINE INDENT a , b = max ( a , b ) , min ( a , b ) NEW_LINE ratio1 = round ( a / b , 3 ) NEW_LINE ratio2 = round ( ( 2 * a + b ) / a , 3 ) NEW_LINE if ratio1 == ratio2 and ratio1 == 2.414 : NEW_LINE INDENT print ( " Yes " ) NEW_LINE return True NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE return False NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 2.414 NEW_LINE b = 1 NEW_LINE checksilverRatio ( a , b ) NEW_LINE DEDENT
Nth term where K + 1 th term is product of Kth term with difference of max and min digit of Kth term | Function to find minimum digit in the decimal representation of N ; Loop to find the minimum digit in the number ; Function to find maximum digit in the decimal representation of N ; Loop to find the maximum digit in the number ; Function to find the value of the given function ; Loop to compute the values of the given number ; Driver Code ; Function Call
def MIN ( n ) : NEW_LINE INDENT ans = 11 NEW_LINE while n : NEW_LINE INDENT ans = min ( ans , n % 10 ) NEW_LINE n //= 10 NEW_LINE DEDENT return ans NEW_LINE DEDENT def MAX ( n ) : NEW_LINE INDENT ans = - 1 NEW_LINE while n : NEW_LINE INDENT ans = max ( ans , n % 10 ) NEW_LINE n //= 10 NEW_LINE DEDENT return ans NEW_LINE DEDENT def Find_value ( n , k ) : NEW_LINE INDENT k -= 1 NEW_LINE ( x , y ) = ( 0 , 0 ) NEW_LINE while k : NEW_LINE INDENT k -= 1 NEW_LINE x = MIN ( n ) NEW_LINE y = MAX ( n ) NEW_LINE if ( ( y - x ) == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT n *= ( y - x ) NEW_LINE DEDENT print ( n , end = ' ▁ ' ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT ( N , D ) = ( 487 , 5 ) NEW_LINE Find_value ( N , D ) NEW_LINE DEDENT
Program to check if N is a Decagonal Number | Python3 program for the above approach ; Function to check if N is a decagonal number ; Condition to check if the number is a decagonal number ; Driver Code ; Given number ; Function Call
import math NEW_LINE def isdecagonal ( N ) : NEW_LINE INDENT n = ( 3 + math . sqrt ( 16 * N + 9 ) ) / 8 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 NEW_LINE if isdecagonal ( N ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT DEDENT
Program to check if N is a Octadecagon number | Python3 program for the above approach ; Function to check if N is a octadecagon number ; Condition to check if the number is a octadecagon number ; Driver code ; Given number ; Function Call
import math NEW_LINE def isOctadecagon ( N ) : NEW_LINE INDENT n = ( 14 + math . sqrt ( 128 * N + 196 ) ) // 32 NEW_LINE return ( ( n - int ( n ) ) == 0 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 18 NEW_LINE if isOctadecagon ( N ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT DEDENT
Count of subsequences which consists exactly K prime numbers | Returns factorial of n ; Function to return total number of combinations ; Function check whether a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Function for finding number of subsequences which consists exactly K primes ; If number of primes are less than k ; Driver code
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 nCr ( n , r ) : NEW_LINE INDENT return ( fact ( n ) // ( fact ( r ) * fact ( n - r ) ) ) NEW_LINE DEDENT def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT for i in range ( 2 , n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def countSubsequences ( arr , n , k ) : NEW_LINE INDENT countPrime = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( isPrime ( arr [ i ] ) ) : NEW_LINE INDENT countPrime += 1 NEW_LINE DEDENT DEDENT if ( countPrime < k ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( nCr ( countPrime , k ) * pow ( 2 , ( n - countPrime ) ) ) NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] NEW_LINE K = 3 NEW_LINE n = len ( arr ) NEW_LINE print ( countSubsequences ( arr , n , K ) ) NEW_LINE
Length of the longest alternating even odd subarray | Function to find the longest subarray ; Length of longest alternating subarray ; Iterate in the array ; Increment count if consecutive elements has an odd sum ; Store maximum count in longest ; Reinitialize cnt as 1 consecutive elements does not have an odd sum ; Length of ' longest ' can never be 1 since even odd has to occur in pair or more so return 0 if longest = 1 ; Driver Code
def longestEvenOddSubarray ( arr , n ) : NEW_LINE INDENT longest = 1 NEW_LINE cnt = 1 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( ( arr [ i ] + arr [ i + 1 ] ) % 2 == 1 ) : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE DEDENT else : NEW_LINE INDENT longest = max ( longest , cnt ) NEW_LINE cnt = 1 NEW_LINE DEDENT DEDENT if ( longest == 1 ) : NEW_LINE return 0 NEW_LINE return max ( cnt , longest ) NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 7 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( longestEvenOddSubarray ( arr , n ) ) NEW_LINE
Check whether given number N is a Moran Number or not | Function to calculate digit sum ; Function to check if number is prime ; Function to check if number is moran number ; Calculate digit sum ; Check if n is completely divisible by digit sum ; Calculate the quotient ; Check if the number is prime ; Driver code
def digSum ( a ) : NEW_LINE INDENT _sum = 0 NEW_LINE while ( a ) : NEW_LINE INDENT _sum += a % 10 NEW_LINE a = a // 10 NEW_LINE DEDENT return _sum NEW_LINE DEDENT def isPrime ( r ) : NEW_LINE INDENT s = True NEW_LINE i = 2 NEW_LINE while i * i <= r : NEW_LINE INDENT if ( r % i == 0 ) : NEW_LINE INDENT s = False NEW_LINE break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return s NEW_LINE DEDENT def moranNo ( n ) : NEW_LINE INDENT dup = n NEW_LINE _sum = digSum ( dup ) NEW_LINE if ( n % _sum == 0 ) : NEW_LINE INDENT c = n // _sum NEW_LINE if ( isPrime ( c ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " No " ) NEW_LINE DEDENT n = 21 NEW_LINE moranNo ( n ) NEW_LINE
Program to check if N is a Hendecagonal Number | Python3 program for the above approach ; Function to check if N is a Hendecagonal Number ; Condition to check if the number is a hendecagonal number ; Given Number ; Function call
import math NEW_LINE def ishendecagonal ( N ) : NEW_LINE INDENT n = ( 7 + math . sqrt ( 72 * N + 49 ) ) // 18 ; NEW_LINE return ( n - int ( n ) ) == 0 ; NEW_LINE DEDENT N = 11 ; NEW_LINE if ( ishendecagonal ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Program to check if N is a Hexadecagonal Number | Python3 program for the above approach ; Function to check if N is a hexadecagonal number ; Condition to check if the number is a hexadecagonal number ; Driver code ; Given number ; Function call
from math import sqrt NEW_LINE def ishexadecagonal ( N ) : NEW_LINE INDENT n = ( 12 + sqrt ( 112 * N + 144 ) ) / 28 ; NEW_LINE return ( n - int ( n ) ) == 0 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 16 ; NEW_LINE if ( ishexadecagonal ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Program to check if N is a Nonagonal Number | Function to check if N is a nonagonal number ; Condition to check if the number is a nonagonal number ; Driver code ; Given number ; Function call
def isnonagonal ( N ) : NEW_LINE INDENT n = ( 5 + pow ( ( 56 * N + 25 ) , 1 / 2 ) ) / 14 ; NEW_LINE return ( n - int ( n ) ) == 0 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 9 ; NEW_LINE if ( isnonagonal ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Count of decreasing pairs formed from numbers 1 to N | Function to count the possible number of pairs ; if the number is even then the answer in ( N / 2 ) - 1 ; if the number is odd then the answer in N / 2 ; Driver code
def divParts ( N ) : NEW_LINE INDENT if ( N % 2 == 0 ) : NEW_LINE INDENT print ( ( N / 2 ) - 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( N / 2 ) ; NEW_LINE DEDENT DEDENT N = 8 ; NEW_LINE divParts ( N ) ; NEW_LINE
Count the minimum steps to reach 0 from the given integer N | Function returns min step to reach 0 from N ; Direct possible reduction of value N ; Remaining steps needs to be reduced by 1 ; Summation of both the values ; Return the final answer ; Driver code
def getMinSteps ( n , jump ) : NEW_LINE INDENT quotient = int ( n / jump ) NEW_LINE remainder = n % jump NEW_LINE steps = quotient + remainder NEW_LINE return steps NEW_LINE DEDENT N = 6 NEW_LINE K = 3 NEW_LINE print ( getMinSteps ( N , K ) ) NEW_LINE
Construct an Array of size N whose sum of cube of all elements is a perfect square | Function to construct an array of size N ; Prints the first N natural numbers ; Driver code
def constructArray ( N ) : NEW_LINE INDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( i , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE constructArray ( N ) NEW_LINE DEDENT
Program to check if N is a Centered Decagonal Number | Python3 program for the above approach ; Function to check if the number N is a centered decagonal number ; Condition to check if N is centered decagonal number ; Driver Code ; Function call
import numpy as np NEW_LINE def isCentereddecagonal ( N ) : NEW_LINE INDENT n = ( 5 + np . sqrt ( 20 * N + 5 ) ) / 10 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT N = 11 NEW_LINE if ( isCentereddecagonal ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT