Greedy algorithm for Egyptian fractions: Difference between revisions

m
re-introduced original whitespace, and used a bigger font for most formulas.
m (re-introduced original whitespace, used bigger font for the formulas.)
m (re-introduced original whitespace, and used a bigger font for most formulas.)
Line 1:
{{task}}
 
An &nbsp; [[wp:Egyptian fraction|<u>Egyptian fraction</u>]] &nbsp; is the sum of distinct unit fractions such as:
 
:::: <big><big> <math> \tfrac{1}{2} + \tfrac{1}{3} + \tfrac{1}{16} \,(= \tfrac{43}{48})</math> </big></big>
 
Each fraction in the expression has a numerator equal to <math>&nbsp; '''1</math>''' &nbsp; (unity) &nbsp; and a denominator that is a positive integer, &nbsp; and all the denominators are distinct &nbsp; (i.e., no repetitions).
 
Fibonacci's &nbsp; [[wp:Greedy algorithm for Egyptian fractions|<u>Greedy algorithm for Egyptian fractions</u>]] &nbsp; expands the fraction &nbsp; <big> <math> \tfrac{x}{y} </math> </big> &nbsp; to be represented by repeatedly performing the replacement
 
:::: <big> <math> \frac{x}{y} = \frac{1}{\lceil y/x\rceil} + \frac{(-y)\!\!\!\!\mod x}{y\lceil y/x\rceil} </math> </big>
 
 
(simplifying the 2<sup>nd</sup> term in this replacement as necessary, and where &nbsp; <big> <math> \lceil x \rceil </math> </big> &nbsp; is the &nbsp; ''ceiling'' &nbsp; function).
 
<!--
This Rosetta Code task will be using the Fibonacci greedy algorithm for computing Egyptian fractions; however, if different method is used instead, please note which method is being employed. &nbsp; Having all the programming examples use the Fibonacci greedy algorithm will make for easier comparison and compatible results.
-->
 
For this task, &nbsp; [[wp:Fraction (mathematics)#Simple.2C_common.2C_or_vulgar_fractions|<u>Proper and improper fractions</u>]] &nbsp; must be able to be expressed.
 
 
Proper &nbsp;fractions &nbsp; are of the form &nbsp; <big> <math>\tfrac{a}{b}</math> </big> &nbsp; where &nbsp; <big> <math>a</math> </big> &nbsp; and &nbsp; <big> <math>b</math> </big> &nbsp; are positive integers, such that &nbsp; <big> <math>a < b</math></big>, &nbsp; &nbsp; and
 
<br>improper fractions are of the form &nbsp; <big> <math>\tfrac{a}{b}</math> </big> &nbsp; where &nbsp; <big> <math>a</math> </big> &nbsp; and &nbsp; <big> <math>b</math> </big> &nbsp; are positive integers, such that &nbsp; <big> <span style="font-family:times">''a'' ≥ ''b''</span></big>.
 
Proper fractions &nbsp; are of the form <math>\tfrac{a}{b}</math> where <math>a</math> and <math>b</math> are positive integers, such that <math>a < b</math>, and
<br>improper fractions are of the form <math>\tfrac{a}{b}</math> where <math>a</math> and <math>b</math> are positive integers, such that <span style="font-family:times">''a'' ≥ ''b''</span>.
 
(See the [[#REXX|REXX programming example]] to view one method of expressing the whole number part of an improper fraction.)
Line 29 ⟶ 33:
;Task requirements:
* &nbsp; show the Egyptian fractions for: <math> \tfrac{43}{48} </math> and <math> \tfrac{5}{121} </math> and <math> \tfrac{2014}{59} </math>
* &nbsp; for all proper fractions, &nbsp; <big> <math>\tfrac{a}{b}</math> </big> &nbsp; where &nbsp; <big> <math>a</math> </big> &nbsp; and &nbsp; <big> <math>b</math> </big> &nbsp; are positive one-or two-digit (decimal) integers, find and show an Egyptian fraction that has:
::* &nbsp; the largest number of terms,
::* &nbsp; the largest denominator.
* &nbsp; for all one-, two-, and three-digit integers (extra credit), &nbsp; find and show (as above). &nbsp; &nbsp; {extra credit}