Hickerson series of almost integers: Difference between revisions
Hickerson series of almost integers (view source)
Revision as of 08:21, 10 February 2024
, 3 months agoAdd C# implementation
m (replaced backtick constants with proper literals (e.g. `(/ 1.0 10) to 0.1)) |
(Add C# implementation) |
||
(15 intermediate revisions by 9 users not shown) | |||
Line 21:
=={{header|Ada}}==
<
with Ada.Numerics.Generic_Elementary_Functions;
Line 82:
end;
end loop;
end Almost_Integers;</
{{out}}
Line 107:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<
# The Hickerson number n is defined by: h(n) = n! / ( 2 * ( (ln 2)^(n+1) ) ) #
# so: h(1) = 1 / ( 2 * ( ( ln 2 ) ^ 2 ) #
Line 143:
)
)
OD</
{{out}}
<pre>
Line 167:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -M -f HICKERSON_SERIES_OF_ALMOST_INTEGERS.AWK
# using GNU Awk 4.1.0, API: 1.0 (GNU MPFR 3.1.2, GNU MP 5.1.2)
Line 186:
return(out)
}
</syntaxhighlight>
<p>Output:</p>
<pre>
Line 207:
17 130370767029135900.45799 almost integer: false
</pre>
=={{header|BBC BASIC}}==
We use the native 80 bit floats precision.
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> Fac=1
FOR I%=1 TO 17
Fac*=I%
@%=&1420300
Hick$=RIGHT$(STRING$(17, " ") + STR$(Fac / (2 * LN2 ^ (I% + 1))), 22)
@%=2
PRINT "H(" I% ") = " Hick$ " which is ";
IF MID$(Hick$, 20, 1) <> "0" IF MID$(Hick$, 20, 1) <> "9" PRINT "NOT ";
PRINT "an almost integer."
NEXT</syntaxhighlight>
{{out}}
<pre>H( 1) = 1.041 which is an almost integer.
H( 2) = 3.003 which is an almost integer.
H( 3) = 12.996 which is an almost integer.
H( 4) = 74.999 which is an almost integer.
H( 5) = 541.002 which is an almost integer.
H( 6) = 4683.001 which is an almost integer.
H( 7) = 47292.999 which is an almost integer.
H( 8) = 545834.998 which is an almost integer.
H( 9) = 7087261.002 which is an almost integer.
H(10) = 102247563.005 which is an almost integer.
H(11) = 1622632572.998 which is an almost integer.
H(12) = 28091567594.982 which is an almost integer.
H(13) = 526858348381.001 which is an almost integer.
H(14) = 10641342970443.085 which is an almost integer.
H(15) = 230283190977853.037 which is an almost integer.
H(16) = 5315654681981354.511 which is NOT an almost integer.
H(17) = 130370767029135900.410 which is NOT an almost integer.</pre>
=={{header|Bracmat}}==
Line 218 ⟶ 251:
and a decimal part (using a string operation) before outputting.
<
& 1:?fac
& whl
Line 245 ⟶ 278:
)
)
);</
{{out}}
<pre>h(1) = 1.041 is an almost-integer.
Line 267 ⟶ 300:
=={{header|C}}==
{{libheader|MPFR}}
<
#include <mpfr.h>
Line 295 ⟶ 328:
return 0;
}</
{{out}}
<pre>
Line 315 ⟶ 348:
16: 5315654681981354.5131 N
17: 130370767029135900.4580 N
</pre>
=={{header|C#}}==
{{trans|Go}}
<syntaxhighlight lang="C#">
using System;
using System.Numerics;
public class Program
{
public static void Main(string[] args)
{
decimal ln2 = 0.6931471805599453094172m;
decimal h = 0.5m / ln2;
BigInteger w = new BigInteger();
decimal f = 0;
for (long i = 1; i <= 17; i++)
{
h = h * i / ln2;
w = (BigInteger)h;
f = h - (decimal)w;
double y = (double)f;
string d = y.ToString("0.000");
Console.WriteLine($"n: {i,2} h: {w}{d.Substring(1)} Nearly integer: {d[2] == '0' || d[2] == '9'}");
}
}
}
</syntaxhighlight>
{{out}}
<pre>
n: 1 h: 1.041 Nearly integer: True
n: 2 h: 3.003 Nearly integer: True
n: 3 h: 12.996 Nearly integer: True
n: 4 h: 74.999 Nearly integer: True
n: 5 h: 541.002 Nearly integer: True
n: 6 h: 4683.001 Nearly integer: True
n: 7 h: 47292.999 Nearly integer: True
n: 8 h: 545834.998 Nearly integer: True
n: 9 h: 7087261.002 Nearly integer: True
n: 10 h: 102247563.005 Nearly integer: True
n: 11 h: 1622632572.998 Nearly integer: True
n: 12 h: 28091567594.982 Nearly integer: True
n: 13 h: 526858348381.001 Nearly integer: True
n: 14 h: 10641342970443.085 Nearly integer: True
n: 15 h: 230283190977853.037 Nearly integer: True
n: 16 h: 5315654681981354.513 Nearly integer: False
n: 17 h: 130370767029135900.458 Nearly integer: False
</pre>
=={{header|C++}}==
{{libheader|Boost|1.53 or later}}
<
#include <iomanip>
#include <boost/multiprecision/cpp_dec_float.hpp>
Line 337 ⟶ 420:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 362 ⟶ 445:
In order to get enough precision, the natural logarithm of 2 had to be entered manually; the BigDecimal implementation does not have a high-precision logarithm function.
<
"Hickerson number, calculated with BigDecimals and manually-entered high-precision value for ln(2)."
[n]
Line 381 ⟶ 464:
(if (almost-integer? h)
"almost integer"
"NOT almost integer")))</
{{out}}
<pre>1 1.04068 almost integer
Line 403 ⟶ 486:
=={{header|COBOL}}==
{{works with|GNU Cobol|2.0}}
<
IDENTIFICATION DIVISION.
PROGRAM-ID. hickerson-series.
Line 431 ⟶ 514:
END-PERFORM
.
END PROGRAM hickerson-series.</
{{out}}
Line 457 ⟶ 540:
=={{header|Crystal}}==
{{trans|Ruby}}
<
LN2 = Math.log(2).to_big_f
Line 478 ⟶ 561:
puts "n:%3i h: %s\t%s" % [n, h, str]
end
</syntaxhighlight>
{{out}}
<pre>
Line 502 ⟶ 585:
=={{header|D}}==
The D <code>real</code> type has enough precision for this task.
<
import std.stdio, std.algorithm, std.mathspecial;
Line 511 ⟶ 594:
tenths.among!(0, 9) ? "" : "NOT ");
}
}</
<pre>H( 1)= 1.04 is nearly integer.
H( 2)= 3.00 is nearly integer.
Line 531 ⟶ 614:
=={{header|Factor}}==
<
math.ranges sequences ;
IN: rosetta-code.hickerson
Line 548 ⟶ 631:
] each ;
MAIN: hickerson-demo</
{{out}}
<pre>
Line 569 ⟶ 652:
h(17) = 130370767029135900.458 almost integer? f
h(18) = 3385534663256845326.390 almost integer? f
</pre>
=={{header|Forth}}==
{{works with|4tH v3.64.0}}
4tH has two different floating point libraries. This version works with both of them.
<syntaxhighlight lang="forth">[UNDEFINED] ANS [IF]
include lib/fp1.4th \ Zen float version
include lib/zenfprox.4th \ for F~
include lib/zenround.4th \ for FROUND
include lib/zenfln.4th \ for FLN
include lib/zenfpow.4th \ for FPOW
[ELSE]
include lib/fp3.4th \ ANS float version
include lib/flnflog.4th \ for FLN
include lib/fpow.4th \ for FPOW
[THEN]
include lib/fast-fac.4th \ for FACTORIAL
fclear \ initialize floating point
float array ln2 2 s>f fln latest f! \ precalculate ln(2)
\ integer exponentiation
: hickerson dup >r factorial s>f ln2 f@ r> 1+ fpow fdup f+ f/ ;
: integer? if ." TRUE " else ." FALSE" then space ;
\ is it an integer?
: first17
18 1 do \ test hickerson 1-17
i hickerson i 2 .r space fdup fdup fround
s" 1e-1" s>float f~ integer? f. cr
loop \ within 0.1 absolute error
;
first17</syntaxhighlight>
{{out}}
<pre>
1 TRUE 1.040684490502803898935311338275660267
2 TRUE 3.002780707156905443502020333103288357
3 TRUE 12.99629050527696646223788568296556096
4 TRUE 74.99873544766160012772833377667325825
5 TRUE 541.0015185164235075700145797037460679
6 TRUE 4683.001247262257437188665462676039563
7 TRUE 47292.99873131462390491745677950570536
8 TRUE 545834.9979074851670685196268094270956
9 TRUE 7087261.001622899120996912279845689616
10 TRUE 102247563.0052710420113696743135681269
11 TRUE 1622632572.997550049857744575227371627
12 TRUE 28091567594.98157244080652083799015398
13 TRUE 526858348381.0012482880251816804368039
14 TRUE 10641342970443.08453196701499663939019
15 TRUE 230283190977853.0374369606024567600908
16 FALSE 5315654681981354.513099343419112934817
17 FALSE 130370767029135900.4585722365605854515
</pre>
=={{header|Fortran}}==
<
implicit none
integer, parameter :: q = selected_real_kind(30)
Line 594 ⟶ 729:
end do
1 format('h(',I2,') = ',F23.3,' is',A,' an almost-integer')
end program</
{{out}}
Line 620 ⟶ 755:
=={{header|FreeBASIC}}==
{{libheader|GMP}}
<
' compile with: fbc -s gui
Line 705 ⟶ 840:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>h(1) = 1.04068 is a almost integer
Line 750 ⟶ 885:
=={{header|Fōrmulæ}}==
'''Solution'''
[[File:Fōrmulæ - Hickerson series of almost integers 01.png]]
[[File:Fōrmulæ - Hickerson series of almost integers 02.png]]
=={{header|Go}}==
<
import (
Line 779 ⟶ 916:
i, &w, d[1:], d[2] == '0' || d[2] == '9')
}
}</
{{out}}
<pre>
Line 802 ⟶ 939:
=={{header|Haskell}}==
<
import qualified Data.Number.CReal as C
Line 822 ⟶ 959:
</syntaxhighlight>
{{out}}
<pre style="font-size:80%">
Line 852 ⟶ 989:
Definitions:
<
h=: ! % 2*(ln2 99)^>:</
Implementation (1 in initial column indicates an almost integer, results displayed to 3 digits after the decimal point):
<
1 1.041
1 3.003
Line 874 ⟶ 1,011:
1 230283190977853.037
0 5315654681981354.513
0 130370767029135900.458</
In other words, multiply the fractional part of h by ten, find the largest integer not greater than this result, and check if it's in the set {0,9}. Then format the result of h along with this set membership result.
=={{header|Java}}==
<
public class Hickerson {
Line 905 ⟶ 1,042:
return c.toString().matches("0|9");
}
}</
<pre> 1 is almost integer: true
Line 928 ⟶ 1,065:
{{works with|jq|1.4}}
jq currently uses IEEE 754 64-bit numbers, and therefore the built-in arithmetic functions lack adequate precision to solve the task completely. In the following, therefore, we include a check for adequate precision.
<
. as $n
| (2|log) as $log2
Line 952 ⟶ 1,089:
end
else "insufficient precision for hickerson(\($i))"
end</
{{out}}
<
hickerson(1) is 1.0406844905028039 -- almost an integer
hickerson(2) is 3.0027807071569055 -- almost an integer
Line 971 ⟶ 1,108:
hickerson(15) is 230283190977853.06 -- almost an integer
insufficient precision for hickerson(16)
insufficient precision for hickerson(17)</
=={{header|Julia}}==
This solution implements its Hickerson Series function as a closure. It explores the effects of datatype precision, implementing a rather conservative "guard number" scheme to identify when the results may have inadequate precision. One can not be confident of the 64-bit floating point results beyond <tt>n=13</tt>, but the 256-bit precision floating point results are easily precise enough for the entire calculation to <tt>n=17</tt>. (A slight variant of this calculation, not shown here, indicates that these extended precision numbers are adequate to <tt>n=50</tt>.)
<syntaxhighlight lang="julia">
function makehickerson{T<:Real}(x::T)
n = 0
Line 1,025 ⟶ 1,162:
a = log(2.0)
reporthickerson(a, 17)
</syntaxhighlight>
{{out}}
Line 1,240 ⟶ 1,377:
=={{header|Kotlin}}==
{{trans|Java}}
<
import java.math.BigDecimal
Line 1,262 ⟶ 1,399:
fun main(args: Array<String>) {
for (n in 1..17) println("${"%2d".format(n)} is almost integer: ${Hickerson.almostInteger(n)}")
}</
{{out}}
Line 1,286 ⟶ 1,423:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
firstdecimal[x_] := Floor[10 x] - 10 Floor[x];
almostIntegerQ[x_] := firstdecimal[x] == 0 || firstdecimal[x] == 9;
Table[{i, AccountingForm[N[h[i], 50], {Infinity, 5}], almostIntegerQ[N[h[i], 50]]}, {i, 1, 17}] // TableForm</
{{out}}
<pre>1 1.04068 True
Line 1,311 ⟶ 1,448:
=={{header|ML}}==
==={{header|mLite}}===
<
fun log (n, k, last = sum, sum) = sum
| (n, k, last, sum) = log (n, k + 1, sum, sum + ( 1 / (k * n ^ k)))
Line 1,327 ⟶ 1,464:
;
foreach show ` iota 17;
</syntaxhighlight>
Output
<pre>(1, 1.04068449050280389893479080186749587, true)
Line 1,347 ⟶ 1,484:
(17, 130370767029135900.457985349196773809, false)
</pre>
=={{header|Nim}}==
Nim floats are limited to 64 bits which is not enough to get correct results for n greater that 14. Unfortunately, the third party "bignum" proposes only big integers and big rationals. But, using an approximation of Ln(2) as a rational, it is possible to compute the terms of the Hickerson series with enough precision, at least until n = 17.
For big values, it is not possible to keep enough precision while converting a rational to a float, so we compute only the fractional part of h(n), which is sufficient to decide whether the number if nearly an integer or not.
<syntaxhighlight lang="nim">import strformat
import bignum
let ln2 = newInt("693147180559945309417232121458") / newInt("1000000000000000000000000000000")
iterator hickerson(): tuple[n: int; val: Rat] =
## Yield the hickerson series values as rational numbers.
var
n = 1
num = 1
denom = 2 * ln2 * ln2
while true:
yield (n, num / denom)
inc n
num *= n
denom *= ln2
func fract(r: Rat): float =
## Return the fractional part of rational "r".
((r.num mod r.denom) / r.denom).toFloat
for i, val in hickerson():
let f = val.fract
let s = if int(10 * f) in {0, 9}: "" else: "not "
echo &"Fractional part of h({i}) is {f:.5f}..., so h({i}) is {s}nearly an integer."
if i == 17: break</syntaxhighlight>
{{out}}
<pre>Fractional part of h(1) is 0.04068..., so h(1) is nearly an integer.
Fractional part of h(2) is 0.00278..., so h(2) is nearly an integer.
Fractional part of h(3) is 0.99629..., so h(3) is nearly an integer.
Fractional part of h(4) is 0.99874..., so h(4) is nearly an integer.
Fractional part of h(5) is 0.00152..., so h(5) is nearly an integer.
Fractional part of h(6) is 0.00125..., so h(6) is nearly an integer.
Fractional part of h(7) is 0.99873..., so h(7) is nearly an integer.
Fractional part of h(8) is 0.99791..., so h(8) is nearly an integer.
Fractional part of h(9) is 0.00162..., so h(9) is nearly an integer.
Fractional part of h(10) is 0.00527..., so h(10) is nearly an integer.
Fractional part of h(11) is 0.99755..., so h(11) is nearly an integer.
Fractional part of h(12) is 0.98157..., so h(12) is nearly an integer.
Fractional part of h(13) is 0.00125..., so h(13) is nearly an integer.
Fractional part of h(14) is 0.08453..., so h(14) is nearly an integer.
Fractional part of h(15) is 0.03744..., so h(15) is nearly an integer.
Fractional part of h(16) is 0.51308..., so h(16) is not nearly an integer.
Fractional part of h(17) is 0.45799..., so h(17) is not nearly an integer.</pre>
=={{header|PARI/GP}}==
<
almost(x)=abs(x-round(x))<.1
select(n->almost(h(n)),[1..17])
</syntaxhighlight>
{{out}}
<pre>%1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]</pre>
Line 1,364 ⟶ 1,552:
calculate the inverse of the natural log of 2, and then compute a running value of h.
<
use warnings;
use Math::BigFloat;
Line 1,378 ⟶ 1,566:
$n, $s, ($s =~ /\.[09]/ ? "" : " NOT");
}
</syntaxhighlight>
{{out}}
<pre>h( 1) = 1.041 is almost an integer.
Line 1,399 ⟶ 1,587:
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #000080;font-style:italic;">-- (no mpfr_log() for pwa/p2js)</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.0"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (mpfr_set_default_prec[ision] renamed)</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #7060A8;">mpfr_set_default_precision</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">23</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (found by trial/error)</span>
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">ln2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">hn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">mpfr_log</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ln2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ln2</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ln2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">17</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">mpfr_mul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ln2</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">n10</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%24.4Rf "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hn</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">n10</span> <span style="color: #0000FF;">&=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n10</span><span style="color: #0000FF;">[</span><span style="color: #000000;">21</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"09"</span><span style="color: #0000FF;">)?</span><span style="color: #008000;">"Y"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"N"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d:%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n10</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,431 ⟶ 1,627:
=={{header|PicoLisp}}==
PicoLisp does not have floating point, but does have library functions to make fixed-point math easier. I use a scale factor of 25 to ensure that there is enough precision.
<syntaxhighlight lang="picolisp">
(load "@lib/misc.l")
Line 1,472 ⟶ 1,664:
(prinl "h(" (align 2 I) ") = " (fmt4 H))))
(bye)
</syntaxhighlight>
{{Out}}
<pre>
Line 1,496 ⟶ 1,688:
=={{header|PL/I}}==
<
declare s float (18), (i, n) fixed binary;
declare is fixed decimal (30);
Line 1,517 ⟶ 1,709:
end;
end Hickerson;</
{{out}}
<pre>
Line 1,538 ⟶ 1,730:
</pre>
Using extended precision available by software:
<
do n = 1 to 18;
s = divide( float(0.5), float(0.693147180559945309417232121458) );
Line 1,555 ⟶ 1,747:
put edit (' is not a near integer') (A);
end;
</syntaxhighlight>
{{out}} Results:
<pre>
Line 1,581 ⟶ 1,773:
This uses Pythons [http://docs.python.org/2/library/decimal.html decimal module] of fixed precision decimal floating point calculations.
<
import math
Line 1,598 ⟶ 1,790:
if 'E' not in norm and ('.0' in norm or '.9' in norm)
else ' NOT nearly integer!')
print('n:%2i h:%s%s' % (n, norm, almostinteger))</
{{out}}
Line 1,624 ⟶ 1,816:
=={{header|Racket}}==
{{trans|Python}}
<
(require math/bigfloat)
Line 1,642 ⟶ 1,834:
(~r n #:min-width 2)
(bigfloat->string hickerson-n)
almost-integer))</
{{out}}
<pre> 0: 0.7213475204444817036799623405009460687136 is not Nearly integer!
Line 1,668 ⟶ 1,860:
We'll use [http://doc.raku.org/type/FatRat FatRat] values, and a series for an [http://mathworld.wolfram.com/NaturalLogarithmof2.html approximation of ln(2)].
<syntaxhighlight lang="raku"
constant h = [\*] 1/2, |(1..*) X/ ln2;
Line 1,676 ⟶ 1,868:
for h[1..17] {
ok m/'.'<[09]>/, .round(0.001)
}</
{{out}}
<pre>ok 1 - 1.041
Line 1,704 ⟶ 1,896:
=={{header|REXX}}==
===version 1===
<
* 04.01.2014 Walter Pachl - using a rather aged ln function of mine
* with probably unreasonably high precision
Line 1,792 ⟶ 1,984:
fact=fact*i
End
Return fact</
{{out}}
<pre> 1 1.0406844905 almost an integer
Line 1,818 ⟶ 2,010:
This version supports up to '''507''' decimal digits.
<
numeric digits length(ln2())-length(.) /*be able to calculate big factorials. */
parse arg N . /*get optional number of values to use.*/
Line 1,837 ⟶ 2,029:
|| 3501153644979552391204751726815749320651555247341395258829504530070953263666426541042391578149520437404,
|| 3038550080194417064167151864471283996817178454695702627163106454615025720740248163777338963855069526066,
|| 83411372738737229289564935470257626520988596932019650585547647033067936544325476327449512504060694381470</
{{out|output|text= when using the input of: <tt> 197 </tt>}}
<pre style="height:91ex">
Line 2,041 ⟶ 2,233:
===version 3===
This REXX version takes advantage that the Hickerson series are computed in order, so that the factorials need not be (re-)computed from scratch.
<
numeric digits length(ln2())-length(.) /*be able to calculate big factorials. */
parse arg N . /*get optional number of values to use.*/
Line 2,059 ⟶ 2,251:
|| 3501153644979552391204751726815749320651555247341395258829504530070953263666426541042391578149520437404,
|| 3038550080194417064167151864471283996817178454695702627163106454615025720740248163777338963855069526066,
|| 83411372738737229289564935470257626520988596932019650585547647033067936544325476327449512504060694381470</
{{out|output|text= is identical to the 2<sup>nd</sup> REXX version.}}<br><br>
=={{header|Ring}}==
<
n = 12
hick = 0
Line 2,088 ⟶ 2,280:
else return 0 ok
return sub
</syntaxhighlight>
=={{header|Ruby}}==
Using the BigDecimal standard library:
<
LN2 = BigMath::log(2,16) #Use LN2 = Math::log(2) to see the difference with floats
Line 2,111 ⟶ 2,303:
str = nearly_int?(h) ? "nearly integer" : "NOT nearly integer"
puts "n:%3i h: %s\t%s" % [n, h.to_s('F')[0,25], str] #increase the 25 to print more digits, there are 856 of them
end</
{{out}}
<pre>
Line 2,131 ⟶ 2,323:
n: 16 h: 5315654681981354.51307674 NOT nearly integer
n: 17 h: 130370767029135900.457985 NOT nearly integer</pre>
=={{header|Rust}}==
<syntaxhighlight lang="rust">
use decimal::d128;
use factorial::Factorial;
fn hickerson(n: u64) -> d128 {
d128::from(n.factorial()) / (d128!(2) * (d128!(2).ln().pow(d128::from(n + 1))))
}
// Some details on floating-points numbers can be found at https://cheats.rs/#basic-types
fn main() {
for i in 1..18 {
let h = hickerson(i);
let string = h.to_string();
let dec_part = string.split('.').nth(1).unwrap();
if dec_part.starts_with('0') || dec_part.starts_with('9') {
println!("{} is an almost integer.", h);
} else {
println!("{} is not an almost integer.", h);
}
}
}
</syntaxhighlight>
{{out}}
<pre>
1.040684490502803898934790801867496 is an almost integer.
3.002780707156905443499767407219304 is an almost integer.
12.99629050527696646222488454296431 is an almost integer.
74.99873544766160012763455037564470 is an almost integer.
541.0015185164235075692027746182565 is an almost integer.
4683.001247262257437180467152479008 is an almost integer.
47292.99873131462390482283548778628 is an almost integer.
545834.9979074851670672910397944921 is an almost integer.
7087261.001622899120979187515908217 is an almost integer.
102247563.0052710420110883885941994 is an almost integer.
1622632572.997550049852874861078498 is an almost integer.
28091567594.98157244071518917609951 is an almost integer.
526858348381.0012482861804893808360 is an almost integer.
10641342970443.08453192709507015038 is an almost integer.
230283190977853.0374360391259771066 is an almost integer.
5315654681981354.513076743456805575 is not an almost integer.
130370767029135900.4579853491967735 is not an almost integer.
</pre>
=={{header|Scala}}==
===Functional Programming ♫===
<
object Hickerson extends App {
Line 2,159 ⟶ 2,395:
println(s"While h(n) gives NOT an almost integer with a n of ${aa.filter(!_._2).map(_._1).mkString(", ")}.")
}</
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/tNNk9jB/2 ScalaFiddle (JavaScript executed in browser)]
or by [https://scastie.scala-lang.org/aX6X59zrTkWpEbCOXmpmAg Scastie (remote JVM)].
=={{header|Seed7}}==
<
include "bigrat.s7i";
Line 2,180 ⟶ 2,416:
stri[succ(pos(stri, '.'))] in {'0', '9'});
end for;
end func;</
{{out}}
Line 2,204 ⟶ 2,440:
=={{header|Sidef}}==
<
n! / (2 * pow(2.log, n+1))
}
Line 2,211 ⟶ 2,447:
"h(%2d) = %22s is%s almost an integer.\n".printf(
n, var hn = h(n).round(-3), hn.to_s ~~ /\.[09]/ ? '' : ' NOT')
} << 1..17</
{{out}}
<pre>h( 1) = 1.041 is almost an integer.
Line 2,233 ⟶ 2,469:
=={{header|Tcl}}==
{{tcllib|math::bigfloat}}
<
namespace import math::bigfloat::*
Line 2,250 ⟶ 2,486:
set almost [regexp {\.[09]} $h]
puts [format "h(%d) = %s (%salmost integer)" $n $h [expr {$almost ? "" : "not "}]]
}</
{{out}}
<pre>
Line 2,273 ⟶ 2,509:
=={{header|TI-83 BASIC}}==
<
N!/(2ln(2)^(N+1→H
Disp N,H,"IS
Line 2,280 ⟶ 2,516:
Disp "NOT
Disp "ALMOST INTEGER
End</
(untested)
=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{libheader|Wren-big}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
import "./big" for BigInt, BigDec
var hickerson = Fn.new { |n|
var fact =
var ln2 =
}
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
var k =
var ai = (h[k] == "0" || h[k] == "9")
}</syntaxhighlight>
{{out}}
Line 2,332 ⟶ 2,561:
17: 130370767029135900.4 false
</pre>
=={{header|zkl}}==
Uses lib GMP (integer) and some fiddling to fake up the floating point math.
<
X =BN("1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
ln2X=BN("693147180559945309417232121458176568075500134360255254120680009493393621969694715605863326996418687")
;
fcn hickerson(n){ BN(n).factorial()*X.pow(n+1)*10/2/ln2X.pow(n+1) }</
<
hs,ld,isH:=hickerson(n).toString(),hs[-1],"90".holds(ld);
println("h(%2d)=%s.%s almost integer: %b".fmt(n,hs[0,-1],ld,isH));
}</
{{out}}
<pre>
|