Advertisement
makispaiktis

Problem 7 - Egyptian Fractions with 3 terms

Jul 5th, 2021 (edited)
1,127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. '''
  2. 4/n = 1/i + 1/j + 1/k
  3. n > 1 <----> 4/n <= 2 <----> i=1, j=2, k=2 the lower bounds
  4. '''
  5.  
  6. # FUNCTION 1 - Egyptian fractions
  7. def egyptian(n, LIMIT):
  8.     if n <= 1 or n != int(n):
  9.         print("Error while using function '" + egyptian.__name__ + "'")
  10.         return -1000
  11.     isThereSolution = False
  12.     for i in range(1, LIMIT+1):
  13.         flag1 = False
  14.         for j in range(2, LIMIT+1):
  15.             flag2 = False
  16.             for k in range(2, LIMIT+1):
  17.                 if 4/n == 1/i + 1/j + 1/k:
  18.                     flag1 = True
  19.                     flag2 = True
  20.                     flag3 = True
  21.                     isThereSolution = True
  22.                     print("4/" + str(n) + " = " + "1/" + str(i) + " + 1/" + str(j) + " + 1/" + str(k))
  23.                     break       # This one will drop me in 2nd loop
  24.             if flag2:
  25.                 break
  26.         if flag1:
  27.             break
  28.     if isThereSolution == False:
  29.         print(str(n) + ": 4/" + str(n) + " cannot be written as sum of 3 egyptian fractions! LIMIT = " + str(LIMIT) + " (lowest = 1/" + str(LIMIT) + ")")
  30.     # With the 3 break-cases, we will gain much time
  31.  
  32. # MAIN FUNCTION
  33. # First, I will decide for the LIMIT
  34. LIMIT = 200
  35. for n in range(2, 100):
  36.     egyptian(n, LIMIT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement