codeparrot/xlcost-text-to-code huggingface dataset:
Count Sexy Prime Pairs in the given array | Python 3 program to count Sexy Prime pairs in array ; A utility function to check if the number n is prime or not ; Base Cases ; Check to skip middle five numbers in below loop ; If n is divisible by i and i + 2 then it is not prime ; A utility function that check if n1 and n2 are SPP ( Sexy Prime Pair ) or not ; Function to find SPP ( Sexy Prime Pair ) pairs from the given array ; Iterate through all pairs ; Increment count if SPP ( Sexy Prime Pair ) pair ; Driver code ; Function call to find SPP ( Sexy Prime Pair ) pair
from math import sqrt 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 ( sqrt ( n ) ) + 1 , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 6 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def SexyPrime ( n1 , n2 ) : NEW_LINE INDENT return ( isPrime ( n1 ) and isPrime ( n2 ) and abs ( n1 - n2 ) == 6 ) NEW_LINE DEDENT def countSexyPairs ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( SexyPrime ( arr [ i ] , arr [ j ] ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 6 , 7 , 5 , 11 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countSexyPairs ( arr , n ) ) NEW_LINE DEDENT
Find the K | Function to find the index of number at first position of kth sequence of set of size n ; n_actual_fact = n ! ; First position of the kth sequence will be occupied by the number present at index = k / ( n - 1 ) ! ; Function to find the kth permutation of n numbers ; Store final answer ; Insert all natural number upto n in set ; Subtract 1 to get 0 based indexing ; Mark the first position ; itr now points to the number at index in set s ; remove current number from the set ; Driver code
def findFirstNumIndex ( k , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 0 , k NEW_LINE DEDENT n -= 1 NEW_LINE first_num_index = 0 NEW_LINE n_partial_fact = n NEW_LINE while ( k >= n_partial_fact and n > 1 ) : NEW_LINE INDENT n_partial_fact = n_partial_fact * ( n - 1 ) NEW_LINE n -= 1 NEW_LINE DEDENT first_num_index = k // n_partial_fact NEW_LINE k = k % n_partial_fact NEW_LINE return first_num_index , k NEW_LINE DEDENT def findKthPermutation ( n , k ) : NEW_LINE INDENT ans = " " NEW_LINE s = set ( ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT s . add ( i ) NEW_LINE DEDENT k = k - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT itr = list ( s ) NEW_LINE index , k = findFirstNumIndex ( k , n - i ) NEW_LINE ans += str ( itr [ index ] ) NEW_LINE itr . pop ( index ) NEW_LINE s = set ( itr ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE k = 4 NEW_LINE kth_perm_seq = findKthPermutation ( n , k ) NEW_LINE print ( kth_perm_seq ) NEW_LINE DEDENT
Find the Largest N digit perfect square number in Base B | Python3 implementation to find the largest N digit perfect square number in base B ; Function to find the largest N digit number ; Largest n - digit perfect square ; Print the result ; Driver Code
import math NEW_LINE def nDigitPerfectSquares ( n , b ) : NEW_LINE INDENT largest = pow ( math . ceil ( math . sqrt ( pow ( b , n ) ) ) - 1 , 2 ) NEW_LINE print ( largest ) NEW_LINE DEDENT N = 1 NEW_LINE B = 8 NEW_LINE nDigitPerfectSquares ( N , B ) NEW_LINE
Find Cube root of a number using Log function | Python3 program to find cube root of a number using logarithm ; Function to find the cube root ; Calculate the cube root ; Return the final answer ; Driver code
import numpy as np NEW_LINE def cubeRoot ( n ) : NEW_LINE INDENT ans = pow ( 3 , ( 1.0 / 3 ) * ( np . log ( n ) / np . log ( 3 ) ) ) NEW_LINE return ans NEW_LINE DEDENT N = 8 NEW_LINE print ( " % .2f " % cubeRoot ( N ) ) NEW_LINE
Find the maximum possible value for the given periodic function | Function to return the maximum value of f ( x ) ; Driver code
def floorMax ( A , B , N ) : NEW_LINE INDENT x = min ( B - 1 , N ) NEW_LINE return ( A * x ) // B NEW_LINE DEDENT A = 11 NEW_LINE B = 10 NEW_LINE N = 9 NEW_LINE print ( floorMax ( A , B , N ) ) NEW_LINE
Minimum moves taken to move coin of each cell to any one cell of Matrix | Function to find the minimum number of moves taken to move the element of each cell to any one cell of the square matrix of odd length ; Initializing count to 0 ; Number of layers that are around the centre element ; Iterating over ranger of layers ; Increase the value of count by 8 * k * k ; Driver code
def calculateMoves ( n ) : NEW_LINE INDENT count = 0 NEW_LINE layers = n // 2 NEW_LINE for k in range ( 1 , layers + 1 ) : NEW_LINE INDENT count += 8 * k * k NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE print ( calculateMoves ( N ) ) NEW_LINE DEDENT
Check if N can be represented as sum of squares of two consecutive integers | Python3 implementation to check that a number is sum of squares of 2 consecutive numbers or not ; Function to check that the a number is sum of squares of 2 consecutive numbers or not ; Condition to check if the a number is sum of squares of 2 consecutive numbers or not ; Driver code ; Function call
import math NEW_LINE def isSumSquare ( N ) : NEW_LINE INDENT n = ( 2 + math . sqrt ( 8 * N - 4 ) ) / 2 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT i = 13 NEW_LINE if isSumSquare ( i ) : 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 Centered heptagonal number | Python3 implementation to check that a number is a centered heptagonal number or not ; Function to check that the number is a centered heptagonal number ; Condition to check if the number is a centered heptagonal number ; Driver Code ; Function call
import math NEW_LINE def isCenteredheptagonal ( N ) : NEW_LINE INDENT n = ( 7 + math . sqrt ( 56 * N - 7 ) ) / 14 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT n = 8 NEW_LINE if ( isCenteredheptagonal ( 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 Centered nonadecagonal number | Python3 implementation to check that a number is a Centered nonadecagonal number or not ; Function to check that the number is a Centered nonadecagonal number ; Condition to check if the number is a Centered nonadecagonal number ; Driver Code ; Function call
import math NEW_LINE def isCenterednonadecagonal ( N ) : NEW_LINE INDENT n = ( 19 + math . sqrt ( 152 * N + 209 ) ) / 38 ; NEW_LINE return ( n - int ( n ) ) == 0 ; NEW_LINE DEDENT n = 20 ; NEW_LINE if ( isCenterednonadecagonal ( 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 Centered Octadecagonal number | Python3 implementation to check that a number is a centered octadecagonal number or not ; Function to check that the number is a centered octadecagonal number ; Implement the formula generated ; Condition to check if the number is a centered octadecagonal number ; Driver code ; Function call
import math NEW_LINE def isCenteredOctadecagonal ( N ) : NEW_LINE INDENT n = ( 9 + math . sqrt ( 36 * N + 45 ) ) / 18 ; NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 19 NEW_LINE if isCenteredOctadecagonal ( n ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT DEDENT
Logarithm tricks for Competitive Programming | Python3 implementation to find Kth root of the number ; Function to find the Kth root of the number ; Driver code
import math NEW_LINE def kth_root ( n , k ) : NEW_LINE INDENT return ( pow ( k , ( ( 1.0 / k ) * ( math . log ( n ) / math . log ( k ) ) ) ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8.0 NEW_LINE k = 3 NEW_LINE print ( round ( kth_root ( n , k ) ) ) NEW_LINE DEDENT
Logarithm tricks for Competitive Programming | Python3 implementation to check if a number is a power of the other number ; Function to check if the number is power of K ; Logarithm function to calculate value ; Compare to the result1 or result2 both are equal ; Driver code
from math import log NEW_LINE def isPower ( n , k ) : NEW_LINE INDENT res1 = int ( log ( n ) / log ( k ) ) NEW_LINE res2 = log ( n ) / log ( k ) NEW_LINE return ( res1 == res2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8 NEW_LINE k = 2 NEW_LINE if ( isPower ( n , k ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Count of subarrays which start and end with the same element | Function to find total sub - array which start and end with same element ; Initialize result with 0 ; Array to count frequency of 1 to N ; Update frequency of A [ i ] ; Update result with sub - array contributed by number i ; Print the result ; Driver code
def cntArray ( A , N ) : NEW_LINE INDENT result = 0 NEW_LINE frequency = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT frequency [ A [ i ] ] = frequency [ A [ i ] ] + 1 NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT frequency_of_i = frequency [ i ] NEW_LINE result = result + ( ( frequency_of_i ) * ( frequency_of_i + 1 ) ) / 2 NEW_LINE DEDENT print ( int ( result ) ) NEW_LINE print ( " " ) NEW_LINE DEDENT A = [ 1 , 5 , 6 , 1 , 9 , 5 , 8 , 10 , 8 , 9 ] NEW_LINE N = len ( A ) NEW_LINE cntArray ( A , N ) NEW_LINE
Length of array pair formed where one contains all distinct elements and other all same elements | Function to find the max size possible ; Counting the maximum frequency ; Counting total distinct elements ; Find max of both the answer ; Driver code
def findMaxSize ( a , n ) : NEW_LINE INDENT frq = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT frq [ a [ i ] ] += 1 NEW_LINE DEDENT maxfrq = max ( frq ) NEW_LINE dist = n + 1 - frq . count ( 0 ) NEW_LINE ans1 = min ( maxfrq - 1 , dist ) NEW_LINE ans2 = min ( maxfrq , dist - 1 ) NEW_LINE ans = max ( ans1 , ans2 ) NEW_LINE return ans NEW_LINE DEDENT arr = [ 4 , 2 , 4 , 1 , 4 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMaxSize ( arr , n ) ) NEW_LINE
Longest subarray with all elements same | Function to find largest sub array with all equal elements . ; Traverse the array ; If element is same as previous increment temp value ; Return the required answer ; Driver code ; Function call
def subarray ( arr , n ) : NEW_LINE INDENT ans , temp = 1 , 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if arr [ i ] == arr [ i - 1 ] : NEW_LINE INDENT temp = temp + 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans = max ( ans , temp ) NEW_LINE temp = 1 NEW_LINE DEDENT DEDENT ans = max ( ans , temp ) NEW_LINE return ans NEW_LINE DEDENT arr = [ 2 , 2 , 1 , 1 , 2 , 2 , 2 , 3 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( subarray ( arr , n ) ) NEW_LINE
Min and max length subarray having adjacent element difference atmost K | Function to find the maximum and minimum length subarray ; Initialise minimum and maximum size of subarray in worst case ; Left will scan the size of possible subarray in left of selected element ; Right will scan the size of possible subarray in right of selected element ; Temp will store size of group associateed with every element ; Loop to find size of subarray for every element of array ; Left will move in left direction and compare difference between itself and element left to it ; Right will move in right direction and compare difference between itself and element right to it ; If subarray of much lesser or much greater is found than yet known then update ; Print minimum and maximum possible size of subarray ; Driver Code ; Function call to find maximum and minimum possible sub array
def findMaxMinSubArray ( arr , K , n ) : NEW_LINE INDENT min = n NEW_LINE max = 0 NEW_LINE left = 0 NEW_LINE right = n NEW_LINE tmp = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT tmp = 1 NEW_LINE left = i NEW_LINE while ( left - 1 >= 0 and abs ( arr [ left ] - arr [ left - 1 ] ) <= K ) : NEW_LINE INDENT left = left - 1 NEW_LINE tmp = tmp + 1 NEW_LINE DEDENT right = i NEW_LINE while ( right + 1 <= n - 1 and abs ( arr [ right ] - arr [ right + 1 ] ) <= K ) : NEW_LINE INDENT right = right + 1 NEW_LINE tmp = tmp + 1 NEW_LINE DEDENT if ( min > tmp ) : NEW_LINE INDENT min = tmp NEW_LINE DEDENT if ( max < tmp ) : NEW_LINE INDENT max = tmp NEW_LINE DEDENT DEDENT print ( min , end = ' , ▁ ' ) NEW_LINE print ( max , end =   ' ' ) NEW_LINE DEDENT arr = [ 1 , 2 , 5 , 6 , 7 ] NEW_LINE K = 2 NEW_LINE n = len ( arr ) NEW_LINE findMaxMinSubArray ( arr , K , n ) NEW_LINE
Count of elements which is the sum of a subarray of the given Array | Function to count element such that their exist a subarray whose sum is equal to this element ; Loop to compute frequency of the given elements ; Loop to iterate over every possible subarray of array ; Driver Code
def countElement ( arr , n ) : NEW_LINE INDENT freq = { } NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] ] = freq . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT tmpsum = arr [ i ] NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT tmpsum += arr [ j ] NEW_LINE if tmpsum in freq : NEW_LINE INDENT ans += freq [ tmpsum ] NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countElement ( arr , n ) ) NEW_LINE DEDENT
Nth root of a number using log | Python3 implementation to find the Kth root of a number using log ; Function to find the Kth root of the number using log function ; Driver Code
import numpy as np NEW_LINE def kthRoot ( n , k ) : NEW_LINE INDENT return pow ( k , ( ( 1.0 / k ) * ( np . log ( n ) / np . log ( k ) ) ) ) NEW_LINE DEDENT n = 81 NEW_LINE k = 4 NEW_LINE print ( " % .6f " % kthRoot ( n , k ) ) NEW_LINE
Make A , B and C equal by adding total value N to them | Python3 Program to Distribute integer N among A , B , C such that they become equal ; find maximum among the three elements ; summation of three elements ; check if difference is divisible by 3 ; Driver code
def distributeN ( A , B , C , n ) : NEW_LINE INDENT maximum = max ( A , B , C ) NEW_LINE sum = A + B + C NEW_LINE p = ( 3 * maximum ) - sum NEW_LINE diff = n - p NEW_LINE if diff < 0 or diff % 3 : NEW_LINE INDENT print " No " NEW_LINE DEDENT else : NEW_LINE INDENT print " Yes " NEW_LINE DEDENT DEDENT A = 10 NEW_LINE B = 20 NEW_LINE C = 15 NEW_LINE n = 14 NEW_LINE distributeN ( A , B , C , n ) NEW_LINE
Represent N as sum of K odd numbers with repetitions allowed | Function to print the representation ; N must be greater than equal to 2 * K and must be odd ; Driver Code
def sumOddNumbers ( N , K ) : NEW_LINE INDENT check = N - ( K - 1 ) NEW_LINE if ( check > 0 and check % 2 == 1 ) : NEW_LINE INDENT for i in range ( 0 , K - 1 ) : NEW_LINE INDENT print ( "1" , end = " ▁ " ) NEW_LINE DEDENT print ( check , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT N = 5 NEW_LINE K = 3 ; NEW_LINE sumOddNumbers ( N , K ) NEW_LINE
Number of continuous reductions of A from B or B from A to make them ( 1 , 1 ) | Function to find the minimum number of steps required ; Condition to check if it is not possible to reach ; Condition to check if the pair is reached to 1 , 1 ; Condition to change the A as the maximum element ; Driver Code
def minimumSteps ( a , b , c ) : NEW_LINE INDENT if a < 1 or b < 1 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if a == 1 and b == 1 : NEW_LINE INDENT return c NEW_LINE DEDENT if a < b : NEW_LINE INDENT a , b = b , a NEW_LINE DEDENT return minimumSteps ( a - b , b , c + 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 75 ; b = 17 NEW_LINE print ( minimumSteps ( a , b , 0 ) ) NEW_LINE DEDENT
Longest Mountain Subarray | Function to find the longest mountain subarray ; If the size of the array is less than 3 , the array won 't show  mountain like behaviour ; When a new mountain sub - array is found , there is a need to set the variables k , j to - 1 in order to help calculate the length of new mountain sub - array ; j marks the starting index of a new mountain sub - array . So set the value of j to current index i . ; Checks if next element is less than current element ; Checks if starting element exists or not , if the starting element of the mountain sub - array exists then the index of ending element is stored in k ; This condition checks if both starting index and ending index exists or not , if yes , the length is calculated . ; d holds the length of the longest mountain sub - array . If the current length is greater than the calculated length , then value of d is updated . ; Ignore if there is no increase or decrease in the value of the next element ; Checks and calculates the length if last element of the array is the last element of a mountain sub - array ; Driver code
def LongestMountain ( a ) : NEW_LINE INDENT i = 0 NEW_LINE j = - 1 NEW_LINE k = - 1 NEW_LINE p = 0 NEW_LINE d = 0 NEW_LINE n = 0 NEW_LINE if ( len ( a ) < 3 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for i in range ( len ( a ) - 1 ) : NEW_LINE INDENT if ( a [ i + 1 ] > a [ i ] ) : NEW_LINE INDENT if ( k != - 1 ) : NEW_LINE INDENT k = - 1 NEW_LINE j = - 1 NEW_LINE DEDENT if ( j == - 1 ) : NEW_LINE INDENT j = i NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( a [ i + 1 ] < a [ i ] ) : NEW_LINE INDENT if ( j != - 1 ) : NEW_LINE INDENT k = i + 1 NEW_LINE DEDENT if ( k != - 1 and j != - 1 ) : NEW_LINE INDENT if ( d < k - j + 1 ) : NEW_LINE INDENT d = k - j + 1 NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT k = - 1 NEW_LINE j = - 1 NEW_LINE DEDENT DEDENT DEDENT if ( k != - 1 and j != - 1 ) : NEW_LINE INDENT if ( d < k - j + 1 ) : NEW_LINE INDENT d = k - j + 1 NEW_LINE DEDENT DEDENT return d NEW_LINE DEDENT d = [ 1 , 3 , 1 , 4 , 5 , 6 , 7 , 8 , 9 , 8 , 7 , 6 , 5 ] NEW_LINE print ( LongestMountain ( d ) ) NEW_LINE
Check whether an Array can be made 0 by splitting and merging repeatedly | Function that finds if it is possible to make the array contain only 1 element i . e . 0 ; Check if element is odd ; According to the logic in above approach ; Driver code
def solve ( A ) : NEW_LINE INDENT ctr = 0 NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT if A [ i ] % 2 == 1 : NEW_LINE INDENT ctr += 1 NEW_LINE DEDENT DEDENT if ctr % 2 == 1 : NEW_LINE INDENT return ' No ' NEW_LINE DEDENT else : NEW_LINE INDENT return ' Yes ' NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 9 , 17 ] NEW_LINE print ( solve ( arr ) ) NEW_LINE DEDENT
Count of perfect squares of given length | Python3 Program to count perfect squares of given length ; Function to check if a number is perfect square ; Find floating point value of square root of x . ; If square root is an integer ; Function to return the count of n digit perfect squares ; Initialize result ; Traverse through all numbers of n digits ; Check if current number ' i ' is perfect square ; Driver code
import math ; NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT sr = math . sqrt ( x ) ; NEW_LINE return ( ( sr - math . floor ( sr ) ) == 0 ) ; NEW_LINE DEDENT def countSquares ( n ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for i in range ( int ( math . pow ( 10 , ( n - 1 ) ) ) , int ( math . pow ( 10 , n ) ) ) : NEW_LINE INDENT if ( i != 0 and isPerfectSquare ( i ) ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE DEDENT DEDENT return cnt ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( countSquares ( n ) ) ; NEW_LINE
Count of perfect squares of given length | Python3 program to count perfect squares of given length ; Function to return the count of n digit perfect squares ; Driver code
import math NEW_LINE def countSquares ( n ) : NEW_LINE INDENT r = math . ceil ( math . sqrt ( math . pow ( 10 , n ) ) ) ; NEW_LINE l = math . ceil ( math . sqrt ( math . pow ( 10 , n - 1 ) ) ) ; NEW_LINE return r - l ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( countSquares ( n ) ) ; NEW_LINE
Sum of alternating sign cubes of first N Natural numbers | Function to compute sum of the cubes with alternating sign ; Driver Code
def summation ( N ) : NEW_LINE INDENT co = ( N + 1 ) / 2 NEW_LINE co = int ( co ) NEW_LINE ce = N / 2 NEW_LINE ce = int ( ce ) NEW_LINE se = 2 * ( ( ce * ( ce + 1 ) ) * ( ce * ( ce + 1 ) ) ) NEW_LINE so = ( co * co ) * ( 2 * ( co * co ) - 1 ) NEW_LINE return so - se NEW_LINE DEDENT n = 3 NEW_LINE print ( summation ( n ) ) NEW_LINE
Program to check if N is a Star Number | Python3 implementation to check that a number is a star number or not ; Function to check that the number is a star number ; Condition to check if the number is a star number ; Driver Code ; Function call
import math NEW_LINE def isStar ( N ) : NEW_LINE INDENT n = ( math . sqrt ( 24 * N + 12 ) + 6 ) / 6 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT i = 13 NEW_LINE if isStar ( i ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Smallest number to make Array sum at most K by dividing each element | Python3 program to find the smallest number such that the sum of the array becomes less than or equal to K when every element of the array is divided by that number ; Function to find the smallest number such that the sum of the array becomes less than or equal to K when every element of the array is divided by that number ; Binary search between 1 and 10 ^ 9 ; Calculating the new sum after dividing every element by mid ; If after dividing by mid , if the new sum is less than or equal to limit move low to mid + 1 ; Else , move mid + 1 to high ; Returning the minimum number ; Driver code
from math import ceil NEW_LINE def findMinDivisor ( arr , n , limit ) : NEW_LINE INDENT low = 0 NEW_LINE high = 10 ** 9 NEW_LINE while ( low < high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += ceil ( arr [ i ] / mid ) NEW_LINE DEDENT if ( sum <= limit ) : NEW_LINE INDENT high = mid NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return low NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE K = 6 NEW_LINE print ( findMinDivisor ( arr , N , K ) ) NEW_LINE DEDENT
Count of triplets ( a , b , c ) in the Array such that a divides b and b divides c | Function to count triplets ; Iterate for middle element ; Iterate left array for a [ i ] ; Iterate right array for a [ k ] ; Return the final result ; Driver code
def getCount ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( 1 , n - 1 ) : NEW_LINE INDENT p , q = 0 , 0 NEW_LINE for i in range ( j ) : NEW_LINE INDENT if ( arr [ j ] % arr [ i ] == 0 ) : NEW_LINE INDENT p += 1 NEW_LINE DEDENT DEDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT if ( arr [ k ] % arr [ j ] == 0 ) : NEW_LINE INDENT q += 1 NEW_LINE DEDENT DEDENT count += p * q NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( getCount ( arr , N ) ) NEW_LINE DEDENT
Find pair with maximum ratio in an Array | Function to find the maximum pair possible for the array ; Loop to iterate over every possible pair in the array ; Check pair ( x , y ) as well as ( y , x ) for maximum value ; Update the answer ; Driver Code
def computeMaxValue ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT val = max ( arr [ i ] / arr [ j ] , arr [ j ] / arr [ i ] ) NEW_LINE ans = max ( ans , val ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 15 , 10 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( computeMaxValue ( arr , n ) ) NEW_LINE DEDENT