greedy_fractions.html


001| '''Greedy Algorithm for Egyptian Fractions.
001| 
001| Highly inefficient method for finding p/q = 1/a + 1/b + 1/c ...
001| 
001| The default sequence 7/15 is well-behaved, but others result in enormous denominators.
001| For example, 5/121 has five terms, the last of which has 25 digits.
001| Even worse is 31/311, which has ten terms, the last of which has 500 digits.
001| Given the simplistic algorithm here, calculating the denominators would take forever!
001| 
001| see: https://en.wikipedia.org/wiki/Greedy_algorithm_for_Egyptian_fractions
001| '''

002| ####################################################################################################
003| from sys import argv
004| from os import path
005| from math import gcd
006| ####################################################################################################
007| BasePath = r'C:\CJS\prj\Python\app\maths'
008| 
009| ##================================================================================================##
010| def find_biggest_fraction (p:int, q:int) -> tuple[int,int,int,int]:
011|     '''Careful! Can take forever!! (e.g. 5/121)'''
012|     print(f'Input: {p}/{q}')
013|     if q == 0:
014|         raise ValueError('Denominator cannot be zero!')
015|     if p == 0:
016|         return (0,0, 0,0)
017|     if p == 1:
018|         return (p,q, 0,0)
019|     # Find biggest 1/d that's smaller than p/q...
020|     value = p/q
021|     denom = 2
022|     while value < (1/denom):
023|         print(f'Tried: 1/{denom}')
024|         denom += 1
025|     # Subtract result from input (normalize both)...
026|     pr = (p * denom) - q
027|     qr = q * denom
028|     # Reduce to smallest possible denominator...
029|     dv = gcd(pr, qr)
030|     pr = pr // dv
031|     qr = qr // dv
032|     # Return (1, denom, new-p,new-q)...
033|     print(f'Return: 1/{denom}, {pr}/{qr}')
034|     print()
035|     return (1,denom, pr, qr)
036| 
037| ##================================================================================================##
038| def main (*args):
039|     # Input parameters or use defaults...
040|     a = int(argv[1]) if 1 < len(argv) else 7
041|     b = int(argv[2]) if 2 < len(argv) else 15
042| 
043|     # fracs is a list of denominators...
044|     fracs = []
045|     t = (None, None, a, b)
046| 
047|     # Keep subtracting 1/denom from p/q until p=1...
048|     while 1 < t[2]:
049|         t = find_biggest_fraction(t[2], t[3])
050|         # Add denominator to list...
051|         fracs.append(t[1])
052| 
053|     # If p/q=1/n, then add n to denominator list...
054|     if t[2]:
055|         fracs.append(t[3])
056| 
057|     # Print result...
058|     fs = ', '.join(f'1/{q}' for q in fracs)
059|     print()
060|     print(f'Result: {a}/{b} = {fs}')
061| 
062|     return (a, b, t, fs)
063| 
064| ####################################################################################################
065| if __name__ == "__main__":
066|     print('autorun: %s' % argv[0])
067|     rv = main(*argv[1:])
068|     print(rv)
069| ####################################################################################################
070| '''eof'''
071|