Hickerson series of almost integers: Difference between revisions

Add C# implementation
m (→‎{{header|Phix}}: mpfr_set_default_precision renamed)
(Add C# implementation)
 
(8 intermediate revisions by 6 users not shown)
Line 21:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with Ada.Numerics.Generic_Elementary_Functions;
 
Line 82:
end;
end loop;
end Almost_Integers;</langsyntaxhighlight>
 
{{out}}
Line 107:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<langsyntaxhighlight lang="algol68"># determine whether the first few Hickerson numbers really are "near integers" #
# 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</langsyntaxhighlight>
{{out}}
<pre>
Line 167:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<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>
</lang>
<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.
<langsyntaxhighlight lang="bracmat">( 0:?n
& 1:?fac
& whl
Line 245 ⟶ 278:
)
)
);</langsyntaxhighlight>
{{out}}
<pre>h(1) = 1.041 is an almost-integer.
Line 267 ⟶ 300:
=={{header|C}}==
{{libheader|MPFR}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <mpfr.h>
 
Line 295 ⟶ 328:
 
return 0;
}</langsyntaxhighlight>
{{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}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <iomanip>
#include <boost/multiprecision/cpp_dec_float.hpp>
Line 337 ⟶ 420:
}
}
</syntaxhighlight>
</lang>
{{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.
 
<langsyntaxhighlight lang="clojure">(defn hickerson
"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")))</langsyntaxhighlight>
{{out}}
<pre>1 1.04068 almost integer
Line 403 ⟶ 486:
=={{header|COBOL}}==
{{works with|GNU Cobol|2.0}}
<langsyntaxhighlight lang="cobol"> >>SOURCE FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. hickerson-series.
Line 431 ⟶ 514:
END-PERFORM
.
END PROGRAM hickerson-series.</langsyntaxhighlight>
 
{{out}}
Line 457 ⟶ 540:
=={{header|Crystal}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">require "big"
LN2 = Math.log(2).to_big_f
Line 478 ⟶ 561:
puts "n:%3i h: %s\t%s" % [n, h, str]
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 502 ⟶ 585:
=={{header|D}}==
The D <code>real</code> type has enough precision for this task.
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.algorithm, std.mathspecial;
 
Line 511 ⟶ 594:
tenths.among!(0, 9) ? "" : "NOT ");
}
}</langsyntaxhighlight>
<pre>H( 1)= 1.04 is nearly integer.
H( 2)= 3.00 is nearly integer.
Line 531 ⟶ 614:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting kernel math math.factorials math.functions
math.ranges sequences ;
IN: rosetta-code.hickerson
Line 548 ⟶ 631:
] each ;
 
MAIN: hickerson-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 574 ⟶ 657:
{{works with|4tH v3.64.0}}
4tH has two different floating point libraries. This version works with both of them.
<langsyntaxhighlight lang="forth">[UNDEFINED] ANS [IF]
include lib/fp1.4th \ ANSZen 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 \ ZenANS float version
include lib/flnflog.4th \ for FLN
include lib/fpow.4th \ for FPOW
[THEN]
 
Line 589 ⟶ 674:
float array ln2 2 s>f fln latest f! \ precalculate ln(2)
\ integer exponentiation
: powhickerson dup >r fdupfactorial s>f ln2 f@ r> 1+ ?dofpow foverfdup f*+ loop fnipf/ ;
: hickerson dup >r factorial s>f ln2 f@ r> 1+ pow fdup f+ f/ ;
: integer? if ." TRUE " else ." FALSE" then space ;
\ is it an integer?
Line 600 ⟶ 684:
;
 
first17</langsyntaxhighlight>
{{out}}
<pre>
Line 623 ⟶ 707:
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="fortran">program hickerson
implicit none
integer, parameter :: q = selected_real_kind(30)
Line 645 ⟶ 729:
end do
1 format('h(',I2,') = ',F23.3,' is',A,' an almost-integer')
end program</langsyntaxhighlight>
 
