text
stringlengths
17
3.65k
code
stringlengths
70
5.84k
Probability for three randomly chosen numbers to be in AP | Function to calculate probability ; Driver code
def procal ( n ) : NEW_LINE INDENT return ( 3.0 * n ) / ( 4.0 * ( n * n ) - 1 ) NEW_LINE DEDENT a = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( a ) NEW_LINE print ( round ( procal ( n ) , 6 ) ) NEW_LINE
Fermat 's Last Theorem | Python3 program to verify fermat 's last theorem for a given range and n. ; Check if there exists a triplet such that a ^ n + b ^ n = c ^ n ; Driver code
def testSomeNumbers ( limit , n ) : NEW_LINE INDENT if ( n < 3 ) : NEW_LINE INDENT return NEW_LINE DEDENT for a in range ( 1 , limit + 1 ) : NEW_LINE INDENT for b in range ( a , limit + 1 ) : NEW_LINE INDENT pow_sum = pow ( a , n ) + pow ( b , n ) NEW_LINE c = pow ( pow_sum , 1.0 / n ) NEW_LINE c_pow = pow ( int ( c ) , n ) NEW_LINE if ( c_pow == pow_sum ) : NEW_LINE INDENT print ( " Count ▁ example ▁ found " ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT print ( " No ▁ counter ▁ example ▁ within ▁ given ▁ range ▁ and ▁ data " ) NEW_LINE DEDENT testSomeNumbers ( 10 , 3 ) NEW_LINE
Product of given N fractions in reduced form | Function to return gcd of a and b ; Print the Product of N fraction in Reduced Form . ; finding the product of all N numerators and denominators . ; Finding GCD of new numerator and denominator ; Converting into reduced form . ; Driver Code
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 productReduce ( n , num , den ) : NEW_LINE INDENT new_num = 1 ; NEW_LINE new_den = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT new_num = new_num * num [ i ] ; NEW_LINE new_den = new_den * den [ i ] ; NEW_LINE DEDENT GCD = gcd ( new_num , new_den ) ; NEW_LINE new_num = new_num / GCD ; NEW_LINE new_den = new_den / GCD ; NEW_LINE print ( int ( new_num ) , " / " , int ( new_den ) ) ; NEW_LINE DEDENT n = 3 ; NEW_LINE num = [ 1 , 2 , 5 ] ; NEW_LINE den = [ 2 , 1 , 6 ] ; NEW_LINE productReduce ( n , num , den ) ; NEW_LINE
Find value of ( n ^ 1 + n ^ 2 + n ^ 3 + n ^ 4 ) mod 5 for given n | Function for f ( n ) mod 5 ; if n % 5 == 1 return 4 ; else return 0 ; Driver Code
def fnMod ( n ) : NEW_LINE INDENT if ( n % 5 == 1 ) : NEW_LINE INDENT return 4 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT n = 10 NEW_LINE print ( fnMod ( n ) ) NEW_LINE n = 11 NEW_LINE print ( fnMod ( n ) ) NEW_LINE
Recursive sum of digits of a number formed by repeated appends | Return single digit sum of a number ; Returns recursive sum of digits of a number formed by repeating a number X number of times until sum become single digit . ; Driver Code
def digSum ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( n % 9 == 0 ) and 9 or ( n % 9 ) NEW_LINE DEDENT def repeatedNumberSum ( n , x ) : NEW_LINE INDENT sum = x * digSum ( n ) NEW_LINE return digSum ( sum ) NEW_LINE DEDENT n = 24 ; x = 3 NEW_LINE print ( repeatedNumberSum ( n , x ) ) NEW_LINE
Sum of n digit numbers divisible by a given number | Returns sum of n digit numbers divisible by 'number ; compute the first and last term ; sum of number which having n digit and divisible by number ; Driver code
' NEW_LINE def totalSumDivisibleByNum ( n , number ) : NEW_LINE INDENT firstnum = pow ( 10 , n - 1 ) NEW_LINE lastnum = pow ( 10 , n ) NEW_LINE sum = 0 NEW_LINE for i in range ( firstnum , lastnum ) : NEW_LINE INDENT if ( i % number == 0 ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT n = 3 ; num = 7 NEW_LINE print ( totalSumDivisibleByNum ( n , num ) ) NEW_LINE
Count n digit numbers divisible by given number | Simple Python 3 program to count n digit divisible numbers ; Returns count of n digit numbers divisible by number ; compute the first and last term ; count total number of which having n digit and divisible by number ; Driver code
import math NEW_LINE def numberofterm ( n , number ) : NEW_LINE INDENT firstnum = math . pow ( 10 , n - 1 ) NEW_LINE lastnum = math . pow ( 10 , n ) NEW_LINE count = 0 NEW_LINE for i in range ( int ( firstnum ) , int ( lastnum ) ) : NEW_LINE INDENT if ( i % number == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT n = 3 NEW_LINE num = 7 NEW_LINE print ( numberofterm ( n , num ) ) NEW_LINE
N | Python3 program to find N - th term in George Cantor set of rational numbers ; let i = numerator ; let j = denominator ; to keep the check of no . of terms ; loop till k is not equal to n ; check if k is already equal to N then the first term is the required rational number ; loop for traversing from right to left downwards diagonally ; loop for traversing from left to right upwards diagonally ; Driver code
def georgeCantor ( n ) : NEW_LINE INDENT i = 1 NEW_LINE j = 1 NEW_LINE k = 1 NEW_LINE while k < n : NEW_LINE INDENT j += 1 NEW_LINE k += 1 NEW_LINE if k == n : NEW_LINE INDENT break NEW_LINE DEDENT while j > 1 and k < n : NEW_LINE INDENT i += 1 NEW_LINE j -= 1 NEW_LINE k += 1 NEW_LINE DEDENT if k == n : NEW_LINE INDENT break NEW_LINE DEDENT i += 1 NEW_LINE k += 1 NEW_LINE if k == n : NEW_LINE INDENT break NEW_LINE DEDENT while i > 1 and k < n : NEW_LINE INDENT i -= 1 NEW_LINE j += 1 NEW_LINE k += 1 NEW_LINE DEDENT DEDENT print ( " N - th ▁ term ▁ : ▁ % d / % d " % ( i , j ) ) NEW_LINE DEDENT n = 15 NEW_LINE georgeCantor ( n ) NEW_LINE
Number is divisible by 29 or not | Returns true if n is divisible by 29 else returns false . ; add the lastdigit * 3 to renaming number until number comes only 2 digit ; return true if number is divisible by 29 another ; Driver Code
def isDivisible ( n ) : NEW_LINE INDENT while ( int ( n / 100 ) ) : NEW_LINE INDENT last_digit = int ( n % 10 ) NEW_LINE n = int ( n / 10 ) NEW_LINE n += last_digit * 3 NEW_LINE DEDENT return ( n % 29 == 0 ) NEW_LINE DEDENT n = 348 NEW_LINE if ( isDivisible ( n ) != 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Solve the Linear Equation of Single Variable | def to solve the given equation ; Traverse the equation ; For cases such as : x , - x , + x ; Flip sign once ' = ' is seen ; There may be a number left in the end ; For infinite solutions ; For no solution ; x = total sum / coeff of x ' - ' sign indicates moving numeric value to right hand side ; Driver code
def solveEquation ( equation ) : NEW_LINE INDENT n = len ( equation ) NEW_LINE sign = 1 NEW_LINE coeff = 0 NEW_LINE total = 0 NEW_LINE i = 0 NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT if ( equation [ j ] == ' + ' or equation [ j ] == ' - ' ) : NEW_LINE INDENT if ( j > i ) : NEW_LINE INDENT total = ( total + sign * int ( equation [ i : j ] ) ) NEW_LINE DEDENT i = j NEW_LINE DEDENT elif ( equation [ j ] == ' x ' ) : NEW_LINE INDENT if ( ( i == j ) or equation [ j - 1 ] == ' + ' ) : NEW_LINE INDENT coeff += sign NEW_LINE DEDENT elif ( equation [ j - 1 ] == ' - ' ) : NEW_LINE INDENT coeff = coeff - sign NEW_LINE DEDENT else : NEW_LINE INDENT coeff = ( coeff + sign * int ( equation [ i : j ] ) ) NEW_LINE DEDENT i = j + 1 NEW_LINE DEDENT elif ( equation [ j ] == ' = ' ) : NEW_LINE INDENT if ( j > i ) : NEW_LINE INDENT total = ( total + sign * int ( equation [ i : j ] ) ) NEW_LINE DEDENT sign = - 1 NEW_LINE i = j + 1 NEW_LINE DEDENT DEDENT if ( i < n ) : NEW_LINE INDENT total = ( total + sign * int ( equation [ i : len ( equation ) ] ) ) NEW_LINE DEDENT if ( coeff == 0 and total == 0 ) : NEW_LINE INDENT return " Infinite ▁ solutions " NEW_LINE DEDENT if ( coeff == 0 and total ) : NEW_LINE INDENT return " No ▁ solution " NEW_LINE DEDENT ans = - total / coeff NEW_LINE return int ( ans ) NEW_LINE DEDENT equation = " x + 5-3 + x = 6 + x - 2" NEW_LINE print ( " x ▁ = ▁ { } " . format ( solveEquation ( equation ) ) ) NEW_LINE
Check if a given number is Pronic | Efficient Approach | Python program to check if a number is pronic or not ; function to check Pronic Number ; Checking Pronic Number by multiplying consecutive numbers ; Driver Code
import math NEW_LINE def pronic_check ( n ) : NEW_LINE INDENT x = ( int ) ( math . sqrt ( n ) ) NEW_LINE if ( x * ( x + 1 ) == n ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT n = 56 NEW_LINE if ( pronic_check ( n ) == True ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Check if given number is perfect square | Python3 program for the above approach ; If ceil and floor are equal the number is a perfect square ; Driver code
import math NEW_LINE def checkperfectsquare ( x ) : NEW_LINE INDENT if ( math . ceil ( math . sqrt ( n ) ) == math . floor ( math . sqrt ( n ) ) ) : NEW_LINE INDENT print ( " perfect ▁ square " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " not ▁ a ▁ perfect ▁ square " ) NEW_LINE DEDENT DEDENT n = 49 NEW_LINE checkperfectsquare ( n ) NEW_LINE
Writing power function for large numbers | Maximum number of digits in output ; This function multiplies x with the number represented by res [ ] . res_size is size of res [ ] or number of digits in the number represented by res [ ] . This function uses simple school mathematics for multiplication . This function may value of res_size and returns the new value of res_size ; Initialize carry ; One by one multiply n with individual digits of res [ ] ; Store last digit of ' prod ' in res [ ] ; Put rest in carry ; Put carry in res and increase result size ; This function finds power of a number x ; printing value "1" for power = 0 ; Initialize result ; Multiply x n times ( x ^ n = x * x * x ... . n times ) ; Driver program
MAX = 100000 NEW_LINE def multiply ( x , res , res_size ) : NEW_LINE INDENT carry = 0 NEW_LINE for i in range ( res_size ) : NEW_LINE INDENT prod = res [ i ] * x + carry NEW_LINE res [ i ] = prod % 10 NEW_LINE carry = prod // 10 NEW_LINE DEDENT while ( carry ) : NEW_LINE INDENT res [ res_size ] = carry % 10 NEW_LINE carry = carry // 10 NEW_LINE res_size += 1 NEW_LINE DEDENT return res_size NEW_LINE DEDENT def power ( x , n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT print ( "1" ) NEW_LINE return NEW_LINE DEDENT res = [ 0 for i in range ( MAX ) ] NEW_LINE res_size = 0 NEW_LINE temp = x NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT res [ res_size ] = temp % 10 ; NEW_LINE res_size += 1 NEW_LINE temp = temp // 10 NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res_size = multiply ( x , res , res_size ) NEW_LINE DEDENT print ( x , " ^ " , n , " ▁ = ▁ " , end = " " ) NEW_LINE for i in range ( res_size - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( res [ i ] , end = " " ) NEW_LINE DEDENT DEDENT exponent = 100 NEW_LINE base = 2 NEW_LINE power ( base , exponent ) NEW_LINE
P | Python 3 program to check if a number is a p - smooth number or not ; function to check if number n is a P - smooth number or not ; prime factorise it by 2 ; if the number is divisible by 2 ; check for all the possible numbers that can divide it ; prime factorize it by i ; stores the maximum if maximum and i , if i divides the number ; if n at the end is a prime number , then it a divisor itself ; Driver program to test above function
import math NEW_LINE def check ( n , p ) : NEW_LINE INDENT maximum = - 1 NEW_LINE while ( not ( n % 2 ) ) : NEW_LINE INDENT maximum = max ( maximum , 2 ) NEW_LINE n = int ( n / 2 ) NEW_LINE DEDENT for i in range ( 3 , int ( math . sqrt ( n ) ) , 2 ) : NEW_LINE INDENT while ( n % i == 0 ) : NEW_LINE INDENT maximum = max ( maximum , i ) NEW_LINE n = int ( n / i ) NEW_LINE DEDENT DEDENT if ( n > 2 ) : NEW_LINE INDENT maximum = max ( maximum , n ) NEW_LINE DEDENT return ( maximum <= p ) NEW_LINE DEDENT n = 24 NEW_LINE p = 7 NEW_LINE if ( check ( n , p ) ) : NEW_LINE INDENT print ( " yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " no " ) NEW_LINE DEDENT
Program to Change RGB color model to HSV color model | Python3 program change RGB Color Model to HSV Color Model ; R , G , B values are divided by 255 to change the range from 0. .255 to 0. .1 : ; if cmax and cmax are equal then h = 0 ; if cmax equal r then compute h ; if cmax equal g then compute h ; if cmax equal b then compute h ; if cmax equal zero ; compute v ; print ( rgb_to_hsv ( 45 , 215 , 0 ) ) print ( rgb_to_hsv ( 31 , 52 , 29 ) )
def rgb_to_hsv ( r , g , b ) : NEW_LINE INDENT r , g , b = r / 255.0 , g / 255.0 , b / 255.0 NEW_LINE if cmax == cmin : NEW_LINE INDENT h = 0 NEW_LINE DEDENT elif cmax == r : NEW_LINE INDENT h = ( 60 * ( ( g - b ) / diff ) + 360 ) % 360 NEW_LINE DEDENT elif cmax == g : NEW_LINE INDENT h = ( 60 * ( ( b - r ) / diff ) + 120 ) % 360 NEW_LINE DEDENT elif cmax == b : NEW_LINE INDENT h = ( 60 * ( ( r - g ) / diff ) + 240 ) % 360 NEW_LINE DEDENT if cmax == 0 : NEW_LINE INDENT s = 0 NEW_LINE DEDENT else : NEW_LINE INDENT s = ( diff / cmax ) * 100 NEW_LINE DEDENT v = cmax * 100 NEW_LINE return h , s , v NEW_LINE DEDENT print ( rgb_to_hsv ( 129 , 88 , 47 ) ) NEW_LINE
Time when minute hand and hour hand coincide | Function to find the minute ; Finding the angle between minute hand and the first hour hand ; Driver code
def find_time ( h1 ) : NEW_LINE INDENT theta = 30 * h1 NEW_LINE print ( " ( " , end = " " ) NEW_LINE print ( ( theta * 2 ) , " / ▁ 11 ) ▁ minutes " ) NEW_LINE DEDENT h1 = 3 NEW_LINE find_time ( h1 ) NEW_LINE
Sum of Series ( n ^ 2 | function that calculate the sum of the nth series ; Using formula of the nth term ; Driver function
def sum_series ( n ) : NEW_LINE INDENT nSquare = n * n NEW_LINE return int ( nSquare * ( nSquare - 1 ) / 4 ) NEW_LINE DEDENT n = 2 NEW_LINE print ( sum_series ( n ) ) NEW_LINE
Check if a number is sandwiched between primes | Python Program to check whether a number is sandwiched between two primes or not ; returns true if number n is prime ; 0 and 1 both are non - primes ; finding square root of n ; checking if n has any factor upto square root of n if yes its not prime ; driver Function
import math NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n == 0 or n == 1 ) : NEW_LINE return False NEW_LINE root = int ( math . sqrt ( n ) ) NEW_LINE for i in range ( 2 , root + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isSandwitched ( n ) : NEW_LINE INDENT return ( isPrime ( n - 1 ) and isPrime ( n + 1 ) ) NEW_LINE DEDENT n = 642 NEW_LINE print ( n , end = " ▁ : ▁ " ) NEW_LINE if ( isSandwitched ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT n = 9 NEW_LINE print ( n , end = " ▁ : ▁ " ) NEW_LINE if ( isSandwitched ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Tomohiko Sakamoto 's Algorithm | function to implement tomohiko sakamoto algorithm ; array with leading number of days values ; if month is less than 3 reduce year by 1 ; Driver Code
def day_of_the_week ( y , m , d ) : NEW_LINE INDENT t = [ 0 , 3 , 2 , 5 , 0 , 3 , 5 , 1 , 4 , 6 , 2 , 4 ] NEW_LINE if ( m < 3 ) : NEW_LINE INDENT y = y - 1 NEW_LINE DEDENT return ( y + y // 4 - y // 100 + y // 400 + t [ m - 1 ] + d ) % 7 NEW_LINE DEDENT day = 13 NEW_LINE month = 7 NEW_LINE year = 2017 NEW_LINE print ( day_of_the_week ( year , month , day ) ) NEW_LINE
Recursive program for prime number | Returns true if n is prime , else return false . i is current divisor to check . ; Base cases ; Check for next divisor ; Driver Program
def isPrime ( n , i = 2 ) : NEW_LINE INDENT if ( n <= 2 ) : NEW_LINE INDENT return True if ( n == 2 ) else False NEW_LINE DEDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( i * i > n ) : NEW_LINE INDENT return true NEW_LINE DEDENT return isPrime ( n , i + 1 ) NEW_LINE DEDENT n = 15 NEW_LINE if ( isPrime ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Square Free Number | Python3 Program to print all prime factors ; Returns true if n is a square free number , else returns false . ; If 2 again divides n , then n is not a square free number . ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; Check if i is a prime factor ; If i again divides , then n is not square free ; Driver program
from math import sqrt NEW_LINE def isSquareFree ( n ) : NEW_LINE INDENT if n % 2 == 0 : NEW_LINE INDENT n = n / 2 NEW_LINE DEDENT if n % 2 == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 3 , int ( sqrt ( n ) + 1 ) ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT n = n / i NEW_LINE DEDENT if n % i == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT n = 10 NEW_LINE if isSquareFree ( n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Area of a square from diagonal length | Returns area of square from given diagonal ; Driver function .
def findArea ( d ) : NEW_LINE INDENT return ( d * d ) / 2 NEW_LINE DEDENT d = 10 NEW_LINE print ( " % .2f " % findArea ( d ) ) NEW_LINE
Sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n | Python Program to find sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n - 1 ) ^ 2. ; Function to find sum of series . ; driver code
import math NEW_LINE def sumOfSeries ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sum = sum + ( 2 * i - 1 ) * ( 2 * i - 1 ) NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 10 NEW_LINE print ( sumOfSeries ( n ) ) NEW_LINE
Sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n | Python Program to find sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n - 1 ) ^ 2. ; Function to find sum of series . ; Formula to find sum of series . ; driver code
import math NEW_LINE def sumOfSeries ( n ) : NEW_LINE INDENT return int ( ( n * ( 2 * n - 1 ) * ( 2 * n + 1 ) ) / 3 ) NEW_LINE DEDENT n = 10 NEW_LINE print ( sumOfSeries ( n ) ) NEW_LINE
Program to implement standard error of mean | Python 3 Program to implement standard error of mean . ; Function to find sample mean . ; loop to calculate sum of array elements . ; Function to calculate sample standard deviation . ; Function to calculate sample error . ; Formula to find sample error . ; Driver function
import math NEW_LINE def mean ( arr , n ) : NEW_LINE INDENT sm = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sm = sm + arr [ i ] NEW_LINE DEDENT return sm / n NEW_LINE DEDENT def SSD ( arr , n ) : NEW_LINE INDENT sm = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sm = sm + ( arr [ i ] - mean ( arr , n ) ) * ( arr [ i ] - mean ( arr , n ) ) NEW_LINE DEDENT return ( math . sqrt ( sm / ( n - 1 ) ) ) NEW_LINE DEDENT def sampleError ( arr , n ) : NEW_LINE INDENT return SSD ( arr , n ) / ( math . sqrt ( n ) ) NEW_LINE DEDENT arr = [ 78.53 , 79.62 , 80.25 , 81.05 , 83.21 , 83.46 ] NEW_LINE n = len ( arr ) NEW_LINE print ( sampleError ( arr , n ) ) NEW_LINE
Minimum moves to reach target on a infinite line | Set 2 | Python code to find minimum moves to reach target ; Function to find minimum steps to reach target ; Handling negatives by symmetry ; Keep moving while sum is smaller i . e calculating n ; case 1 : d is even ; d is odd ; Driver code ; Function call
import math NEW_LINE def StepstoReachTarget ( target ) : NEW_LINE INDENT target = abs ( target ) NEW_LINE n = math . ceil ( ( - 1.0 + math . sqrt ( 1 + 8.0 * target ) ) / 2 ) NEW_LINE sum = n * ( n + 1 ) / 2 NEW_LINE if ( sum == target ) : NEW_LINE INDENT return n NEW_LINE DEDENT d = sum - target NEW_LINE if ( ( int ( d ) & 1 ) == 0 ) : NEW_LINE INDENT return n NEW_LINE DEDENT else : NEW_LINE INDENT if ( int ( d ) & 1 ) : NEW_LINE INDENT return n + 2 NEW_LINE DEDENT return n + 1 NEW_LINE DEDENT DEDENT target = 5 NEW_LINE print ( StepstoReachTarget ( target ) ) NEW_LINE
Sum of series 2 / 3 | Function to find sum of series up - to n terms ; initializing counter by 1 ; variable to calculate result ; while loop until nth term is not reached ; boolean type variable for checking validation ; Driver Code
def seriesSum ( n ) : NEW_LINE INDENT i = 1 ; NEW_LINE res = 0.0 ; NEW_LINE sign = True ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT n = n - 1 ; NEW_LINE if ( sign ) : NEW_LINE INDENT sign = False ; NEW_LINE res = res + ( i + 1 ) / ( i + 2 ) ; NEW_LINE i = i + 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT sign = True ; NEW_LINE res = res - ( i + 1 ) / ( i + 2 ) ; NEW_LINE i = i + 2 ; NEW_LINE DEDENT DEDENT return res ; NEW_LINE DEDENT n = 5 ; NEW_LINE print ( round ( seriesSum ( n ) , 6 ) ) ; NEW_LINE
Number of Symmetric Relations on a Set | function find the square of n ; Base case ; Return 2 ^ ( n ( n + 1 ) / 2 ) ; Driver code
def countSymmetric ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return ( 1 << ( ( n * ( n + 1 ) ) // 2 ) ) NEW_LINE DEDENT n = 3 NEW_LINE print ( countSymmetric ( n ) ) NEW_LINE
Program for centered nonagonal number | Function to find nth centered nonagonal number ; Formula to find nth centered nonagonal number . ; Driver function .
def centeredNonagonal ( n ) : NEW_LINE INDENT return ( 3 * n - 2 ) * ( 3 * n - 1 ) // 2 NEW_LINE DEDENT n = 10 NEW_LINE print ( centeredNonagonal ( n ) ) NEW_LINE
Program for Mean Absolute Deviation | Function to find mean of the array elements . ; Calculate sum of all elements . ; Function to find mean absolute deviation of given elements . ; Calculate the sum of absolute deviation about mean . ; Return mean absolute deviation about mean . ; Driver function .
def Mean ( arr , n ) : NEW_LINE INDENT sm = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sm = sm + arr [ i ] NEW_LINE DEDENT return sm // n NEW_LINE DEDENT def meanAbsoluteDeviation ( arr , n ) : NEW_LINE INDENT absSum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT absSum = absSum + abs ( arr [ i ] - Mean ( arr , n ) ) NEW_LINE DEDENT return absSum / n NEW_LINE DEDENT arr = [ 10 , 15 , 15 , 17 , 18 , 21 ] NEW_LINE n = len ( arr ) NEW_LINE print ( meanAbsoluteDeviation ( arr , n ) ) NEW_LINE
Find if it is possible to get a ratio from given ranges of costs and quantities | Returns true if it is possible to get ratio r from given cost and quantity ranges . ; Calculating cost corresponding to value of i ; Driver Code
def isRatioPossible ( lowCost , upCost , lowQuant , upQuant , r ) : NEW_LINE INDENT for i in range ( lowQuant , upQuant + 1 ) : NEW_LINE INDENT ans = i * r NEW_LINE if ( lowCost <= ans and ans <= upCost ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT lowCost = 14 ; upCost = 30 NEW_LINE lowQuant = 5 ; upQuant = 12 ; r = 9 NEW_LINE if ( isRatioPossible ( lowCost , upCost , lowQuant , upQuant , r ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find N integers with given difference between product and sum | Function to implement calculation ; Driver code
def pattern ( n , d ) : NEW_LINE INDENT for i in range ( 0 , n - 2 ) : NEW_LINE INDENT print ( "1" , end = " ▁ " ) NEW_LINE DEDENT print ( "2" , end = " ▁ " ) NEW_LINE print ( n + d ) NEW_LINE DEDENT N = 3 NEW_LINE D = 5 NEW_LINE pattern ( N , D ) NEW_LINE ' NEW_LINE
Sum of fourth powers of first n odd natural numbers | calculate the sum of fourth power of first n odd natural numbers ; Driven Program
def oddNumSum ( n ) : NEW_LINE INDENT return ( n * ( 2 * n + 1 ) * ( 24 * n * n * n - 12 * n * n - 14 * n + 7 ) ) / 15 NEW_LINE DEDENT n = 4 NEW_LINE print ( int ( oddNumSum ( n ) ) ) NEW_LINE
Trailing number of 0 s in product of two factorials | Returns number of zeros in factorial n ; Dividing x by powers of 5 and update count ; Returns count of trailing zeros in M ! x N ! ; Driver program
def trailingZero ( x ) : NEW_LINE INDENT i = 5 NEW_LINE count = 0 NEW_LINE while ( x > i ) : NEW_LINE INDENT count = count + x // i NEW_LINE i = i * 5 NEW_LINE DEDENT return count NEW_LINE DEDENT def countProductTrailing ( M , N ) : NEW_LINE INDENT return trailingZero ( N ) + trailingZero ( M ) NEW_LINE DEDENT N = 67 NEW_LINE M = 98 NEW_LINE print ( countProductTrailing ( N , M ) ) NEW_LINE
Trimorphic Number | Function to check Trimorphic number ; Store the cube ; Start Comparing digits ; Return false , if any digit of N doesn ' t ▁ match ▁ with ▁ ▁ its ▁ cube ' s digits from last ; Reduce N and cube ; Driver code
def isTrimorphic ( N ) : NEW_LINE INDENT cube = N * N * N NEW_LINE while ( N > 0 ) : NEW_LINE INDENT if ( N % 10 != cube % 10 ) : NEW_LINE INDENT return False NEW_LINE DEDENT N = N // 10 NEW_LINE cube = cube // 10 NEW_LINE DEDENT return True NEW_LINE DEDENT N = 24 NEW_LINE if ( isTrimorphic ( N ) ) : NEW_LINE INDENT print ( " trimorphic " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " not ▁ trimporphic " ) NEW_LINE DEDENT
Trimorphic Number | Python3 program to find nth Trimorphic number ; Functions to find nth Trimorphic number ; Comparing the digits ; Return false , if any digit of num doesn ' t ▁ match ▁ with ▁ ▁ its ▁ cube ' s digits from last ; Reduce num and cube ; Check in max int size ; check number is Trimorphic or not ; if counter is equal to the n then return nth number ; Driver code
import sys NEW_LINE def checkTrimorphic ( num ) : NEW_LINE INDENT cube = num * num * num NEW_LINE while ( num > 0 ) : NEW_LINE INDENT if ( num % 10 != cube % 10 ) : NEW_LINE INDENT return False NEW_LINE DEDENT num = int ( num / 10 ) NEW_LINE cube = int ( cube / 10 ) NEW_LINE DEDENT return True NEW_LINE DEDENT def nthTrimorphic ( n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( sys . maxsize ) : NEW_LINE INDENT if ( checkTrimorphic ( i ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if ( count == n ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 9 NEW_LINE print ( nthTrimorphic ( n ) ) NEW_LINE DEDENT
Find minimum moves to reach target on an infinite line | Python 3 program to find minimum moves to reach target if we can move i steps in i - th move . ; Handling negatives by symmetry ; Keep moving while sum is smaller or difference is odd . ; Driver code
def reachTarget ( target ) : NEW_LINE INDENT target = abs ( target ) NEW_LINE sum = 0 NEW_LINE step = 0 NEW_LINE while ( sum < target or ( sum - target ) % 2 != 0 ) : NEW_LINE INDENT step = step + 1 NEW_LINE sum = sum + step NEW_LINE DEDENT return step NEW_LINE DEDENT target = 5 NEW_LINE print ( reachTarget ( target ) ) NEW_LINE
Sum of fifth powers of the first n natural numbers | Calculate the sum of fifth power of first n natural numbers ; Driven Program
def fifthPowerSum ( n ) : NEW_LINE INDENT return ( ( 2 * n * n * n * n * n * n ) + ( 6 * n * n * n * n * n ) + ( 5 * n * n * n * n ) - ( n * n ) ) // 12 NEW_LINE DEDENT n = 5 NEW_LINE print ( fifthPowerSum ( n ) ) NEW_LINE
Find unit digit of x raised to power y | Returns unit digit of x raised to power y ; Initialize result as 1 to handle case when y is 0. ; One by one multiply with x mod 10 to avoid overflow . ; Driver program
def unitDigitXRaisedY ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( y ) : NEW_LINE INDENT res = ( res * x ) % 10 NEW_LINE DEDENT return res NEW_LINE DEDENT print ( unitDigitXRaisedY ( 4 , 2 ) ) NEW_LINE
Max occurring divisor in an interval | function to find max occurring divisor interval [ x , y ] ; if there is only one number in the in the interval , return that number ; otherwise , 2 is max occurring divisor ; Driver code
def findDivisor ( x , y ) : NEW_LINE INDENT if ( x == y ) : NEW_LINE INDENT return y NEW_LINE DEDENT return 2 NEW_LINE DEDENT x = 3 NEW_LINE y = 16 NEW_LINE print ( findDivisor ( x , y ) ) NEW_LINE
Average of Squares of Natural Numbers | Function to calculate average of square number ; Driver code
def AvgofSquareN ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sum += ( i * i ) NEW_LINE DEDENT return sum / n NEW_LINE DEDENT n = 2 NEW_LINE print ( AvgofSquareN ( n ) ) NEW_LINE
Find sum of even factors of a number | Formula based Python3 program to find sum of alldivisors of n . ; Returns sum of all factors of n . ; If n is odd , then there are no even factors . ; Traversing through all prime factors . ; While i divides n print i and divide n ; here we remove the 2 ^ 0 that is 1. All other factors ; This condition is to handle the case when n is a prime number . ; Driver code
import math NEW_LINE def sumofFactors ( n ) : NEW_LINE INDENT if ( n % 2 != 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT res = 1 NEW_LINE for i in range ( 2 , ( int ) ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT count = 0 NEW_LINE curr_sum = 1 NEW_LINE curr_term = 1 NEW_LINE while ( n % i == 0 ) : NEW_LINE INDENT count = count + 1 NEW_LINE n = n // i NEW_LINE if ( i == 2 and count == 1 ) : NEW_LINE INDENT curr_sum = 0 NEW_LINE DEDENT curr_term = curr_term * i NEW_LINE curr_sum = curr_sum + curr_term NEW_LINE DEDENT res = res * curr_sum NEW_LINE DEDENT if ( n >= 2 ) : NEW_LINE INDENT res = res * ( 1 + n ) NEW_LINE DEDENT return res NEW_LINE DEDENT n = 18 NEW_LINE print ( sumofFactors ( n ) ) NEW_LINE
Find LCM of rational numbers | Python program to find LCM of given array ; get lcm of two numbers ; Finds LCM of numerators ; calculate the lcm of all numerators ; return all numerator lcm ; Get GCD of all the denominators ; calculate the gcd of all the denominators ; return all denominator gcd ; find lcm of all the rational number ; return the LCM of all numerator / GCD of all denominator ; Driver code ; give rational number 2 / 7 , 3 / 14 , 5 / 3 make pair as a numerator and denominator
import math NEW_LINE def LCM ( a , b ) : NEW_LINE INDENT return ( a * b ) // ( math . gcd ( a , b ) ) NEW_LINE DEDENT def lcmOfNumerator ( vect ) : NEW_LINE INDENT lcm = vect [ 0 ] [ 0 ] NEW_LINE for i in range ( 1 , len ( vect ) ) : NEW_LINE INDENT lcm = LCM ( vect [ i ] [ 0 ] , lcm ) NEW_LINE DEDENT return lcm NEW_LINE DEDENT def gcdOfDemoninators ( vect ) : NEW_LINE INDENT gcd = vect [ 0 ] [ 1 ] NEW_LINE for i in range ( 1 , len ( vect ) ) : NEW_LINE INDENT gcd = math . gcd ( vect [ i ] [ 1 ] , gcd ) NEW_LINE DEDENT return gcd NEW_LINE DEDENT def lcmOfRationals ( vect ) : NEW_LINE INDENT print ( lcmOfNumerator ( vect ) , " / " , gcdOfDemoninators ( vect ) , sep = " " ) NEW_LINE DEDENT vect = [ ] NEW_LINE vect . append ( ( 2 , 7 ) ) NEW_LINE vect . append ( ( 3 , 14 ) ) NEW_LINE vect . append ( ( 5 , 3 ) ) NEW_LINE lcmOfRationals ( vect ) NEW_LINE
Program to determine focal length of a spherical mirror | Determines focal length of a spherical concave mirror ; Determines focal length of a spherical convex mirror ; Driver function
def focal_length_concave ( R ) : NEW_LINE INDENT return R / 2 NEW_LINE DEDENT def focal_length_convex ( R ) : NEW_LINE INDENT return - ( R / 2 ) NEW_LINE DEDENT R = 30 ; NEW_LINE print ( " Focal ▁ length ▁ of ▁ spherical ▁ concave ▁ mirror ▁ is ▁ : " , focal_length_concave ( R ) , " ▁ units " ) NEW_LINE print ( " Focal ▁ length ▁ of ▁ spherical ▁ convex ▁ mirror ▁ is ▁ : ▁ " , focal_length_convex ( R ) , " ▁ units " ) NEW_LINE
Find sum of odd factors of a number | Formula based Python3 program to find sum of all divisors of n . ; Returns sum of all factors of n . ; Traversing through all prime factors . ; ignore even factors by of 2 ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number . ; Driver code
import math NEW_LINE def sumofoddFactors ( n ) : NEW_LINE INDENT res = 1 NEW_LINE while n % 2 == 0 : NEW_LINE INDENT n = n // 2 NEW_LINE DEDENT for i in range ( 3 , int ( math . sqrt ( n ) + 1 ) ) : NEW_LINE INDENT count = 0 NEW_LINE curr_sum = 1 NEW_LINE curr_term = 1 NEW_LINE while n % i == 0 : NEW_LINE INDENT count += 1 NEW_LINE n = n // i NEW_LINE curr_term *= i NEW_LINE curr_sum += curr_term NEW_LINE DEDENT res *= curr_sum NEW_LINE DEDENT if n >= 2 : NEW_LINE INDENT res *= ( 1 + n ) NEW_LINE DEDENT return res NEW_LINE DEDENT n = 30 NEW_LINE print ( sumofoddFactors ( n ) ) NEW_LINE
Number of non | return number of non negative integral solutions ; initialize total = 0 ; Base Case if n = 1 and val >= 0 then it should return 1 ; iterate the loop till equal the val ; total solution of equations and again call the recursive function Solutions ( variable , value ) ; return the total no possible solution ; driver code
def countSolutions ( n , val ) : NEW_LINE INDENT total = 0 NEW_LINE if n == 1 and val >= 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT for i in range ( val + 1 ) : NEW_LINE INDENT total += countSolutions ( n - 1 , val - i ) NEW_LINE DEDENT return total NEW_LINE DEDENT n = 5 NEW_LINE val = 20 NEW_LINE print ( countSolutions ( n , val ) ) NEW_LINE
Fibonomial coefficient and Fibonomial triangle | Python3 Program to print Fibonomial Triangle of height n . ; Function to produce Fibonacci Series . ; 0 th and 1 st number of the series are 0 and 1 ; Add the previous 2 numbers in the series and store it ; Function to produce fibonomial coefficient ; Function to print Fibonomial Triangle . ; Finding the fibonacci series . ; to store triangle value . ; initialising the 0 th element of each row and diagonal element equal to 0. ; for each row . ; for each column . ; finding each element using recurrence relation . ; printing the Fibonomial Triangle . ; Driver Code
N = 6 ; NEW_LINE def fib ( f , n ) : NEW_LINE INDENT f [ 0 ] = 0 ; NEW_LINE f [ 1 ] = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; NEW_LINE DEDENT DEDENT def fibcoef ( fc , f , n ) : NEW_LINE INDENT for i in range ( n + 1 ) : NEW_LINE INDENT fc [ i ] [ 0 ] = 1 ; NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT k = j ; NEW_LINE while ( k > 0 ) : NEW_LINE INDENT k -= 1 ; NEW_LINE fc [ i ] [ j ] *= f [ k ] ; NEW_LINE DEDENT k = 1 ; NEW_LINE while ( ( j + 1 ) != k ) : NEW_LINE INDENT fc [ i ] [ j ] /= f [ k ] ; NEW_LINE k += 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def printFibonomialTriangle ( n ) : NEW_LINE INDENT f = [ 0 ] * ( N + 1 ) ; NEW_LINE fib ( f , n ) ; NEW_LINE dp = [ [ 0 for x in range ( N + 1 ) ] for y in range ( N + 1 ) ] ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = 1 ; NEW_LINE dp [ i ] [ i ] = 1 ; NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , i ) : NEW_LINE INDENT dp [ i ] [ j ] = ( f [ i - j + 1 ] * dp [ i - 1 ] [ j - 1 ] + f [ j - 1 ] * dp [ i - 1 ] [ j ] ) ; NEW_LINE DEDENT DEDENT for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( i + 1 ) : NEW_LINE INDENT print ( dp [ i ] [ j ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( " " ) ; NEW_LINE DEDENT DEDENT n = 6 ; NEW_LINE printFibonomialTriangle ( n ) ; NEW_LINE
Sum of Arithmetic Geometric Sequence | Python3 code to find the sum of first n terms . ; Return the sum of first n term of AGP ; finding the each term of AGP and adding it to sum . ; Driven Code
import math NEW_LINE def sumofNterm ( a , d , b , r , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sum += ( ( a + ( i - 1 ) * d ) * ( b * math . pow ( r , i - 1 ) ) ) NEW_LINE DEDENT return int ( sum ) NEW_LINE DEDENT a = 1 NEW_LINE d = 1 NEW_LINE b = 2 NEW_LINE r = 2 NEW_LINE n = 3 NEW_LINE print ( sumofNterm ( a , d , b , r , n ) ) NEW_LINE
Sum of the series 2 + ( 2 + 4 ) + ( 2 + 4 + 6 ) + ( 2 + 4 + 6 + 8 ) + …… + ( 2 + 4 + 6 + 8 + … . + 2 n ) | function to find the sum of the given series ; first term of each i - th term ; next term ; required sum ; Driver program to test above
def sumOfTheSeries ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT k = 2 NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT sum = sum + k ; NEW_LINE k = k + 2 NEW_LINE DEDENT DEDENT return sum ; NEW_LINE DEDENT n = 5 NEW_LINE ans = sumOfTheSeries ( n ) ; NEW_LINE print ( ans ) NEW_LINE
Program to find sum of series 1 * 2 * 3 + 2 * 3 * 4 + 3 * 4 * 5 + . . . + n * ( n + 1 ) * ( n + 2 ) | Function to calculate sum of series . ; Driver code
def sumOfSeries ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE i = 1 ; NEW_LINE while i <= n : NEW_LINE INDENT sum = sum + i * ( i + 1 ) * ( i + 2 ) NEW_LINE i = i + 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 10 NEW_LINE print ( sumOfSeries ( n ) ) NEW_LINE
Program to get the Sum of series : 1 | Python3 code to get the sum of the series ; Function to get the series ; Sum of n - 1 terms starting from 2 nd term ; Driver Code
import math NEW_LINE def Series ( x , n ) : NEW_LINE INDENT sum = 1 NEW_LINE term = 1 NEW_LINE y = 2 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT fct = 1 NEW_LINE for j in range ( 1 , y + 1 ) : NEW_LINE INDENT fct = fct * j NEW_LINE DEDENT term = term * ( - 1 ) NEW_LINE m = term * math . pow ( x , y ) / fct NEW_LINE sum = sum + m NEW_LINE y += 2 NEW_LINE DEDENT return sum NEW_LINE DEDENT x = 9 NEW_LINE n = 10 NEW_LINE print ( ' % .4f ' % Series ( x , n ) ) NEW_LINE
Program to get the Sum of series : 1 | Function to get the series ; Computing sum of remaining n - 1 terms . ; Driver Code
def Series ( x , n ) : NEW_LINE INDENT sum = 1 NEW_LINE term = 1 NEW_LINE fct = 1 NEW_LINE p = 1 NEW_LINE multi = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT fct = fct * multi * ( multi + 1 ) NEW_LINE p = p * x * x NEW_LINE term = ( - 1 ) * term NEW_LINE multi += 2 NEW_LINE sum = sum + ( term * p ) / fct NEW_LINE DEDENT return sum NEW_LINE DEDENT x = 9 NEW_LINE n = 10 NEW_LINE print ( ' % .4f ' % Series ( x , n ) ) NEW_LINE
Find the number of consecutive zero at the end after multiplying n numbers | Function to count two 's factor ; Count number of 2 s present in n ; Function to count five 's factor ; Function to count number of zeros ; Count the two 's factor of n number ; Count the five 's factor of n number ; Return the minimum ; Driver Code
def two_factor ( n ) : NEW_LINE INDENT twocount = 0 NEW_LINE while n % 2 == 0 : NEW_LINE INDENT twocount += 1 NEW_LINE n = int ( n / 2 ) NEW_LINE DEDENT return twocount NEW_LINE DEDENT def five_factor ( n ) : NEW_LINE INDENT fivecount = 0 NEW_LINE while n % 5 == 0 : NEW_LINE INDENT fivecount += 1 NEW_LINE n = int ( n / 5 ) NEW_LINE DEDENT return fivecount NEW_LINE DEDENT def find_con_zero ( arr , n ) : NEW_LINE INDENT twocount = 0 NEW_LINE fivecount = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT twocount += two_factor ( arr [ i ] ) NEW_LINE fivecount += five_factor ( arr [ i ] ) NEW_LINE DEDENT if twocount < fivecount : NEW_LINE INDENT return twocount NEW_LINE DEDENT else : NEW_LINE INDENT return fivecount NEW_LINE DEDENT DEDENT arr = [ 100 , 10 , 5 , 25 , 35 , 14 ] NEW_LINE n = 6 NEW_LINE print ( find_con_zero ( arr , n ) ) NEW_LINE
First occurrence of a digit in a given fraction | function to print the first digit ; reduce the number to its mod ; traverse for every decimal places ; get every fraction places when ( a * 10 / b ) / c ; check if it is equal to the required integer ; mod the number ; driver code to test the above function
def first ( a , b , c ) : NEW_LINE INDENT a %= b NEW_LINE for i in range ( 1 , b + 1 ) : NEW_LINE INDENT a = a * 10 NEW_LINE if int ( a / b ) == c : NEW_LINE INDENT return i NEW_LINE DEDENT a %= b NEW_LINE DEDENT return - 1 NEW_LINE DEDENT a = 1 NEW_LINE b = 4 NEW_LINE c = 5 NEW_LINE print ( first ( a , b , c ) ) NEW_LINE
Minimize the absolute difference of sum of two subsets | function to print difference ; summation of n elements ; if divisible by 4 ; if remainder 1 or 2. In case of remainder 2 , we divide elements from 3 to n in groups of size 4 and put 1 in one group and 2 in group . This also makes difference 1. ; We put elements from 4 to n in groups of size 4. Remaining elements 1 , 2 and 3 can be divided as ( 1 , 2 ) and ( 3 ) . ; driver code to test the above function
def subsetDifference ( n ) : NEW_LINE INDENT s = int ( n * ( n + 1 ) / 2 ) NEW_LINE if n % 4 == 0 : NEW_LINE INDENT print ( " First ▁ subset ▁ sum ▁ = ▁ " , int ( s / 2 ) ) NEW_LINE print ( " Second ▁ subset ▁ sum ▁ = ▁ " , int ( s / 2 ) ) NEW_LINE print ( " Difference ▁ = ▁ " , 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT if n % 4 == 1 or n % 4 == 2 : NEW_LINE INDENT print ( " First ▁ subset ▁ sum ▁ = ▁ " , int ( s / 2 ) ) NEW_LINE print ( " Second ▁ subset ▁ sum ▁ = ▁ " , int ( s / 2 ) + 1 ) NEW_LINE print ( " Difference ▁ = ▁ " , 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " First ▁ subset ▁ sum ▁ = ▁ " , int ( s / 2 ) ) NEW_LINE print ( " Second ▁ subset ▁ sum ▁ = ▁ " , int ( s / 2 ) ) NEW_LINE print ( " Difference ▁ = ▁ " , 0 ) NEW_LINE DEDENT DEDENT DEDENT n = 6 NEW_LINE subsetDifference ( n ) NEW_LINE
Time required to meet in equilateral triangle | function to calculate time to meet ; Driver Code
def timeToMeet ( s , v ) : NEW_LINE INDENT V = 3 * v / 2 ; NEW_LINE time = s / V ; NEW_LINE print ( time ) ; NEW_LINE DEDENT s = 25 ; NEW_LINE v = 56 ; NEW_LINE timeToMeet ( s , v ) ; NEW_LINE
Sum of the series 1 + ( 1 + 3 ) + ( 1 + 3 + 5 ) + ( 1 + 3 + 5 + 7 ) + Γ’ €¦ Γ’ €¦ + ( 1 + 3 + 5 + 7 + Γ’ €¦ + ( 2 n | functionn to find the sum of the given series ; first term of each i - th term ; next term ; required sum ; Driver program
def sumOfTheSeries ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT k = 1 NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT sum += k NEW_LINE k += 2 NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT n = 5 NEW_LINE print ( " Sum ▁ = " , sumOfTheSeries ( n ) ) NEW_LINE
Check if a number can be written as sum of three consecutive integers | function to check if a number can be written as sum of three consecutive integer . ; if n is 0 ; if n is positive , increment loop by 1. ; if n is negative , decrement loop by 1. ; Running loop from 0 to n - 2 ; check if sum of three consecutive integer is equal to n . ; Driver Code
def checksum ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT print ( " - 1 ▁ 0 ▁ 1" ) NEW_LINE return 0 NEW_LINE DEDENT inc = 0 NEW_LINE if n > 0 : NEW_LINE INDENT inc = 1 NEW_LINE DEDENT else : NEW_LINE INDENT inc = - 1 NEW_LINE DEDENT for i in range ( 0 , n - 1 , inc ) : NEW_LINE INDENT if i + i + 1 + i + 2 == n : NEW_LINE INDENT print ( i , " ▁ " , i + 1 , " ▁ " , i + 2 ) NEW_LINE return 0 NEW_LINE DEDENT DEDENT print ( " - 1" ) NEW_LINE DEDENT n = 6 NEW_LINE checksum ( n ) NEW_LINE
Sum of all divisors from 1 to n | Python3 code to find sum of all divisor of number up to 'n ; Utility function to find sum of all divisor of number up to 'n ; Find all divisors of i and add them ; Driver code
' NEW_LINE ' NEW_LINE def divisorSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT j = 1 NEW_LINE while j * j <= i : NEW_LINE INDENT if i % j == 0 : NEW_LINE INDENT if i / j == j : NEW_LINE INDENT sum += j NEW_LINE DEDENT else : NEW_LINE INDENT sum += j + i / j NEW_LINE DEDENT DEDENT j = j + 1 NEW_LINE DEDENT DEDENT return int ( sum ) NEW_LINE DEDENT n = 4 NEW_LINE print ( divisorSum ( n ) ) NEW_LINE n = 5 NEW_LINE print ( divisorSum ( n ) ) NEW_LINE
Program for Binomial Coefficients table | Function to print binomial table ; B ( m , x ) is 1 if either m or x is is 0. ; Otherwise using recursive formula B ( m , x ) = B ( m , x - 1 ) * ( m - x + 1 ) / x ; Driver Function
def printbinomial ( max ) : NEW_LINE INDENT for m in range ( max + 1 ) : NEW_LINE INDENT print ( ' % ▁ 2d ' % m , end = ' ▁ ' ) NEW_LINE binom = 1 NEW_LINE for x in range ( m + 1 ) : NEW_LINE INDENT if m != 0 and x != 0 : NEW_LINE INDENT binom = binom * ( m - x + 1 ) / x NEW_LINE DEDENT print ( ' % ▁ 4d ' % binom , end = ' ▁ ' ) NEW_LINE DEDENT print ( " " , end = ' ' ) NEW_LINE DEDENT DEDENT max = 10 NEW_LINE printbinomial ( max ) NEW_LINE
Find largest prime factor of a number | Python3 code to find largest prime factor of number ; A function to find largest prime factor ; Initialize the maximum prime factor variable with the lowest one ; Print the number of 2 s that divide n ; n must be odd at this point ; now we have to iterate only for integers who does not have prime factor 2 and 3 ; This condition is to handle the case when n is a prime number greater than 4 ; Driver code to test above function
import math NEW_LINE def maxPrimeFactors ( n ) : NEW_LINE INDENT maxPrime = - 1 NEW_LINE while n % 2 == 0 : NEW_LINE INDENT maxPrime = 2 NEW_LINE DEDENT while n % 3 == 0 : NEW_LINE INDENT maxPrime = 3 NEW_LINE n = n / 3 NEW_LINE DEDENT for i in range ( 5 , int ( math . sqrt ( n ) ) + 1 , 6 ) : NEW_LINE INDENT while n % i == 0 : NEW_LINE INDENT maxPrime = i NEW_LINE n = n / i NEW_LINE DEDENT while n % ( i + 2 ) == 0 : NEW_LINE INDENT maxPrime = i + 2 NEW_LINE n = n / ( i + 2 ) NEW_LINE DEDENT DEDENT if n > 4 : NEW_LINE INDENT maxPrime = n NEW_LINE DEDENT return int ( maxPrime ) NEW_LINE DEDENT n = 15 NEW_LINE print ( maxPrimeFactors ( n ) ) NEW_LINE n = 25698751364526 NEW_LINE print ( maxPrimeFactors ( n ) ) NEW_LINE
Count unset bits in a range | Function to get no of set bits in the binary representation of 'n ; function to count unset bits in the given range ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; returns number of unset bits in the range ' l ' to ' r ' in 'n ; Driver code to test above
' NEW_LINE def countSetBits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while n : NEW_LINE INDENT n &= ( n - 1 ) NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def countUnsetBitsInGivenRange ( n , l , r ) : NEW_LINE INDENT num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) NEW_LINE DEDENT ' NEW_LINE INDENT return ( r - l + 1 ) - countSetBits ( n & num ) NEW_LINE DEDENT n = 80 NEW_LINE l = 1 NEW_LINE r = 4 NEW_LINE print ( countUnsetBitsInGivenRange ( n , l , r ) ) NEW_LINE
Sum of fourth power of first n even natural numbers | calculate the sum of fourth power of first n even natural numbers ; made even number ; Driver Code
def evenPowerSum ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT j = 2 * i ; NEW_LINE sum = sum + ( j * j * j * j ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT n = 5 ; NEW_LINE print ( evenPowerSum ( n ) ) ; NEW_LINE
Sum of fourth power of first n even natural numbers | calculate the sum of fourth power of first n even natural numbers ; Driver Code
def evenPowerSum ( n ) : NEW_LINE INDENT return ( 8 * n * ( n + 1 ) * ( 2 * n + 1 ) * ( 3 * n * n + 3 * n - 1 ) ) / 15 ; NEW_LINE DEDENT n = 4 ; NEW_LINE print ( int ( evenPowerSum ( n ) ) ) ; NEW_LINE
Balanced Prime | Python3 code to find Nth Balanced Prime ; Return the Nth balanced prime . ; Sieve of Eratosthenes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; storing all primes ; Finding the Nth balanced Prime ; Driven Program
MAX = 501 NEW_LINE def balancedprime ( n ) : NEW_LINE INDENT prime = [ True ] * ( MAX + 1 ) NEW_LINE p = 2 NEW_LINE while p * p <= MAX : NEW_LINE INDENT if prime [ p ] == True : NEW_LINE INDENT i = p * 2 NEW_LINE while i <= MAX : NEW_LINE INDENT prime [ i ] = False NEW_LINE i = i + p NEW_LINE DEDENT DEDENT p = p + 1 NEW_LINE DEDENT v = list ( ) NEW_LINE p = 3 NEW_LINE while p <= MAX : NEW_LINE INDENT if prime [ p ] : NEW_LINE INDENT v . append ( p ) NEW_LINE DEDENT p = p + 2 NEW_LINE DEDENT count = 0 NEW_LINE i = 1 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT if v [ i ] == ( v [ i + 1 ] + v [ i - 1 ] ) / 2 : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if count == n : NEW_LINE INDENT return v [ i ] NEW_LINE DEDENT DEDENT DEDENT n = 4 NEW_LINE print ( balancedprime ( n ) ) NEW_LINE
Smallest integer which has n factors or more | Python3 program to print the smallest integer with n factors or more ; array to store prime factors ; function to generate all prime factors of numbers from 1 to 10 ^ 6 ; Initializes all the positions with their value . ; Initializes all multiples of 2 with 2 ; A modified version of Sieve of Eratosthenes to store the smallest prime factor that divides every number . ; check if it has no prime factor . ; Initializes of j starting from i * i ; if it has no prime factor before , then stores the smallest prime divisor ; function to calculate number of factors ; stores the smallest prime number that divides n ; stores the count of number of times a prime number divides n . ; reduces to the next number after prime factorization of n ; false when prime factorization is done ; if the same prime number is dividing n , then we increase the count ; if its a new prime factor that is factorizing n , then we again set c = 1 and change dup to the new prime factor , and apply the formula explained above . ; prime factorizes a number ; for the last prime factor ; function to find the smallest integer with n factors or more . ; check if no of factors is more than n or not ; generate prime factors of number upto 10 ^ 6
MAX = 100001 ; NEW_LINE factor = [ 0 ] * MAX ; NEW_LINE def generatePrimeFactors ( ) : NEW_LINE INDENT factor [ 1 ] = 1 ; NEW_LINE for i in range ( 2 , MAX ) : NEW_LINE INDENT factor [ i ] = i ; NEW_LINE DEDENT i = 4 NEW_LINE while ( i < MAX ) : NEW_LINE INDENT factor [ i ] = 2 ; NEW_LINE i += 2 ; NEW_LINE DEDENT i = 3 ; NEW_LINE while ( i * i < MAX ) : NEW_LINE INDENT if ( factor [ i ] == i ) : NEW_LINE INDENT j = i * i ; NEW_LINE while ( j < MAX ) : NEW_LINE INDENT if ( factor [ j ] == j ) : NEW_LINE INDENT factor [ j ] = i ; NEW_LINE DEDENT j += i ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT DEDENT def calculateNoOFactors ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT ans = 1 ; NEW_LINE dup = factor [ n ] ; NEW_LINE c = 1 ; NEW_LINE j = int ( n / factor [ n ] ) ; NEW_LINE while ( j != 1 ) : NEW_LINE INDENT if ( factor [ j ] == dup ) : NEW_LINE INDENT c += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT dup = factor [ j ] ; NEW_LINE ans = ans * ( c + 1 ) ; NEW_LINE c = 1 ; NEW_LINE DEDENT j = int ( j / factor [ j ] ) ; NEW_LINE DEDENT ans = ans * ( c + 1 ) ; NEW_LINE return ans ; NEW_LINE DEDENT def smallest ( n ) : NEW_LINE INDENT i = 1 ; NEW_LINE while ( True ) : NEW_LINE INDENT if ( calculateNoOFactors ( i ) >= n ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT DEDENT generatePrimeFactors ( ) ; NEW_LINE n = 4 ; NEW_LINE print ( smallest ( n ) ) ; NEW_LINE
Sum of squares of first n natural numbers | Return the sum of square of first n natural numbers ; Iterate i from 1 and n finding square of i and add to sum . ; Driven Program
def squaresum ( n ) : NEW_LINE INDENT sm = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sm = sm + ( i * i ) NEW_LINE DEDENT return sm NEW_LINE DEDENT n = 4 NEW_LINE print ( squaresum ( n ) ) NEW_LINE
Break a number such that sum of maximum divisors of all parts is minimum | Function to check if a number is prime or not . ; If n is an even number ( we can write it as sum of two primes ) ; If n is odd and n - 2 is prime . ; If n is odd , n - 3 must be even . ; Driver code
def isPrime ( n ) : NEW_LINE INDENT i = 2 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def minimumSum ( n ) : NEW_LINE INDENT if ( isPrime ( n ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n % 2 == 0 ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT if ( isPrime ( n - 2 ) ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT return 3 NEW_LINE DEDENT n = 27 NEW_LINE print ( minimumSum ( n ) ) NEW_LINE
Find first and last digits of a number | Find the first digit ; Remove last digit from number till only one digit is left ; return the first digit ; Find the last digit ; return the last digit ; Driver Code
def firstDigit ( n ) : NEW_LINE INDENT while n >= 10 : NEW_LINE INDENT n = n / 10 ; NEW_LINE DEDENT return int ( n ) NEW_LINE DEDENT def lastDigit ( n ) : NEW_LINE INDENT return ( n % 10 ) NEW_LINE DEDENT n = 98562 ; NEW_LINE print ( firstDigit ( n ) , end = " ▁ " ) NEW_LINE print ( lastDigit ( n ) ) NEW_LINE
Find first and last digits of a number | Python3 program to find first and last digits of a number ; Find the first digit ; Find total number of digits - 1 ; Find first digit ; Return first digit ; Find the last digit ; return the last digit ; Driver Code
import math NEW_LINE def firstDigit ( n ) : NEW_LINE INDENT digits = ( int ) ( math . log10 ( n ) ) NEW_LINE n = ( int ) ( n / pow ( 10 , digits ) ) NEW_LINE return n ; NEW_LINE DEDENT def lastDigit ( n ) : NEW_LINE INDENT return ( n % 10 ) NEW_LINE DEDENT n = 98562 ; NEW_LINE print ( firstDigit ( n ) , end = " ▁ " ) NEW_LINE print ( lastDigit ( n ) ) NEW_LINE
Express an odd number as sum of prime numbers | Function to check if a number is prime or not . ; Prints at most three prime numbers whose sum is n . ; CASE - I ; CASE - II ; CASE - III ; Driver Code
def isPrime ( x ) : NEW_LINE INDENT if ( x == 0 or x == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT i = 2 NEW_LINE while i * i <= x : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT return 1 NEW_LINE DEDENT def findPrimes ( n ) : NEW_LINE INDENT if ( isPrime ( n ) ) : NEW_LINE INDENT print ( n , end = " ▁ " ) NEW_LINE DEDENT elif ( isPrime ( n - 2 ) ) : NEW_LINE INDENT print ( "2" , end = " ▁ " ) NEW_LINE print ( n - 2 , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( "3" , end = " ▁ " ) NEW_LINE n = n - 3 NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT if ( isPrime ( i ) and isPrime ( n - i ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE print ( ( n - i ) , end = " ▁ " ) NEW_LINE break NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT DEDENT DEDENT n = 27 ; NEW_LINE findPrimes ( n ) ; NEW_LINE
AKS Primality Test | array used to store coefficients . ; function to calculate the coefficients of ( x - 1 ) ^ n - ( x ^ n - 1 ) with the help of Pascal 's triangle . ; function to check whether the number is prime or not ; Calculating all the coefficients by the function coef and storing all the coefficients in c array . ; subtracting c [ n ] and adding c [ 0 ] by 1 as ( x - 1 ) ^ n - ( x ^ n - 1 ) , here we are subtracting c [ n ] by 1 and adding 1 in expression . ; checking all the coefficients whether they are divisible by n or not . if n is not prime , then loop breaks and ( i > 0 ) . ; Return true if all coefficients are divisible by n . ; Driver Code
c = [ 0 ] * 100 ; NEW_LINE def coef ( n ) : NEW_LINE INDENT c [ 0 ] = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT c [ 1 + i ] = 1 ; NEW_LINE for j in range ( i , 0 , - 1 ) : NEW_LINE INDENT c [ j ] = c [ j - 1 ] - c [ j ] ; NEW_LINE DEDENT c [ 0 ] = - c [ 0 ] ; NEW_LINE DEDENT DEDENT def isPrime ( n ) : NEW_LINE INDENT coef ( n ) ; NEW_LINE c [ 0 ] = c [ 0 ] + 1 ; NEW_LINE c [ n ] = c [ n ] - 1 ; NEW_LINE i = n ; NEW_LINE while ( i > - 1 and c [ i ] % n == 0 ) : NEW_LINE INDENT i = i - 1 ; NEW_LINE DEDENT return True if i < 0 else False ; NEW_LINE DEDENT n = 37 ; NEW_LINE if ( isPrime ( n ) ) : NEW_LINE INDENT print ( " Prime " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Prime " ) ; NEW_LINE DEDENT
Motzkin number | Return the nth Motzkin Number . ; Base Case ; Recursive step ; Driver code
def motzkin ( n ) : NEW_LINE INDENT if ( n == 0 or n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return ( ( 2 * n + 1 ) * motzkin ( n - 1 ) + ( 3 * n - 3 ) * motzkin ( n - 2 ) ) / ( n + 2 ) NEW_LINE DEDENT n = 8 NEW_LINE print ( motzkin ( n ) ) NEW_LINE
Sum of the series 0.6 , 0.06 , 0.006 , 0.0006 , ... to n terms | Python3 program to find sum of 0.6 , 0.06 , 0.006 , 0.0006 , ... to n terms ; function which return the sum of series ; Driver code
import math NEW_LINE def sumOfSeries ( n ) : NEW_LINE INDENT return ( ( 0.666 ) * ( 1 - 1 / pow ( 10 , n ) ) ) ; NEW_LINE DEDENT n = 2 ; NEW_LINE print ( sumOfSeries ( n ) ) ; NEW_LINE
Narcissistic number | function to count digits ; Returns true if n is Narcissistic number ; Count the number of digits ; Calculates the sum of digits raised to power ; Driver code
def countDigit ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( 1 + countDigit ( n // 10 ) ) NEW_LINE DEDENT def check ( n ) : NEW_LINE INDENT l = countDigit ( n ) NEW_LINE dup = n ; sm = 0 NEW_LINE while ( dup ) : NEW_LINE INDENT sm = sm + pow ( dup % 10 , l ) NEW_LINE dup = dup // 10 NEW_LINE DEDENT return ( n == sm ) NEW_LINE DEDENT n = 1634 NEW_LINE if ( check ( n ) ) : NEW_LINE INDENT print ( " yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " no " ) NEW_LINE DEDENT
Sum of squares of first n natural numbers | Function to calculate series ; Driver Code
def summation ( n ) : NEW_LINE INDENT return sum ( [ i ** 2 for i in range ( 1 , n + 1 ) ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 NEW_LINE print ( summation ( n ) ) NEW_LINE DEDENT
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