Jump to content

Hickerson series of almost integers: Difference between revisions

→‎{{header|Wren}}: Replaced existing outdated solutions with one which uses BigDec.
No edit summary
(→‎{{header|Wren}}: Replaced existing outdated solutions with one which uses BigDec.)
Line 2,470:
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
{{libheader|Wren-big}}
<syntaxhighlight lang="ecmascriptwren">import "./mathfmt" for IntFmt
This is a tricky task for Wren which doesn't have arbitrary precision float or decimal but does have (via the above module) BigInt.
import "./big" for BigInt, BigDec
 
I've therefore used the most accurate value I could find for log2 (63 digit accuracy), represented this as a BigInt, and worked from there.
<syntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
import "/big" for BigInt
 
var hickerson = Fn.new { |n|
var fact = BigIntBigDec.newfromBigInt(IntBigInt.factorial(n), 64) // accurate up to n == 18
var ln2 = BigIntBigDec.new("693147180559945309417232121458176568075500134360255254120680009")ln2 // 63precise to 64 decimal digits
varreturn multfact =/ BigInt.new("1e64")BigDec.two * ln2.pow(n+1) // 64 == ln2 digit count + 1)
return fact * mult /(BigInt.two * ln2.pow(n+1))
}
 
System.print("Values of h(n), truncated to 1 dp, and whether 'almost integers' or not:")
for (i in 1..17) {
// truncate to 1 d.p and show final zero if any
var h = hickerson.call(i).toString
var hlh = hhickerson.countcall(i).toString(1, false, true)
var k = hl - ih.count - 1
var ai = (h[k] == "0" || h[k] == "9")
var s = h[0Fmt...k]print("$2d: +$20s $s".", +i, h[k], ai)
Fmt.print("$2d: $20s $s", i, s, ai)
}</syntaxhighlight>
 
Line 2,518 ⟶ 2,511:
17: 130370767029135900.4 false
</pre>
 
Since the above solution was posted, support for arbitrary precision rational numbers has been added to the Wren-big module. The BigRat class has methods to convert to and from decimal number representation. However, it doesn't support transcendental functions and so again I've used the most accurate value I could find for log2 and represented it as a BigRat.
 
The following produces the same results as before though is slower than the BigInt version due to the implicit conversions needed.
<syntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
import "/big" for BigRat
 
var hickerson = Fn.new { |n|
var fact = BigRat.new(Int.factorial(n))
var ln2 = BigRat.fromDecimal("0.693147180559945309417232121458176568075500134360255254120680009")
return fact / (BigRat.two * ln2.pow(n+1))
}
 
System.print("Values of h(n), truncated to 1 dp, and whether 'almost integers' or not:")
for (i in 1..17) {
var h = hickerson.call(i).toDecimal(1, false)
var hl = h[-1]
var ai = (hl == "0" || hl == "9")
Fmt.print("$2d: $20s $s", i, h, ai)
}</syntaxhighlight>
 
=={{header|zkl}}==
9,485

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.