# Category: Python Programs

• ## Program for Tower of Hanoi using Python

Introduction The task is to solve Tower of Hanoi problem using python programming. There are set of rules to follow while solving the problem: At a time only one disk is allowed to be moved. Only the uppermost disk is allowed to be moved. Disks can only be placed on the top of larger disks.…

• ## Program to print smallest m digit number divisible by n using Python

Introduction The task to find and print the smallest M digit number divisible by N. Program def result(n, m):     x = pow(10, m-1)        if( x % n == 0):         print(x)          else:         print((x + n) – ((x + n) % n))    n = 64  m = 8  result(n, m)…

• ## Program to find product of unique prime factors of a given number using Python

Introduction The task is to print the product of unique prime factors of the given number. Program def find(ip_num): # Initializing product with 1     prod = 1     for num in range(2, ip_num+1):         if (ip_num % num == 0):             prime_true = 1                          for num2 in range(2, int(num/2 + 1)):                 if…

• ## Program for Sieve of Eratosthenes using Python

Introduction The task is to print all the prime numbers smaller than or equal to the given number n (given n must be small).   Program def SOE(ip_num): # Initialize all the element with True, the value will be False if p_num[val] is not prime     p_num = [True for val in range(ip_num + 1)]…

• ## Program to print the matrix in Z form using Python

Introduction Given a matrix of size n*n, the task is to print the elements in Z form. Program ip_array = [[1, 2, 3],                     [4, 5, 6],                     [7, 8, 9]] length = len(ip_array[0]) #Print first line                   n = 0 for m in range(0, length):     print(ip_array[n][m])  #Print diagonals          l = 1 for…

• ## Program for number of stopping station problems using Python

Introduction The task is to find the number of ways we can stop the train at K stations out of N stations between S1 and S2 such that the stopping stations are non-consecutive. Program def find( s, ts): #select s non-consecutive station from ts number of stations “        temp, temp1  = 1, 1    …

• ## Program to find the most frequent character with its occurrence count using Python

Introduction The task is to find the frequent occurred character in the given string with it occurrence number.   Program from collections import Counter def find(ip_char):     count = Counter(ip_char) # Most occured char     max_occur = max(count.values())     index = count.values().index(max_occur)        print(count.items()[index]) ip_char = ‘alphabet’ find(ip_char) Output (‘a’, 2) Explanation In the…

• ## Program to find the minimum sum of factors of given number using Python

Introduction The task is to find the minimum sum of factors of the given number. Program def find(ip_no): # Initilize sum with 0     output_sum = 0     fact = 2     while(fact * fact <= ip_no):         while(ip_no % fact == 0):             output_sum += fact             ip_no /= fact         fact += 1…

• ## Program to find the maximum height when coins are arranged in triangle using Python

Introduction Given N number of coins, the task is to construct a triangle of maximum height H and print the max height. Program def sq_root(val):     n = val      m = 1 # Accuracy       e = 0.000001     while (n – m > e):         n = (n + m) / 2        …

• ## Program for Legendre’s Conjecture using Python

Introduction There is always one prime between n2 and (n+1)2 . The task is to print all the prime number between the range n2 to (n+1)2 .     Program import math  def check( ip_num ):     n = 2     for n in range (2, int((math.sqrt(ip_num)+1))):         if ip_num % n == 0:            …