codeparrot/xlcost-text-to-code huggingface dataset:
Leyland Number | Python3 program to print first N Leyland Numbers . ; Print first n Leyland Number . ; Outer loop for x from 2 to n . ; Inner loop for y from 2 to x . ; Calculating x ^ y + y ^ x ; Sorting the all Leyland Number . ; Printing first n Leyland number . ; Driver Code
import math NEW_LINE def leyland ( n ) : NEW_LINE INDENT ans = [ ] NEW_LINE x = 2 NEW_LINE y = 2 NEW_LINE while x <= n : NEW_LINE INDENT y = 2 NEW_LINE while y <= x : NEW_LINE INDENT temp = pow ( x , y ) + pow ( y , x ) NEW_LINE ans . append ( temp ) ; NEW_LINE y = y + 1 NEW_LINE DEDENT x = x + 1 NEW_LINE DEDENT ans . sort ( ) ; NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE i = i + 1 NEW_LINE DEDENT DEDENT n = 6 NEW_LINE leyland ( n ) NEW_LINE
Nicomachusâ €™ s Theorem ( Sum of k | Return the sum of k - th group of positive odd integers . ; Finding first element of kth group . ; Finding the sum . ; Driver code
def kthgroupsum ( k ) : NEW_LINE INDENT cur = int ( ( k * ( k - 1 ) ) + 1 ) NEW_LINE sum = 0 NEW_LINE while k : NEW_LINE INDENT sum += cur NEW_LINE cur += 2 NEW_LINE k = k - 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT k = 3 NEW_LINE print ( kthgroupsum ( k ) ) NEW_LINE
n | Returns n - th term of the series 2 , 12 , 36 , 80 , 150 ; driver code
def nthTerm ( n ) : NEW_LINE INDENT return ( n * n ) + ( n * n * n ) NEW_LINE DEDENT n = 4 NEW_LINE print ( nthTerm ( n ) ) NEW_LINE
Sum of the series 1 , 3 , 6 , 10. . . ( Triangular Numbers ) | Function to find the sum of series ; Driver code
def seriesSum ( n ) : NEW_LINE INDENT return int ( ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ) NEW_LINE DEDENT n = 4 NEW_LINE print ( seriesSum ( n ) ) NEW_LINE
Calculate speed , distance and time | Function to calculate speed ; Function to calculate distance traveled ; Function to calculate time taken ; Calling function cal_speed ( ) ; Calling function cal_dis ( ) ; Calling function cal_time ( )
def cal_speed ( dist , time ) : NEW_LINE INDENT print ( " ▁ Distance ( km ) ▁ : " , dist ) ; NEW_LINE print ( " ▁ Time ( hr ) ▁ : " , time ) ; NEW_LINE return dist / time ; NEW_LINE DEDENT def cal_dis ( speed , time ) : NEW_LINE INDENT print ( " ▁ Time ( hr ) ▁ : " , time ) ; NEW_LINE print ( " ▁ Speed ( km ▁ / ▁ hr ) ▁ : " , speed ) ; NEW_LINE return speed * time ; NEW_LINE DEDENT def cal_time ( dist , speed ) : NEW_LINE INDENT print ( " ▁ Distance ( km ) ▁ : " , dist ) ; NEW_LINE print ( " ▁ Speed ( km ▁ / ▁ hr ) ▁ : " , speed ) ; NEW_LINE return speed * dist ; NEW_LINE DEDENT print ( " ▁ The ▁ calculated ▁ Speed ( km ▁ / ▁ hr ) ▁ is ▁ : " , cal_speed ( 45.9 , 2.0 ) ) ; NEW_LINE print ( " " ) ; NEW_LINE print ( " ▁ The ▁ calculated ▁ Distance ( km ) ▁ : " , cal_dis ( 62.9 , 2.5 ) ) ; NEW_LINE print ( " " ) ; NEW_LINE print ( " ▁ The ▁ calculated ▁ Time ( hr ) ▁ : " , cal_time ( 48.0 , 4.5 ) ) ; NEW_LINE
Find n | Function to find the nth term of series ; Loop to add numbers ; Driver code
def term ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ans = ans + i NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 4 NEW_LINE print ( term ( n ) ) NEW_LINE
Find the average of first N natural numbers | Return the average of first n natural numbers ; Driven Program
def avgOfFirstN ( n ) : NEW_LINE INDENT return ( float ) ( 1 + n ) / 2 ; NEW_LINE DEDENT n = 20 NEW_LINE print ( avgOfFirstN ( n ) ) NEW_LINE
Find the sum of the series 1 + 11 + 111 + 1111 + ... . . upto n terms | Python3 program to find the sum of the series 1 + 11 + 111 + 1111 + ... . ; Function for finding summation ; Driver Code
import math NEW_LINE def summation ( n ) : NEW_LINE INDENT return int ( ( pow ( 10 , n + 1 ) - 10 - ( 9 * n ) ) / 81 ) ; NEW_LINE DEDENT print ( summation ( 5 ) ) ; NEW_LINE
Sum of the Series 1 + x / 1 + x ^ 2 / 2 + x ^ 3 / 3 + . . + x ^ n / n | Python 3 code to print the sum of the series ; Driver code
def sum ( x , n ) : NEW_LINE INDENT total = 1.0 NEW_LINE multi = x NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT total = total + multi / i NEW_LINE multi = multi * x NEW_LINE DEDENT return total NEW_LINE DEDENT x = 2 NEW_LINE n = 5 NEW_LINE print ( round ( sum ( x , n ) , 2 ) ) NEW_LINE
Find n | Python program to find the nth term of the series 1 2 2 3 3 3 ... ; function to solve the quadratic equation ; calculating the Nth term ; Driver code
import math NEW_LINE def term ( n ) : NEW_LINE INDENT x = ( ( ( 1 ) + math . sqrt ( 1 + ( 8 * n ) ) ) / 2 ) NEW_LINE return x NEW_LINE DEDENT n = 5 NEW_LINE print ( int ( term ( n ) ) ) NEW_LINE
Deserium Number | Returns count of digits in n . ; Returns true if x is Diserium ; Compute powers of digits from right to left . ; If sum of powers is same as given number . ; Driver code
def countDigits ( n ) : NEW_LINE INDENT c = 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT c += 1 NEW_LINE n = int ( n / 10 ) NEW_LINE DEDENT return c NEW_LINE DEDENT def isDeserium ( x ) : NEW_LINE INDENT temp = x NEW_LINE p = countDigits ( x ) NEW_LINE sum = 0 NEW_LINE while ( x != 0 ) : NEW_LINE INDENT digit = int ( x % 10 ) NEW_LINE sum += pow ( digit , p ) NEW_LINE p -= 1 NEW_LINE x = int ( x / 10 ) NEW_LINE DEDENT return ( sum == temp ) NEW_LINE DEDENT x = 135 NEW_LINE if ( isDeserium ( x ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Largest number by which given 3 numbers should be divided such that they leaves same remainder | __gcd function ; function return number which divides these three number and leaves same remainder . ; We find the differences of all three pairs ; Return GCD of three differences . ; Driver program
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 sameRemainder ( a , b , c ) : NEW_LINE INDENT a1 = ( b - a ) NEW_LINE b1 = ( c - b ) NEW_LINE c1 = ( c - a ) NEW_LINE return gcd ( a1 , gcd ( b1 , c1 ) ) NEW_LINE DEDENT a = 62 NEW_LINE b = 132 NEW_LINE c = 237 NEW_LINE print ( sameRemainder ( a , b , c ) ) NEW_LINE
Find combined mean and variance of two series | Function to find mean of series . ; Function to find the standard deviation of series . ; Function to find combined variance of two different series . ; mean1 and mean2 are the mean of two arrays . ; sd1 and sd2 are the standard deviation of two array . ; combinedMean is variable to store the combined mean of both array . ; d1_square and d2_square are the combined mean deviation . ; combinedVar is variable to store combined variance of both array . ; Driver Code ; Function call to combined mean .
def mean ( arr , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + arr [ i ] ; NEW_LINE DEDENT mean = sum / n ; NEW_LINE return mean ; NEW_LINE DEDENT def sd ( arr , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + ( ( arr [ i ] - mean ( arr , n ) ) * ( arr [ i ] - mean ( arr , n ) ) ) ; NEW_LINE DEDENT sdd = sum / n ; NEW_LINE return sdd ; NEW_LINE DEDENT def combinedVariance ( arr1 , arr2 , n , m ) : NEW_LINE INDENT mean1 = mean ( arr1 , n ) ; NEW_LINE mean2 = mean ( arr2 , m ) ; NEW_LINE print ( " Mean1 : ▁ " , round ( mean1 , 2 ) , " ▁ mean2 : ▁ " , round ( mean2 , 2 ) ) ; NEW_LINE sd1 = sd ( arr1 , n ) ; NEW_LINE sd2 = sd ( arr2 , m ) ; NEW_LINE print ( " StandardDeviation1 : ▁ " , round ( sd1 , 2 ) , " ▁ StandardDeviation2 : ▁ " , round ( sd2 , 2 ) ) ; NEW_LINE combinedMean = ( n * mean1 + m * mean2 ) / ( n + m ) ; NEW_LINE print ( " Combined ▁ Mean : ▁ " , round ( combinedMean , 2 ) ) ; NEW_LINE d1_square = ( ( mean1 - combinedMean ) * ( mean1 - combinedMean ) ) ; NEW_LINE d2_square = ( ( mean2 - combinedMean ) * ( mean2 - combinedMean ) ) ; NEW_LINE print ( " d1 ▁ square : ▁ " , round ( d1_square , 2 ) , " ▁ d2 _ square : ▁ " , round ( d2_square , 2 ) ) ; NEW_LINE combinedVar = ( n * ( sd1 + d1_square ) + m * ( sd2 + d2_square ) ) / ( n + m ) ; NEW_LINE print ( " Combined ▁ Variance : ▁ " , round ( combinedVar , 2 ) ) ; NEW_LINE DEDENT arr1 = [ 23 , 45 , 34 , 78 , 12 , 76 , 34 ] ; NEW_LINE arr2 = [ 65 , 67 , 34 , 23 , 45 ] ; NEW_LINE n = len ( arr1 ) ; NEW_LINE m = len ( arr2 ) ; NEW_LINE combinedVariance ( arr1 , arr2 , n , m ) ; NEW_LINE
Check if a large number is divisible by 13 or not | Returns true if number is divisible by 13 else returns false ; Append required 0 s at the beginning . ; Same as strcat ( num , "00" ) ; in c . ; Same as strcat ( num , "0" ) ; in c . ; Alternatively add / subtract digits in group of three to result . ; Store group of three numbers in group variable . ; Generate alternate series of plus and minus ; Driver code
def checkDivisibility ( num ) : NEW_LINE INDENT length = len ( num ) NEW_LINE if ( length == 1 and num [ 0 ] == '0' ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( length % 3 == 1 ) : NEW_LINE INDENT num = str ( num ) + "00" NEW_LINE length += 2 NEW_LINE DEDENT elif ( length % 3 == 2 ) : NEW_LINE INDENT num = str ( num ) + "0" NEW_LINE length += 1 NEW_LINE DEDENT sum = 0 NEW_LINE p = 1 NEW_LINE for i in range ( length - 1 , - 1 , - 1 ) : NEW_LINE INDENT group = 0 NEW_LINE group += ord ( num [ i ] ) - ord ( '0' ) NEW_LINE i -= 1 NEW_LINE group += ( ord ( num [ i ] ) - ord ( '0' ) ) * 10 NEW_LINE i -= 1 NEW_LINE group += ( ord ( num [ i ] ) - ord ( '0' ) ) * 100 NEW_LINE sum = sum + group * p NEW_LINE p *= ( - 1 ) NEW_LINE DEDENT sum = abs ( sum ) NEW_LINE return ( sum % 13 == 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT number = "83959092724" NEW_LINE if ( checkDivisibility ( number ) ) : NEW_LINE INDENT print ( number , " is ▁ divisible ▁ by ▁ 13 . " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( number , " is ▁ not ▁ divisible ▁ by ▁ 13 . " ) NEW_LINE DEDENT DEDENT
Given two numbers a and b find all x such that a % x = b | Python3 program to find x such that a % x is equal to b . ; if a is less than b then no solution ; if a is equal to b then every number greater than a will be the solution so its infinity ; count variable store the number of values possible ; checking for both divisor and quotient whether they divide ( a - b ) completely and greater than b . ; Here y is added twice in the last iteration so 1 y should be decremented to get correct solution ; Driver code
import math NEW_LINE def modularEquation ( a , b ) : NEW_LINE INDENT if ( a < b ) : NEW_LINE INDENT print ( " No ▁ solution ▁ possible ▁ " ) NEW_LINE return NEW_LINE DEDENT if ( a == b ) : NEW_LINE INDENT print ( " Infinite ▁ Solution ▁ possible ▁ " ) NEW_LINE return NEW_LINE DEDENT count = 0 NEW_LINE n = a - b NEW_LINE y = ( int ) ( math . sqrt ( a - b ) ) NEW_LINE for i in range ( 1 , y + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( n / i > b ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT if ( i > b ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT DEDENT if ( y * y == n and y > b ) : NEW_LINE INDENT count = count - 1 NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT a = 21 NEW_LINE b = 5 NEW_LINE modularEquation ( a , b ) NEW_LINE
Check whether a number can be represented by sum of two squares | function to check if there exist two numbers sum of whose squares is n . ; store square value in hashmap ; Driver Code
def sumSquare ( n ) : NEW_LINE INDENT s = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if i * i > n : NEW_LINE INDENT break NEW_LINE DEDENT s [ i * i ] = 1 NEW_LINE if ( n - i * i ) in s . keys ( ) : NEW_LINE INDENT print ( ( n - i * i ) ** ( 1 / 2 ) , " ^ 2 ▁ + " , i , " ^ 2" ) NEW_LINE return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT n = 1 NEW_LINE if n == 1 : NEW_LINE INDENT print ( '0 ^ 2 ▁ + ▁ 1 ^ 2' ) NEW_LINE DEDENT elif ( sumSquare ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Check whether a number can be represented by sum of two squares | Check whether a number can be represented by sum of two squares using Fermat Theorem . ; Count all the prime factors . ; Ifany prime factor of the form ( 4 k + 3 ) ( 4 k + 3 ) occurs an odd number of times . ; If n itself is a x prime number and can be expressed in the form of 4 k + 3 we return false . ; Driver Code
def judgeSquareSum ( n ) : NEW_LINE INDENT i = 2 ; NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT count = 0 ; NEW_LINE if ( n % i == 0 ) : NEW_LINE INDENT while ( n % i == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE n = int ( n / i ) ; NEW_LINE DEDENT if ( i % 4 == 3 and count % 2 != 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT return n % 4 != 3 ; NEW_LINE DEDENT n = 17 ; NEW_LINE if ( judgeSquareSum ( n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Total no of 1 's in numbers | function to count the frequency of 1. ; Driver Code
def countDigitOne ( n ) : NEW_LINE INDENT countr = 0 ; NEW_LINE i = 1 ; NEW_LINE while ( i <= n ) : NEW_LINE INDENT divider = i * 10 ; NEW_LINE countr += ( int ( n / divider ) * i + min ( max ( n % divider - i + 1 , 0 ) , i ) ) ; NEW_LINE i *= 10 ; NEW_LINE DEDENT return countr ; NEW_LINE DEDENT n = 13 ; NEW_LINE print ( countDigitOne ( n ) ) ; NEW_LINE n = 113 ; NEW_LINE print ( countDigitOne ( n ) ) ; NEW_LINE n = 205 ; NEW_LINE print ( countDigitOne ( n ) ) ; NEW_LINE
Largest number with prime digits | check if character is prime ; replace with previous prime character ; if 2 erase s [ i ] and replace next with 7 ; find first non prime char ; find first char greater than 2 ; like 20 ; like 7721 ; replace remaining with 7 ; Driver code
def isPrime ( c ) : NEW_LINE INDENT return ( c == '2' or c == '3' or c == '5' or c == '7' ) NEW_LINE DEDENT def decrease ( s , i ) : NEW_LINE INDENT if ( s [ i ] <= '2' ) : NEW_LINE INDENT s . pop ( i ) NEW_LINE s [ i ] = '7' NEW_LINE DEDENT elif ( s [ i ] == '3' ) : NEW_LINE INDENT s [ i ] = '2' NEW_LINE DEDENT elif ( s [ i ] <= '5' ) : NEW_LINE INDENT s [ i ] = '3' NEW_LINE DEDENT elif ( s [ i ] <= '7' ) : NEW_LINE INDENT s [ i ] = '5' NEW_LINE DEDENT else : NEW_LINE INDENT s [ i ] = '7' NEW_LINE DEDENT DEDENT def primeDigits ( s ) : NEW_LINE INDENT s = [ i for i in s ] NEW_LINE i = 0 NEW_LINE while i < len ( s ) : NEW_LINE INDENT if ( isPrime ( s [ i ] ) == False ) : NEW_LINE INDENT while ( s [ i ] <= '2' and i >= 0 ) : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT if ( i < 0 ) : NEW_LINE INDENT i = 0 NEW_LINE decrease ( s , i ) NEW_LINE DEDENT else : NEW_LINE INDENT decrease ( s , i ) NEW_LINE DEDENT for j in range ( i + 1 , len ( s ) ) : NEW_LINE INDENT s [ j ] = '7' NEW_LINE DEDENT break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return " " . join ( s ) NEW_LINE DEDENT s = "45" NEW_LINE print ( primeDigits ( s ) ) NEW_LINE s = "1000" NEW_LINE print ( primeDigits ( s ) ) NEW_LINE s = "7721" NEW_LINE print ( primeDigits ( s ) ) NEW_LINE s = "7221" NEW_LINE print ( primeDigits ( s ) ) NEW_LINE s = "74545678912345689748593275897894708927680" NEW_LINE print ( primeDigits ( s ) ) NEW_LINE
Divisors of n | Python3 program to count number of divisors of n ^ 2 which are not divisible by divisor of n ; Function to count divisors of n ^ 2 having no factors of 'n ; Increment count of i - th prime divisor ; Find next prime divisor ; Increment count if divisor still remains ; Initialize variable for counting the factors of n ^ 2 and n as ans1 and ans2 respectively ; Range based for - loop ; Use formula as discussed in above ; return the difference of answers ; Driver code
import math as mt NEW_LINE ' NEW_LINE def factors ( n ) : NEW_LINE INDENT prime = dict ( ) NEW_LINE for i in range ( 2 , mt . ceil ( mt . sqrt ( n + 1 ) ) ) : NEW_LINE INDENT while ( n % i == 0 ) : NEW_LINE INDENT if i in prime . keys ( ) : NEW_LINE INDENT prime [ i ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT prime [ i ] = 1 NEW_LINE DEDENT n = n // i NEW_LINE DEDENT DEDENT if ( n > 2 ) : NEW_LINE INDENT if n in prime . keys ( ) : NEW_LINE INDENT prime [ n ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT prime [ n ] = 1 NEW_LINE DEDENT DEDENT ans1 = 1 NEW_LINE ans2 = 1 NEW_LINE for it in prime : NEW_LINE INDENT ans1 *= 2 * prime [ it ] + 1 NEW_LINE ans2 *= prime [ it ] + 1 NEW_LINE DEDENT return ans1 - ans2 NEW_LINE DEDENT n = 5 NEW_LINE print ( factors ( n ) ) NEW_LINE n = 8 NEW_LINE print ( factors ( n ) ) NEW_LINE
Print digit 's position to be removed to make a number divisible by 6 | Python program to print digit 's position to be removed to make number divisible by 6 ; function to print the number divisible by 6 after exactly removing a digit ; stores the Sum of all elements ; traverses the string and converts string to number array and Sums up ; if second last is odd or Sum of n - 1 elements are not divisible by 3. ; second last is even and prn - 1 elements removing last digit ; last digit removed ; counter to check if any element after removing , its Sum % 3 == 0 ; traverse till second last element ; to check if any element after removing , its Sum % 3 == 0 ; the leftmost element ; break at the leftmost element ; stores the right most element ; if no element has been found as a [ i + 1 ] > a [ i ] ; if second last is even , then remove last if ( Sum - last ) % 3 == 0 ; if no element which on removing gives Sum % 3 == 0 ; driver program to test the above function
import math as mt NEW_LINE def greatest ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE a = [ 0 for i in range ( n ) ] NEW_LINE Sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE Sum += a [ i ] NEW_LINE if ( a [ n - 2 ] % 2 != 0 or ( Sum - a [ n - 1 ] ) % 3 != 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( n ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT re = Sum % 3 NEW_LINE dell = - 1 NEW_LINE flag = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( ( a [ i ] ) % 3 == re ) : NEW_LINE INDENT if ( a [ i + 1 ] > a [ i ] ) : NEW_LINE INDENT dell = i NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT dell = i NEW_LINE DEDENT DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT if ( a [ n - 2 ] % 2 == 0 and re == a [ n - 1 ] % 3 ) : NEW_LINE INDENT dell = n - 1 NEW_LINE DEDENT DEDENT if ( dell == - 1 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( dell + 1 ) NEW_LINE DEDENT DEDENT DEDENT s = "7510222" NEW_LINE greatest ( s ) NEW_LINE
Representation of a number in powers of other | Python3 program to check if m can be represented as powers of w . ; If m is not zero means , it can 't be  represented in terms of powers of w. ; Driver code
def asPowerSum ( w , m ) : NEW_LINE INDENT while ( m > 0 ) : NEW_LINE INDENT if ( ( m - 1 ) % w == 0 ) : NEW_LINE INDENT m = ( m - 1 ) / w ; NEW_LINE DEDENT elif ( ( m + 1 ) % w == 0 ) : NEW_LINE INDENT m = ( m + 1 ) / w ; NEW_LINE DEDENT elif ( m % w == 0 ) : NEW_LINE INDENT m = m / w ; NEW_LINE DEDENT else : NEW_LINE DEDENT return ( m == 0 ) ; NEW_LINE DEDENT w = 3 ; NEW_LINE m = 7 ; NEW_LINE if ( asPowerSum ( w , m ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Number of digits to be removed to make a number divisible by 3 | function to count the no of removal of digits to make a very large number divisible by 3 ; add up all the digits of num ; if num is already is divisible by 3 then no digits are to be removed ; if there is single digit , then it is not possible to remove one digit . ; traverse through the number and find out if any number on removal makes the sum divisible by 3 ; if there are two numbers then it is not possible to remove two digits . ; Otherwise we can always make a number multiple of 2 by removing 2 digits . ; Driver Code
def divisible ( num ) : NEW_LINE INDENT n = len ( num ) NEW_LINE sum_ = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum_ += int ( num [ i ] ) NEW_LINE DEDENT if ( sum_ % 3 == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n == 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( sum_ % 3 == int ( num [ i ] ) % 3 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT if ( n == 2 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT num = "1234" NEW_LINE print ( divisible ( num ) ) NEW_LINE DEDENT
Program for dot product and cross product of two vectors | Python3 implementation for dot product and cross product of two vector . ; Function that return dot product of two vector array . ; Loop for calculate cot product ; Function to find cross product of two vector array . ; Driver function ; dotProduct function call ; crossProduct function call ; Loop that print cross product of two vector array .
n = 3 NEW_LINE def dotProduct ( vect_A , vect_B ) : NEW_LINE INDENT product = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT product = product + vect_A [ i ] * vect_B [ i ] NEW_LINE DEDENT return product NEW_LINE DEDENT def crossProduct ( vect_A , vect_B , cross_P ) : NEW_LINE INDENT cross_P . append ( vect_A [ 1 ] * vect_B [ 2 ] - vect_A [ 2 ] * vect_B [ 1 ] ) NEW_LINE cross_P . append ( vect_A [ 2 ] * vect_B [ 0 ] - vect_A [ 0 ] * vect_B [ 2 ] ) NEW_LINE cross_P . append ( vect_A [ 0 ] * vect_B [ 1 ] - vect_A [ 1 ] * vect_B [ 0 ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT vect_A = [ 3 , - 5 , 4 ] NEW_LINE vect_B = [ 2 , 6 , 5 ] NEW_LINE cross_P = [ ] NEW_LINE print ( " Dot ▁ product : " , end = " ▁ " ) NEW_LINE print ( dotProduct ( vect_A , vect_B ) ) NEW_LINE print ( " Cross ▁ product : " , end = " ▁ " ) NEW_LINE crossProduct ( vect_A , vect_B , cross_P ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( cross_P [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Number of n digit numbers that do not contain 9 | function to find number of n digit numbers possible ; driver function
def totalNumber ( n ) : NEW_LINE INDENT return 8 * pow ( 9 , n - 1 ) ; NEW_LINE DEDENT n = 3 NEW_LINE print ( totalNumber ( n ) ) NEW_LINE
Count ways to express even number â €˜ nâ €™ as sum of even integers | Initialize mod variable as constant ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 y = y / 2 ; Return number of ways to write ' n ' as sum of even integers ; Driver code
MOD = 1e9 + 7 NEW_LINE def power ( x , y , p ) : NEW_LINE INDENT while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( 1 * res * x ) % p NEW_LINE DEDENT x = ( 1 * x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT def countEvenWays ( n ) : NEW_LINE INDENT return power ( 2 , n / 2 - 1 , MOD ) NEW_LINE DEDENT n = 6 NEW_LINE print ( int ( countEvenWays ( n ) ) ) NEW_LINE n = 8 NEW_LINE print ( int ( countEvenWays ( n ) ) ) NEW_LINE