Category: Python Programming Examples

python tutorials and learn python

Created with Sketch.

Python Program for Difference between sums of odd and even digits

Python Program for Difference between sums of odd and even digits Given a long integer, we need to find if the difference between sum of odd digits and sum of even digits is 0 or not. The indexes start from zero (0 index is for leftmost digit). Examples: Input : 1212112 Output : Yes Explanation:-…
Read more

Python Program for Find minimum sum of factors of number

Python Program for Find minimum sum of factors of number Given a number, find minimum sum of its factors. Examples: Input : 12 Output : 7 Explanation: Following are different ways to factorize 12 and sum of factors in different ways. 12 = 12 * 1 = 12 + 1 = 13 12 = 2…
Read more

Python Program for Check if count of divisors is even or odd

Python Program for Check if count of divisors is even or odd Given a number “n”, find its total number of divisors are even or odd. Examples : Input : n = 10 Output : Even Input: n = 100 Output: Odd Input: n = 125 Output: Even A naive approach would be to find…
Read more

Python Program for GCD of more than two (or array) numbers

Python Program for GCD of more than two (or array) numbers The GCD of three or more numbers equals the product of the prime factors common to all the numbers, but it can also be calculated by repeatedly taking the GCDs of pairs of numbers. gcd(a, b, c) = gcd(a, gcd(b, c)) = gcd(gcd(a, b),…
Read more

Python Program for Maximum height when coins are arranged in a triangle

Python Program for Maximum height when coins are arranged in a triangle We have N coins which need to arrange in form of a triangle, i.e. first row will have 1 coin, second row will have 2 coins and so on, we need to tell maximum height which we can achieve by using these N…
Read more

Python Program for Common Divisors of Two Numbers

Python Program for Common Divisors of Two Numbers Given two integer numbers, the task is to find count of all common divisors of given numbers? Input : a = 12, b = 24 Output: 6 // all common divisors are 1, 2, 3, // 4, 6 and 12 Input : a = 3, b =…
Read more

Python Program for Number of elements with odd factors in given range

Python Program for Number of elements with odd factors in given range Given a range [n,m], find the number of elements that have odd number of factors in the given range (n and m inclusive). Examples: Input : n = 5, m = 100 Output : 8 The numbers with odd factors are 9, 16,…
Read more

Python Program for Extended Euclidean algorithms

Python Program for Extended Euclidean algorithms # Python program to demonstrate working of extended # Euclidean Algorithm   # function for extended Euclidean Algorithm def gcdExtended(a, b, x, y):     # Base Case     if a == 0 :          x = 0         y = 1         return b               x1 = 1     y1 = 1 # To store…
Read more

Python Program for Basic Euclidean algorithms

Python Program for Basic Euclidean algorithms # Python program to demonstrate Basic Euclidean Algorithm     # Function to return gcd of a and b def gcd(a, b):      if a == 0 :         return b            return gcd(b%a, a)   a = 10 b = 15 print(“gcd(“, a , “,” , b, “) = “,…
Read more

Python | Check if binary representation is palindrome

Python | Check if binary representation is palindrome Given an integer ‘n’, write a Python function that returns true if binary representation of x is palindrome else return false. Examples: Input : n = 9 Output : True Binary representation of n=9 is 1001 which is palindrome as well. Input : n = 10 Output…
Read more