How to resolve the algorithm Greedy algorithm for Egyptian fractions step by step in the Frink programming language
How to resolve the algorithm Greedy algorithm for Egyptian fractions step by step in the Frink programming language
Table of Contents
Problem Statement
An Egyptian fraction is the sum of distinct unit fractions such as:
Each fraction in the expression has a numerator equal to 1 (unity) and a denominator that is a positive integer, and all the denominators are distinct (i.e., no repetitions).
Fibonacci's Greedy algorithm for Egyptian fractions expands the fraction
x y
{\displaystyle {\tfrac {x}{y}}}
to be represented by repeatedly performing the replacement
(simplifying the 2nd term in this replacement as necessary, and where
⌈ x ⌉
{\displaystyle \lceil x\rceil }
is the ceiling function).
For this task, Proper and improper fractions must be able to be expressed.
Proper fractions are of the form
a b
{\displaystyle {\tfrac {a}{b}}}
where
a
{\displaystyle a}
and
b
{\displaystyle b}
are positive integers, such that
a < b
{\displaystyle a<b}
, and improper fractions are of the form
a b
{\displaystyle {\tfrac {a}{b}}}
where
a
{\displaystyle a}
and
b
{\displaystyle b}
are positive integers, such that a ≥ b.
(See the REXX programming example to view one method of expressing the whole number part of an improper fraction.) For improper fractions, the integer part of any improper fraction should be first isolated and shown preceding the Egyptian unit fractions, and be surrounded by square brackets [n].
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Greedy algorithm for Egyptian fractions step by step in the Frink programming language
Source code in the frink programming language
frac[p, q] :=
{
a = makeArray[[0]]
if p > q
{
a.push[floor[p / q]]
p = p mod q
}
while p > 1
{
d = ceil[q / p]
a.push[1/d]
[p, q] = [-q mod p, d q]
}
if p == 1
a.push[1/q]
a
}
showApproximations[false]
egypt[p, q] := join[" + ", frac[p, q]]
rosetta[] :=
{
lMax = 0
longest = 0
dMax = 0
biggest = 0
for n = 1 to 99
for d = n+1 to 99
{
egypt = frac[n, d]
if length[egypt] > lMax
{
lMax = length[egypt]
longest = n/d
}
d2 = denominator[last[egypt, 1]@0]
if d2 > dMax
{
dMax = d2
biggest = n/d
}
}
println["The fraction with the largest number of terms is $longest"]
println["The fraction with the largest denominator is $biggest"]
}
You may also check:How to resolve the algorithm Text processing/2 step by step in the C# programming language
You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the Ring programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Circles of given radius through two points step by step in the Run BASIC programming language