Engel expansion: Difference between revisions

julia example
m (→‎{{header|Phix}}: removed the 70 limit now improves the p2js output)
(julia example)
Line 93:
0.0+sq2_179-from_engle to_engle sq2_179
9.66281e_196</lang>
 
=={{header|Julia}}==
<lang ruby>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))
r2 = fromEngel(eng)
println("Back to rational: ", biginput ? BigFloat(s) : Float64(r2))
end
 
setprecision(725)
 
foreach(testEngels, [
"3.14159265358979",
"2.71828182845904",
"1.414213562373095",
"7.59375",
"3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384",
"2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642743",
"1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387",
"25.628906",
])
</lang>{{out}}
<pre>
Number: 3.14159265358979
Engel expansion: [1, 1, 1, 8, 8, 17, 19, 300, 1991, 2768, 4442, 4830, 10560, 37132, 107315, 244141, 651042, 1953125]
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]
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]
Back to rational: 1.414213562373095
 
Number: 7.59375
Engel expansion: [1, 1, 1, 1, 1, 1, 1, 2, 6, 8]
Back to rational: 7.59375
 
Number: 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384Engel 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, 2717375531, 323878055376, 339280401894, 386771504748]
Back to rational: 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093840000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009
 
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]
Back to rational: 2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427430000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002
 
Number: 1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387
Engel expansion: BigInt[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]
Back to rational: 1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004
 
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]
Back to rational: 25.628906
</pre>
 
 
=={{header|Phix}}==
4,102

edits