Jump to content

Hickerson series of almost integers: Difference between revisions

→‎{{header|Wren}}: Added a second version using BigRat.
m (→‎version 3: added whitespace.)
(→‎{{header|Wren}}: Added a second version using BigRat.)
Line 2,323:
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.
<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)
}</lang>
 
=={{header|zkl}}==
9,485

edits

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