codeparrot/xlcost-text-to-code huggingface dataset:
Minimize difference between two sequences obtained by splitting first N powers of 2 | Function to partition first N powers of 2 into two subsequences with minimum difference between their sum ; Largest element in the first part ; Place N / 2 - 1 smallest elements in the first sequence ; Place remaining N / 2 elements in the second sequence ; Print the minimum difference ; Driver Code
def minimumDifference ( N ) : NEW_LINE INDENT sum1 = ( 1 << N ) NEW_LINE sum2 = 0 NEW_LINE for i in range ( 1 , N // 2 ) : NEW_LINE INDENT sum1 += ( 1 << i ) NEW_LINE DEDENT for i in range ( N // 2 , N ) : NEW_LINE INDENT sum2 += ( 1 << i ) NEW_LINE DEDENT print ( sum1 - sum2 ) NEW_LINE DEDENT N = 4 NEW_LINE minimumDifference ( N ) NEW_LINE
Check whether N can be a Perfect Cube after adding or subtracting K | Function to check if a number is a perfect Cube or not ; Driver code
def isPerfectCube ( x ) : NEW_LINE INDENT cr = int ( x ** ( 1 / 3 ) ) ; NEW_LINE return ( cr * cr * cr == x ) ; NEW_LINE DEDENT def canBePerfectCube ( N , K ) : NEW_LINE INDENT if ( isPerfectCube ( N + K ) or isPerfectCube ( N - K ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 7 ; K = 1 ; NEW_LINE canBePerfectCube ( N , K ) ; NEW_LINE N = 5 ; K = 4 ; NEW_LINE canBePerfectCube ( N , K ) ; NEW_LINE N = 7 ; K = 2 ; NEW_LINE canBePerfectCube ( N , K ) ; NEW_LINE DEDENT
Floor square root without using sqrt ( ) function : Recursive | Function to find the square root of the number N using BS ; If the range is still valid ; Find the mid - value of the range ; Base Case ; Condition to check if the left search space is useless ; Driver Code
def sqrtSearch ( low , high , N ) : NEW_LINE INDENT if ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 ; NEW_LINE if ( ( mid * mid <= N ) and ( ( mid + 1 ) * ( mid + 1 ) > N ) ) : NEW_LINE INDENT return mid ; NEW_LINE DEDENT elif ( mid * mid < N ) : NEW_LINE INDENT return sqrtSearch ( mid + 1 , high , N ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return sqrtSearch ( low , mid - 1 , N ) ; NEW_LINE DEDENT DEDENT return low ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 25 ; NEW_LINE print ( sqrtSearch ( 0 , N , N ) ) NEW_LINE DEDENT
Common divisors of N numbers | Function to calculate gcd of two numbers ; Function to prall the common divisors ; Variable to find the gcd of N numbers ; Set to store all the common divisors ; Finding GCD of the given N numbers ; Finding divisors of the HCF of n numbers ; Prall the divisors ; Driver 's Code ; Function to prall the common divisors
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT def printAllDivisors ( arr , N ) : NEW_LINE INDENT g = arr [ 0 ] NEW_LINE divisors = dict ( ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT g = gcd ( arr [ i ] , g ) NEW_LINE DEDENT for i in range ( 1 , g + 1 ) : NEW_LINE INDENT if i * i > g : NEW_LINE INDENT break NEW_LINE DEDENT if ( g % i == 0 ) : NEW_LINE INDENT divisors [ i ] = 1 NEW_LINE if ( g // i != i ) : NEW_LINE INDENT divisors [ g // i ] = 1 NEW_LINE DEDENT DEDENT DEDENT for it in sorted ( divisors ) : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 6 , 90 , 12 , 18 , 30 , 24 ] NEW_LINE n = len ( arr ) NEW_LINE printAllDivisors ( arr , n ) NEW_LINE DEDENT
Recursive Program to print multiplication table of a number | Function that print the table of a given number using recursion ; Base Case ; Print the table for current iteration ; Recursive call to next iteration ; Input number whose table is to print ; Function call to print the table
def mul_table ( N , i ) : NEW_LINE INDENT if ( i > 10 ) : NEW_LINE INDENT return NEW_LINE DEDENT print ( N , " * " , i , " = " , N * i ) NEW_LINE return mul_table ( N , i + 1 ) NEW_LINE DEDENT N = 8 NEW_LINE mul_table ( N , 1 ) NEW_LINE
Find the largest composite number that divides N but is strictly lesser than N | Python3 program to find the largest composite number that divides N which is less than N ; Function to check whether a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Function that find the largest composite number which divides N and less than N ; Find the prime number ; Driver Code ; Get the smallest prime factor ; Check if ( N / a ) is prime or not . If Yes print " - 1" ; Else print largest composite number ( N / a )
import math NEW_LINE 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 getSmallestPrimefactor ( n ) : NEW_LINE INDENT for i in range ( 2 , ( int ) ( math . sqrt ( n ) + 1 ) ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT N = 100 ; NEW_LINE a = getSmallestPrimefactor ( N ) ; NEW_LINE if ( ( isPrime ( ( int ) ( N / a ) ) ) ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ( int ) ( N / a ) ) ; NEW_LINE DEDENT
Check if N and M can be made equal by increasing N by A and decreasing M by B | Function to whether the numbers can be made equal or not ; Check whether the numbers can reach an equal point or not ; M and N cannot be made equal by increasing M and decreasing N when M is already greater than N ; Driver code
def checkEqualNo ( m , n , a , b ) : NEW_LINE INDENT if ( m <= n ) : NEW_LINE INDENT if ( ( n - m ) % ( a + b ) == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT M = 2 ; N = 8 ; NEW_LINE A = 3 ; B = 3 ; NEW_LINE if ( checkEqualNo ( M , N , A , B ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
XOR and OR of all N | Function to check if a number is palindrome or not ; Convert the num n to string ; Iterate over string to check whether it is palindromic or not ; Function to find XOR of all N - digits palindrome number ; To store the XOR and OR of all palindromic number ; Starting N - digit palindromic number ; Ending N - digit palindromic number ; Iterate over starting and ending number ; To check if i is palindromic or not ; Print the XOR and OR of all palindromic number ; Driver Code
def ispalin ( num ) : NEW_LINE INDENT s = str ( num ) ; NEW_LINE st = 0 ; NEW_LINE ed = len ( s ) - 1 ; NEW_LINE while ( st <= ed ) : NEW_LINE INDENT if ( s [ st ] != s [ ed ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT st += 1 ; NEW_LINE ed -= 1 ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT def CalculateXORandOR ( n ) : NEW_LINE INDENT CalculateXOR = 0 ; NEW_LINE CalculateOR = 0 ; NEW_LINE start = 10 ** ( n - 1 ) ; NEW_LINE end = ( 10 ** n ) - 1 ; NEW_LINE for i in range ( start , end + 1 ) : NEW_LINE INDENT if ( ispalin ( i ) ) : NEW_LINE INDENT CalculateXOR = CalculateXOR ^ i ; NEW_LINE CalculateOR = CalculateOR | i ; NEW_LINE DEDENT DEDENT print ( " XOR ▁ = " , CalculateXOR , end = " ▁ " ) ; NEW_LINE print ( " OR ▁ = ▁ " , CalculateOR ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 ; NEW_LINE CalculateXORandOR ( n ) ; NEW_LINE DEDENT
Sum of all palindromic numbers lying in the range [ L , R ] for Q queries | pref [ ] array to precompute the sum of all palindromic number ; Function that return number num if num is palindromic else return 0 ; Convert num to string ; Function to precompute the sum of all palindrome numbers upto 100000 ; checkPalindrome ( ) return the number i if i is palindromic else return 0 ; Function to print the sum for each query ; Function to prsum of all palindromic numbers between [ L , R ] ; Function that pre computes the sum of all palindromic numbers ; Iterate over all Queries to print the sum ; Queries ; Function that print the the sum of all palindromic number in Range [ L , R ]
pref = [ 0 ] * 100001 NEW_LINE def checkPalindrome ( num ) : NEW_LINE INDENT strr = str ( num ) NEW_LINE l = 0 NEW_LINE r = len ( strr ) - 1 NEW_LINE while ( l < r ) : NEW_LINE INDENT if ( strr [ l ] != strr [ r ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT l += 1 NEW_LINE r -= 1 NEW_LINE DEDENT return num NEW_LINE DEDENT def preCompute ( ) : NEW_LINE INDENT for i in range ( 1 , 100001 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + checkPalindrome ( i ) NEW_LINE DEDENT DEDENT def printSum ( L , R ) : NEW_LINE INDENT print ( pref [ R ] - pref [ L - 1 ] ) NEW_LINE DEDENT def printSumPalindromic ( arr , Q ) : NEW_LINE INDENT preCompute ( ) NEW_LINE for i in range ( Q ) : NEW_LINE INDENT printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) NEW_LINE DEDENT DEDENT Q = 2 NEW_LINE arr = [ [ 10 , 13 ] , [ 12 , 21 ] ] NEW_LINE printSumPalindromic ( arr , Q ) NEW_LINE
Sum of alternating sign Squares of first N natural numbers | Function to calculate the alternating sign sum ; Variable to store the sum ; Loop to iterate each number from 1 to N ; The alternating sign is put by checking if the number is even or odd ; Add the square with the sign ; Add the square with the sign ; Driver code
def summation ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( i % 2 == 1 ) : NEW_LINE INDENT sum += ( i * i ) ; NEW_LINE DEDENT else : NEW_LINE INDENT sum -= ( i * i ) ; NEW_LINE DEDENT DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 ; NEW_LINE print ( summation ( N ) ) ; NEW_LINE DEDENT
Sum of alternating sign Squares of first N natural numbers | Function to calculate the alternating sign sum ; Variable to store the absolute sum ; Variable to store the sign ; Variable to store the resultant sum ; Driver code
def summation ( n ) : NEW_LINE INDENT abs_sum = n * ( n + 1 ) // 2 ; NEW_LINE sign = 1 if ( ( n + 1 ) % 2 == 0 ) else - 1 ; NEW_LINE result_sum = sign * abs_sum ; NEW_LINE return result_sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 ; NEW_LINE print ( summation ( N ) ) ; NEW_LINE DEDENT
Count pairs from 1 to N such that their Sum is divisible by their XOR | Function to count pairs ; variable to store count ; Generate all possible pairs such that 1 <= x < y < n ; Driver code
def countPairs ( n ) : NEW_LINE INDENT count = 0 ; NEW_LINE for x in range ( 1 , n ) : NEW_LINE INDENT for y in range ( x + 1 , n + 1 ) : NEW_LINE INDENT if ( ( y + x ) % ( y ^ x ) == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 6 ; NEW_LINE print ( countPairs ( n ) ) ; NEW_LINE DEDENT
Count pairs in array such that one element is power of another | Python3 program to count pairs in array such that one element is power of another ; Function to check if given number number y is power of x ; log function to calculate value ; compare to the result1 or result2 both are equal ; Function to find pairs from array ; Iterate through all pairs ; Increment count if one is the power of other ; Driver code
from math import log NEW_LINE def isPower ( x , y ) : NEW_LINE INDENT res1 = log ( y ) // log ( x ) ; NEW_LINE res2 = log ( y ) / log ( x ) ; NEW_LINE return ( res1 == res2 ) ; NEW_LINE DEDENT def countPower ( arr , n ) : NEW_LINE INDENT res = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if isPower ( arr [ i ] , arr [ j ] ) or isPower ( arr [ j ] , arr [ i ] ) : NEW_LINE INDENT res += 1 ; NEW_LINE DEDENT DEDENT DEDENT return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 16 , 2 , 3 , 9 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( countPower ( a , n ) ) ; NEW_LINE DEDENT
Find the original Array using XOR values of all adjacent elements | XOR of all elements from 1 to N ; Function to find the Array from the XOR Array ; Take a vector to store the permutation ; XOR of N natural numbers ; Loop to find the XOR of adjacent elements of the XOR Array ; Loop to find the other elements of the permutation ; Finding the next and next elements ; Driver Code ; Required Permutation
def xor_all_elements ( n ) : NEW_LINE INDENT if n & 3 == 0 : NEW_LINE INDENT return n NEW_LINE DEDENT elif n & 3 == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif n & 3 == 2 : NEW_LINE INDENT return n + 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def findArray ( xorr , n ) : NEW_LINE INDENT arr = [ ] NEW_LINE xor_all = xor_all_elements ( n ) NEW_LINE xor_adjacent = 0 NEW_LINE for i in range ( 0 , n - 1 , 2 ) : NEW_LINE INDENT xor_adjacent = xor_adjacent ^ xorr [ i ] NEW_LINE DEDENT last_element = xor_all ^ xor_adjacent NEW_LINE arr . append ( last_element ) NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT last_element = xorr [ i ] ^ last_element NEW_LINE arr . append ( last_element ) NEW_LINE DEDENT return arr NEW_LINE DEDENT xorr = [ 7 , 5 , 3 , 7 ] NEW_LINE n = 5 NEW_LINE arr = findArray ( xorr , n ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Sum of non | Function to return a vector which consists the sum of four portions of the matrix ; Iterating through the matrix ; Condition for selecting all values before the second diagonal of metrics ; Top portion of the matrix ; Left portion of the matrix ; Bottom portion of the matrix ; Right portion of the matrix ; Adding all the four portions into a vecto ; Driver code
def sumOfParts ( arr , N ) : NEW_LINE INDENT sum_part1 , sum_part2 , sum_part3 , sum_part4 = 0 , 0 , 0 , 0 NEW_LINE totalsum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if i + j < N - 1 : NEW_LINE INDENT if ( i < j and i != j and i + j ) : NEW_LINE INDENT sum_part1 += arr [ i ] [ j ] NEW_LINE DEDENT elif i != j : NEW_LINE INDENT sum_part2 += arr [ i ] [ j ] NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if i > j and i + j != N - 1 : NEW_LINE INDENT sum_part3 += arr [ i ] [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT if i + j != N - 1 and i != j : NEW_LINE INDENT sum_part4 += arr [ i ] [ j ] NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return sum_part1 + sum_part2 + sum_part3 + sum_part4 NEW_LINE DEDENT N = 4 NEW_LINE arr = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] NEW_LINE print ( sumOfParts ( arr , N ) ) NEW_LINE
Check if X and Y can be made zero by using given operation any number of times | Function to check if X and Y can be made equal to zero by using given operation any number of times ; Check for the two conditions ; Driver code
def ifPossible ( X , Y ) : NEW_LINE INDENT if ( X > Y ) : NEW_LINE INDENT X , Y = Y , X NEW_LINE DEDENT if ( ( X + Y ) % 5 == 0 and 3 * X >= 2 * Y ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT X = 33 NEW_LINE Y = 27 NEW_LINE ifPossible ( X , Y ) NEW_LINE
Largest Even and Odd N | Function to print the largest n - digit even and odd numbers in hexadecimal number system ; Append ' F ' ( N - 1 ) times ; Append ' E ' for an even number ; Append ' F ' for an odd number ; Driver code
def findNumbers ( n ) : NEW_LINE INDENT ans = ' F ' * ( n - 1 ) ; NEW_LINE even = ans + ' E ' ; NEW_LINE odd = ans + ' F ' ; NEW_LINE print ( " Even : ▁ " , even ) ; NEW_LINE print ( " Odd : ▁ " , odd ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 ; NEW_LINE findNumbers ( n ) ; NEW_LINE DEDENT
Program to compute log a to any base b ( logb a ) | Python3 program to find log ( a ) on any base b ; Driver code
from math import log NEW_LINE def log_a_to_base_b ( a , b ) : NEW_LINE INDENT return log ( a ) // log ( b ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 3 ; NEW_LINE b = 2 ; NEW_LINE print ( log_a_to_base_b ( a , b ) ) ; NEW_LINE a = 256 ; NEW_LINE b = 4 ; NEW_LINE print ( log_a_to_base_b ( a , b ) ) ; NEW_LINE DEDENT
Program to compute log a to any base b ( logb a ) | Recursive function to compute log a to the base b ; Driver code
def log_a_to_base_b ( a , b ) : NEW_LINE INDENT rslt = ( 1 + log_a_to_base_b ( a // b , b ) ) if ( a > ( b - 1 ) ) else 0 ; NEW_LINE return rslt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 3 ; NEW_LINE b = 2 ; NEW_LINE print ( log_a_to_base_b ( a , b ) ) ; NEW_LINE a = 256 ; NEW_LINE b = 4 ; NEW_LINE print ( log_a_to_base_b ( a , b ) ) ; NEW_LINE DEDENT
Largest and Smallest N | Function to return the largest N - digit number in Hexa - Decimal Number System ; Append ' F ' N times ; Function to return the smallest N - digit number in Hexa - Decimal Number System ; Append '0' ( N - 1 ) times to 1 ; Function to print the largest and smallest N - digit Hexa - Decimal number ; Driver code
def findLargest ( N ) : NEW_LINE INDENT largest = ' F ' * N NEW_LINE return largest ; NEW_LINE DEDENT def findSmallest ( N ) : NEW_LINE INDENT smallest = '1' + '0' * ( N - 1 ) NEW_LINE return smallest ; NEW_LINE DEDENT def printAns ( largest ) : NEW_LINE INDENT print ( " Largest : ▁ " , findLargest ( largest ) ) ; NEW_LINE print ( " Smallest : ▁ " , findSmallest ( largest ) ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 ; NEW_LINE printAns ( N ) ; NEW_LINE DEDENT
Highest and Smallest power of K less than and greater than equal to N respectively | Python3 implementation of the approach ; Function to return the highest power of k less than or equal to n ; Function to return the smallest power of k greater than or equal to n ; Function to print the result ; Driver code
import math NEW_LINE def prevPowerofK ( n , k ) : NEW_LINE INDENT p = int ( math . log ( n ) / math . log ( k ) ) NEW_LINE return int ( math . pow ( k , p ) ) NEW_LINE DEDENT def nextPowerOfK ( n , k ) : NEW_LINE INDENT return prevPowerofK ( n , k ) * k NEW_LINE DEDENT def printResult ( n , k ) : NEW_LINE INDENT print ( prevPowerofK ( n , k ) , nextPowerOfK ( n , k ) ) NEW_LINE DEDENT n = 6 NEW_LINE k = 3 NEW_LINE printResult ( n , k ) NEW_LINE
Check if all elements of the given array can be made 0 by decrementing value in pairs | Function to check if all the elements can be made 0 in an array ; Variable to store sum and maximum element in an array ; Loop to calculate the sum and max value of the given array ; If n is 1 or sum is odd or sum - max element < max then no solution ; For the remaining case , print Yes ; Driver code
def canMake ( n , ar ) : NEW_LINE INDENT sum = 0 ; maxx = - 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += ar [ i ] ; NEW_LINE maxx = max ( maxx , ar [ i ] ) ; NEW_LINE DEDENT if ( n == 1 or sum % 2 == 1 or sum - maxx < maxx ) : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 6 ; NEW_LINE arr = [ 1 , 1 , 2 , 3 , 6 , 11 ] ; NEW_LINE canMake ( n , arr ) ; NEW_LINE DEDENT
Sum of all Perfect Squares lying in the range [ L , R ] for Q queries | Python3 program to find the sum of all perfect squares in the given range ; Array to precompute the sum of squares from 1 to 100010 so that for every query , the answer can be returned in O ( 1 ) . ; Function to check if a number is a perfect square or not ; Find floating point value of square root of x . ; If square root is an integer ; Function to precompute the perfect squares upto 100000. ; Function to print the sum for each query ; Driver code ; To calculate the precompute function ; Calling the printSum function for every query
from math import sqrt , floor NEW_LINE pref = [ 0 ] * 100010 ; NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT sr = sqrt ( x ) ; NEW_LINE rslt = x if ( sr - floor ( sr ) == 0 ) else 0 ; NEW_LINE return rslt ; NEW_LINE DEDENT def compute ( ) : NEW_LINE INDENT for i in range ( 1 , 100001 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + isPerfectSquare ( i ) ; NEW_LINE DEDENT DEDENT def printSum ( L , R ) : NEW_LINE INDENT sum = pref [ R ] - pref [ L - 1 ] ; NEW_LINE print ( sum , end = " ▁ " ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT compute ( ) ; NEW_LINE Q = 4 ; NEW_LINE arr = [ [ 1 , 10 ] , [ 1 , 100 ] , [ 2 , 25 ] , [ 4 , 50 ] ] ; NEW_LINE for i in range ( Q ) : NEW_LINE INDENT printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; NEW_LINE DEDENT DEDENT
Program to find all Factors of a Number using recursion | Recursive function to prfactors of a number ; Checking if the number is less than N ; Calling the function recursively for the next number ; Driver code
def factors ( n , i ) : NEW_LINE INDENT if ( i <= n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE DEDENT factors ( n , i + 1 ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 16 ; NEW_LINE factors ( N , 1 ) ; NEW_LINE DEDENT
Maximize value of ( a + b ) such that ( a * a | Function to maximize the value of ( a + b ) such that ( a * a - b * b = n ) ; Driver code
def maxValue ( n ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1 ; NEW_LINE print ( maxValue ( n ) ) ; NEW_LINE DEDENT
Maximum number of edges that N | Function to find the maximum number of edges in a N - vertex graph . ; According to the Mantel 's theorem  the maximum number of edges will be  floor of [(n^2)/4] ; Driver Function
def solve ( n ) : NEW_LINE INDENT ans = ( n * n // 4 ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE print ( solve ( n ) ) NEW_LINE DEDENT
Multiplication table till N rows where every Kth row is table of K upto Kth term | Function to pr the multiplication table upto K - th term ; For loop to iterate from 1 to N where i serves as the value of K ; Inner loop which at every iteration goes till i ; Printing the table value for i ; New line after every row ; Driver code
def prMultiples ( N ) : NEW_LINE INDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT print ( ( i * j ) , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE prMultiples ( N ) NEW_LINE DEDENT