codeparrot/xlcost-text-to-code huggingface dataset:
Repeated sum of first N natural numbers | Function to return the sum of the first n natural numbers ; Function to return the repeated sum ; Perform the operation exactly k times ; Update n with the sum of first n natural numbers ; Driver code
def sum ( n ) : NEW_LINE INDENT sum = ( n * ( n + 1 ) ) // 2 NEW_LINE return sum NEW_LINE DEDENT def repeatedSum ( n , k ) : NEW_LINE INDENT for i in range ( k ) : NEW_LINE INDENT n = sum ( n ) NEW_LINE DEDENT return n NEW_LINE DEDENT n = 2 NEW_LINE k = 2 NEW_LINE print ( repeatedSum ( n , k ) ) NEW_LINE
Difference between Sum of Cubes and Sum of First N Natural Numbers | Python3 program to find the difference between the sum of the cubes of the first N natural numbers and the sum of the first N natural number ; Sum of first n natural numbers ; Find the required difference ; Driver Code
def difference ( n ) : NEW_LINE INDENT S = ( n * ( n + 1 ) ) // 2 ; NEW_LINE res = S * ( S - 1 ) ; NEW_LINE return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE print ( difference ( n ) ) ; NEW_LINE DEDENT
Check if the sum of digits of number is divisible by all of its digits | Function that returns true if all the digits of n divide the sum of the digits of n ; Store a copy of the original number ; Find the sum of the digits of n ; Restore the original value ; Check if all the digits divide the calculated sum ; If current digit doesn 't  divide the sum ; Driver code
def isDivisible ( n ) : NEW_LINE INDENT temp = n NEW_LINE sum = 0 NEW_LINE while ( n ) : NEW_LINE INDENT digit = n % 10 NEW_LINE sum += digit NEW_LINE n //= 10 NEW_LINE DEDENT n = temp NEW_LINE while ( n ) : NEW_LINE INDENT digit = n % 10 NEW_LINE if ( sum % digit != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT n //= 10 ; NEW_LINE DEDENT return True NEW_LINE DEDENT n = 123 NEW_LINE if ( isDivisible ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Check if the sum of digits of number is divisible by all of its digits | Python implementation of above approach ; Converting integer to string ; Initialising sum to 0 ; Traversing through the string ; Converting character to int ; Comparing number and sum Traversing again ; Check if any digit is not dividing the sum ; Return false ; If any value is not returned then all the digits are dividing the sum SO return true ; Driver Code ; passing this number to get result function
def getResult ( n ) : NEW_LINE INDENT st = str ( n ) NEW_LINE sum = 0 NEW_LINE length = len ( st ) NEW_LINE for i in st : NEW_LINE INDENT sum = sum + int ( i ) NEW_LINE DEDENT for i in st : NEW_LINE INDENT if ( sum % int ( i ) != 0 ) : NEW_LINE INDENT return ' No ' NEW_LINE DEDENT DEDENT return ' Yes ' NEW_LINE DEDENT n = 123 NEW_LINE print ( getResult ( n ) ) NEW_LINE
Program for Mobius Function | Set 2 | Python3 implementation of the approach ; Function to calculate least prime factor of each number ; If it is a prime number ; For all multiples which are not visited yet . ; Function to find the value of Mobius function for all the numbers from 1 to n ; To store the values of Mobius function ; If number is one ; If number has a squared prime factor ; Multiply - 1 with the previous number ; Driver code ; Function to find least prime factor ; Function to find mobius function
N = 100005 NEW_LINE lpf = [ 0 ] * N ; NEW_LINE def least_prime_factor ( ) : NEW_LINE INDENT for i in range ( 2 , N ) : NEW_LINE INDENT if ( not lpf [ i ] ) : NEW_LINE INDENT for j in range ( i , N , i ) : NEW_LINE INDENT if ( not lpf [ j ] ) : NEW_LINE INDENT lpf [ j ] = i ; NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def Mobius ( n ) : NEW_LINE INDENT mobius = [ 0 ] * N ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( i == 1 ) : NEW_LINE INDENT mobius [ i ] = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( lpf [ i // lpf [ i ] ] == lpf [ i ] ) : NEW_LINE INDENT mobius [ i ] = 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT mobius [ i ] = - 1 * mobius [ i // lpf [ i ] ] ; NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( mobius [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE least_prime_factor ( ) ; NEW_LINE Mobius ( n ) ; NEW_LINE DEDENT
Make the list non | Python3 implementation of the approach ; Function to return the minimum element from the range [ prev , MAX ] such that it differs in at most 1 digit with cur ; To start with the value we have achieved in the last step ; Store the value with which the current will be compared ; Current value ; There are at most 4 digits ; If the current digit differs in both the numbers ; Eliminate last digits in both the numbers ; If the number of different digits is at most 1 ; If we can 't find any number for which  the number of change is less than or  equal to 1 then return -1 ; Function to get the non - decreasing list ; Creating a vector for the updated list ; Let 's assume that it is possible to  make the list non-decreasing ; Element of the original array ; Can 't make the list non-decreasing ; If possible then print the list ; Driver code
DIGITS = 4 ; MIN = 1000 ; MAX = 9999 ; NEW_LINE def getBest ( prev , cur ) : NEW_LINE INDENT maximum = max ( MIN , prev ) ; NEW_LINE for i in range ( maximum , MAX + 1 ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE a = i ; NEW_LINE b = cur ; NEW_LINE for k in range ( DIGITS ) : NEW_LINE INDENT if ( a % 10 != b % 10 ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE DEDENT a //= 10 ; NEW_LINE b //= 10 ; NEW_LINE DEDENT if ( cnt <= 1 ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT def getList ( arr , n ) : NEW_LINE INDENT myList = [ ] ; NEW_LINE possible = True ; NEW_LINE myList . append ( 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT cur = arr [ i ] ; NEW_LINE myList . append ( getBest ( myList [ - 1 ] , cur ) ) ; NEW_LINE if ( myList [ - 1 ] == - 1 ) : NEW_LINE INDENT possible = False ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( possible ) : NEW_LINE INDENT for i in range ( 1 , len ( myList ) ) : NEW_LINE INDENT print ( myList [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1095 , 1094 , 1095 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE getList ( arr , n ) ; NEW_LINE DEDENT
Maximum items that can be bought with the given type of coins | Python3 implementation of the approach ; Function to find maximum fruits Can buy from given values of x , y , z . ; Items of type 1 that can be bought ; Update the coins ; Items of type 2 that can be bought ; Update the coins ; Items of type 3 that can be bought ; Update the coins ; Items of type 4 that can be bought To buy a type 4 item , a coin of each type is required ; Total items that can be bought ; Driver code
COST = 3 ; NEW_LINE def maxItems ( x , y , z ) : NEW_LINE INDENT type1 = x // COST ; NEW_LINE x %= COST ; NEW_LINE type2 = y // COST ; NEW_LINE y %= COST ; NEW_LINE type3 = z // COST ; NEW_LINE z %= COST ; NEW_LINE type4 = min ( x , min ( y , z ) ) ; NEW_LINE maxItems = type1 + type2 + type3 + type4 ; NEW_LINE return maxItems ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 4 ; y = 5 ; z = 6 ; NEW_LINE print ( maxItems ( x , y , z ) ) ; NEW_LINE DEDENT
Count occurrences of a prime number in the prime factorization of every element from the given range | Function to return the highest power of p that divides n ; Function to return the count of times p appears in the prime factors of the elements from the range [ l , r ] ; To store the required count ; For every element of the range ; Add the highest power of p that divides i ; Driver code
def countFactors ( n , p ) : NEW_LINE INDENT pwr = 0 ; NEW_LINE while ( n > 0 and n % p == 0 ) : NEW_LINE INDENT n //= p ; NEW_LINE pwr += 1 ; NEW_LINE DEDENT return pwr ; NEW_LINE DEDENT def getCount ( l , r , p ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT cnt += countFactors ( i , p ) ; NEW_LINE DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 2 ; r = 8 ; p = 2 ; NEW_LINE print ( getCount ( l , r , p ) ) ; NEW_LINE DEDENT
Count occurrences of a prime number in the prime factorization of every element from the given range | Function to return the count of times p appears in the prime factors of the elements from the range [ l , r ] ; To store the required count ; Number of values in the range [ 0 , r ] that are divisible by val ; Number of values in the range [ 0 , l - 1 ] that are divisible by val ; Increment the power of the val ; ( a - b ) is the count of numbers in the range [ l , r ] that are divisible by val ; No values that are divisible by val thus exiting from the loop ; Driver Code
def getCount ( l , r , p ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE val = p ; NEW_LINE while ( True ) : NEW_LINE INDENT a = r // val ; NEW_LINE b = ( l - 1 ) // val ; NEW_LINE val *= p ; NEW_LINE if ( a - b ) : NEW_LINE INDENT cnt += ( a - b ) ; NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT return int ( cnt ) ; NEW_LINE DEDENT l = 2 ; NEW_LINE r = 8 ; NEW_LINE p = 2 ; NEW_LINE print ( getCount ( l , r , p ) ) ; NEW_LINE
Check if the number is valid when flipped upside down | Function that returns true if str is Topsy Turvy ; For every character of the string ; If the current digit cannot form a valid digit when turned upside - down ; Driver code
def topsyTurvy ( string ) : NEW_LINE INDENT for i in range ( len ( string ) ) : NEW_LINE INDENT if ( string [ i ] == '2' or string [ i ] == '4' or string [ i ] == '5' or string [ i ] == '6' or string [ i ] == '7' or string [ i ] == '9' ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "1234" ; NEW_LINE if ( topsyTurvy ( string ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Find the count of subsequences where each element is divisible by K | Function to return the count of all valid subsequences ; To store the count of elements which are divisible by k ; If current element is divisible by k then increment the count ; Total ( 2 ^ n - 1 ) non - empty subsequences are possible with n element ; Driver code
def countSubSeq ( arr , n , k ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % k == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return ( 2 ** count - 1 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 6 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 3 ; NEW_LINE print ( countSubSeq ( arr , n , k ) ) ; NEW_LINE DEDENT
Count of numbers below N whose sum of prime divisors is K | Python3 implementation of the approach ; Function to return the count of numbers below N whose sum of prime factors is K ; To store the sum of prime factors for all the numbers ; If i is prime ; Add i to all the numbers which are divisible by i ; To store the count of required numbers ; Return the required count ; Driver code
MAX = 1000001 NEW_LINE def countNum ( N , K ) : NEW_LINE INDENT sumPF = [ 0 ] * MAX ; NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT if ( sumPF [ i ] == 0 ) : NEW_LINE INDENT for j in range ( i , N , i ) : NEW_LINE INDENT sumPF [ j ] += i ; NEW_LINE DEDENT DEDENT DEDENT count = 0 ; NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT if ( sumPF [ i ] == K ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 20 ; K = 7 ; NEW_LINE print ( countNum ( N , K ) ) ; NEW_LINE DEDENT
Queries for the smallest and the largest prime number of given digit | Python3 implementation of the approach ; 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 greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to return the smallest prime number with d digits ; check if prime ; Function to return the largest prime number with d digits ; check if prime ; Driver code ; Perform queries
from math import sqrt NEW_LINE MAX = 100000 NEW_LINE prime = [ True ] * ( MAX + 1 ) NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT for p in range ( 2 , int ( sqrt ( MAX ) ) + 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , MAX + 1 , p ) : NEW_LINE INDENT prime [ i ] = False ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def smallestPrime ( d ) : NEW_LINE INDENT l = 10 ** ( d - 1 ) ; NEW_LINE r = ( 10 ** d ) - 1 ; NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT def largestPrime ( d ) : NEW_LINE INDENT l = 10 ** ( d - 1 ) ; NEW_LINE r = ( 10 ** d ) - 1 ; NEW_LINE for i in range ( r , l , - 1 ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT SieveOfEratosthenes ( ) ; NEW_LINE queries = [ 2 , 5 ] ; NEW_LINE q = len ( queries ) ; NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( smallestPrime ( queries [ i ] ) , " ▁ " , largestPrime ( queries [ i ] ) ) ; NEW_LINE DEDENT DEDENT
Find a Square Matrix such that sum of elements in every row and column is K | Function to print the required matrix ; Print k for the left diagonal elements ; Print 0 for the rest ; Driver code
def printMatrix ( n , k ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT print ( k , end = " ▁ " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( "0" , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT print ( ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 ; k = 7 ; NEW_LINE printMatrix ( n , k ) ; NEW_LINE DEDENT
Divide first N natural numbers into 3 equal sum subsets | Function that returns true if the subsets are possible ; If n <= 3 then it is not possible to divide the elements in three subsets satisfying the given conditions ; Sum of all the elements in the range [ 1 , n ] ; If the sum is divisible by 3 then it is possible ; Driver code
def possible ( n ) : NEW_LINE INDENT if ( n > 3 ) : NEW_LINE INDENT sum = ( n * ( n + 1 ) ) // 2 ; NEW_LINE if ( sum % 3 == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE if ( possible ( n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Find the Nth element of the modified Fibonacci series | Function to return the Nth number of the modified Fibonacci series where A and B are the first two terms ; To store the current element which is the sum of previous two elements of the series ; This loop will terminate when the Nth element is found ; Return the Nth element ; Driver code
def findNthNumber ( A , B , N ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT sum = A + B NEW_LINE A = B NEW_LINE B = sum NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 5 NEW_LINE B = 7 NEW_LINE N = 10 NEW_LINE print ( findNthNumber ( A , B , N ) ) NEW_LINE DEDENT
Check if an array is increasing or decreasing | Function to check the type of the array ; If the first two and the last two elements of the array are in increasing order ; If the first two and the last two elements of the array are in decreasing order ; If the first two elements of the array are in increasing order and the last two elements of the array are in decreasing order ; If the first two elements of the array are in decreasing order and the last two elements of the array are in increasing order ; Driver code
def checkType ( arr , n ) : NEW_LINE INDENT if ( arr [ 0 ] <= arr [ 1 ] and arr [ n - 2 ] <= arr [ n - 1 ] ) : NEW_LINE INDENT print ( " Increasing " ) ; NEW_LINE DEDENT elif ( arr [ 0 ] >= arr [ 1 ] and arr [ n - 2 ] >= arr [ n - 1 ] ) : NEW_LINE INDENT print ( " Decreasing " ) ; NEW_LINE DEDENT elif ( arr [ 0 ] <= arr [ 1 ] and arr [ n - 2 ] >= arr [ n - 1 ] ) : NEW_LINE INDENT print ( " Increasing ▁ then ▁ decreasing " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Decreasing ▁ then ▁ increasing " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE checkType ( arr , n ) ; NEW_LINE DEDENT
Calculate the IST : Indian Standard Time | Python3 implementation of the approach ; Function to calculate Indian Standard Time ; Separate integer part ; Separate float part and return ceil value ; Driver code ; Number of hours ( 1 - 24 ) ; Rotations in degrees
from math import ceil NEW_LINE def cal_IST ( h , r ) : NEW_LINE INDENT IST = round ( ( h * r * 1.0 ) / 360 , 3 ) ; NEW_LINE int_IST = int ( IST ) ; NEW_LINE float_IST = ceil ( ( IST - int_IST ) * 60 ) ; NEW_LINE print ( int_IST , " : " , float_IST ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT h = 20 ; NEW_LINE r = 150 ; NEW_LINE cal_IST ( h , r ) ; NEW_LINE DEDENT
Check if it is possible to perform the given Grid Division | Function that returns true if it is possible to divide the grid satisfying the given conditions ; To store the sum of all the cells of the given parts ; If the sum is equal to the total number of cells in the given grid ; Driver code
def isPossible ( arr , p , n , m ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( p ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT if ( sum == ( n * m ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 ; NEW_LINE m = 4 ; NEW_LINE arr = [ 6 , 3 , 2 , 1 ] ; NEW_LINE p = len ( arr ) ; NEW_LINE if ( isPossible ( arr , p , n , m ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Find the first N integers such that the sum of their digits is equal to 10 | Function to return the sum of digits of n ; Add the last digit to the sum ; Remove last digit ; Return the sum of digits ; Function to print the first n numbers whose sum of digits is 10 ; First number of the series is 19 ; If the sum of digits of the current number is equal to 10 ; Print the number ; Add 9 to the previous number ; Driver code
def sum ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE while ( n ) : NEW_LINE INDENT sum = sum + n % 10 ; NEW_LINE n = n // 10 ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def firstN ( n ) : NEW_LINE INDENT num = 19 ; cnt = 1 ; NEW_LINE while ( cnt != n ) : NEW_LINE INDENT if ( sum ( num ) == 10 ) : NEW_LINE INDENT print ( num , end = " ▁ " ) ; NEW_LINE cnt += 1 ; NEW_LINE DEDENT num += 9 ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 ; NEW_LINE firstN ( n ) ; NEW_LINE DEDENT
Sum of last digit of all integers from 1 to N divisible by M | Function to return the required sum ; Number of element between 1 to n divisible by m ; Array to store the last digit of elements in a cycle ; Storing and adding last digit of cycle ; Number of elements present in last cycle ; Sum of k / 10 cycle ; Adding value of digits of last cycle to the answer ; Driver Code ; input n and m
def sumOfLastDig ( n , m ) : NEW_LINE INDENT sum = 0 ; NEW_LINE k = n // m ; NEW_LINE arr = [ 0 ] * 10 ; NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT arr [ i ] = m * ( i + 1 ) % 10 ; NEW_LINE sum += arr [ i ] ; NEW_LINE DEDENT rem = k % 10 ; NEW_LINE ans = ( k // 10 ) * sum ; NEW_LINE for i in range ( rem ) : NEW_LINE INDENT ans += arr [ i ] ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 100 ; m = 3 ; NEW_LINE print ( sumOfLastDig ( n , m ) ) ; NEW_LINE DEDENT
Number of Subsequences with Even and Odd Sum | Set 2 | Function to find number of Subsequences with Even and Odd Sum ; Counting number of odds ; Even count ; Total Subsequences is ( 2 ^ n - 1 ) For NumberOfEvenSubsequences subtract NumberOfOddSubsequences from total ; Driver code ; Calling the function
def countSum ( arr , n ) : NEW_LINE INDENT NumberOfOdds = 0 ; NumberOfEvens = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT NumberOfOdds += 1 ; NEW_LINE DEDENT DEDENT NumberOfEvens = n - NumberOfOdds ; NEW_LINE NumberOfOddSubsequences = ( 1 << NumberOfEvens ) * ( 1 << ( NumberOfOdds - 1 ) ) ; NEW_LINE NumberOfEvenSubsequences = ( 1 << n ) - 1 - NumberOfOddSubsequences ; NEW_LINE return ( NumberOfEvenSubsequences , NumberOfOddSubsequences ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 3 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE ans = countSum ( arr , n ) ; NEW_LINE print ( " EvenSum ▁ = " , ans [ 0 ] , end = " ▁ " ) ; NEW_LINE print ( " OddSum ▁ = " , ans [ 1 ] ) ; NEW_LINE DEDENT
Program to find the next prime number | Python3 implementation of the approach ; Function that returns True if n is prime else returns False ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to return the smallest prime number greater than N ; Base case ; Loop continuously until isPrime returns True for a number greater than n ; Driver code
import math NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 5 , int ( math . sqrt ( n ) + 1 ) , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def nextPrime ( N ) : NEW_LINE INDENT if ( N <= 1 ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT prime = N NEW_LINE found = False NEW_LINE while ( not found ) : NEW_LINE INDENT prime = prime + 1 NEW_LINE if ( isPrime ( prime ) == True ) : NEW_LINE INDENT found = True NEW_LINE DEDENT DEDENT return prime NEW_LINE DEDENT N = 3 NEW_LINE print ( nextPrime ( N ) ) NEW_LINE
Check if a number is Flavius Number | Return the number is Flavious Number or not ; index i starts from 2 because at 1 st iteration every 2 nd element was remove and keep going for k - th iteration ; removing the elements which are already removed at kth iteration ; Driver Code
def Survives ( n ) : NEW_LINE INDENT i = 2 NEW_LINE while ( True ) : NEW_LINE INDENT if ( i > n ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT if ( n % i == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT n -= n // i ; NEW_LINE i += 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 17 ; NEW_LINE if ( Survives ( n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Nth XOR Fibonacci number | Function to return the nth XOR Fibonacci number ; Driver code
def nthXorFib ( n , a , b ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return a NEW_LINE DEDENT if n == 1 : NEW_LINE INDENT return b NEW_LINE DEDENT if n == 2 : NEW_LINE INDENT return a ^ b NEW_LINE DEDENT return nthXorFib ( n % 3 , a , b ) NEW_LINE DEDENT a = 1 NEW_LINE b = 2 NEW_LINE n = 10 NEW_LINE print ( nthXorFib ( n , a , b ) ) NEW_LINE
Sand Timer Flip Counting Problem | Recursive function to return the gcd of a and b ; Everything divides 0 ; Function to print the number of flips for both the sand timers ; Driver code
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def flip ( a , b ) : NEW_LINE INDENT lcm = ( a * b ) // gcd ( a , b ) NEW_LINE a = lcm // a NEW_LINE b = lcm // b NEW_LINE print ( a - 1 , b - 1 ) NEW_LINE DEDENT a = 10 NEW_LINE b = 5 NEW_LINE flip ( a , b ) NEW_LINE