text stringlengths 17 3.65k | code stringlengths 70 5.84k |
|---|---|
Program to find the profit or loss when CP of N items is equal to SP of M items | Function to calculate Profit or loss ; Driver Code | def profitLoss ( N , M ) : NEW_LINE INDENT if ( N == M ) : NEW_LINE INDENT print ( " No β Profit β nor β Loss " ) NEW_LINE DEDENT else : NEW_LINE INDENT result = 0.0 NEW_LINE result = float ( abs ( N - M ) ) / M NEW_LINE if ( N - M < 0 ) : NEW_LINE INDENT print ( " Loss β = β - " , ' { 0 : . 6 } ' . format ( result * 1... |
Find all good indices in the given Array | Python3 program to find all good indices in the given array ; Function to find all good indices in the given array ; hash to store frequency of each element ; Storing frequency of each element and calculating sum simultaneously ; check if array is good after removing i - th in... | from collections import defaultdict NEW_LINE def niceIndices ( A , n ) : NEW_LINE INDENT Sum = 0 NEW_LINE m = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ A [ i ] ] += 1 NEW_LINE Sum += A [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT k = Sum - A [ i ] NEW_LINE if k % 2 =... |
Count pieces of circle after N cuts | Function to find number of pieces of circle after N cuts ; Driver Code | def countPieces ( N ) : NEW_LINE INDENT return 2 * N NEW_LINE DEDENT N = 100 NEW_LINE print ( countPieces ( N ) ) NEW_LINE |
Sum of all the multiples of 3 and 7 below N | Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of 3 and 7 below N ; Since , we need the sum of multiples less than N ; Driver code | def sumAP ( n , d ) : NEW_LINE INDENT n = int ( n / d ) ; NEW_LINE return ( n ) * ( 1 + n ) * ( d / 2 ) ; NEW_LINE DEDENT def sumMultiples ( n ) : NEW_LINE INDENT n -= 1 ; NEW_LINE return int ( sumAP ( n , 3 ) + sumAP ( n , 7 ) - sumAP ( n , 21 ) ) ; NEW_LINE DEDENT n = 24 ; NEW_LINE print ( sumMultiples ( n ) ) ; NEW_... |
Check whether product of digits at even places is divisible by sum of digits at odd place of a number | Below function checks whether product of digits at even places is divisible by sum of digits at odd places ; if size is even ; if size is odd ; Driver code | def productSumDivisible ( n , size ) : NEW_LINE INDENT sum = 0 NEW_LINE product = 1 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT if ( size % 2 == 0 ) : NEW_LINE INDENT product *= n % 10 NEW_LINE DEDENT else : NEW_LINE INDENT sum += n % 10 NEW_LINE DEDENT n = n // 10 NEW_LINE size -= 1 NEW_LINE DEDENT if ( product % sum =... |
Check whether product of digits at even places is divisible by sum of digits at odd place of a number | Below function checks whether product of digits at even places is divisible by sum of digits at odd places ; Converting integer to string ; Traveersing the string ; Driver code | def productSumDivisible ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE product = 1 NEW_LINE num = str ( n ) NEW_LINE for i in range ( len ( num ) ) : NEW_LINE INDENT if ( i % 2 != 0 ) : NEW_LINE INDENT product = product * int ( num [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT sum = sum + int ( num [ i ] ) NEW_LINE DEDENT D... |
GCD of a number raised to some power and another number | Python 3 program to find GCD of a ^ n and b . ; Returns GCD of a ^ n and b . ; 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 powGCD ( a , n , b ) : NEW_LINE INDENT for i in range ( 0 , n + 1 , 1 ) : NEW_LINE INDENT a = a * a NEW_LINE DEDENT return gcd ( a , b ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _... |
Position after taking N steps to the right and left in an alternate manner | Function to return the last destination ; Driver Code | def lastCoordinate ( n , a , b ) : NEW_LINE INDENT return ( ( ( n + 1 ) // 2 ) * a - ( n // 2 ) * b ) NEW_LINE DEDENT n = 3 NEW_LINE a = 5 NEW_LINE b = 2 NEW_LINE print ( lastCoordinate ( n , a , b ) ) NEW_LINE |
Smallest number greater than or equal to N divisible by K | Function to find the smallest number greater than or equal to N that is divisible by k ; Driver Code | def findNum ( N , K ) : NEW_LINE INDENT rem = ( N + K ) % K ; NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT else : NEW_LINE INDENT return ( N + K - rem ) NEW_LINE DEDENT DEDENT N = 45 NEW_LINE K = 6 NEW_LINE print ( ' Smallest β number β greater β than ' , ' or β equal β to ' , N , ' that β is β d... |
Sum and Product of digits in a number that divide the number | Print the sum and product of digits that divides the number . ; Fetching each digit of the number ; Checking if digit is greater than 0 and can divides n . ; Driver code | def countDigit ( n ) : NEW_LINE INDENT temp = n NEW_LINE sum = 0 NEW_LINE product = 1 NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT d = temp % 10 NEW_LINE temp //= 10 NEW_LINE if ( d > 0 and n % d == 0 ) : NEW_LINE INDENT sum += d NEW_LINE product *= d NEW_LINE DEDENT DEDENT print ( " Sum β = " , sum ) NEW_LINE print ... |
Largest number smaller than or equal to N divisible by K | Function to find the largest number smaller than or equal to N that is divisible by k ; Driver code | def findNum ( N , K ) : NEW_LINE INDENT rem = N % K NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT else : NEW_LINE INDENT return N - rem NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 45 NEW_LINE K = 6 NEW_LINE print ( " Largest β number β smaller β than β or β equal β... |
Check if any permutation of a number is divisible by 3 and is Palindromic | Function to check if any permutation of a number is divisible by 3 and is Palindromic ; Hash array to store frequency of digits of n ; traverse the digits of integer and store their frequency ; Calculate the sum of digits simultaneously ; Check... | def isDivisiblePalindrome ( n ) : NEW_LINE INDENT hash = [ 0 ] * 10 NEW_LINE digitSum = 0 NEW_LINE while ( n ) : NEW_LINE INDENT digitSum += n % 10 NEW_LINE hash [ n % 10 ] += 1 NEW_LINE n //= 10 NEW_LINE DEDENT if ( digitSum % 3 != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT oddCount = 0 NEW_LINE for i in range... |
Minimum time to return array to its original state after given modifications | Python implementation of above approach ; Function to return lcm of two numbers ; Make a graph ; Add edge ; Count reachable nodes from every node . ; Driver code | import math NEW_LINE def lcm ( a , b ) : NEW_LINE INDENT return ( a * b ) // ( math . gcd ( a , b ) ) NEW_LINE DEDENT def dfs ( src , adj , visited ) : NEW_LINE INDENT visited [ src ] = True NEW_LINE count = 1 NEW_LINE if adj [ src ] != 0 : NEW_LINE INDENT for i in range ( len ( adj [ src ] ) ) : NEW_LINE INDENT if ( n... |
Check whether product of digits at even places of a number is divisible by K | below function checks whether product of digits at even places is divisible by K ; if position is even ; Driver code | def productDivisible ( n , k ) : NEW_LINE INDENT product = 1 NEW_LINE position = 1 NEW_LINE while n > 0 : NEW_LINE INDENT if position % 2 == 0 : NEW_LINE INDENT product *= n % 10 NEW_LINE DEDENT n = n / 10 NEW_LINE position += 1 NEW_LINE DEDENT if product % k == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return Fa... |
Check whether product of digits at even places of a number is divisible by K | Function checks whether product of digits at even places is divisible by K ; Converting integer to string ; Traversing the string ; Driver code | def productDivisible ( n , k ) : NEW_LINE INDENT product = 1 NEW_LINE num = str ( n ) NEW_LINE for i in range ( len ( num ) ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT product = product * int ( num [ i ] ) NEW_LINE DEDENT DEDENT if product % k == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return False ... |
Permutations of n things taken r at a time with k things together | def to find factorial of a number ; def to calculate p ( n , r ) ; def to find the number of permutations of n different things taken r at a time with k things grouped together ; Driver code | def factorial ( n ) : NEW_LINE INDENT fact = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT fact = fact * i ; NEW_LINE DEDENT return fact ; NEW_LINE DEDENT def npr ( n , r ) : NEW_LINE INDENT pnr = factorial ( n ) / factorial ( n - r ) ; NEW_LINE return pnr ; NEW_LINE DEDENT def countPermutations ( n , r ,... |
Greatest Integer Function | Python3 program to illustrate greatest integer Function ; Function to calculate the GIF value of a number ; GIF is the floor of a number ; Driver code | import math NEW_LINE def GIF ( n ) : NEW_LINE INDENT return int ( math . floor ( n ) ) ; NEW_LINE DEDENT n = 2.3 ; NEW_LINE print ( GIF ( n ) ) ; NEW_LINE |
Number of triangles formed from a set of points on three lines | Returns factorial of a number ; calculate c ( n , r ) ; Driver code | def factorial ( n ) : NEW_LINE INDENT fact = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT fact = fact * i NEW_LINE DEDENT return fact NEW_LINE DEDENT def ncr ( n , r ) : NEW_LINE INDENT return ( factorial ( n ) // ( factorial ( r ) * factorial ( n - r ) ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " :... |
Check whether sum of digits at odd places of a number is divisible by K | function that checks the divisibility of the sum of the digits at odd places of the given number ; if position is odd ; Driver code | def SumDivisible ( n , k ) : NEW_LINE INDENT sum = 0 NEW_LINE position = 1 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT if ( position % 2 == 1 ) : NEW_LINE INDENT sum += n % 10 NEW_LINE DEDENT n = n // 10 NEW_LINE position += 1 NEW_LINE DEDENT if ( sum % k == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False... |
Check whether sum of digits at odd places of a number is divisible by K | Python3 implementation of the above approach ; Converting integer to string ; Traversing the string ; Driver code | def sumDivisible ( n , k ) : NEW_LINE INDENT sum = 0 NEW_LINE num = str ( n ) NEW_LINE for i in range ( len ( num ) ) : NEW_LINE INDENT if ( i % 2 != 0 ) : NEW_LINE INDENT sum = sum + int ( num [ i ] ) NEW_LINE DEDENT DEDENT if sum % k == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT n = ... |
Check if a triangle of positive area is possible with the given angles | Python program to check if a triangle of positive area is possible with the given angles ; Checking if the sum of three angles is 180 and none of the angles is zero ; Checking if sum of any two angles is greater than equal to the third one ; Drive... | def isTriangleExists ( a , b , c ) : NEW_LINE INDENT if ( a != 0 and b != 0 and c != 0 and ( a + b + c ) == 180 ) : NEW_LINE INDENT if ( ( a + b ) >= c or ( b + c ) >= a or ( a + c ) >= b ) : NEW_LINE INDENT return " YES " NEW_LINE DEDENT else : NEW_LINE INDENT return " NO " NEW_LINE DEDENT DEDENT else : NEW_LINE INDEN... |
Find maximum value of x such that n ! % ( k ^ x ) = 0 | Python 3 program to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Function to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Find square root of k and add 1 to it ; Run the loop from 2 to m and k should be greater than 1 ; optimize the value of ... | import math NEW_LINE def findX ( n , k ) : NEW_LINE INDENT r = n NEW_LINE m = int ( math . sqrt ( k ) ) + 1 NEW_LINE i = 2 NEW_LINE while i <= m and k > 1 : NEW_LINE INDENT if ( i == m ) : NEW_LINE INDENT i = k NEW_LINE DEDENT u = 0 NEW_LINE v = 0 NEW_LINE while k % i == 0 : NEW_LINE INDENT k //= i NEW_LINE v += 1 NEW_... |
Ways of selecting men and women from a group to make a team | Returns factorial of the number ; Function to calculate ncr ; Function to calculate the total possible ways ; Driver code | def fact ( n ) : NEW_LINE INDENT fact = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT fact *= i NEW_LINE DEDENT return fact NEW_LINE DEDENT def ncr ( n , r ) : NEW_LINE INDENT ncr = fact ( n ) // ( fact ( r ) * fact ( n - r ) ) NEW_LINE return ncr NEW_LINE DEDENT def ways ( m , w , n , k ) : NEW_LINE INDENT... |
Product of every K β th prime number in an array | Python 3 implementation of the approach ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; compute the answer ; count of primes ; product of the primes ; traverse the array ; if the number is a prime ; inc... | MAX = 1000000 NEW_LINE prime = [ True ] * ( MAX + 1 ) NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT prime [ 1 ] = False ; NEW_LINE prime [ 0 ] = False ; NEW_LINE p = 2 NEW_LINE while p * p <= MAX : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , MAX + 1 , p ) : NEW_LINE IN... |
Sum of greatest odd divisor of numbers in given range | Function to return sum of first n odd numbers ; Recursive function to return sum of greatest odd divisor of numbers in range [ 1 , n ] ; Odd n ; Even n ; Function to return sum of greatest odd divisor of numbers in range [ a , b ] ; Driver code | def square ( n ) : NEW_LINE INDENT return n * n ; NEW_LINE DEDENT def sum ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( n % 2 == 1 ) : NEW_LINE INDENT return ( square ( int ( ( n + 1 ) / 2 ) ) + sum ( int ( n / 2 ) ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( square ( ... |
Minimum numbers needed to express every integer below N as a sum | function to count length of binary expression of n ; Driver code | def countBits ( n ) : NEW_LINE INDENT count = 0 ; NEW_LINE while ( n ) : NEW_LINE INDENT count += 1 ; NEW_LINE n >>= 1 ; NEW_LINE DEDENT return count ; NEW_LINE DEDENT n = 32 ; NEW_LINE print ( " Minimum β value β of β K β is β = " , countBits ( n ) ) ; NEW_LINE |
Check if a number is an Achilles number or not | Program to check if the given number is an Achilles Number ; function to check if the number is powerful number ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; if n is not a power of 2 then this loop will ex... | from math import sqrt , log NEW_LINE def isPowerful ( n ) : NEW_LINE INDENT while ( n % 2 == 0 ) : NEW_LINE INDENT power = 0 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT n /= 2 NEW_LINE power += 1 NEW_LINE DEDENT if ( power == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT p = int ( sqrt ( n ) ) + 1 NEW_L... |
Program to find count of numbers having odd number of divisors in given range | Function to count numbers having odd number of divisors in range [ A , B ] ; variable to odd divisor count ; iterate from a to b and count their number of divisors ; variable to divisor count ; if count of divisor is odd then increase res b... | def OddDivCount ( a , b ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( a , b + 1 ) : NEW_LINE INDENT divCount = 0 NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT if ( i % j == 0 ) : NEW_LINE INDENT divCount += 1 NEW_LINE DEDENT DEDENT if ( divCount % 2 ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT ... |
Largest factor of a given number which is a perfect square | Python 3 program to find the largest factor of a number which is also a perfect square ; Function to find the largest factor of a given number which is a perfect square ; Initialise the answer to 1 ; Finding the prime factors till sqrt ( num ) ; Frequency of ... | import math NEW_LINE def largestSquareFactor ( num ) : NEW_LINE INDENT answer = 1 NEW_LINE for i in range ( 2 , int ( math . sqrt ( num ) ) ) : NEW_LINE INDENT cnt = 0 NEW_LINE j = i NEW_LINE while ( num % j == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE j *= i NEW_LINE DEDENT if ( cnt & 1 ) : NEW_LINE INDENT cnt -= 1 NEW_... |
Find the Nth term of the series 2 + 6 + 13 + 23 + . . . | calculate Nth term of given series ; Driver code | def Nth_Term ( n ) : NEW_LINE INDENT return ( 3 * pow ( n , 2 ) - n + 2 ) // ( 2 ) NEW_LINE DEDENT N = 5 NEW_LINE print ( Nth_Term ( N ) ) NEW_LINE |
All possible numbers of N digits and base B without leading zeros | function to count all permutations ; count of all permutations ; count of permutations with leading zeros ; Return the permutations without leading zeros ; Driver code | def countPermutations ( N , B ) : NEW_LINE INDENT x = B ** N NEW_LINE y = B ** ( N - 1 ) NEW_LINE print ( x - y ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , B = 6 , 4 NEW_LINE countPermutations ( N , B ) NEW_LINE DEDENT |
Absolute difference between the Product of Non | Function to find the difference between the product of non - primes and the product of primes of an array . ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in pri... | def calculateDifference ( arr , n ) : NEW_LINE INDENT max_val = max ( arr ) NEW_LINE prime = ( max_val + 1 ) * [ True ] NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE p = 2 NEW_LINE while p * p <= max_val : NEW_LINE INDENT if prime [ p ] == True : NEW_LINE INDENT for i in range ( p * 2 , max_val + 1... |
Maximum count of equal numbers in an array after performing given operations | Function to find the maximum number of equal numbers in an array ; to store sum of elements ; if sum of numbers is not divisible by n ; Driver Code ; size of an array | def EqualNumbers ( a , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += a [ i ] ; NEW_LINE DEDENT if ( sum % n ) : NEW_LINE INDENT return n - 1 ; NEW_LINE DEDENT return n ; NEW_LINE DEDENT a = [ 1 , 4 , 1 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( EqualNumbers ( a , n ) ) ; NEW_... |
Count number of ordered pairs with Even and Odd Sums | function to count odd sum pair ; if number is even ; if number is odd ; count of ordered pairs ; function to count even sum pair ; Driver code | def count_odd_pair ( n , a ) : NEW_LINE INDENT odd = 0 NEW_LINE even = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( a [ i ] % 2 == 0 ) : NEW_LINE INDENT even = even + 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd = odd + 1 NEW_LINE DEDENT DEDENT ans = odd * even * 2 NEW_LINE return ans NEW_LINE DEDENT def ... |
Steps required to visit M points in order on a circular ring of N points | Function to count the steps required ; Start at 1 ; Initialize steps ; If nxt is greater than cur ; Now we are at a [ i ] ; Driver code | def findSteps ( n , m , a ) : NEW_LINE INDENT cur = 1 NEW_LINE steps = 0 NEW_LINE for i in range ( 0 , m ) : NEW_LINE INDENT if ( a [ i ] >= cur ) : NEW_LINE INDENT steps += ( a [ i ] - cur ) NEW_LINE DEDENT else : NEW_LINE INDENT steps += ( n - cur + a [ i ] ) NEW_LINE DEDENT cur = a [ i ] NEW_LINE DEDENT return steps... |
Program to Convert Hexadecimal Number to Binary | Function to convert Hexadecimal to Binary Number ; Driver code ; Get the Hexadecimal number ; Convert HexaDecimal to Binary | def HexToBin ( hexdec ) : NEW_LINE INDENT for i in hexdec : NEW_LINE INDENT if i == '0' : NEW_LINE INDENT print ( '0000' , end = ' ' ) NEW_LINE DEDENT elif i == '1' : NEW_LINE INDENT print ( '0001' , end = ' ' ) NEW_LINE DEDENT elif i == '2' : NEW_LINE INDENT print ( '0010' , end = ' ' ) NEW_LINE DEDENT elif i == '3' :... |
Count unordered pairs ( i , j ) such that product of a [ i ] and a [ j ] is power of two | Function to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; Function to Count unordered pairs ; is a number can be expressed as power of two ; count total number of unordered pairs ; Drive... | def isPowerOfTwo ( x ) : NEW_LINE INDENT return ( x and ( not ( x & ( x - 1 ) ) ) ) NEW_LINE DEDENT def Count_pairs ( a , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if isPowerOfTwo ( a [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT ans = ( count * ( count - 1 ) ) / 2 NE... |
Ways of dividing a group into two halves such that two elements are in different groups | This function will return the factorial of a given number ; This function will calculate nCr of given n and r ; This function will Calculate number of ways ; Driver code | def factorial ( n ) : NEW_LINE INDENT result = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT result *= i NEW_LINE DEDENT return result NEW_LINE DEDENT def nCr ( n , r ) : NEW_LINE INDENT return ( factorial ( n ) // ( factorial ( r ) * factorial ( n - r ) ) ) NEW_LINE DEDENT def calculate_result ( n ) : NEW_... |
Print values of ' a ' in equation ( a + b ) <= n and a + b is divisible by x | function to Find values of a , in equation ( a + b ) <= n and a + b is divisible by x . ; least possible which is divisible by x ; run a loop to get required answer ; increase value by x ; answer is possible ; Driver code ; function call | def PossibleValues ( b , x , n ) : NEW_LINE INDENT leastdivisible = int ( b / x + 1 ) * x NEW_LINE flag = 1 NEW_LINE while ( leastdivisible <= n ) : NEW_LINE INDENT if ( leastdivisible - b >= 1 ) : NEW_LINE INDENT print ( leastdivisible - b , end = " β " ) NEW_LINE leastdivisible += x NEW_LINE flag = 0 NEW_LINE DEDENT ... |
Check if all sub | Function to calculate product of digits between given indexes ; Function to check if all sub - numbers have distinct Digit product ; Length of number N ; Digit array ; set to maintain digit products ; Finding all possible subarrays ; Driver Code | def digitProduct ( digits , start , end ) : NEW_LINE INDENT pro = 1 NEW_LINE for i in range ( start , end + 1 ) : NEW_LINE INDENT pro *= digits [ i ] NEW_LINE DEDENT return pro NEW_LINE DEDENT def isDistinct ( N ) : NEW_LINE INDENT s = str ( N ) NEW_LINE length = len ( s ) NEW_LINE digits = [ None ] * length NEW_LINE p... |
Ulam Number Sequence | Python3 code to print nth Ulam number ; Array to store Ulam Number ; function to compute ulam Number ; Set to search specific Ulam number efficiently ; push First 2 two term of the sequence in the array and set for further calculation ; loop to generate Ulam number ; traverse the array and check ... | MAX = 10000 NEW_LINE arr = [ ] NEW_LINE def ulam ( ) : NEW_LINE INDENT s = set ( ) NEW_LINE arr . append ( 1 ) NEW_LINE s . add ( 1 ) NEW_LINE arr . append ( 2 ) NEW_LINE s . add ( 2 ) NEW_LINE for i in range ( 3 , MAX ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( 0 , len ( arr ) ) : NEW_LINE INDENT if ( i - ... |
Hilbert Number | Utility function to return Nth Hilbert Number ; Driver code | def nthHilbertNumber ( n ) : NEW_LINE INDENT return 4 * ( n - 1 ) + 1 NEW_LINE DEDENT n = 5 NEW_LINE print ( nthHilbertNumber ( n ) ) NEW_LINE |
Program to find the nth Kynea number | Function to calculate nth kynea number ; Firstly calculate 2 ^ n + 1 ; Now calculate ( 2 ^ n + 1 ) ^ 2 ; Now calculate ( ( 2 ^ n + 1 ) ^ 2 ) - 2 ; return nth Kynea number ; Driver Code ; print nth kynea number | def nthKyneaNumber ( n ) : NEW_LINE INDENT n = ( 1 << n ) + 1 NEW_LINE n = n * n NEW_LINE n = n - 2 NEW_LINE return n NEW_LINE DEDENT n = 2 NEW_LINE print ( nthKyneaNumber ( n ) ) NEW_LINE |
Program to find the nth Kynea number | Function to calculate nth kynea number ; Calculate nth kynea number ; Driver Code ; print nth kynea number | def nthKyneaNumber ( n ) : NEW_LINE INDENT return ( ( 1 << ( 2 * n ) ) + ( 1 << ( n + 1 ) ) - 1 ) NEW_LINE DEDENT n = 2 NEW_LINE print ( nthKyneaNumber ( n ) ) NEW_LINE |
Program to check whether a number is Proth number or not | Utility function to Check power of two ; Function to check if the Given number is Proth number or not ; check if k divides n or not ; Check if n / k is power of 2 or not ; update k to next odd number ; If we reach here means there exists no value of K Such that... | def isPowerOfTwo ( n ) : NEW_LINE INDENT return ( n and ( not ( n & ( n - 1 ) ) ) ) NEW_LINE DEDENT def isProthNumber ( n ) : NEW_LINE INDENT k = 1 NEW_LINE while ( k < ( n // k ) ) : NEW_LINE INDENT if ( n % k == 0 ) : NEW_LINE INDENT if ( isPowerOfTwo ( n // k ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT ... |
Find last two digits of sum of N factorials | Function to find the unit ' s β and β ten ' s place digit ; Let us write for cases when N is smaller than or equal to 10 ; We know following ( 1 ! + 2 ! + 3 ! + 4 ! ... + 10 ! ) % 100 = 13 ( N >= 10 ) ; Driver Code | def get_last_two_digit ( N ) : NEW_LINE INDENT if N <= 10 : NEW_LINE INDENT ans = 0 NEW_LINE fac = 1 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT fac = fac * i NEW_LINE ans += fac NEW_LINE DEDENT ans = ans % 100 NEW_LINE return ans NEW_LINE DEDENT else : NEW_LINE INDENT return 13 NEW_LINE DEDENT DEDENT N = 1... |
Check whether product of ' n ' numbers is even or odd | function to check whether product of ' n ' numbers is even or odd ; if a single even number is found , then final product will be an even number ; product is an odd number ; Driver Code | def isProductEven ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( ( arr [ i ] & 1 ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT arr = [ 2 , 4 , 3 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE if ( isProductEven ( arr , n ) ) : NEW_LINE INDENT print ( ... |
Sum of squares of Fibonacci numbers | Function to calculate sum of squares of Fibonacci numbers ; Initialize result ; Add remaining terms ; Driver Code | def calculateSquareSum ( n ) : NEW_LINE INDENT fibo = [ 0 ] * ( n + 1 ) ; NEW_LINE if ( n <= 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT fibo [ 0 ] = 0 ; NEW_LINE fibo [ 1 ] = 1 ; NEW_LINE sum = ( ( fibo [ 0 ] * fibo [ 0 ] ) + ( fibo [ 1 ] * fibo [ 1 ] ) ) ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT ... |
Value of the series ( 1 ^ 3 + 2 ^ 3 + 3 ^ 3 + ... + n ^ 3 ) mod 4 for a given n | function for obtaining the value of f ( n ) mod 4 ; Find the remainder of n when divided by 4 ; If n is of the form 4 k or 4 k + 3 ; If n is of the form 4 k + 1 or 4 k + 2 ; Driver code | def fnMod ( n ) : NEW_LINE INDENT rem = n % 4 NEW_LINE if ( rem == 0 or rem == 3 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( rem == 1 or rem == 2 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 6 NEW_LINE print ( fnMod ( n ) ) NEW_LINE DEDENT |
Minimum increment operations to make the array in increasing order | function to find minimum moves required to make the array in increasing order ; to store answer ; iterate over an array ; non - increasing order ; add moves to answer ; increase the element ; return required answer ; Driver code | def MinimumMoves ( a , n , x ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if a [ i ] <= a [ i - 1 ] : NEW_LINE INDENT p = ( a [ i - 1 ] - a [ i ] ) // x + 1 NEW_LINE ans += p NEW_LINE a [ i ] += p * x NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : ... |
Check if a large number is divisible by 2 , 3 and 5 or not | function to return sum of digits of a number ; function to Check if a large number is divisible by 2 , 3 and 5 or not ; Driver code | def SumOfDigits ( str , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum += int ( ord ( str [ i ] ) - ord ( '0' ) ) NEW_LINE DEDENT return sum NEW_LINE DEDENT def Divisible ( str , n ) : NEW_LINE INDENT if ( ( SumOfDigits ( str , n ) % 3 == 0 and str [ n - 1 ] == '0' ) ) : NEW_LINE ... |
Count all the numbers in a range with smallest factor as K | Function to check if k is a prime number or not ; Corner case ; Check from 2 to n - 1 ; Function to check if a number is not divisible by any number between 2 and K - 1 ; to check if the num is divisible by any numbers between 2 and k - 1 ; if not divisible b... | def isPrime ( k ) : NEW_LINE INDENT if ( k <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , k ) : NEW_LINE INDENT if ( k % i == 0 ) : NEW_LINE INDENT return false NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def check ( num , k ) : NEW_LINE INDENT flag = 1 NEW_LINE for i in range ( 2 , k... |
Largest number with the given set of N digits that is divisible by 2 , 3 and 5 | Function to find the largest integer with the given set ; find sum of all the digits look if any 0 is present or not ; if 0 is not present , the resultant number won 't be divisible by 5 ; sort all the elements in a non - decreasing manner... | def findLargest ( n , v ) : NEW_LINE INDENT flag = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( v [ i ] == 0 ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT sum += v [ i ] NEW_LINE DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( " Not β possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT v . s... |
Find the k | Python3 program to find the K - th smallest factor ; Function to find the k 'th divisor ; initialize vectors v1 and v2 ; store all the divisors in the two vectors accordingly ; reverse the vector v2 to sort it in increasing order ; if k is greater than the size of vectors then no divisor can be possible ; ... | import math as mt NEW_LINE def findkth ( n , k ) : NEW_LINE INDENT v1 = list ( ) NEW_LINE v2 = list ( ) NEW_LINE for i in range ( 1 , mt . ceil ( n ** ( .5 ) ) ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT v1 . append ( i ) NEW_LINE if ( i != mt . ceil ( mt . sqrt ( n ) ) ) : NEW_LINE INDENT v2 . append ( n /... |
Number of solutions for x < y , where a <= x <= b and c <= y <= d and x , y are integers | function to Find the number of solutions for x < y , where a <= x <= b and c <= y <= d and x , y integers . ; to store answer ; iterate explicitly over all possible values of x ; return answer ; Driver code ; function call | def NumberOfSolutions ( a , b , c , d ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( a , b + 1 ) : NEW_LINE INDENT if d >= max ( c , i + 1 ) : NEW_LINE INDENT ans += d - max ( c , i + 1 ) + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b , c , d = 2 , 3... |
Minimum value possible of a given function from the given set | Function to find the value of F ( n ) ; Driver code | def findF_N ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = ans + ( i + 1 ) * ( n - i - 1 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 3 NEW_LINE print ( findF_N ( n ) ) NEW_LINE |
Find N digits number which is divisible by D | Function to return N digits number which is divisible by D ; to store answer ; Driver code | def findNumber ( n , d ) : NEW_LINE INDENT ans = " " NEW_LINE if ( d != 10 ) : NEW_LINE INDENT ans += str ( d ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT ans += '0' NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT ans += " Impossible " NEW_LINE DEDENT else : NEW_LINE INDENT ans +=... |
Count all the numbers less than 10 ^ 6 whose minimum prime factor is N | Python3 implementation of above approach ; the sieve of prime number and count of minimum prime factor ; form the prime sieve ; 1 is not a prime number ; form the sieve ; if i is prime ; if i is the least prime factor ; mark the number j as non pr... | MAX = 1000000 NEW_LINE sieve_Prime = [ 0 for i in range ( MAX + 4 ) ] NEW_LINE sieve_count = [ 0 for i in range ( MAX + 4 ) ] NEW_LINE def form_sieve ( ) : NEW_LINE INDENT sieve_Prime [ 1 ] = 1 NEW_LINE for i in range ( 2 , MAX + 1 ) : NEW_LINE INDENT if sieve_Prime [ i ] == 0 : NEW_LINE INDENT for j in range ( i * 2 ,... |
How to access elements of a Square Matrix | Python3 Program to read a square matrix and print the elements above secondary diagonal ; Get the square matrix ; Display the matrix ; Print the elements above secondary diagonal ; check for elements above secondary diagonal | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT matrix = [ [ 0 for i in range ( 5 ) ] for j in range ( 5 ) ] ; NEW_LINE row_index , column_index , x , size = 0 , 0 , 0 , 5 ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT x += 1 ; NEW_LINE matrix [ row_i... |
How to access elements of a Square Matrix | Python3 Program to read a square matrix and print the Corner Elements ; Get the square matrix ; Display the matrix ; Print the Corner elements ; check for corner elements | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT matrix = [ [ 0 for i in range ( 5 ) ] for j in range ( 5 ) ] NEW_LINE row_index , column_index , x , size = 0 , 0 , 0 , 5 ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT x += 1 ; NEW_LINE matrix [ row_ind... |
Find the largest good number in the divisors of given number N | function to return distinct prime factors ; to store distinct prime factors ; run a loop upto sqrt ( n ) ; place this prime factor in vector ; This condition is to handle the case when n is a prime number greater than 1 ; function that returns good number... | def PrimeFactors ( n ) : NEW_LINE INDENT v = [ ] NEW_LINE x = n NEW_LINE i = 2 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT v . append ( i ) NEW_LINE while ( x % i == 0 ) : NEW_LINE INDENT x //= i NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT if ( x > 1 ) : NEW_LINE INDENT v . app... |
Find Largest Special Prime which is less than or equal to a given number | Function to check whether the number is a special prime or not ; While number is not equal to zero ; If the number is not prime return false . ; Else remove the last digit by dividing the number by 10. ; If the number has become zero then the nu... | def checkSpecialPrime ( sieve , num ) : NEW_LINE INDENT while ( num ) : NEW_LINE INDENT if ( not sieve [ num ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT num //= 10 NEW_LINE DEDENT return True NEW_LINE DEDENT def findSpecialPrime ( N ) : NEW_LINE INDENT sieve = [ True ] * ( N + 10 ) NEW_LINE sieve [ 0 ] = sieve [... |
Check if an integer can be expressed as a sum of two semi | Python3 Code to check if an integer can be expressed as sum of two semi - primes ; Utility function to compute semi - primes in a range ; Increment count of prime numbers ; If number is greater than 1 , add it to the count variable as it indicates the number r... | MAX = 10000 NEW_LINE arr = [ ] NEW_LINE sprime = [ False ] * ( MAX ) NEW_LINE def computeSemiPrime ( ) : NEW_LINE INDENT for i in range ( 2 , MAX ) : NEW_LINE INDENT cnt , num , j = 0 , i , 2 NEW_LINE while cnt < 2 and j * j <= num : NEW_LINE INDENT while num % j == 0 : NEW_LINE INDENT num /= j NEW_LINE cnt += 1 NEW_LI... |
Check if a number is a Pythagorean Prime or not | Utility function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver Code ; Check if number is prime and of the form 4 n + 1 | def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2... |
Divide an isosceles triangle in two parts with ratio of areas as n : m | Python 3 program , to find height h which divide isosceles triangle into ratio n : m ; Function to return the height ; type cast the n , m into float ; calculate the height for cut ; Driver code | from math import sqrt NEW_LINE def heightCalculate ( H , n , m ) : NEW_LINE INDENT N = n * 1.0 NEW_LINE M = m * 1.0 NEW_LINE h = H * sqrt ( N / ( N + M ) ) NEW_LINE return h NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT H = 10 NEW_LINE n = 3 NEW_LINE m = 4 NEW_LINE print ( " { 0 : . 6 } " . format (... |
Check n ^ 2 | Check a number is prime or not ; run a loop upto square of given number ; Check if n ^ 2 - m ^ 2 is prime ; Driver code | def isprime ( x ) : NEW_LINE INDENT for i in range ( 2 , math . sqrt ( x ) ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def isNSqMinusnMSqPrime ( m , n ) : NEW_LINE INDENT if ( n - m == 1 and isprime ( m + n ) ) : NEW_LINE INDENT return True... |
Find ' N ' number of solutions with the given inequality equations | Function to calculate all the solutions ; there is no solutions ; print first element as y - n + 1 ; print rest n - 1 elements as 1 ; initialize the number of elements and the value of x an y | def findsolution ( n , x , y ) : NEW_LINE INDENT if ( ( y - n + 1 ) * ( y - n + 1 ) + n - 1 < x or y < n ) : NEW_LINE INDENT print ( " No β solution " ) ; NEW_LINE return ; NEW_LINE DEDENT print ( y - n + 1 ) ; NEW_LINE while ( n > 1 ) : NEW_LINE INDENT print ( 1 ) ; NEW_LINE n -= 1 ; NEW_LINE DEDENT DEDENT n = 5 ; NEW... |
Number of different positions where a person can stand | Function to find the position ; Driver code | def findPosition ( n , f , b ) : NEW_LINE INDENT return n - max ( f + 1 , n - b ) + 1 ; NEW_LINE DEDENT n , f , b = 5 , 2 , 3 NEW_LINE print ( findPosition ( n , f , b ) ) NEW_LINE |
Program for n | Function to find the nth odd number ; Driver code | def nthOdd ( n ) : NEW_LINE INDENT return ( 2 * n - 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE print ( nthOdd ( n ) ) NEW_LINE DEDENT |
Program for n | Function to find the nth Even number ; Driver code | def nthEven ( n ) : NEW_LINE INDENT return ( 2 * n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE print ( nthEven ( n ) ) NEW_LINE DEDENT |
Program to find the Nth Harmonic Number | Function to find N - th Harmonic Number ; H1 = 1 ; loop to apply the forumula Hn = H1 + H2 + H3 ... + Hn - 1 + Hn - 1 + 1 / n ; Driver code | def nthHarmonic ( N ) : NEW_LINE INDENT harmonic = 1.00 NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT harmonic += 1 / i NEW_LINE DEDENT return harmonic NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 8 NEW_LINE print ( round ( nthHarmonic ( N ) , 5 ) ) NEW_LINE DEDENT |
Program to find Nth term of series 0 , 7 , 18 , 33 , 51 , 75 , 102 , 133 , ... . . | calculate Nth term of series ; Driver code | def nthTerm ( n ) : NEW_LINE INDENT return 2 * pow ( n , 2 ) + n - 3 NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE |
Program to find Nth term of series 0 , 10 , 30 , 60 , 99 , 150 , 210 , 280. ... ... ... . | calculate Nth term of series ; Driver code | def nthTerm ( n ) : NEW_LINE INDENT return 5 * pow ( n , 2 ) - 5 * n NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE |
Program to find Nth term of series 2 , 12 , 28 , 50 , 77 , 112 , 152 , 198 , ... . . | calculate Nth term of series ; Driver code | def nthTerm ( n ) : NEW_LINE INDENT return 3 * pow ( n , 2 ) + n - 2 NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE |
Program to find Nth term of series 4 , 14 , 28 , 46 , 68 , 94 , 124 , 158 , ... . . | calculate Nth term of series ; Driver code | def nthTerm ( n ) : NEW_LINE INDENT return 2 * pow ( n , 2 ) + 4 * n - 2 NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE |
Program to find Nth term of series 0 , 11 , 28 , 51 , 79 , 115 , 156 , 203 , ... . | calculate Nth term of series ; Driver code | def nthTerm ( n ) : NEW_LINE INDENT return 3 * pow ( n , 2 ) + 2 * n - 5 NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE |
Program to find Nth term of series 0 , 9 , 22 , 39 , 60 , 85 , 114 , 147 , ... . . | calculate Nth term of series ; Driver code | def nthTerm ( n ) : NEW_LINE INDENT return 2 * pow ( n , 2 ) + 3 * n - 5 NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE |
Program to find Nth term of series 3 , 12 , 29 , 54 , 86 , 128 , 177 , 234 , ... . . | calculate Nth term of series ; Driver code | def nthTerm ( n ) : NEW_LINE INDENT return 4 * pow ( n , 2 ) - 3 * n + 2 NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE |
Find other two sides and angles of a right angle triangle | Python 3 program to print all sides and angles of right angle triangle given one side ; Function to find angle A Angle in front of side a ; applied cosine rule ; convert into degrees ; Function to find angle B Angle in front of side b ; applied cosine rule ; c... | import math NEW_LINE PI = 3.1415926535 NEW_LINE def findAnglesA ( a , b , c ) : NEW_LINE INDENT A = math . acos ( ( b * b + c * c - a * a ) / ( 2 * b * c ) ) NEW_LINE return A * 180 / PI NEW_LINE DEDENT def findAnglesB ( a , b , c ) : NEW_LINE INDENT B = math . acos ( ( a * a + c * c - b * b ) / ( 2 * a * c ) ) NEW_LIN... |
Sum of the first N terms of the series 2 , 6 , 12 , 20 , 30. ... | Function to calculate the sum ; number of terms to be included in the sum ; find the Sum | def calculateSum ( n ) : NEW_LINE INDENT return ( n * ( n + 1 ) // 2 + n * ( n + 1 ) * ( 2 * n + 1 ) // 6 ) NEW_LINE DEDENT n = 3 NEW_LINE print ( " Sum β = β " , calculateSum ( n ) ) NEW_LINE |
Program to find the Nth term of the series 0 , 5 , 14 , 27 , 44 , ... ... . . | Calculate Nth term of series ; Driver code | def nthTerm ( n ) : NEW_LINE INDENT return 2 * pow ( n , 2 ) - n - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE DEDENT |
Program to find the Nth term of the series 0 , 5 , 18 , 39 , 67 , 105 , 150 , 203 , ... | calculate Nth term of series ; Driver code | def nthTerm ( n ) : NEW_LINE INDENT return 4 * pow ( n , 2 ) - 7 * n + 3 NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE |
Check whether a given Number is Power | Python3 program to find whether a number is power - isolated or not ; for 2 as prime factor ; for odd prime factor ; calculate product of powers and prime factors ; check result for power - isolation ; Driver code | def checkIfPowerIsolated ( num ) : NEW_LINE INDENT input1 = num ; NEW_LINE count = 0 ; NEW_LINE factor = [ 0 ] * ( num + 1 ) ; NEW_LINE if ( num % 2 == 0 ) : NEW_LINE INDENT while ( num % 2 == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE num //= 2 ; NEW_LINE DEDENT factor [ 2 ] = count ; NEW_LINE DEDENT i = 3 ; NEW_LINE... |
Program to find the Nth term of the series 3 , 7 , 13 , 21 , 31. ... . | Function to calculate sum ; Return Nth term ; driver code ; declaration of number of terms ; Get the Nth term | def getNthTerm ( N ) : NEW_LINE INDENT return ( pow ( N , 2 ) + N + 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 11 NEW_LINE print ( getNthTerm ( N ) ) NEW_LINE DEDENT |
Sum of the numbers upto N that are divisible by 2 or 5 | Function to find the sum ; sum2 is sum of numbers divisible by 2 ; sum5 is sum of number divisible by 5 ; sum10 of numbers divisible by 2 and 5 ; Driver code | def findSum ( n ) : NEW_LINE INDENT sum2 = ( ( n // 2 ) * ( 4 + ( n // 2 - 1 ) * 2 ) ) // 2 NEW_LINE sum5 = ( ( n // 5 ) * ( 10 + ( n // 5 - 1 ) * 5 ) ) // 2 NEW_LINE sum10 = ( ( n // 10 ) * ( 20 + ( n // 10 - 1 ) * 10 ) ) // 2 NEW_LINE return sum2 + sum5 - sum10 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_... |
Find four factors of N with maximum product and sum equal to N | Set 3 | Python3 implementation of above approach ; Function to find primes ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to find factors ; run a loop upto square root of that number ; if the n is perfect... | from math import sqrt , ceil , floor NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 5 , ceil ( sqrt ( n )... |
Ratio of mth and nth terms of an A . P . with given ratio of sums | function to calculate ratio of mth and nth term ; ratio will be tm / tn = ( 2 * m - 1 ) / ( 2 * n - 1 ) ; Driver code | def CalculateRatio ( m , n ) : NEW_LINE INDENT return ( 2 * m - 1 ) / ( 2 * n - 1 ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT m = 6 ; NEW_LINE n = 2 ; NEW_LINE print ( float ( CalculateRatio ( m , n ) ) ) ; NEW_LINE DEDENT |
Find the sum of n terms of the series 1 , 8 , 27 , 64 ... . | Function to calculate the sum ; return total sum ; Driver code | def calculateSum ( n ) : NEW_LINE INDENT return ( n * ( n + 1 ) / 2 ) ** 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE print ( calculateSum ( n ) ) NEW_LINE DEDENT |
Sum of Digits in a ^ n till a single digit | This function finds single digit sum of n . ; Returns single digit sum of a ^ n . We use modular exponentiation technique . ; Driver Code | def digSum ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif n % 9 == 0 : NEW_LINE INDENT return 9 NEW_LINE DEDENT else : NEW_LINE INDENT return n % 9 NEW_LINE DEDENT DEDENT def powerDigitSum ( a , n ) : NEW_LINE INDENT res = 1 NEW_LINE while ( n ) : NEW_LINE INDENT if n % 2 == 1 : NEW_L... |
Program to find total number of edges in a Complete Graph | Function to find the total number of edges in a complete graph with N vertices ; Driver Code | def totEdge ( n ) : NEW_LINE INDENT result = ( n * ( n - 1 ) ) // 2 NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 6 NEW_LINE print ( totEdge ( n ) ) NEW_LINE DEDENT |
Find minimum number of Log value needed to calculate Log upto N | Python3 program to find number of log values needed to calculate all the log values from 1 to N ; In this list prime [ i ] will store true if prime [ i ] is prime , else store false ; Using sieve of Eratosthenes to find all prime upto N ; Function to fin... | MAX = 1000005 NEW_LINE prime = [ True for i in range ( MAX ) ] NEW_LINE def sieve ( N ) : NEW_LINE INDENT prime [ 0 ] , prime [ 1 ] = False , False NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT for j in range ( 2 , N + 1 ) : NEW_LINE INDENT if ( i * j > N ) : NEW_LINE INDE... |
Program to find the count of coins of each type from the given ratio | function to calculate coin . ; Converting each of them in rupees . As we are given totalRupees = 1800 ; Driver code | def coin ( totalRupees , X , Y , Z ) : NEW_LINE INDENT one = X * 1 NEW_LINE fifty = ( ( Y * 1 ) / 2.0 ) NEW_LINE twentyfive = ( ( Z * 1 ) / 4.0 ) NEW_LINE total = one + fifty + twentyfive NEW_LINE result = ( ( totalRupees ) / total ) NEW_LINE return int ( result ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_L... |
Find the sum of series 0. X + 0. XX + 0. XXX + ... upto k terms | function which return the sum of series ; Driver code | def sumOfSeries ( x , k ) : NEW_LINE INDENT return ( float ( x ) / 81 ) * ( 9 * k - 1 + 10 ** ( ( - 1 ) * k ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 9 NEW_LINE k = 20 NEW_LINE print ( sumOfSeries ( x , k ) ) NEW_LINE DEDENT |
Find four factors of N with maximum product and sum equal to N | Set | Python3 program to find four factors of N with maximum product and sum equal to N ; Function to find factors and to prthose four factors ; push all the factors in the container ; number of factors ; Initial maximum ; hash - array to mark the pairs ;... | from math import sqrt , ceil , floor NEW_LINE def findfactors ( n ) : NEW_LINE INDENT mpp = dict ( ) NEW_LINE v = [ ] NEW_LINE v1 = [ ] NEW_LINE for i in range ( 1 , ceil ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT v . append ( i ) NEW_LINE if ( i != ( n // i ) and i != 1 ) : NEW_LINE IND... |
Maximize the product of four factors of a Number | Python 3 implementation of above approach ; Declare the vector of factors for storing the ; function to find out the factors of a number ; Loop until the i reaches the sqrt ( n ) ; Check if i is a factor of n ; if both the factors are same we only push one factor ; fac... | from math import sqrt NEW_LINE factors = [ ] NEW_LINE def findFactors ( n ) : NEW_LINE INDENT for i in range ( 1 , int ( sqrt ( n ) ) + 1 , 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( ( n / i ) == i ) : NEW_LINE INDENT factors . append ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT factors . append ( n... |
Maximize the product of four factors of a Number | For calculation of a ^ b ; Function to check ; every odd and number less than 3. ; every number divisible by 4. ; every number divisible by 6. ; every number divisible by 10. ; for every even number which is not divisible by above values . ; Driver code | def modExp ( a , b ) : NEW_LINE INDENT result = 1 NEW_LINE while ( b > 0 ) : NEW_LINE INDENT if ( int ( b ) & 1 ) : NEW_LINE INDENT result = result * a NEW_LINE DEDENT a = a * a NEW_LINE b /= 2 NEW_LINE DEDENT return result NEW_LINE DEDENT def check ( num ) : NEW_LINE INDENT if ( num & 1 or num < 3 ) : NEW_LINE INDENT ... |
Check if any large number is divisible by 17 or not | Function to check if the number is divisible by 17 or not ; Extracting the last digit ; Truncating the number ; Subtracting the five times the last digit from the remaining number ; Return n is divisible by 17 ; Driver Code | def isDivisible ( n ) : NEW_LINE INDENT while ( n // 100 ) : NEW_LINE INDENT d = n % 10 NEW_LINE n //= 10 NEW_LINE n -= d * 5 NEW_LINE DEDENT return ( n % 17 == 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 19877658 NEW_LINE if isDivisible ( n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE D... |
Minimum number of elements to be removed to make XOR maximum | Python 3 to find minimum number of elements to remove to get maximum XOR value ; First n in the below condition is for the case where n is 0 ; Function to find minimum number of elements to be removed . ; Driver Code ; print minimum number of elements to be... | def nextPowerOf2 ( n ) : NEW_LINE INDENT count = 0 NEW_LINE if ( n and not ( n and ( n - 1 ) ) ) : NEW_LINE INDENT return n NEW_LINE DEDENT while n != 0 : NEW_LINE INDENT n >>= 1 NEW_LINE count += 1 NEW_LINE DEDENT return 1 << count NEW_LINE DEDENT def removeElement ( n ) : NEW_LINE INDENT if n == 1 or n == 2 : NEW_LIN... |
Program to find Length of Bridge using Speed and Length of Train | function to calculate the length of bridge . ; Driver Code ; Assuming the input variables | def bridge_length ( trainLength , Speed , Time ) : NEW_LINE INDENT return ( ( Time * Speed ) - trainLength ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT trainLength = 120 NEW_LINE Speed = 30 NEW_LINE Time = 18 NEW_LINE print ( " Length β of β bridge β = β " , bridge_length ( trainLength , Speed , ... |
Program to find sum of the given sequence | Python3 program to find the sum of the given sequence ; function to find moudulo inverse under 10 ^ 9 + 7 ; Function to find the sum of the given sequence ; Driver code | MOD = 1000000007 ; NEW_LINE def modInv ( x ) : NEW_LINE INDENT n = MOD - 2 ; NEW_LINE result = 1 ; NEW_LINE while ( n ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT result = result * x % MOD ; NEW_LINE DEDENT x = x * x % MOD ; NEW_LINE n = int ( n / 2 ) ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT def getSum (... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.