codeparrot/xlcost-text-to-code huggingface dataset:
Array with GCD of any of its subset belongs to the given array | Python 3 implementation to generate the required array ; Function to find gcd of array of numbers ; Function to generate the array with required constraints . ; computing GCD of the given set ; Solution exists if GCD of array is equal to the minimum element of the array ; Printing the built array ; Driver function ; Taking in the input and initializing the set STL set in cpp has a property that it maintains the elements in sorted order , thus we do not need to sort them externally ; Calling the computing function .
from math import gcd NEW_LINE def findGCD ( arr , n ) : NEW_LINE INDENT result = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT result = gcd ( arr [ i ] , result ) NEW_LINE DEDENT return result NEW_LINE DEDENT def compute ( arr , n ) : NEW_LINE INDENT answer = [ ] NEW_LINE GCD_of_array = findGCD ( arr , n ) NEW_LINE if ( GCD_of_array == arr [ 0 ] ) : NEW_LINE INDENT answer . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT answer . append ( arr [ 0 ] ) NEW_LINE answer . append ( arr [ i ] ) NEW_LINE DEDENT for i in range ( len ( answer ) ) : NEW_LINE INDENT print ( answer [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " No ▁ array ▁ can ▁ be ▁ build " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE input = [ 2 , 5 , 6 , 7 , 11 ] NEW_LINE GCD = set ( ) NEW_LINE for i in range ( len ( input ) ) : NEW_LINE INDENT GCD . add ( input [ i ] ) NEW_LINE DEDENT arr = [ ] NEW_LINE for i in GCD : NEW_LINE INDENT arr . append ( i ) NEW_LINE DEDENT compute ( arr , n ) NEW_LINE DEDENT
Combinatorics on ordered trees | Function returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to calculate the number of trees with exactly k leaves . ; Function to calculate total number of Nodes of degree d in these trees . ; Function to calculate the number of trees in which the root has degree r . ; Driver code ; Number of trees having 3 edges and exactly 2 leaves ; Number of Nodes of degree 3 in a tree having 4 edges ; Number of trees having 3 edges where root has degree 2
def binomialCoeff ( n , k ) : NEW_LINE INDENT C = [ [ 0 for i in range ( k + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , k ) + 1 ) : NEW_LINE INDENT if ( j == 0 or j == i ) : NEW_LINE INDENT C [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT return C [ n ] [ k ] NEW_LINE DEDENT def k_Leaves ( n , k ) : NEW_LINE INDENT ans = ( ( binomialCoeff ( n , k ) * binomialCoeff ( n , k - 1 ) ) // n ) NEW_LINE print ( " Number ▁ of ▁ trees ▁ " , " having ▁ 4 ▁ edges ▁ and ▁ exactly ▁ 2 ▁ " , " leaves ▁ : ▁ " , ans ) NEW_LINE DEDENT def numberOfNodes ( n , d ) : NEW_LINE INDENT ans = binomialCoeff ( 2 * n - 1 - d , n - 1 ) NEW_LINE print ( " Number ▁ of ▁ Nodes ▁ " , " of ▁ degree ▁ 1 ▁ in ▁ a ▁ tree ▁ having ▁ 4 ▁ " , " edges ▁ : ▁ " , ans ) NEW_LINE DEDENT def rootDegreeR ( n , r ) : NEW_LINE INDENT ans = r * binomialCoeff ( 2 * n - 1 - r , n - 1 ) NEW_LINE ans = ans // n NEW_LINE print ( " Number ▁ of ▁ trees ▁ " , " having ▁ 4 ▁ edges ▁ where ▁ root ▁ has ▁ " , " degree ▁ 2 ▁ : ▁ " , ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT k_Leaves ( 3 , 2 ) NEW_LINE numberOfNodes ( 3 , 1 ) NEW_LINE rootDegreeR ( 3 , 2 ) NEW_LINE DEDENT
Repeated Unit Divisibility | To find least value of k ; To check n is coprime or not ; to store R ( k ) mod n and 10 ^ k mod n value ; Driver code
def repUnitValue ( n ) : NEW_LINE INDENT if ( n % 2 == 0 or n % 5 == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT rem = 1 NEW_LINE power = 1 NEW_LINE k = 1 NEW_LINE while ( rem % n != 0 ) : NEW_LINE INDENT k += 1 NEW_LINE power = power * 10 % n NEW_LINE rem = ( rem + power ) % n NEW_LINE DEDENT return k NEW_LINE DEDENT n = 13 NEW_LINE print ( repUnitValue ( n ) ) NEW_LINE
First N natural can be divided into two sets with given difference and co | Python3 code to determine whether numbers 1 to N can be divided into two sets such that absolute difference between sum of these two sets is M and these two sum are co - prime ; function that returns boolean value on the basis of whether it is possible to divide 1 to N numbers into two sets that satisfy given conditions . ; initializing total sum of 1 to n numbers ; since ( 1 ) total_sum = sum_s1 + sum_s2 and ( 2 ) m = sum_s1 - sum_s2 assuming sum_s1 > sum_s2 . solving these 2 equations to get sum_s1 and sum_s2 ; total_sum = sum_s1 + sum_s2 and therefore ; if total sum is less than the absolute difference then there is no way we can split n numbers into two sets so return false ; check if these two sums are integers and they add up to total sum and also if their absolute difference is m . ; Now if two sum are co - prime then return true , else return false . ; if two sums don 't add up to total  sum or if their absolute difference  is not m, then there is no way to  split n numbers, hence return false ; Driver code ; function call to determine answer
def __gcd ( a , b ) : NEW_LINE INDENT return a if ( b == 0 ) else __gcd ( b , a % b ) ; NEW_LINE DEDENT def isSplittable ( n , m ) : NEW_LINE INDENT total_sum = ( int ) ( ( n * ( n + 1 ) ) / 2 ) ; NEW_LINE sum_s1 = int ( ( total_sum + m ) / 2 ) ; NEW_LINE sum_s2 = total_sum - sum_s1 ; NEW_LINE if ( total_sum < m ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT if ( sum_s1 + sum_s2 == total_sum and sum_s1 - sum_s2 == m ) : NEW_LINE INDENT return ( __gcd ( sum_s1 , sum_s2 ) == 1 ) ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT n = 5 ; NEW_LINE m = 7 ; NEW_LINE if ( isSplittable ( n , m ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Making zero array by decrementing pairs of adjacent | Python3 implementation of the above approach ; converting array element into number ; Check if divisible by 11 ; Driver Code
def isPossibleToZero ( a , n ) : NEW_LINE INDENT num = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT num = num * 10 + a [ i ] ; NEW_LINE DEDENT return ( num % 11 == 0 ) ; NEW_LINE DEDENT arr = [ 0 , 1 , 1 , 0 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE if ( isPossibleToZero ( arr , n ) ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT
Blum Integer | Function to cheek if number is Blum Integer ; to store prime numbers from 2 to n ; If prime [ i ] is not changed , then it is a prime ; Update all multiples of p ; to check if the given odd integer is Blum Integer or not ; checking the factors are of 4 t + 3 form or not ; give odd integer greater than 20
def isBlumInteger ( n ) : NEW_LINE INDENT prime = [ True ] * ( n + 1 ) NEW_LINE i = 2 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( prime [ i ] == True ) : NEW_LINE INDENT for j in range ( i * 2 , n + 1 , i ) : NEW_LINE INDENT prime [ j ] = False NEW_LINE DEDENT DEDENT i = i + 1 NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT if ( ( n % i == 0 ) and ( ( i - 3 ) % 4 ) == 0 ) : NEW_LINE INDENT q = int ( n / i ) NEW_LINE return ( q != i and prime [ q ] and ( q - 3 ) % 4 == 0 ) NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT n = 249 NEW_LINE if ( isBlumInteger ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Program to calculate value of nCr | Python 3 program To calculate The Value Of nCr ; Returns factorial of n ; Driver code
def nCr ( n , r ) : NEW_LINE INDENT return ( fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ) NEW_LINE DEDENT def fact ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT return res NEW_LINE DEDENT n = 5 NEW_LINE r = 3 NEW_LINE print ( int ( nCr ( n , r ) ) ) NEW_LINE
Program to print the sum of the given nth term | Python3 program to illustrate ... Summation of series ; function to calculate sum of series ; Sum of n terms is n ^ 2 ; Driver Code
import math NEW_LINE def summingSeries ( n ) : NEW_LINE INDENT return math . pow ( n , 2 ) NEW_LINE DEDENT n = 100 NEW_LINE print ( " The ▁ sum ▁ of ▁ n ▁ term ▁ is : ▁ " , summingSeries ( n ) ) NEW_LINE
Brahmagupta Fibonacci Identity | Python 3 code to verify Brahmagupta Fibonacci identity ; represent the product as sum of 2 squares ; check identity criteria ; 1 ^ 2 + 2 ^ 2 ; 3 ^ 2 + 4 ^ 2 ; express product of sum of 2 squares as sum of ( sum of 2 squares )
def find_sum_of_two_squares ( a , b ) : NEW_LINE INDENT ab = a * b NEW_LINE i = 0 ; NEW_LINE while ( i * i <= ab ) : NEW_LINE INDENT j = i NEW_LINE while ( i * i + j * j <= ab ) : NEW_LINE INDENT if ( i * i + j * j == ab ) : NEW_LINE INDENT print ( i , " ^ 2 ▁ + ▁ " , j , " ^ 2 ▁ = ▁ " , ab ) NEW_LINE DEDENT j += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT a = 1 * 1 + 2 * 2 NEW_LINE b = 3 * 3 + 4 * 4 NEW_LINE print ( " Representation ▁ of ▁ a ▁ * ▁ b ▁ as ▁ sum " " ▁ of ▁ 2 ▁ squares : " ) NEW_LINE find_sum_of_two_squares ( a , b ) NEW_LINE
Tetrahedral Numbers | Python3 Program to find the nth tetrahedral number ; Function to find Tetrahedral Number ; Driver Code
def tetrahedralNumber ( n ) : NEW_LINE INDENT return ( n * ( n + 1 ) * ( n + 2 ) ) / 6 NEW_LINE DEDENT def tetrahedralNumber ( n ) : NEW_LINE INDENT return ( n * ( n + 1 ) * ( n + 2 ) ) / 6 NEW_LINE DEDENT n = 5 NEW_LINE print ( tetrahedralNumber ( n ) ) NEW_LINE
Euler 's Four Square Identity | function to check euler four square identity ; loops checking the sum of squares ; sum of 2 squares ; sum of 3 squares ; sum of 4 squares ; product of 2 numbers represented as sum of four squares i , j , k , l ; product of 2 numbers a and b represented as sum of four squares i , j , k , l ; given numbers can be represented as sum of 4 squares By euler 's four square identity product also can be represented as sum of 4 squares
def check_euler_four_square_identity ( a , b , ab ) : NEW_LINE INDENT s = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( i * i <= ab ) : NEW_LINE INDENT s = i * i ; NEW_LINE j = i ; NEW_LINE while ( j * j <= ab ) : NEW_LINE INDENT s = j * j + i * i ; NEW_LINE k = j ; NEW_LINE while ( k * k <= ab ) : NEW_LINE INDENT s = k * k + j * j + i * i ; NEW_LINE l = k ; NEW_LINE while ( l * l <= ab ) : NEW_LINE INDENT s = l * l + k * k + j * j + i * i ; NEW_LINE if ( s == ab ) : NEW_LINE INDENT print ( " i ▁ = " , i ) ; NEW_LINE print ( " j ▁ = " , j ) ; NEW_LINE print ( " k ▁ = " , k ) ; NEW_LINE print ( " l ▁ = " , l ) ; NEW_LINE print ( " Product ▁ of ▁ " , a , " and " , b , end = " " ) ; NEW_LINE print ( " ▁ can ▁ be ▁ written ▁ as ▁ sum ▁ of " , " squares ▁ of ▁ i , ▁ j , ▁ k , ▁ l " ) ; NEW_LINE print ( ab , " = ▁ " , end = " " ) ; NEW_LINE print ( i , " * " , i , " + ▁ " , end = " " ) ; NEW_LINE print ( j , " * " , j , " + ▁ " , end = " " ) ; NEW_LINE print ( k , " * " , k , " + ▁ " , end = " " ) ; NEW_LINE print ( l , " * " , l ) ; NEW_LINE print ( " " ) ; NEW_LINE DEDENT l += 1 ; NEW_LINE DEDENT k += 1 ; NEW_LINE DEDENT j += 1 ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT DEDENT ab = a * b ; NEW_LINE check_euler_four_square_identity ( a , b , ab ) ; NEW_LINE
Number of solutions to Modular Equations | Python Program to find number of possible values of X to satisfy A mod X = B ; Returns the number of divisors of ( A - B ) greater than B ; if N is divisible by i ; count only the divisors greater than B ; checking if a divisor isnt counted twice ; Utility function to calculate number of all possible values of X for which the modular equation holds true ; if A = B there are infinitely many solutions to equation or we say X can take infinitely many values > A . We return - 1 in this case ; if A < B , there are no possible values of X satisfying the equation ; the last case is when A > B , here we calculate the number of divisors of ( A - B ) , which are greater than B ; Wrapper function for numberOfPossibleWaysUtil ( ) ; if infinitely many solutions available ; Driver code
import math NEW_LINE def calculateDivisors ( A , B ) : NEW_LINE INDENT N = A - B NEW_LINE noOfDivisors = 0 NEW_LINE a = math . sqrt ( N ) NEW_LINE for i in range ( 1 , int ( a + 1 ) ) : NEW_LINE INDENT if ( ( N % i == 0 ) ) : NEW_LINE INDENT if ( i > B ) : NEW_LINE INDENT noOfDivisors += 1 NEW_LINE DEDENT if ( ( N / i ) != i and ( N / i ) > B ) : NEW_LINE INDENT noOfDivisors += 1 ; NEW_LINE DEDENT DEDENT DEDENT return noOfDivisors NEW_LINE DEDENT def numberOfPossibleWaysUtil ( A , B ) : NEW_LINE INDENT if ( A == B ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( A < B ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT noOfDivisors = 0 NEW_LINE noOfDivisors = calculateDivisors ; NEW_LINE return noOfDivisors NEW_LINE DEDENT def numberOfPossibleWays ( A , B ) : NEW_LINE INDENT noOfSolutions = numberOfPossibleWaysUtil ( A , B ) NEW_LINE if ( noOfSolutions == - 1 ) : NEW_LINE INDENT print ( " For ▁ A ▁ = ▁ " , A , " ▁ and ▁ B ▁ = ▁ " , B , " , ▁ X ▁ can ▁ take ▁ Infinitely ▁ many ▁ values " , " ▁ greater ▁ than ▁ " , A ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " For ▁ A ▁ = ▁ " , A , " ▁ and ▁ B ▁ = ▁ " , B , " , ▁ X ▁ can ▁ take ▁ " , noOfSolutions , " ▁ values " ) NEW_LINE DEDENT DEDENT A = 26 NEW_LINE B = 2 NEW_LINE numberOfPossibleWays ( A , B ) NEW_LINE A = 21 NEW_LINE B = 5 NEW_LINE numberOfPossibleWays ( A , B ) NEW_LINE
Perfect power ( 1 , 4 , 8 , 9 , 16 , 25 , 27 , ... ) | Python3 program to count number of numbers from 1 to n are of type x ^ y where x > 0 and y > 1 ; Function that keeps all the odd power numbers upto n ; We need exclude perfect squares . ; sort the vector ; Return sum of odd and even powers . ; Driver Code
import math NEW_LINE def powerNumbers ( n ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( 2 , int ( math . pow ( n , 1.0 / 3.0 ) ) + 1 ) : NEW_LINE INDENT j = i * i NEW_LINE while ( j * i <= n ) : NEW_LINE INDENT j = j * i NEW_LINE s = int ( math . sqrt ( j ) ) NEW_LINE if ( s * s != j ) : NEW_LINE INDENT v . append ( j ) NEW_LINE DEDENT DEDENT DEDENT v . sort ( ) NEW_LINE v = list ( dict . fromkeys ( v ) ) NEW_LINE return len ( v ) + int ( math . sqrt ( n ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( powerNumbers ( 50 ) ) NEW_LINE DEDENT
Variance and standard | Python3 program to find mean and variance of a matrix . ; variance function declaration Function for calculating mean ; Calculating sum ; Returning mean ; Function for calculating variance ; subtracting mean from elements ; a [ i ] [ j ] = fabs ( a [ i ] [ j ] ) ; squaring each terms ; taking sum ; declaring and initializing matrix ; for mean ; for variance ; for standard deviation ; displaying variance and deviation
import math ; NEW_LINE def mean ( a , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT sum += a [ i ] [ j ] ; NEW_LINE DEDENT DEDENT return math . floor ( int ( sum / ( n * n ) ) ) ; NEW_LINE DEDENT def variance ( a , n , m ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT a [ i ] [ j ] -= m ; NEW_LINE a [ i ] [ j ] *= a [ i ] [ j ] ; NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT sum += a [ i ] [ j ] ; NEW_LINE DEDENT DEDENT return math . floor ( int ( sum / ( n * n ) ) ) ; NEW_LINE DEDENT mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; NEW_LINE m = mean ( mat , 3 ) ; NEW_LINE var = variance ( mat , 3 , m ) ; NEW_LINE dev = math . sqrt ( var ) ; NEW_LINE print ( " Mean : " , m ) ; NEW_LINE print ( " Variance : " , var ) ; NEW_LINE print ( " Deviation : " , math . floor ( dev ) ) ; NEW_LINE
Find N Arithmetic Means between A and B | Prints N arithmetic means between A and B . ; Calculate common difference ( d ) ; For finding N the arithmetic mean between A and B ; Driver code
def printAMeans ( A , B , N ) : NEW_LINE INDENT d = ( B - A ) / ( N + 1 ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( int ( A + i * d ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT A = 20 ; B = 32 ; N = 5 NEW_LINE printAMeans ( A , B , N ) NEW_LINE
Sum of the series 1.2 . 3 + 2.3 . 4 + ... + n ( n + 1 ) ( n + 2 ) | function to calculate sum of series ; Driver program
def sumofseries ( n ) : NEW_LINE INDENT return int ( n * ( n + 1 ) * ( n + 2 ) * ( n + 3 ) / 4 ) NEW_LINE DEDENT print ( sumofseries ( 3 ) ) NEW_LINE
Largest number in [ 2 , 3 , . . n ] which is co | Python3 code to find Largest number in [ 2 , 3 , . . n ] which is co - prime with numbers in [ 2 , 3 , . . m ] ; Returns true if i is co - prime with numbers in set [ 2 , 3 , ... m ] ; Running the loop till square root of n to reduce the time complexity from n ; Find the minimum of square root of n and m to run the loop until the smaller one ; Check from 2 to min ( m , sqrt ( n ) ) ; def to find the largest number less than n which is Co - prime with all numbers from 2 to m ; Iterating from n to m + 1 to find the number ; checking every number for the given conditions ; The first number which satisfy the conditions is the answer ; If there is no number which satisfy the conditions , then print number does not exist . ; Driver Code
import math NEW_LINE def isValid ( i , m ) : NEW_LINE INDENT sq_i = math . sqrt ( i ) NEW_LINE sq = min ( m , sq_i ) NEW_LINE for j in range ( 2 , sq + 1 ) : NEW_LINE INDENT if ( i % j == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def findLargestNum ( n , m ) : NEW_LINE INDENT for i in range ( n , m , - 1 ) : NEW_LINE INDENT if ( isValid ( i , m ) ) : NEW_LINE INDENT print ( " { } " . format ( i ) ) ; NEW_LINE return NEW_LINE DEDENT DEDENT print ( " Number Doesn ' t Exists " ) NEW_LINE DEDENT n = 16 NEW_LINE m = 3 NEW_LINE findLargestNum ( n , m ) NEW_LINE
Check whether a given matrix is orthogonal or not |  ; Find transpose ; Find product of a [ ] [ ] and its transpose ; Since we are multiplying with transpose of itself . We use ; Check if product is identity matrix ; Driver Code
/ * Function to check orthogonalilty * / NEW_LINE def isOrthogonal ( a , m , n ) : NEW_LINE INDENT if ( m != n ) : NEW_LINE INDENT return False NEW_LINE DEDENT trans = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT trans [ i ] [ j ] = a [ j ] [ i ] NEW_LINE DEDENT DEDENT prod = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for k in range ( 0 , n ) : NEW_LINE INDENT sum = sum + ( a [ i ] [ k ] * a [ j ] [ k ] ) NEW_LINE DEDENT prod [ i ] [ j ] = sum NEW_LINE DEDENT DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT if ( i != j and prod [ i ] [ j ] != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( i == j and prod [ i ] [ j ] != 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT a = [ [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] , [ 0 , 0 , 1 ] ] NEW_LINE if ( isOrthogonal ( a , 3 , 3 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Check if given number is perfect square | Python program to find if x is a perfect square . ; Find floating point value of square root of x . ; sqrt function returns floating value so we have to convert it into integer return boolean T / F ; Driver code
import math NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT if ( x >= 0 ) : NEW_LINE INDENT sr = math . sqrt ( x ) NEW_LINE return ( ( sr * sr ) == float ( x ) ) NEW_LINE DEDENT return false NEW_LINE DEDENT x = 2502 NEW_LINE if ( isPerfectSquare ( x ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Program to print GP ( Geometric Progression ) | function to print GP ; starting number ; Common ratio ; N th term to be find
def printGP ( a , r , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT curr_term = a * pow ( r , i ) NEW_LINE print ( curr_term , end = " ▁ " ) NEW_LINE DEDENT DEDENT a = 2 NEW_LINE r = 3 NEW_LINE n = 5 NEW_LINE printGP ( a , r , n ) NEW_LINE
HCF of array of fractions ( or rational numbers ) | Python 3 program to find HCF of array of ; find hcf of numerator series ; return hcf of numerator ; find lcm of denominator series ; ans contains LCM of arr [ 0 ] [ 1 ] , . . arr [ i ] [ 1 ] ; return lcm of denominator ; Core Function ; found hcf of numerator ; found lcm of denominator ; return result ; Driver Code ; Initialize the every row with size 2 ( 1 for numerator and 2 for denominator ) ; function for calculate the result ; print the result
from math import gcd NEW_LINE def findHcf ( arr , size ) : NEW_LINE INDENT ans = arr [ 0 ] [ 0 ] NEW_LINE for i in range ( 1 , size , 1 ) : NEW_LINE INDENT ans = gcd ( ans , arr [ i ] [ 0 ] ) NEW_LINE DEDENT return ( ans ) NEW_LINE DEDENT def findLcm ( arr , size ) : NEW_LINE INDENT ans = arr [ 0 ] [ 1 ] NEW_LINE for i in range ( 1 , size , 1 ) : NEW_LINE INDENT ans = int ( ( ( ( arr [ i ] [ 1 ] * ans ) ) / ( gcd ( arr [ i ] [ 1 ] , ans ) ) ) ) NEW_LINE DEDENT return ( ans ) NEW_LINE DEDENT def hcfOfFraction ( arr , size ) : NEW_LINE INDENT hcf_of_num = findHcf ( arr , size ) NEW_LINE lcm_of_deno = findLcm ( arr , size ) NEW_LINE result = [ 0 for i in range ( 2 ) ] NEW_LINE result [ 0 ] = hcf_of_num NEW_LINE result [ 1 ] = lcm_of_deno NEW_LINE i = int ( result [ 0 ] / 2 ) NEW_LINE while ( i > 1 ) : NEW_LINE INDENT if ( ( result [ 1 ] % i == 0 ) and ( result [ 0 ] % i == 0 ) ) : NEW_LINE INDENT result [ 1 ] = int ( result [ 1 ] / i ) NEW_LINE result [ 0 ] = ( result [ 0 ] / i ) NEW_LINE DEDENT DEDENT return ( result ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT size = 4 NEW_LINE arr = [ 0 for i in range ( size ) ] NEW_LINE for i in range ( size ) : NEW_LINE INDENT arr [ i ] = [ 0 for i in range ( 2 ) ] NEW_LINE DEDENT arr [ 0 ] [ 0 ] = 9 NEW_LINE arr [ 0 ] [ 1 ] = 10 NEW_LINE arr [ 1 ] [ 0 ] = 12 NEW_LINE arr [ 1 ] [ 1 ] = 25 NEW_LINE arr [ 2 ] [ 0 ] = 18 NEW_LINE arr [ 2 ] [ 1 ] = 35 NEW_LINE arr [ 3 ] [ 0 ] = 21 NEW_LINE arr [ 3 ] [ 1 ] = 40 NEW_LINE result = hcfOfFraction ( arr , size ) NEW_LINE print ( result [ 0 ] , " , " , result [ 1 ] ) NEW_LINE DEDENT
Space efficient iterative method to Fibonacci number | get second MSB ; consectutively set all the bits ; returns the second MSB ; Multiply function ; Function to calculate F [ ] [ ] raise to the power n ; Base case ; take 2D array to store number 's ; run loop till MSB > 0 ; To return fibonacci number ; Given n
def getMSB ( n ) : NEW_LINE INDENT n |= n >> 1 NEW_LINE n |= n >> 2 NEW_LINE n |= n >> 4 NEW_LINE n |= n >> 8 NEW_LINE n |= n >> 16 NEW_LINE return ( ( n + 1 ) >> 2 ) NEW_LINE DEDENT def multiply ( F , M ) : NEW_LINE INDENT x = F [ 0 ] [ 0 ] * M [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] * M [ 1 ] [ 0 ] NEW_LINE y = F [ 0 ] [ 0 ] * M [ 0 ] [ 1 ] + F [ 0 ] [ 1 ] * M [ 1 ] [ 1 ] NEW_LINE z = F [ 1 ] [ 0 ] * M [ 0 ] [ 0 ] + F [ 1 ] [ 1 ] * M [ 1 ] [ 0 ] NEW_LINE w = F [ 1 ] [ 0 ] * M [ 0 ] [ 1 ] + F [ 1 ] [ 1 ] * M [ 1 ] [ 1 ] NEW_LINE F [ 0 ] [ 0 ] = x NEW_LINE F [ 0 ] [ 1 ] = y NEW_LINE F [ 1 ] [ 0 ] = z NEW_LINE F [ 1 ] [ 1 ] = w NEW_LINE DEDENT def power ( F , n ) : NEW_LINE INDENT if ( n == 0 or n == 1 ) : NEW_LINE INDENT return NEW_LINE DEDENT M = [ [ 1 , 1 ] , [ 1 , 0 ] ] NEW_LINE m = getMSB ( n ) NEW_LINE while m : NEW_LINE INDENT multiply ( F , F ) NEW_LINE if ( n & m ) : NEW_LINE INDENT multiply ( F , M ) NEW_LINE DEDENT m = m >> 1 NEW_LINE DEDENT DEDENT def fib ( n ) : NEW_LINE INDENT F = [ [ 1 , 1 ] , [ 1 , 0 ] ] NEW_LINE if ( n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT power ( F , n - 1 ) NEW_LINE return F [ 0 ] [ 0 ] NEW_LINE DEDENT n = 6 NEW_LINE print ( fib ( n ) ) NEW_LINE