Engel expansion: Difference between revisions

Content added Content deleted
(Created Nim solution.)
(Added stretch task.)
Line 159: Line 159:


=={{header|Nim}}==
=={{header|Nim}}==
===Task===
We use the module “rationals” from the standard library which is limited to <code>int64</code> 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.
We use the module “rationals” from the standard library which is limited to <code>int64</code> 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.
<syntaxhighlight lang="Nim">import std/[math, rationals, strutils]
<syntaxhighlight lang="Nim">import std/[math, rationals, strutils]
Line 207: Line 208:
Value: 1.414213562373095
Value: 1.414213562373095
Engel expansion: 1 3 5 5 16 18 78 102 120 144 260 968 18531 46065 63005 65105 78125
Engel expansion: 1 3 5 5 16 18 78 102 120 144 260 968 18531 46065 63005 65105 78125
</pre>

===Stretch task===
{{libheader|bignum}}
The package “bignum” provides a “Rat” type but lacks a function to convert the string representing a real number to a <code>Rat</code>.
<syntaxhighlight lang="Nim">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
result.add a
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()
</syntaxhighlight>

{{out}}
<pre>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
</pre>
</pre>