# Engel expansion

The Engel expansion of a positive real number x is the unique non-decreasing sequence of positive integers { a1, a2, a3 ... } such that

Engel expansion is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
```    x = 1 / a1 + 1 / a1a2  + 1 / a1a2a3 ...
```

In other words, each term is the reciprocal of the cumulative product of the expansion and x is the sum of those terms.

Rational numbers have a finite Engel expansion, while irrational numbers have an infinite Engel expansion.

Tiny amounts of imprecision can cause wild variation from actual values as the (reciprocal) terms grow smaller. It can be quite challenging to maintain precision in later terms.

• Write routines (functions, procedures, whatever it may be called in your language) to convert a rational number to an Engel expansion representation and from an Engle expansion to a rational number.
• Demonstrate converting some rational numbers to an Engel expansion and back.

Test it with at least the following rational approximations of:

• 𝜋 - 3.14159265358979
• 𝑒 - 2.71828182845904
• √2 - 1.414213562373095

Stretch
• If your language supports high precision rational numbers, do the same with at least:

• 𝜋 - 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211
• 𝑒 - 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743
• √2 - 1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927558

There almost definitely will be some imprecision in the later terms. Feel free to limit the display of the expansion to the first 30 terms.

## J

```to_engle=: {{>.@% ({.~ i.&0)<:@(* >.@%)^:(i.30) y}}
from_engle=: {{+/%*/\y}}
```

```   to_engle 3.14159265358979
1 1 1 8 8 17 19 300 1991 2767 8641 16313 1628438 7702318 25297938 431350188 765676622 776491263 1739733589 2329473788 6871947674 17179869184
from_engle to_engle 3.14159265358979
3.14159
3.14159265358979-from_engle to_engle 3.14159265358979
0

to_engle 2.71828182845904
1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 60 89 126 565 686 1293 7419 13529 59245 65443 133166 225384 655321 656924
from_engle to_engle 2.71828182845904
2.71828
2.71828182845904-from_engle to_engle 2.71828182845904
0

to_engle 1.414213562373095
1 3 5 5 16 18 78 102 120 144 277 286 740 38370 118617 120453 169594 5696244 6316129 10129640 67108864
from_engle to_engle 1.414213562373095
1.41421
1.414213562373095-from_engle to_engle 1.414213562373095
0
```

(by default, J displays the first six digits of floating point numbers)

Stretch goal (note that we seem to have a problem here with e, presumably because of the limited length of the series):

```   pi175=: (%10x^175)*<.@o.10x^175
e101=: +/ %@!@i. 101x
sq2_179=: (10x^179)%~<.@%:2*10x^2*179

177j175":pi175
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211
103j101":e101
2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743
180j178":sq2_179
1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927558

to_engle pi175
1 1 1 8 8 17 19 300 1991 2492 7236 10586 34588 63403 70637 1236467 5417668 5515697 5633167 7458122 9637848 9805775 41840855 58408380 213130873 424342175 2366457522 4109464489 21846713216 27803071890 31804388758
to_engle e101
1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
to_engle sq2_179
1 3 5 5 16 18 78 102 120 144 251 363 1402 31169 88630 184655 259252 298770 4196070 38538874 616984563 1975413035 5345718057 27843871197 54516286513 334398528974 445879679626 495957494386 2450869042061 2629541150828 3557465729164

0.0+pi175-from_engle to_engle pi175
8.21074e_177
0.0+e101-from_engle to_engle e101
1.25532e_34
0.0+sq2_179-from_engle to_engle sq2_179
9.66281e_196
```

## Julia

```tobigrational(s) = (d = length(s) - something(findfirst(==('.'), s), 0); parse(BigInt, replace(s, '.' => "")) // big"10"^d)

toEngel(x) = (a = BigInt[]; while x != 0; y = ceil(big"1" // x); push!(a, y); x = x * y - 1; end; a)

fromEngel(a) = sum(accumulate((x, y) -> x // y, BigInt.(a)))

function testEngels(s)
biginput = length(s) > 21
r = tobigrational(s)
println("\nNumber:           \$s")
eng = toEngel(r)
println("Engel expansion:  ", biginput ? eng[1:min(length(s), 30)] : Int64.(eng), " (\$(length(eng)) components)")
r2 = fromEngel(eng)
println("Back to rational: ", biginput ? BigFloat(r2) : Float64(r2))
end

setprecision(700)

foreach(testEngels, [
"3.14159265358979",
"2.71828182845904",
"1.414213562373095",
"7.59375",
"3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211",
"2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743",
"1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927558",
"25.628906",
])
```
Output:
```Number:           3.14159265358979
Engel expansion:  [1, 1, 1, 8, 8, 17, 19, 300, 1991, 2768, 4442, 4830, 10560, 37132, 107315, 244141, 651042, 1953125] (18 components)
Back to rational: 3.14159265358979

Number:           2.71828182845904
Engel expansion:  [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 82, 144, 321, 2289, 9041, 21083, 474060, 887785, 976563, 1953125] (27 components)
Back to rational: 2.71828182845904

Number:           1.414213562373095
Engel expansion:  [1, 3, 5, 5, 16, 18, 78, 102, 120, 144, 260, 968, 18531, 46065, 63005, 65105, 78125] (17 components)
Back to rational: 1.414213562373095

Number:           7.59375
Engel expansion:  [1, 1, 1, 1, 1, 1, 1, 2, 6, 8] (10 components)
Back to rational: 7.59375

Number:           3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211
Engel expansion:  BigInt[1, 1, 1, 8, 8, 17, 19, 300, 1991, 2492, 7236, 10586, 34588, 63403, 70637, 1236467, 5417668, 5515697, 5633167, 7458122, 9637848, 9805775, 41840855, 58408380, 213130873, 424342175, 2366457522, 4109464489, 21846713216, 27803071890] (231 components)
Back to rational: 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211000000000000000000000000000000000001

Number:           2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743
Engel expansion:  BigInt[1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29] (150 components)
Back to rational: 2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003

Number:           1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927558
Engel expansion:  BigInt[1, 3, 5, 5, 16, 18, 78, 102, 120, 144, 251, 363, 1402, 31169, 88630, 184655, 259252, 298770, 4196070, 38538874, 616984563, 1975413035, 5345718057, 27843871197, 54516286513, 334398528974, 445879679626, 495957494386, 2450869042061, 2629541150527] (185 components)
Back to rational: 1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927558000000000000000000000000000000001

Number:           25.628906
Engel expansion:  [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 4, 33, 33, 35, 58, 62, 521, 3125] (34 components)
Back to rational: 25.628906
```

## Maxima

```engel_encode(x) := block (
[a:[]],
while(x > 0) do (
ai: ceiling(1/x),
x: x*ai - 1,
a: append(a, [ai])
),
return(a)
);
engel_decode(a) := block (
[x:0, my_product:1],
for ai in a do (
my_product: my_product*ai,
x: x + 1/(my_product)
),
return(x)
);
```
Output:
```engel_encode(3.14159265358979);
[1,1,1,8,8,17,19,300,1991,2767,8641,16313,1628438,7702318,25297938,431350188,765676622,776491263,1739733589,2329473788,6871947674,17179869184]
engel_decode(%);
7074237752028433/2251799813685248

engel_encode(2.71828182845904);
[1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,17,60,89,126,565,686,1293,7419,13529,59245,65443,133166,225384,655321,656924,2365071,2618883,5212339,107374183,178956971,536870912]
engel_decode(%);
3060513257434031/1125899906842624

engel_encode(1.414213562373095);
[1,3,5,5,16,18,78,102,120,144,277,286,740,38370,118617,120453,169594,5696244,6316129,10129640,67108864]
engel_decode(%);
1592262918131443/1125899906842624
```

## Nim

We use the module “rationals” from the standard library which is limited to `int64` numerators and denominators. We had to define a conversion function from string to Rational as using the provided conversion function from float to Rational gave inaccurate results.

```import std/[math, rationals, strutils]

type Fract = Rational[int64]

func engel(x: Fract): seq[Natural] =
## Return the Engel expansion of rational "x".
var u = x
while u.num != 0:
let a = ceil(u.den.float / u.num.float).toInt
u = u * a - 1

func toRational(s: string): Fract =
## Convert the string representation of a real to a rational
## without using an intermediate float representation.
var num = 0i64
var den = 1i64
var i = 0
var c = s[0]
while c != '.':
num = 10 * num + ord(c) - ord('0')
inc i
c = s[i]
inc i
while i < s.len:
num = 10 * num + ord(s[i]) - ord('0')
den *= 10
inc i
result = num // den

for val in ["3.14159265358979", "2.71828182845904", "1.414213562373095"]:
let e = engel(val.toRational)
echo "Value: ", val
echo "Engel expansion: ", e.join(" ")
echo()
```
Output:
```Value: 3.14159265358979
Engel expansion: 1 1 1 8 8 17 19 300 1991 2768 4442 4830 10560 37132 107315 244141 651042 1953125

Value: 2.71828182845904
Engel expansion: 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 82 144 321 2289 9041 21083 474060 887785 976563 1953125

Value: 1.414213562373095
Engel expansion: 1 3 5 5 16 18 78 102 120 144 260 968 18531 46065 63005 65105 78125
```

Library: bignum

The package “bignum” provides a “Rat” type but lacks a function to convert the string representing a real number to a `Rat`.

```import std/strutils
import bignum

func engel(x: Rat): seq[Int] =
## Return the Engel expansion of rational "x".
var u = x
while u.num != 0:
let a = (u.denom + u.num - 1) div u.num
u = u * a - 1

func toRat(s: string): Rat =
## Convert the string representation of a real to a rational.
var num = newInt(0)
var den = newInt(1)
var i = 0
var c = s[0]
while c != '.':
num = 10 * num + ord(c) - ord('0')
inc i
c = s[i]
inc i
while i < s.len:
num = 10 * num + ord(s[i]) - ord('0')
den *= 10
inc i
result = newRat(num, den)

for val in ["3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211",
"2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743",
"1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927558"]:
let e = engel(val.toRat)
echo "Value: ", val
echo "Engel expansion: ", e[0..29].join(" ")
echo "Number of terms: ", e.len
echo()
```
Output:
```Value: 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211
Engel expansion: 1 1 1 8 8 17 19 300 1991 2492 7236 10586 34588 63403 70637 1236467 5417668 5515697 5633167 7458122 9637848 9805775 41840855 58408380 213130873 424342175 2366457522 4109464489 21846713216 27803071890
Number of terms: 231

Value: 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743
Engel expansion: 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Number of terms: 150

Value: 1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927558
Engel expansion: 1 3 5 5 16 18 78 102 120 144 251 363 1402 31169 88630 184655 259252 298770 4196070 38538874 616984563 1975413035 5345718057 27843871197 54516286513 334398528974 445879679626 495957494386 2450869042061 2629541150527
Number of terms: 185
```

## Perl

Translation of: Raku
```use v5.36;
use bigrat;
use experimental <builtin for_list>;
use List::Util <min product>;

sub ceiling (\$n) { \$n == int \$n ? \$n : int \$n + 1 }
sub abbr (\$d) { my \$l = length \$d; \$l < 61 ? \$d : substr(\$d,0,30) . '..' . substr(\$d,-30) . " (\$l digits)" }

sub to_engel (\$rat) {
my @E;
while (\$rat) {
push @E, ceiling 1/\$rat;
\$rat = \$rat*\$E[-1] - 1;
}
@E
}

sub from_engel (@expanded) {
my @a;
sum( map { push @a, \$_; 1/product(@a) } @expanded )
}

for my(\$rat,\$p) (
#  low precision 𝜋, 𝑒, √2 and 1.5 to powers
3.14159265358979, 15,
2.71828182845904, 15,
1.414213562373095, 16,
1.5**5, 6,
1.5**8, 10,

# high precision 𝜋, 𝑒, and √2
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211, 176,
2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743, 102,
1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927558, 179,
) {
say "Rational number: " . abbr \$rat->as_float(\$p);
my \$terms = join ' ', my @expanded = to_engel \$rat;
say "Engel expansion: " . (length(\$terms) > 100 ? \$terms =~ s/^(.{90}\S*).*\$/\$1/r . '... (' . +@expanded . ' terms)' : \$terms);
say " Converted back: " . abbr from_engel(@expanded)->as_float(\$p);
say '';
}
```
Output:
```Rational number: 3.14159265358979
Engel expansion: 1 1 1 8 8 17 19 300 1991 2768 4442 4830 10560 37132 107315 244141 651042 1953125
Converted back: 3.14159265358979

Rational number: 2.71828182845904
Engel expansion: 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 82 144 321 2289 9041 21083 474060 887785 976563 1953125
Converted back: 2.71828182845904

Rational number: 1.414213562373095
Engel expansion: 1 3 5 5 16 18 78 102 120 144 260 968 18531 46065 63005 65105 78125
Converted back: 1.414213562373095

Rational number: 7.59375
Engel expansion: 1 1 1 1 1 1 1 2 6 8
Converted back: 7.59375

Rational number: 25.62890625
Engel expansion: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 4 32
Converted back: 25.62890625

Rational number: 3.1415926535897932384626433832..081284811174502841027019385211 (177 digits)
Engel expansion: 1 1 1 8 8 17 19 300 1991 2492 7236 10586 34588 63403 70637 1236467 5417668 5515697 5633167... (231 terms)
Converted back: 3.1415926535897932384626433832..081284811174502841027019385211 (177 digits)

Rational number: 2.7182818284590452353602874713..035354759457138217852516642743 (103 digits)
Engel expansion: 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33... (150 terms)
Converted back: 2.7182818284590452353602874713..035354759457138217852516642743 (103 digits)

Rational number: 1.4142135623730950488016887242..999358314132226659275055927558 (180 digits)
Engel expansion: 1 3 5 5 16 18 78 102 120 144 251 363 1402 31169 88630 184655 259252 298770 4196070 38538874... (185 terms)
Converted back: 1.4142135623730950488016887242..999358314132226659275055927558 (180 digits)```

## Phix

```with javascript_semantics
include mpfr.e
mpfr_set_default_precision(-134)    -- see notes

function toEngel(string x)
sequence engel = {}
mpfr u = mpfr_init(x),
a = mpfr_init()
while mpfr_cmp_si(u,0)!=0 do
mpfr_si_div(a,1,u)
mpfr_ceil(a,a)
engel &= mpfr_get_si(a)
mpfr_mul(u,u,a)
mpfr_sub_si(u,u,1)
end while
return engel
end function

function fromEngel(sequence engel)
mpfr res = mpfr_init(0),
prod = mpfr_init(1),
r = mpfr_init()
for e in engel do
mpfr_set_d(r,e)
mpfr_si_div(r,1,r)
mpfr_mul(prod,prod,r)
end for
return res
end function

constant rats = {
"3.14159265358979", "2.71828182845904", "1.414213562373095", "7.59375",
"3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384",
"2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743",
"1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387",
"25.628906"
}
for rat in rats do
printf(1,"Rational number : %s\n", rat)
sequence engel = toEngel(rat)
integer dix = find('.',rat),
places = length(rat)-dix,
l = length(engel),
cp = 0
string s = mpfr_get_fixed(fromEngel(engel), places)
for i=1 to places do
dix += 1
if rat[dix]!=s[dix] then exit end if
cp = i
end for
printf(1,"Engel expansion : %s\n", join(engel[1..min(l,30)]," ",fmt:="%d"))
printf(1,"Number of terms : %d, places : %d (%d correct)\n", {l,places,cp})
printf(1,"Back to rational: %s\n\n", s)
end for
```
Output:

I could only get pi accurate to 125 decimal places and root2 to 87, so cut the input strings accordingly.
Increasing the precision helps but only up to a (relatively small) point, ie that 134 is needed, nowt greater helps at all.
You may or may not have better luck with completely rewriting this to use mpq (rationals).
In fact it works slightly better in a browser (which uses rationals behind the scenes) than on desktop/Phix, as shown below.

```Rational number : 3.14159265358979
Engel expansion : 1 1 1 8 8 17 19 300 1991 2768 4442 4830 10560 37132 107315 244141 651042 1953125 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647
Number of terms : 83, places : 14 (14 correct)
Back to rational: 3.14159265358979

Rational number : 2.71828182845904
Engel expansion : 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 82 144 321 2289 9041 21083 474060 887785 976563 1953125 2147483647 2147483647 2147483647
Number of terms : 101, places : 14 (14 correct)
Back to rational: 2.71828182845904

Rational number : 1.414213562373095
Engel expansion : 1 3 5 5 16 18 78 102 120 144 260 968 18531 46065 63005 65105 78125 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647
Number of terms : 67, places : 15 (15 correct)
Back to rational: 1.414213562373095

Rational number : 7.59375
Engel expansion : 1 1 1 1 1 1 1 2 6 8
Number of terms : 10, places : 5 (5 correct)
Back to rational: 7.59375

Rational number : 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384
Engel expansion : 1 1 1 8 8 17 19 300 1991 2492 7236 10586 34588 63403 70637 1236467 5417668 5515697 5633167 7458122 9637848 9805775 41840855 58408380 213130873 424342175 2147483647 2147483647 2147483647 2147483647
Number of terms : 181, places : 125 (125 correct)
Back to rational: 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384

Rational number : 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743
Engel expansion : 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Number of terms : 222, places : 101 (101 correct)
Back to rational: 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743

Rational number : 1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387
Engel expansion : 1 3 5 5 16 18 78 102 120 144 251 363 1402 31169 88630 184655 259252 298770 4196070 38538874 616984563 1975413038 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647
Number of terms : 175, places : 87 (87 correct)
Back to rational: 1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387

Rational number : 25.628906
Engel expansion : 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 4 33 33 35
Number of terms : 65, places : 6 (6 correct)
Back to rational: 25.628906
```

Output under p2js:

```Rational number : 3.14159265358979
Engel expansion : 1 1 1 8 8 17 19 300 1991 2768 4442 4830 10560 37132 107315 244141 651042 1953125
Number of terms : 18, places : 14 (14 correct)
Back to rational: 3.14159265358979

Rational number : 2.71828182845904
Engel expansion : 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 82 144 321 2289 9041 21083 474060 887785 976563 1953125
Number of terms : 27, places : 14 (14 correct)
Back to rational: 2.71828182845904

Rational number : 1.414213562373095
Engel expansion : 1 3 5 5 16 18 78 102 120 144 260 968 18531 46065 63005 65105 78125
Number of terms : 17, places : 15 (15 correct)
Back to rational: 1.414213562373095

Rational number : 7.59375
Engel expansion : 1 1 1 1 1 1 1 2 6 8
Number of terms : 10, places : 5 (5 correct)
Back to rational: 7.59375

Rational number : 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384
Engel expansion : 1 1 1 8 8 17 19 300 1991 2492 7236 10586 34588 63403 70637 1236467 5417668 5515697 5633167 7458122 9637848 9805775 41840855 58408380 213130873 424342175 2717375531 323878055376 339280401894 386771504748
Number of terms : 161, places : 125 (125 correct)
Back to rational: 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384

Rational number : 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743
Engel expansion : 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Number of terms : 150, places : 101 (101 correct)
Back to rational: 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743

Rational number : 1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387
Engel expansion : 1 3 5 5 16 18 78 102 120 144 251 363 1402 31169 88630 184655 259252 298770 4196070 38538874 616984563 1975413038 7855284583 34680535992 47012263568 82957997141 1709576125547 42630379527673 164312229775505 404736776022426
Number of terms : 110, places : 87 (87 correct)
Back to rational: 1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387

Rational number : 25.628906
Engel expansion : 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 4 33 33 35
Number of terms : 34, places : 6 (6 correct)
Back to rational: 25.628906
```

## Quackery

Quackery uses bignum rationals and only generates approximations when the programmer deems it necessary, so loss of precision is not an issue.

```  [ \$ "bigrat.qky" loadfile ] now!

[ /mod 0 != + ]      is ceiling  ( n/d --> n   )

[ [] unrot
[ 2dup 1/v ceiling
dip rot
dup dip
[ join unrot ]
1 v* 1 1 v-
2dup v0= until ]
2drop ]           is v->engel ( n/d --> [   )

[ 0 1 rot
1 1 rot
witheach
[ n->v v/
2swap 2over v+
2swap ]
2drop ]            is engel->v (  [ --> n/d )

\$ "3.14159265358979 2.71828182845904 1.414213562373095"
nest\$
witheach
[ \$->v drop
2dup 200 point\$ echo\$ cr
v->engel
dup witheach [ echo i if sp ] cr
engel->v
200 point\$ echo\$ cr
cr ]

\$ "3.1415926535897932384626433832795028841971693993751058"
\$ "209749445923078164062862089986280348253421170679821480" join
\$ "865132823066470938446095505822317253594081284811174502" join
\$ "841027019385211"                                        join
nested
\$ "2.7182818284590452353602874713526624977572470936999595"
\$ "7496696762772407663035354759457138217852516642743"      join
nested join
\$ "1.4142135623730950488016887242096980785696718753769480"
\$ "731766797379907324784621070388503875343276415727350138" join
\$ "462309122970249248360558507372126441214970999358314132" join
\$ "226659275055927558"                                     join
nested join
witheach
[ \$->v drop
2dup 200 point\$ echo\$ cr cr
v->engel
dup 30 split drop
witheach [ echo i if sp ]
say "... " cr cr
engel->v
200 point\$ echo\$ cr cr
cr ]```
Output:
```3.14159265358979
1 1 1 8 8 17 19 300 1991 2768 4442 4830 10560 37132 107315 244141 651042 1953125
3.14159265358979

2.71828182845904
1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 82 144 321 2289 9041 21083 474060 887785 976563 1953125
2.71828182845904

1.414213562373095
1 3 5 5 16 18 78 102 120 144 260 968 18531 46065 63005 65105 78125
1.414213562373095

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211

1 1 1 8 8 17 19 300 1991 2492 7236 10586 34588 63403 70637 1236467 5417668 5515697 5633167 7458122 9637848 9805775 41840855 58408380 213130873 424342175 2366457522 4109464489 21846713216 27803071890...

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211

2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743

1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29...

2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743

1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927558

1 3 5 5 16 18 78 102 120 144 251 363 1402 31169 88630 184655 259252 298770 4196070 38538874 616984563 1975413035 5345718057 27843871197 54516286513 334398528974 445879679626 495957494386 2450869042061 2629541150527...

1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927558```

## Raku

```sub to-engel (\$rat is copy) { do while \$rat { my \$a = ceiling 1 / \$rat; \$rat = \$rat × \$a - 1; \$a } }

sub from-engel (@expanded) { sum [\×] @expanded.map: { FatRat.new: 1, \$_ } }

for #  low precision 𝜋, 𝑒, √2 and 1.5 to a power
3.14159265358979, 2.71828182845904, 1.414213562373095, 1.5 ** 5,

# high precision 𝜋, 𝑒, and √2 and 1.5 to a power
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211.FatRat,

2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743.FatRat,

1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927558.FatRat,

1.5 ** 8
-> \$rat {
say "Rational number: \$rat";
my @expanded = \$rat.&to-engel;
put "Engel expansion: " ~ @expanded.head(30);
say " Converted back: " ~ @expanded.&from-engel;
put '';
}
```
Output:
```Rational number: 3.14159265358979
Engel expansion: 1 1 1 8 8 17 19 300 1991 2768 4442 4830 10560 37132 107315 244141 651042 1953125
Converted back: 3.14159265358979

Rational number: 2.71828182845904
Engel expansion: 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 82 144 321 2289 9041 21083 474060 887785 976563 1953125
Converted back: 2.71828182845904

Rational number: 1.414213562373095
Engel expansion: 1 3 5 5 16 18 78 102 120 144 260 968 18531 46065 63005 65105 78125
Converted back: 1.414213562373095

Rational number: 7.59375
Engel expansion: 1 1 1 1 1 1 1 2 6 8
Converted back: 7.59375

Rational number: 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211
Engel expansion: 1 1 1 8 8 17 19 300 1991 2492 7236 10586 34588 63403 70637 1236467 5417668 5515697 5633167 7458122 9637848 9805775 41840855 58408380 213130873 424342175 2366457522 4109464489 21846713216 27803071890
Converted back: 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211

Rational number: 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743
Engel expansion: 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Converted back: 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743

Rational number: 1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927558
Engel expansion: 1 3 5 5 16 18 78 102 120 144 251 363 1402 31169 88630 184655 259252 298770 4196070 38538874 616984563 1975413035 5345718057 27843871197 54516286513 334398528974 445879679626 495957494386 2450869042061 2629541150527
Converted back: 1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927558

Rational number: 25.628906
Engel expansion: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 4 32
Converted back: 25.628906```

## Wren

Library: Wren-big
Library: Wren-fmt

As in the case of the Raku example, I've limited the display of the Engel expansion to a maximum of 30 terms though I've also shown the total number of terms.

However, I've also limited the number of terms accumulated by the 'fromEngel' function to 70 which is just enough to reproduce the high precision rationals in decimal notation. To accumulate all the terms in a reasonable time would require the use of Wren-gmp which I've tried to avoid so the solution will run under Wren-CLI.

```import "./big" for BigRat
import "./fmt" for Fmt

var toEngel = Fn.new { |x|
var engel = []
var u = BigRat.fromDecimal(x)
while (true) {
var a = u.inverse.ceil
u = u * a - BigRat.one
if (u == 0) return engel
}
}

var fromEngel = Fn.new { |engel|
var sum = BigRat.zero
var prod = BigRat.one
for (e in engel) {
var r = BigRat.new(e).inverse
prod = prod * r
sum = sum + prod
}
return sum
}

var rats = [
"3.14159265358979", "2.71828182845904", "1.414213562373095", "7.59375",
"3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211",
"2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743",
"1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927558",
"25.628906"
]
for (rat in rats) {
Fmt.print("Rational number : \$s", rat)
var dix = rat.indexOf(".") + 1
var places = rat.count - dix
var engel = toEngel.call(rat)
Fmt.print("Engel expansion : \$i", engel.take(30).toList)
Fmt.print("Number of terms : \$d", engel.count)
Fmt.print("Back to rational: \$s\n", fromEngel.call(engel.take(70).toList).toDecimal(places))
}
```
Output:
```Rational number : 3.14159265358979
Engel expansion : 1 1 1 8 8 17 19 300 1991 2768 4442 4830 10560 37132 107315 244141 651042 1953125
Number of terms : 18
Back to rational: 3.14159265358979

Rational number : 2.71828182845904
Engel expansion : 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 82 144 321 2289 9041 21083 474060 887785 976563 1953125
Number of terms : 27
Back to rational: 2.71828182845904

Rational number : 1.414213562373095
Engel expansion : 1 3 5 5 16 18 78 102 120 144 260 968 18531 46065 63005 65105 78125
Number of terms : 17
Back to rational: 1.414213562373095

Rational number : 7.59375
Engel expansion : 1 1 1 1 1 1 1 2 6 8
Number of terms : 10
Back to rational: 7.59375

Rational number : 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211
Engel expansion : 1 1 1 8 8 17 19 300 1991 2492 7236 10586 34588 63403 70637 1236467 5417668 5515697 5633167 7458122 9637848 9805775 41840855 58408380 213130873 424342175 2366457522 4109464489 21846713216 27803071890
Number of terms : 231
Back to rational: 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211

Rational number : 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743
Engel expansion : 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Number of terms : 150
Back to rational: 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743

Rational number : 1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927558
Engel expansion : 1 3 5 5 16 18 78 102 120 144 251 363 1402 31169 88630 184655 259252 298770 4196070 38538874 616984563 1975413035 5345718057 27843871197 54516286513 334398528974 445879679626 495957494386 2450869042061 2629541150527
Number of terms : 185
Back to rational: 1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927558

Rational number : 25.628906
Engel expansion : 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 4 33 33 35
Number of terms : 34
Back to rational: 25.628906
```