{{out}}
Line 671 ⟶ 755:
=={{header|FreeBASIC}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="freebasic">' version 08-10-2016
' compile with: fbc -s gui
 
Line 756 ⟶ 840:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>h(1) = 1.04068 is a almost integer
Line 801 ⟶ 885:
=={{header|Fōrmulæ}}==
 
In [http{{FormulaeEntry|page=https://wiki.formulae.org/?script=examples/Hickerson_series_of_almost_integers this] page you can see the solution of this task.}}
 
'''Solution'''
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
[[File:Fōrmulæ - Hickerson series of almost integers 01.png]]
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
[[File:Fōrmulæ - Hickerson series of almost integers 02.png]]
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 830 ⟶ 916:
i, &w, d[1:], d[2] == '0' || d[2] == '9')
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 853 ⟶ 939:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Number.CReal -- from numbers
 
import qualified Data.Number.CReal as C
Line 873 ⟶ 959:
 
 
</syntaxhighlight>
</lang>
{{out}}
<pre style="font-size:80%">
Line 903 ⟶ 989:
Definitions:
 
<langsyntaxhighlight Jlang="j">ln2=: [: +/ 1 % [: (*2x&^) 1+i.
h=: ! % 2*(ln2 99)^>:</langsyntaxhighlight>
 
Implementation (1 in initial column indicates an almost integer, results displayed to 3 digits after the decimal point):
 
<langsyntaxhighlight Jlang="j"> 1 23j3": (h ,.~ 0 9 e.~ 10 <.@* 1 | h) x:1+i.17
1 1.041
1 3.003
Line 925 ⟶ 1,011:
1 230283190977853.037
0 5315654681981354.513
0 130370767029135900.458</langsyntaxhighlight>
 
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}}==
<langsyntaxhighlight lang="java">import java.math.*;
 
public class Hickerson {
Line 956 ⟶ 1,042:
return c.toString().matches("0|9");
}
}</langsyntaxhighlight>
 
<pre> 1 is almost integer: true
Line 979 ⟶ 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.
<langsyntaxhighlight lang="jq">def hickerson:
. as $n
| (2|log) as $log2
Line 1,003 ⟶ 1,089:
end
else "insufficient precision for hickerson(\($i))"
end</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">$ jq -M -r -n -f Hickerson_series.jq
hickerson(1) is 1.0406844905028039 -- almost an integer
hickerson(2) is 3.0027807071569055 -- almost an integer
Line 1,022 ⟶ 1,108:
hickerson(15) is 230283190977853.06 -- almost an integer
insufficient precision for hickerson(16)
insufficient precision for hickerson(17)</langsyntaxhighlight>
 
=={{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">
<lang Julia>
function makehickerson{T<:Real}(x::T)
n = 0
Line 1,076 ⟶ 1,162:
a = log(2.0)
reporthickerson(a, 17)
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,291 ⟶ 1,377:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="kotlin">// version 1.1.4
 
import java.math.BigDecimal
Line 1,313 ⟶ 1,399:
fun main(args: Array<String>) {
for (n in 1..17) println("${"%2d".format(n)} is almost integer: ${Hickerson.almostInteger(n)}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,337 ⟶ 1,423:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">h[n_] = n!/(2 (Log[2])^(n + 1));
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</langsyntaxhighlight>
{{out}}
<pre>1 1.04068 True
Line 1,362 ⟶ 1,448:
=={{header|ML}}==
==={{header|mLite}}===
<langsyntaxhighlight lang="ocaml">local
fun log (n, k, last = sum, sum) = sum
| (n, k, last, sum) = log (n, k + 1, sum, sum + ( 1 / (k * n ^ k)))
Line 1,378 ⟶ 1,464:
;
foreach show ` iota 17;
</syntaxhighlight>
</lang>
Output
<pre>(1, 1.04068449050280389893479080186749587, true)
Line 1,404 ⟶ 1,490:
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.
 
<langsyntaxhighlight Nimlang="nim">import strformat
import bignum
 
Line 1,429 ⟶ 1,515:
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</langsyntaxhighlight>
 
{{out}}
Line 1,451 ⟶ 1,537:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">h(n)=n!/2/log(2)^(n+1)
almost(x)=abs(x-round(x))<.1
select(n->almost(h(n)),[1..17])
</syntaxhighlight>
</lang>
{{out}}
<pre>%1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]</pre>
Line 1,466 ⟶ 1,552:
calculate the inverse of the natural log of 2, and then compute a running value of h.
 
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use Math::BigFloat;
Line 1,480 ⟶ 1,566:
$n, $s, ($s =~ /\.[09]/ ? "" : " NOT");
}
</syntaxhighlight>
</lang>
{{out}}
<pre>h( 1) = 1.041 is almost an integer.
Line 1,501 ⟶ 1,587:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(notonline)-->
<lang Phix>requires("1.0.0") -- (mpfr_set_default_prec[ision] renamed)
<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>
include mpfr.e
<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>
mpfr_set_default_precision(-23) -- (found by trial/error)
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
mpfr ln2 = mpfr_init(2),
<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>
hn = mpfr_init(0.5)
<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>
mpfr_log(ln2,ln2)
<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>
mpfr_div(hn,hn,ln2)
<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>
for n=1 to 17 do
<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>
mpfr_mul_si(hn,hn,n)
<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>
mpfr_div(hn,hn,ln2)
<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>
string n10 = mpfr_sprintf("%24.4Rf ",hn)
<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>
n10 &= iff(find(n10[21],"09")?"Y":"N")
<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>
printf(1,"%2d:%s\n",{n,n10})
<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>
end for</lang>
<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,538 ⟶ 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">
<lang PicoLisp>
(load "@lib/misc.l")
 
Line 1,575 ⟶ 1,664:
(prinl "h(" (align 2 I) ") = " (fmt4 H))))
(bye)
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,599 ⟶ 1,688:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">Hickerson: procedure options (main); /* 12 January 2014 */
declare s float (18), (i, n) fixed binary;
declare is fixed decimal (30);
Line 1,620 ⟶ 1,709:
end;
 
end Hickerson;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,641 ⟶ 1,730:
</pre>
Using extended precision available by software:
<langsyntaxhighlight lang="pli">
do n = 1 to 18;
s = divide( float(0.5), float(0.693147180559945309417232121458) );
Line 1,658 ⟶ 1,747:
put edit (' is not a near integer') (A);
end;
</syntaxhighlight>
</lang>
{{out}} Results:
<pre>
Line 1,684 ⟶ 1,773:
This uses Pythons [http://docs.python.org/2/library/decimal.html decimal module] of fixed precision decimal floating point calculations.
 
<langsyntaxhighlight lang="python">from decimal import Decimal
import math
 
Line 1,701 ⟶ 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))</langsyntaxhighlight>
 
{{out}}
Line 1,727 ⟶ 1,816:
=={{header|Racket}}==
{{trans|Python}}
<langsyntaxhighlight Racketlang="racket">#lang racket
(require math/bigfloat)
 
Line 1,745 ⟶ 1,834:
(~r n #:min-width 2)
(bigfloat->string hickerson-n)
almost-integer))</langsyntaxhighlight>
{{out}}
<pre> 0: 0.7213475204444817036799623405009460687136 is not Nearly integer!
Line 1,771 ⟶ 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" perl6line>constant ln2 = [+] (1/2.FatRat, */2 ... *) Z/ 1 .. 100;
constant h = [\*] 1/2, |(1..*) X/ ln2;
 
Line 1,779 ⟶ 1,868:
for h[1..17] {
ok m/'.'<[09]>/, .round(0.001)
}</langsyntaxhighlight>
{{out}}
<pre>ok 1 - 1.041
Line 1,807 ⟶ 1,896:
=={{header|REXX}}==
===version 1===
<langsyntaxhighlight lang="rexx">/* REXX ---------------------------------------------------------------
* 04.01.2014 Walter Pachl - using a rather aged ln function of mine
* with probably unreasonably high precision
Line 1,895 ⟶ 1,984:
fact=fact*i
End
Return fact</langsyntaxhighlight>
{{out}}
<pre> 1 1.0406844905 almost an integer
Line 1,921 ⟶ 2,010:
 
This version supports up to &nbsp; '''507''' &nbsp; decimal digits.
<langsyntaxhighlight lang="rexx">/*REXX program to calculate and show the Hickerson series (are near integer). */
numeric digits length(ln2())-length(.) /*be able to calculate big factorials. */
parse arg N . /*get optional number of values to use.*/
Line 1,940 ⟶ 2,029:
|| 3501153644979552391204751726815749320651555247341395258829504530070953263666426541042391578149520437404,
|| 3038550080194417064167151864471283996817178454695702627163106454615025720740248163777338963855069526066,
|| 83411372738737229289564935470257626520988596932019650585547647033067936544325476327449512504060694381470</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 197 </tt>}}
<pre style="height:91ex">
Line 2,144 ⟶ 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.
<langsyntaxhighlight lang="rexx">/*REXX program to calculate and show the Hickerson series (are near integer). */
numeric digits length(ln2())-length(.) /*be able to calculate big factorials. */
parse arg N . /*get optional number of values to use.*/
Line 2,162 ⟶ 2,251:
|| 3501153644979552391204751726815749320651555247341395258829504530070953263666426541042391578149520437404,
|| 3038550080194417064167151864471283996817178454695702627163106454615025720740248163777338963855069526066,
|| 83411372738737229289564935470257626520988596932019650585547647033067936544325476327449512504060694381470</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 2<sup>nd</sup> REXX version.}}<br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
n = 12
hick = 0
Line 2,191 ⟶ 2,280:
else return 0 ok
return sub
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
Using the BigDecimal standard library:
<langsyntaxhighlight lang="ruby">require "bigdecimal"
 
LN2 = BigMath::log(2,16) #Use LN2 = Math::log(2) to see the difference with floats
Line 2,214 ⟶ 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</langsyntaxhighlight>
{{out}}
<pre>
Line 2,236 ⟶ 2,325:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
use decimal::d128;
use factorial::Factorial;
Line 2,257 ⟶ 2,346:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,281 ⟶ 2,370:
=={{header|Scala}}==
===Functional Programming ♫===
<langsyntaxhighlight Scalalang="scala">import scala.annotation.tailrec
 
object Hickerson extends App {
Line 2,306 ⟶ 2,395:
println(s"While h(n) gives NOT an almost integer with a n of ${aa.filter(!_._2).map(_._1).mkString(", ")}.")
 
}</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "bigrat.s7i";
 
Line 2,327 ⟶ 2,416:
stri[succ(pos(stri, '.'))] in {'0', '9'});
end for;
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,351 ⟶ 2,440:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func h(n) {
n! / (2 * pow(2.log, n+1))
}
Line 2,358 ⟶ 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</langsyntaxhighlight>
{{out}}
<pre>h( 1) = 1.041 is almost an integer.
Line 2,380 ⟶ 2,469:
=={{header|Tcl}}==
{{tcllib|math::bigfloat}}
<langsyntaxhighlight lang="tcl">package require math::bigfloat
namespace import math::bigfloat::*
 
Line 2,397 ⟶ 2,486:
set almost [regexp {\.[09]} $h]
puts [format "h(%d) = %s (%salmost integer)" $n $h [expr {$almost ? "" : "not "}]]
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,420 ⟶ 2,509:
 
=={{header|TI-83 BASIC}}==
<langsyntaxhighlight lang="ti83b">For(N,1,17
N!/(2ln(2)^(N+1→H
Disp N,H,"IS
Line 2,427 ⟶ 2,516:
Disp "NOT
Disp "ALMOST INTEGER
End</langsyntaxhighlight>
(untested)
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
{{libheader|Wren-big}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
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.
<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)
}</syntaxhighlight>
Fmt.print("$2d: $20s $s", i, s, ai)
}</lang>
 
{{out}}
Line 2,479 ⟶ 2,561:
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}}==
Uses lib GMP (integer) and some fiddling to fake up the floating point math.
<langsyntaxhighlight lang="zkl">var [const] BN=Import("zklBigNum"),
X =BN("1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
ln2X=BN("693147180559945309417232121458176568075500134360255254120680009493393621969694715605863326996418687")
;
fcn hickerson(n){ BN(n).factorial()*X.pow(n+1)*10/2/ln2X.pow(n+1) }</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach n in ([1..18]){
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));
}</langsyntaxhighlight>
{{out}}
<pre>
338

edits