Advertisement
Programmin-in-Python

Program to find simplest whole ratio between 2 integers

Oct 3rd, 2021
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. def AllFactors(num):
  2.     res = []
  3.  
  4.     for i in range(1, num):
  5.         if num%i == 0:
  6.             res.append(i)
  7.  
  8.     res.append(num)
  9.  
  10.     return res
  11.  
  12. def CommonFactors(num1, num2):
  13.     temp = AllFactors(Tnum1) + AllFactors(Tnum2)
  14.     res = []
  15.  
  16.     for i in temp:
  17.         if (temp.count(i) == 2) and (i not in res):
  18.             res.append(i)
  19.  
  20.     return res
  21.  
  22. # Main Program
  23. num1 = int(input("Enter 1st Number : "))
  24. num2 = int(input("Enter 2nd Number : "))
  25.  
  26. Tnum1, Tnum2 = num1, num2
  27.  
  28. ComFac = CommonFactors(Tnum1, Tnum2)
  29.  
  30. while ComFac != [1]:
  31.     max_ComFac = max(ComFac)
  32.  
  33.     Tnum1 //= max_ComFac
  34.     Tnum2 //= max_ComFac
  35.  
  36.     ComFac = CommonFactors(Tnum1, Tnum2)
  37. else:
  38.     print(f"Simplest Whole Ratio of {num1}:{num2} => {Tnum1}:{Tnum2}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement