codeparrot/xlcost-text-to-code huggingface dataset:
Number of subarrays having sum of the form k ^ m , m >= 0 | Python3 implementation of the above approach ; Function to count number of sub - arrays whose sum is k ^ p where p >= 0 ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; b = k ^ p , p >= 0 ; k ^ m can be maximum equal to 10 ^ 14. ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; Driver code
from collections import defaultdict NEW_LINE MAX = 100005 NEW_LINE def partial_sum ( prefix_sum , arr , n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT prefix_sum [ i ] = ( prefix_sum [ i - 1 ] + arr [ i - 1 ] ) NEW_LINE DEDENT return prefix_sum NEW_LINE DEDENT def countSubarrays ( arr , n , k ) : NEW_LINE INDENT prefix_sum = [ 0 ] * MAX NEW_LINE prefix_sum [ 0 ] = 0 NEW_LINE prefix_sum = partial_sum ( prefix_sum , arr , n ) NEW_LINE if ( k == 1 ) : NEW_LINE INDENT sum = 0 NEW_LINE m = defaultdict ( int ) NEW_LINE for i in range ( n , - 1 , - 1 ) : NEW_LINE INDENT if ( ( prefix_sum [ i ] + 1 ) in m ) : NEW_LINE INDENT sum += m [ prefix_sum [ i ] + 1 ] NEW_LINE DEDENT m [ prefix_sum [ i ] ] += 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT if ( k == - 1 ) : NEW_LINE INDENT sum = 0 NEW_LINE m = defaultdict ( int ) NEW_LINE for i in range ( n , - 1 , - 1 ) : NEW_LINE INDENT if ( ( prefix_sum [ i ] + 1 ) in m ) : NEW_LINE INDENT sum += m [ prefix_sum [ i ] + 1 ] NEW_LINE DEDENT if ( ( prefix_sum [ i ] - 1 ) in m ) : NEW_LINE INDENT sum += m [ prefix_sum [ i ] - 1 ] NEW_LINE DEDENT m [ prefix_sum [ i ] ] += 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT sum = 0 NEW_LINE m = defaultdict ( int ) NEW_LINE for i in range ( n , - 1 , - 1 ) : NEW_LINE INDENT b = 1 NEW_LINE while ( True ) : NEW_LINE INDENT if ( b > 100000000000000 ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( ( prefix_sum [ i ] + b ) in m ) : NEW_LINE INDENT sum += m [ prefix_sum [ i ] + b ] NEW_LINE DEDENT b *= k NEW_LINE DEDENT m [ prefix_sum [ i ] ] += 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 2 , 2 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE print ( countSubarrays ( arr , n , k ) ) NEW_LINE DEDENT
Given two binary strings perform operation until B > 0 and print the result | Python 3 implementation of the approach ; Function to return the required result ; Reverse the strings ; Count the number of set bits in b ; To store the powers of 2 ; power [ i ] = pow ( 2 , i ) % mod ; To store the final answer ; Add power [ i ] to the ans after multiplying it with the number of set bits in b ; Divide by 2 means right shift b >> 1 if b has 1 at right most side than number of set bits will get decreased ; If no more set bits in b i . e . b = 0 ; Return the required answer ; Driver code
mod = 1000000007 NEW_LINE def BitOperations ( a , n , b , m ) : NEW_LINE INDENT a = a [ : : - 1 ] NEW_LINE b = b [ : : - 1 ] NEW_LINE c = 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT if ( b [ i ] == '1' ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT power = [ None ] * n NEW_LINE power [ 0 ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT power [ i ] = ( power [ i - 1 ] * 2 ) % mod NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( a [ i ] == '1' ) : NEW_LINE INDENT ans += c * power [ i ] NEW_LINE if ( ans >= mod ) : NEW_LINE INDENT ans %= mod NEW_LINE DEDENT DEDENT if ( b [ i ] == '1' ) : NEW_LINE INDENT c -= 1 NEW_LINE DEDENT if ( c == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = "1001" NEW_LINE b = "10101" NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE print ( BitOperations ( a , n , b , m ) ) NEW_LINE DEDENT
Print prime numbers with prime sum of digits in an array | Python3 implementation of the above approach ; Function to store the primes ; Function to return the sum of digits ; Function to print additive primes ; If the number is prime ; Check if it 's digit sum is prime ; Driver code
from math import sqrt NEW_LINE def sieve ( maxEle , prime ) : NEW_LINE INDENT prime [ 0 ] , prime [ 1 ] = 1 , 1 NEW_LINE for i in range ( 2 , int ( sqrt ( maxEle ) ) + 1 ) : NEW_LINE INDENT if ( not prime [ i ] ) : NEW_LINE INDENT for j in range ( 2 * i , maxEle + 1 , i ) : NEW_LINE INDENT prime [ j ] = 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT def digitSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( n ) : NEW_LINE INDENT sum += n % 10 NEW_LINE n = n // 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def printAdditivePrime ( arr , n ) : NEW_LINE INDENT maxEle = max ( arr ) NEW_LINE prime = [ 0 ] * ( maxEle + 1 ) NEW_LINE sieve ( maxEle , prime ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ arr [ i ] ] == 0 ) : NEW_LINE INDENT sum = digitSum ( arr [ i ] ) NEW_LINE if ( prime [ sum ] == 0 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 4 , 6 , 11 , 12 , 18 , 7 ] NEW_LINE n = len ( a ) NEW_LINE printAdditivePrime ( a , n ) NEW_LINE DEDENT
Find the Nth term of the series 9 , 45 , 243 , 1377 | Function to return the nth term of the given series ; nth term ; Driver code
def nthterm ( n ) : NEW_LINE INDENT An = ( 1 ** n + 2 ** n ) * ( 3 ** n ) NEW_LINE return An ; NEW_LINE DEDENT n = 3 NEW_LINE print ( nthterm ( n ) ) NEW_LINE
Count the numbers < N which have equal number of divisors as K | Function to return the count of the divisors of a number ; Count the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ; While i divides n ; This condition is to handle the case when n is a prime number > 2 ; Count the total elements that have divisors exactly equal to as that of k 's ; Exclude k from the result if it is smaller than n . ; Driver code
def countDivisors ( n ) : NEW_LINE INDENT x , ans = 0 , 1 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT x += 1 NEW_LINE n = n / 2 NEW_LINE DEDENT ans = ans * ( x + 1 ) NEW_LINE for i in range ( 3 , int ( n ** 1 / 2 ) + 1 , 2 ) : NEW_LINE INDENT x = 0 NEW_LINE while ( n % i == 0 ) : NEW_LINE INDENT x += 1 NEW_LINE n = n / i NEW_LINE DEDENT ans = ans * ( x + 1 ) NEW_LINE DEDENT if ( n > 2 ) : NEW_LINE INDENT ans = ans * 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT def getTotalCount ( n , k ) : NEW_LINE INDENT k_count = countDivisors ( k ) NEW_LINE count = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( k_count == countDivisors ( i ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( k < n ) : NEW_LINE INDENT count = count - 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n , k = 500 , 6 NEW_LINE print ( getTotalCount ( n , k ) ) NEW_LINE DEDENT
Find the nth term of the series 0 , 8 , 64 , 216 , 512 , . . . | Function to return the nth term of the given series ; Common difference ; First term ; nth term ; nth term of the given series ; Driver code
def term ( n ) : NEW_LINE INDENT d = 2 NEW_LINE a1 = 0 NEW_LINE An = a1 + ( n - 1 ) * d NEW_LINE An = An ** 3 NEW_LINE return An ; NEW_LINE DEDENT n = 5 NEW_LINE print ( term ( n ) ) NEW_LINE
Count the number of intervals in which a given value lies | Python3 program to count the number of intervals in which a given value lies ; Function to count the number of intervals in which a given value lies ; Variables to store overall minimumimum and maximumimum of the intervals ; Frequency array to keep track of how many of the given intervals an element lies in ; Constructing the frequency array ; Driver Code ; length of the array
MAX_VAL = 200000 NEW_LINE def countIntervals ( arr , V , N ) : NEW_LINE INDENT minimum = float ( " inf " ) NEW_LINE maximum = 0 NEW_LINE freq = [ 0 ] * ( MAX_VAL ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT li = arr [ i ] [ 0 ] NEW_LINE freq [ li ] = freq [ li ] + 1 NEW_LINE ri = arr [ i ] [ 1 ] NEW_LINE freq [ ri + 1 ] = freq [ ri + 1 ] - 1 NEW_LINE if li < minimum : NEW_LINE INDENT minimum = li NEW_LINE DEDENT if ri > maximum : NEW_LINE INDENT maximum = ri NEW_LINE DEDENT DEDENT for i in range ( minimum , maximum + 1 ) : NEW_LINE INDENT freq [ i ] = freq [ i ] + freq [ i - 1 ] NEW_LINE DEDENT return freq [ V ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 1 , 10 ] , [ 5 , 10 ] , [ 15 , 25 ] , [ 7 , 12 ] , [ 20 , 25 ] ] NEW_LINE V = 7 NEW_LINE N = len ( arr ) NEW_LINE print ( countIntervals ( arr , V , N ) ) NEW_LINE DEDENT
Split N ^ 2 numbers into N groups of equal sum | Function to print N groups of equal sum ; No . of Groups ; n / 2 pairs ; Driver code
def printGroups ( n ) : NEW_LINE INDENT x = 1 NEW_LINE y = n * n NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , n // 2 + 1 ) : NEW_LINE INDENT print ( " { " , x , " , " , y , " } " , end = " ▁ " ) NEW_LINE x += 1 NEW_LINE y -= 1 NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE printGroups ( n ) NEW_LINE DEDENT
Program to find the Break Even Point | Python 3 program to find Break Even Point ; Function to calculate Break Even Point ; Calculating number of articles to be sold ; Driver Code
import math NEW_LINE def breakEvenPoint ( exp , S , M ) : NEW_LINE INDENT earn = S - M NEW_LINE if res != 0 : NEW_LINE res = math . ceil ( exp / earn ) NEW_LINE else : NEW_LINE res = float ( ' inf ' ) NEW_LINE return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT exp = 3550 NEW_LINE S = 90 NEW_LINE M = 65 NEW_LINE print ( int ( breakEvenPoint ( exp , S , M ) ) ) NEW_LINE DEDENT
Minimize the value of N by applying the given operations | function to return the product of distinct prime factors of a numberdef minSteps ( str ) : ; find distinct prime ; Driver code
def minimum ( n ) : NEW_LINE INDENT product = 1 NEW_LINE i = 2 NEW_LINE while i * i <= n : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT while n % i == 0 : NEW_LINE INDENT n = n / i NEW_LINE DEDENT product = product * i NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT if n >= 2 : NEW_LINE INDENT product = product * n NEW_LINE DEDENT return product NEW_LINE DEDENT n = 20 NEW_LINE print ( minimum ( n ) ) NEW_LINE
N digit numbers divisible by 5 formed from the M digits | Function to find the count of all possible N digit numbers which are divisible by 5 formed from M digits ; If it is not possible to form n digit number from the given m digits without repetition ; If both zero and five exists ; Remaining N - 1 iterations ; Remaining N - 1 iterations ; Driver code
def numbers ( n , arr , m ) : NEW_LINE INDENT isZero = 0 NEW_LINE isFive = 0 NEW_LINE result = 0 NEW_LINE if ( m < n ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT isZero = 1 NEW_LINE DEDENT if ( arr [ i ] == 5 ) : NEW_LINE INDENT isFive = 1 NEW_LINE DEDENT DEDENT if ( isZero and isFive ) : NEW_LINE INDENT result = 2 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT m -= 1 NEW_LINE result = result * ( m ) NEW_LINE DEDENT DEDENT elif ( isZero or isFive ) : NEW_LINE INDENT result = 1 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT m -= 1 NEW_LINE result = result * ( m ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT result = - 1 NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE m = 6 NEW_LINE arr = [ 2 , 3 , 5 , 6 , 7 , 9 ] NEW_LINE print ( numbers ( n , arr , m ) ) NEW_LINE DEDENT
Program to find the smallest element among three elements | Python implementation to find the smallest of three elements
a , b , c = 5 , 7 , 10 NEW_LINE if ( a <= b and a <= c ) : NEW_LINE INDENT print ( a , " is ▁ the ▁ smallest " ) NEW_LINE DEDENT elif ( b <= a and b <= c ) : NEW_LINE INDENT print ( b , " is ▁ the ▁ smallest " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( c , " is ▁ the ▁ smallest " ) NEW_LINE DEDENT
Find subsequences with maximum Bitwise AND and Bitwise OR | Function to find the maximum sum ; Maximum AND is maximum element ; Maximum OR is bitwise OR of all . ; Driver code
def maxSum ( a , n ) : NEW_LINE INDENT maxAnd = max ( a ) NEW_LINE maxOR = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT maxOR |= a [ i ] NEW_LINE DEDENT print ( maxAnd + maxOR ) NEW_LINE DEDENT n = 4 NEW_LINE a = [ 3 , 5 , 6 , 1 ] NEW_LINE maxSum ( a , n ) NEW_LINE
Count Magic squares in a grid | Python3 program to count magic squares ; function to check is subgrid is Magic Square ; Elements of grid must contain all numbers from 1 to 9 , sum of all rows , columns and diagonals must be same , i . e . , 15. ; Function to count total Magic square subgrids ; if condition true skip check ; check for magic square subgrid ; return total magic square ; Driver Code ; Function call to print required answer
R = 3 NEW_LINE C = 4 NEW_LINE def magic ( a , b , c , d , e , f , g , h , i ) : NEW_LINE INDENT s1 = set ( [ a , b , c , d , e , f , g , h , i ] ) NEW_LINE s2 = set ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ) NEW_LINE if ( s1 == s2 and ( a + b + c ) == 15 and ( d + e + f ) == 15 and ( g + h + i ) == 15 and ( a + d + g ) == 15 and ( b + e + h ) == 15 and ( c + f + i ) == 15 and ( a + e + i ) == 15 and ( c + e + g ) == 15 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return false NEW_LINE DEDENT def CountMagicSquare ( Grid ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 0 , R - 2 ) : NEW_LINE INDENT for j in range ( 0 , C - 2 ) : NEW_LINE INDENT if Grid [ i + 1 ] [ j + 1 ] != 5 : NEW_LINE INDENT continue NEW_LINE DEDENT if ( magic ( Grid [ i ] [ j ] , Grid [ i ] [ j + 1 ] , Grid [ i ] [ j + 2 ] , Grid [ i + 1 ] [ j ] , Grid [ i + 1 ] [ j + 1 ] , Grid [ i + 1 ] [ j + 2 ] , Grid [ i + 2 ] [ j ] , Grid [ i + 2 ] [ j + 1 ] , Grid [ i + 2 ] [ j + 2 ] ) == True ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT G = [ [ 4 , 3 , 8 , 4 ] , [ 9 , 5 , 1 , 9 ] , [ 2 , 7 , 6 , 2 ] ] NEW_LINE print ( CountMagicSquare ( G ) ) NEW_LINE DEDENT
Sum of minimum elements of all subarrays | Function to return required minimum sum ; getting number of element strictly larger than A [ i ] on Left . ; get elements from stack until element greater than A [ i ] found ; getting number of element larger than A [ i ] on Right . ; get elements from stack until element greater or equal to A [ i ] found ; calculating required resultult ; Driver Code ; function call to get required resultult
def sumSubarrayMins ( A , n ) : NEW_LINE INDENT left , right = [ None ] * n , [ None ] * n NEW_LINE s1 , s2 = [ ] , [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT cnt = 1 NEW_LINE while len ( s1 ) > 0 and s1 [ - 1 ] [ 0 ] > A [ i ] : NEW_LINE INDENT cnt += s1 [ - 1 ] [ 1 ] NEW_LINE s1 . pop ( ) NEW_LINE DEDENT s1 . append ( [ A [ i ] , cnt ] ) NEW_LINE left [ i ] = cnt NEW_LINE DEDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT cnt = 1 NEW_LINE while len ( s2 ) > 0 and s2 [ - 1 ] [ 0 ] >= A [ i ] : NEW_LINE INDENT cnt += s2 [ - 1 ] [ 1 ] NEW_LINE s2 . pop ( ) NEW_LINE DEDENT s2 . append ( [ A [ i ] , cnt ] ) NEW_LINE right [ i ] = cnt NEW_LINE DEDENT result = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT result += A [ i ] * left [ i ] * right [ i ] NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 3 , 1 , 2 , 4 ] NEW_LINE n = len ( A ) NEW_LINE print ( sumSubarrayMins ( A , n ) ) NEW_LINE DEDENT
Minimum and Maximum element of an array which is divisible by a given number k | Python 3 implementation of the above approach ; Function to find the minimum element ; Function to find the maximum element ; Driver code
import sys NEW_LINE def getMin ( arr , n , k ) : NEW_LINE INDENT res = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % k == 0 ) : NEW_LINE INDENT res = min ( res , arr [ i ] ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT def getMax ( arr , n , k ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] % k == 0 ) : NEW_LINE INDENT res = max ( res , arr [ i ] ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 1230 , 45 , 67 , 1 ] NEW_LINE k = 10 NEW_LINE n = len ( arr ) NEW_LINE print ( " Minimum ▁ element ▁ of ▁ array ▁ which " , " is ▁ divisible ▁ by ▁ k : ▁ " , getMin ( arr , n , k ) ) NEW_LINE print ( " Maximum ▁ element ▁ of ▁ array ▁ which " , " is ▁ divisible ▁ by ▁ k : ▁ " , getMax ( arr , n , k ) ) NEW_LINE DEDENT
Print a number containing K digits with digital root D | Function to find a number ; If d is 0 , k has to be 1 ; Print k - 1 zeroes ; Driver code
def printNumberWithDR ( k , d ) : NEW_LINE INDENT if d == 0 and k != 1 : NEW_LINE INDENT print ( - 1 , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( d , end = " " ) NEW_LINE k -= 1 NEW_LINE while k : NEW_LINE INDENT print ( 0 , end = " " ) NEW_LINE k -= 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT k , d = 4 , 4 NEW_LINE printNumberWithDR ( k , d ) NEW_LINE DEDENT
Count number of integers less than or equal to N which has exactly 9 divisors | Function to count numbers having exactly 9 divisors ; Sieve array , initially prime [ i ] = i ; use sieve concept to store the first prime factor of every number ; mark all factors of i ; check for all numbers if they can be expressed in form p * q ; p prime factor ; q prime factor ; if both prime factors are different if p * q <= n and q != ; Check if it can be expressed as p ^ 8 ; Driver Code
def countNumbers ( n ) : NEW_LINE INDENT c = 0 NEW_LINE limit = int ( n ** ( 0.5 ) ) NEW_LINE prime = [ i for i in range ( limit + 1 ) ] NEW_LINE i = 2 NEW_LINE while i * i <= limit : NEW_LINE INDENT if prime [ i ] == i : NEW_LINE INDENT for j in range ( i * i , limit + 1 , i ) : NEW_LINE INDENT if prime [ j ] == j : NEW_LINE INDENT prime [ j ] = i NEW_LINE DEDENT DEDENT DEDENT i += 1 NEW_LINE DEDENT for i in range ( 2 , limit + 1 ) : NEW_LINE INDENT p = prime [ i ] NEW_LINE q = prime [ i // prime [ i ] ] NEW_LINE if p * q == i and q != 1 and p != q : NEW_LINE INDENT c += 1 NEW_LINE DEDENT elif prime [ i ] == i : NEW_LINE INDENT if i ** 8 <= n : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT DEDENT return c NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1000 NEW_LINE print ( countNumbers ( n ) ) NEW_LINE DEDENT
Interprime | 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 ; Function to check if the given number is interprime or not ; Smallest Interprime is 4 So the number less than 4 can not be a Interprime ; Calculate first prime number < n ; Calculate first prime number > n ; check if next_prime and prev_prime have the same average ; 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 def isInterprime ( n ) : NEW_LINE INDENT if ( n < 4 ) : NEW_LINE INDENT return False NEW_LINE DEDENT prev_prime = n NEW_LINE next_prime = n NEW_LINE while ( isPrime ( prev_prime ) == 0 ) : NEW_LINE INDENT prev_prime = prev_prime - 1 NEW_LINE DEDENT while ( isPrime ( next_prime ) == 0 ) : NEW_LINE INDENT next_prime = next_prime + 1 NEW_LINE DEDENT if ( ( prev_prime + next_prime ) == 2 * n ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT n = 9 NEW_LINE if ( isInterprime ( n ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Find the unit place digit of sum of N factorials | Function to find the unit 's place digit ; Let us write for cases when N is smaller than or equal to 4. ; We know following ( 1 ! + 2 ! + 3 ! + 4 ! ) % 10 = 3 ; Driver code
def get_unit_digit ( N ) : NEW_LINE INDENT if ( N == 0 or N == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif ( N == 2 ) : NEW_LINE INDENT return 3 NEW_LINE DEDENT elif ( N == 3 ) : NEW_LINE INDENT return 9 NEW_LINE DEDENT else : NEW_LINE INDENT return 3 NEW_LINE DEDENT DEDENT N = 1 NEW_LINE for N in range ( 11 ) : NEW_LINE INDENT print ( " For ▁ N ▁ = ▁ " , N , " : " , get_unit_digit ( N ) , sep = ' ▁ ' ) NEW_LINE DEDENT
Sum of squares of Fibonacci numbers | Python3 program to find sum of squares of Fibonacci numbers in O ( Log n ) time . ; Create an array for memoization ; Returns n 'th Fibonacci number using table f[] ; Base cases ; If fib ( n ) is already computed ; Applying above formula [ Note value n & 1 is 1 if n is odd , else 0 ] . ; Function to calculate sum of squares of Fibonacci numbers ; Driver Code
MAX = 1000 NEW_LINE f = [ 0 for i in range ( MAX ) ] NEW_LINE def fib ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT if n == 1 or n == 2 : NEW_LINE INDENT return 1 NEW_LINE DEDENT if f [ n ] : NEW_LINE INDENT return f [ n ] NEW_LINE DEDENT if n & 1 : NEW_LINE INDENT k = ( n + 1 ) // 2 NEW_LINE DEDENT else : NEW_LINE INDENT k = n // 2 NEW_LINE DEDENT if n & 1 : NEW_LINE INDENT f [ n ] = ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT f [ n ] = ( ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ) NEW_LINE DEDENT return f [ n ] NEW_LINE DEDENT def calculateSumOfSquares ( n ) : NEW_LINE INDENT return fib ( n ) * fib ( n + 1 ) NEW_LINE DEDENT n = 6 NEW_LINE print ( " Sum ▁ of ▁ Squares ▁ of ▁ " " Fibonacci ▁ numbers ▁ is ▁ : " , calculateSumOfSquares ( n ) ) NEW_LINE
Number of solutions for the equation x + y + z <= n | function to find the number of solutions for the equation x + y + z <= n , such that 0 <= x <= X , 0 <= y <= Y , 0 <= z <= Z . ; to store answer ; for values of x ; for values of y ; maximum possible value of z ; if z value greater than equals to 0 then only it is valid ; find minimum of temp and z ; return required answer ; Driver code
def NumberOfSolutions ( x , y , z , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( x + 1 ) : NEW_LINE INDENT for j in range ( y + 1 ) : NEW_LINE INDENT temp = n - i - j NEW_LINE if temp >= 0 : NEW_LINE INDENT temp = min ( temp , z ) NEW_LINE ans += temp + 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x , y , z , n = 1 , 2 , 3 , 4 NEW_LINE print ( NumberOfSolutions ( x , y , z , n ) ) NEW_LINE DEDENT
Program to find the Nth term of series 5 , 12 , 21 , 32 , 45. ... . . | calculate Nth term of series ; Driver code
def nthTerm ( n ) : NEW_LINE INDENT return n ** 2 + 4 * n ; NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE