Engel expansion: Difference between revisions

Added Wren
m (→‎{{header|J}}: J's interactive prompt is 3 spaces)
(Added Wren)
Line 128:
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</pre>
 
=={{header|Wren}}==
{{libheader|Wren-big}}
{{libheader|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.
<lang ecmascript>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
engel.add(a.toBigInt)
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))
}</lang>
 
{{out}}
<pre>
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
</pre>
9,476

edits