How to resolve the algorithm Greedy algorithm for Egyptian fractions step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Greedy algorithm for Egyptian fractions step by step in the Raku 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 Raku programming language

Source code in the raku programming language

role Egyptian {
    method gist {
	join ' + ',
	    ("[{self.floor}]" if self.abs >= 1),
	    map {"1/$_"}, self.denominators;
    }
    method denominators {
	my ($x, $y) = self.nude;
	$x %= $y;
	my @denom = gather ($x, $y) = -$y % $x, $y * take ($y / $x).ceiling
	    while $x;
    }
}

say .nude.join('/'), " = ", $_ but Egyptian for 43/48, 5/121, 2014/59;

my @sample = map { $_ => .denominators },
    grep * < 1, 
        map {$_ but Egyptian}, 
            (2 .. 99 X/ 2 .. 99);

say .key.nude.join("/"),
    " has max denominator, namely ",
    .value.max
        given max :by(*.value.max), @sample;

say .key.nude.join("/"),
    " has max number of denominators, namely ",
    .value.elems
        given max :by(*.value.elems), @sample;


role Egyptian {
    method gist { join ' + ', map {"1/$_"}, self.list }
    method list {
	my $sum = 0;
	gather for 2 .. * {
	    last if $sum == self;
	    $sum += 1 / .take unless $sum + 1 / $_ > self;
	}
    }
}
 
say 5/4 but Egyptian;


  

You may also check:How to resolve the algorithm CRC-32 step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Approximate equality step by step in the R programming language
You may also check:How to resolve the algorithm Exceptions step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Recaman's sequence step by step in the C++ programming language