Hickerson series of almost integers: Difference between revisions

Add C# implementation
mNo edit summary
(Add C# implementation)
 
(47 intermediate revisions by 18 users not shown)
Line 1:
{{task}}
The following function,   due to D. Hickerson,   is said to generate "Almost integers" by the [http://mathworld.wolfram.com/AlmostInteger.html "Almost Integer" page of Wolfram Mathworld]. (December 31 2013).
<br>[http://mathworld.wolfram.com/AlmostInteger.html "Almost Integer" page of Wolfram MathWorld], &nbsp; (December 31 2013). &nbsp; (See formula numbered &nbsp; '''51'''.)
 
The function is:
 
The function is: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <math>h(n) = {\operatorname{n}!\over2(\ln{2})^{n+1}}</math>
 
 
It is said to produce "almost integers" for n between 1 and 17.
It is said to produce "almost integers" for &nbsp; '''n''' &nbsp; between &nbsp; '''1''' &nbsp; and &nbsp; '''17'''.
The purpose of the task is to verify this assertion.
 
Assume that an "almost integer" has '''either a nine or a zero as its first digit after the decimal point''' of its decimal string representation
 
 
The task is to calculate all values of the function checking and stating which are "almost integers".
;Task:
Calculate all values of the function checking and stating which are "almost integers".
 
Note: Use extended/arbitrary precision numbers in your calculation if necessary to ensure you have adequate precision of results as for example:
h(18) = 3385534663256845326.39...
<br><br>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_IO;
with Ada.Numerics.Generic_Elementary_Functions;
 
procedure Almost_Integers is
 
type Real is new Long_Long_Float;
 
package Real_IO is
new Ada.Text_IO.Float_IO (Real);
package Integer_IO is
new Ada.Text_IO.Integer_IO (Integer);
 
function Faculty (N : in Long_Long_Integer) return Long_Long_Integer is
(if N < 2 then N else N * Faculty (N - 1));
 
function Hickerson (N : in Integer) return Real
is
package Math is
new Ada.Numerics.Generic_Elementary_Functions (Real);
LN2 : constant Real := Math.Log (2.0, Base => Ada.Numerics.E);
Numerator : constant Real := Real (Faculty (Long_Long_Integer (N)));
Denominator : constant Real := 2.0 * LN2 ** (N + 1);
begin
return Numerator / Denominator;
end Hickerson;
 
function Is_Almost_Integer (N : Real) return Boolean is
Image : String (1 .. 100);
begin
Real_IO.Put (Image, N, Exp => 0, Aft => 2);
 
pragma Assert (Image (Image'Last - 2) = '.');
case Image (Image'Last - 1) is
when '0' | '9' => return True;
when others => return False;
end case;
end Is_Almost_Integer;
 
use Ada.Text_IO;
Placeholder : String := " n h(n) almost";
Image_N : String renames Placeholder ( 1 .. 2);
Image_H : String renames Placeholder ( 4 .. 31);
Image_A : String renames Placeholder (34 .. 39);
begin
Put_Line (Placeholder);
Image_N := (others => '-');
Image_H := (others => '-');
Image_A := (others => '-');
Put_Line (Placeholder);
 
for N in 1 .. 17 loop
declare
H : constant Real := Hickerson (N);
I : constant Boolean := Is_Almost_Integer (H);
begin
Integer_IO.Put (Image_N, N);
Real_IO.Put (Image_H, H, Exp => 0, Aft => 4);
Image_A := (if I then "TRUE " else "FALSE");
Put_Line (Placeholder);
end;
end loop;
end Almost_Integers;</syntaxhighlight>
 
{{out}}
<pre> n h(n) almost
-- ---------------------------- ------
1 1.0407 TRUE
2 3.0028 TRUE
3 12.9963 TRUE
4 74.9987 TRUE
5 541.0015 TRUE
6 4683.0012 TRUE
7 47292.9987 TRUE
8 545834.9979 TRUE
9 7087261.0016 TRUE
10 102247563.0053 TRUE
11 1622632572.9976 TRUE
12 28091567594.9816 TRUE
13 526858348381.0012 TRUE
14 10641342970443.0845 TRUE
15 230283190977853.0370 TRUE
16 5315654681981354.5100 FALSE
17 130370767029135900.0000 TRUE</pre>
 
=={{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 54 ⟶ 143:
)
)
OD</langsyntaxhighlight>
{{out}}
<pre>
Line 78 ⟶ 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 97 ⟶ 186:
return(out)
}
</syntaxhighlight>
</lang>
<p>Output:</p>
<pre>
Line 118 ⟶ 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 129 ⟶ 251:
and a decimal part (using a string operation) before outputting.
<langsyntaxhighlight lang="bracmat">( 0:?n
& 1:?fac
& whl
Line 156 ⟶ 278:
)
)
);</langsyntaxhighlight>
{{out}}
<pre>h(1) = 1.041 is an almost-integer.
Line 178 ⟶ 300:
=={{header|C}}==
{{libheader|MPFR}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <mpfr.h>
 
Line 206 ⟶ 328:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 226 ⟶ 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 248 ⟶ 420:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 273 ⟶ 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 292 ⟶ 464:
(if (almost-integer? h)
"almost integer"
"NOT almost integer")))</langsyntaxhighlight>
{{out}}
<pre>1 1.04068 almost integer
Line 314 ⟶ 486:
=={{header|COBOL}}==
{{works with|GNU Cobol|2.0}}
<langsyntaxhighlight lang="cobol"> >>SOURCE FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. hickerson-series.
Line 342 ⟶ 514:
END-PERFORM
.
END PROGRAM hickerson-series.</langsyntaxhighlight>
 
{{out}}
Line 364 ⟶ 536:
h(16) = 5315654681981354.5130767434 which is not an almost integer.
h(17) = 130370767029135900.4579853491 which is not an almost integer.
</pre>
 
=={{header|Crystal}}==
{{trans|Ruby}}
<syntaxhighlight lang="ruby">require "big"
LN2 = Math.log(2).to_big_f
 
FACTORIALS = Hash(Int32, Float64).new{|h,k| h[k] = k * h[k-1]}
FACTORIALS[0] = 1
def hickerson(n)
FACTORIALS[n] / (2 * LN2 ** (n+1))
end
def nearly_int?(n)
int = n.round
(int - 0.1..int + 0.1).includes? n
end
1.upto(17) do |n|
h = hickerson(n)
str = nearly_int?(h) ? "nearly integer" : "NOT nearly integer"
puts "n:%3i h: %s\t%s" % [n, h, str]
end
</syntaxhighlight>
{{out}}
<pre>
n: 1 h: 1.04068449050280396857 nearly integer
n: 2 h: 3.00278070715690574485 nearly integer
n: 3 h: 12.9962905052769682015 nearly integer
n: 4 h: 74.9987354476616126737 nearly integer
n: 5 h: 541.00151851642361617 nearly integer
n: 6 h: 4683.00124726225853393 nearly integer
n: 7 h: 47292.998731314636563 nearly integer
n: 8 h: 545834.997907485331424 nearly integer
n: 9 h: 7087261.00162290149215 nearly integer
n: 10 h: 102247563.005271079641 nearly integer
n: 11 h: 1622632572.99755070131 nearly integer
n: 12 h: 28091567594.9815846588 nearly integer
n: 13 h: 526858348381.001495064 nearly integer
n: 14 h: 10641342970443.0898723 nearly integer
n: 15 h: 230283190977853.160709 NOT nearly integer
n: 16 h: 5315654681981357.53644 NOT nearly integer
n: 17 h: 130370767029135978.97 NOT nearly integer
</pre>
 
=={{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 377 ⟶ 594:
tenths.among!(0, 9) ? "" : "NOT ");
}
}</langsyntaxhighlight>
<pre>H( 1)= 1.04 is nearly integer.
H( 2)= 3.00 is nearly integer.
Line 395 ⟶ 612:
H(16)= 5315654681981354.51 is NOT nearly integer.
H(17)= 130370767029135900.50 is NOT nearly integer.</pre>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: formatting kernel math math.factorials math.functions
math.ranges sequences ;
IN: rosetta-code.hickerson
 
: ln2 ( -- x )
99 [1,b] [ [ 2 swap ^ ] [ * ] bi recip ] map-sum ;
 
: hickerson ( n -- x ) [ n! ] [ 1 + ln2 swap ^ 2 * ] bi / ;
 
: almost-int? ( x -- ? ) 10 * truncate 10 mod { 0 9 } member? ;
 
: hickerson-demo ( -- )
18 [1,b] [
dup hickerson dup almost-int?
"h(%02d) = %23.3f almost integer? %u\n" printf
] each ;
 
MAIN: hickerson-demo</syntaxhighlight>
{{out}}
<pre>
h(01) = 1.041 almost integer? t
h(02) = 3.003 almost integer? t
h(03) = 12.996 almost integer? t
h(04) = 74.999 almost integer? t
h(05) = 541.002 almost integer? t
h(06) = 4683.001 almost integer? t
h(07) = 47292.999 almost integer? t
h(08) = 545834.998 almost integer? t
h(09) = 7087261.002 almost integer? t
h(10) = 102247563.005 almost integer? t
h(11) = 1622632572.998 almost integer? t
h(12) = 28091567594.982 almost integer? t
h(13) = 526858348381.001 almost integer? t
h(14) = 10641342970443.085 almost integer? t
h(15) = 230283190977853.037 almost integer? t
h(16) = 5315654681981354.513 almost integer? f
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}}==
<langsyntaxhighlight lang="fortran">Programprogram Hickersonhickerson
implicit none
! 3 February 2014
integer, parameter :: q = selected_real_kind(30)
! not all Fortran compilers provide REAL*16 and INTEGER*8
integer, parameter :: l = selected_int_kind(15)
implicit none
real(kind=kind(1q0)q) :: s, l2
integer(kind=kind(1_8)) :: i, n,f,is k
 
l2 = log(2.0_q)
do n = 1, 17
do sn = 0.5q0 /1, log(2q0)17
do i s = 1,n0.5_q / l2
do si = (s * i) /1, log(2q0)n
end do s = (s * i) / l2
end do
 
k = floor((s - floor(s, l)) * 10)
if (k == 0 .or. k == 9) then
print 1, n, s, ""
else
print 1, n, s, " NOT"
endif
end do
1 format('h(',I2,') = ',F23.3,' is',A,' an almost-integer')
end program</syntaxhighlight>
 
is = s
f = (s-is)*10 !first digit after decimal point
if (f == 0 .or. f == 9) then
write(*,10)n,s,''
else
write(*,10)n,s,' NOT'
endif
end do
10 format('h(',i2,') = ',F23.3,' is',A,' an almost-integer')
end program Hickerson</lang>
{{out}}
 
<pre>
h( 1) = 1.041 is an almost-integer
Line 440 ⟶ 752:
h(17) = 130370767029135900.458 is NOT an almost-integer
</pre>
 
=={{header|FreeBASIC}}==
{{libheader|GMP}}
<syntaxhighlight lang="freebasic">' version 08-10-2016
' compile with: fbc -s gui
 
#Include Once "gmp.bi"
 
#Macro init_float_size (big_float ,size)
Dim As Mpf_ptr big_float = Allocate( Len( __mpf_struct))
Mpf_init2( big_float,size)
#EndMacro
 
#Macro mpf_remove(big_float)
Mpf_clear(big_float)
DeAllocate (big_float)
#EndMacro
 
Screen 20
 
init_float_size(tmp,1024)
init_float_size(ln2,1024)
init_float_size(pow_ln2,2048)
init_float_size(answer,2048)
 
Dim As UInteger n, num
Dim As String st
Dim As ZString Ptr text
text = Allocate (1500) ' size 1500 char.
 
' need to calculate ln(2)
 
' x 1 1 1 1
' ln --- = - + ------ + ------ ... + ------
' x-1 x 2x^2 3x^3 nx^n
 
Mpf_set_ui(answer, 1)
Mpf_div_ui(answer, answer, 2) ' answer = 1/2
 
For n = 2 To 1024
Mpf_set_ui(tmp, 2) ' tmp = x = 2
Mpf_pow_ui(tmp, tmp, n) ' tmp = x ^ n
Mpf_mul_ui(tmp, tmp, n) ' tmp = n * (x ^ n)
Mpf_ui_div(tmp, 1, tmp) ' tmp = 1 / (n * (x ^ n))
Mpf_add(answer, answer, tmp) ' answer += tmp
Next
 
/' remove this line if you want to print ln(2)
Gmp_sprintf(text,!"ln(2) =\t %2.100Ff ", answer)
Print *text
Print
'/
 
Mpf_set_ui(tmp, 1)
Mpf_set(ln2, answer )
Mpf_set(pow_ln2, ln2)
 
Print
For n = 1 To 40
Mpf_mul_ui(tmp, tmp, n) ' n!
Mpf_mul(pow_ln2, pow_ln2, ln2) ' ln(2)^(n+1)
Mpf_div_ui(answer, tmp , 2) ' / 2
Mpf_div(answer, answer, pow_ln2) ' / ln(2)^(n+1)
Gmp_sprintf(text,!"h(%i) =\t %65.5Ff ",n, answer)
st = *text
num = st[InStr(st,".")] ' get the first character after the "."
If num = Asc("0") Or num = Asc("9") Then
Color 10
Print st;" is a almost integer"
Color 15
Else
Color 12
Print st;" not close enough"
Color 15
End If
Next
 
mpf_remove(answer) 'cleanup
mpf_remove(pow_ln2)
mpf_remove(ln2)
mpf_remove(tmp)
DeAllocate (text)
 
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre>h(1) = 1.04068 is a almost integer
h(2) = 3.00278 is a almost integer
h(3) = 12.99629 is a almost integer
h(4) = 74.99874 is a almost integer
h(5) = 541.00152 is a almost integer
h(6) = 4683.00125 is a almost integer
h(7) = 47292.99873 is a almost integer
h(8) = 545834.99791 is a almost integer
h(9) = 7087261.00162 is a almost integer
h(10) = 102247563.00527 is a almost integer
h(11) = 1622632572.99755 is a almost integer
h(12) = 28091567594.98157 is a almost integer
h(13) = 526858348381.00125 is a almost integer
h(14) = 10641342970443.08453 is a almost integer
h(15) = 230283190977853.03744 is a almost integer
h(16) = 5315654681981354.51308 not close enough
h(17) = 130370767029135900.45799 not close enough
h(18) = 3385534663256845326.39030 not close enough
h(19) = 92801587319328411139.87379 not close enough
h(20) = 2677687796244384203087.52849 not close enough
h(21) = 81124824998504073881728.73642 not close enough
h(22) = 2574844419803190384544450.20255 not close enough
h(23) = 85438451336745709294581778.58838 not close enough
h(24) = 2958279121074145472650646597.12769 not close enough
h(25) = 106697365438475775825583475660.43285 not close enough
h(26) = 4002225759844168492486127555858.62763 not close enough
h(27) = 155897763918621623249276226664346.97380 is a almost integer
h(28) = 6297562064950066033518373935416160.57852 not close enough
h(29) = 263478385263023690020893329036314027.57946 not close enough
h(30) = 11403568794011880483742464196174527074.25258 not close enough
h(31) = 510008036574269388430841024076099269561.72065 not close enough
h(32) = 23545154085734896649184490637145314147689.71316 not close enough
h(33) = 1120959742203056268267494209293002620410998.73593 not close enough
h(34) = 54984904077825684862426868390301031843623475.17448 not close enough
h(35) = 2776425695289206002630310219593685601350868792.44731 not close enough
h(36) = 144199280951655469628360978109406918279521398522.98537 is a almost integer
h(37) = 7697316738562185268347644943000493477791292787264.22720 not close enough
h(38) = 421985466101260424678587486718115935816310614908533.21973 not close enough
h(39) = 23743057231588741419119534567705900419845239863116450.98123 is a almost integer
h(40) = 1370159636942236704917645663312384364387429049720377464.47614 not close enough</pre>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Hickerson_series_of_almost_integers}}
 
'''Solution'''
 
[[File:Fōrmulæ - Hickerson series of almost integers 01.png]]
 
[[File:Fōrmulæ - Hickerson series of almost integers 02.png]]
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 463 ⟶ 916:
i, &w, d[1:], d[2] == '0' || d[2] == '9')
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 484 ⟶ 937:
n: 17 h: 130370767029135900.458 Nearly integer: false
</pre>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Number.CReal -- from numbers
 
import qualified Data.Number.CReal as C
Line 505 ⟶ 959:
 
 
</syntaxhighlight>
</lang>
{{out}}
<pre style="font-size:80%">
Line 535 ⟶ 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 557 ⟶ 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 588 ⟶ 1,042:
return c.toString().matches("0|9");
}
}</langsyntaxhighlight>
 
<pre> 1 is almost integer: true
Line 611 ⟶ 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 635 ⟶ 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 654 ⟶ 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 708 ⟶ 1,162:
a = log(2.0)
reporthickerson(a, 17)
</syntaxhighlight>
</lang>
 
{{out}}
Line 919 ⟶ 1,373:
+> 130370767029135600.0000
The precision is inadequate for a definite result.
</pre>
 
=={{header|Kotlin}}==
{{trans|Java}}
<syntaxhighlight lang="kotlin">// version 1.1.4
 
import java.math.BigDecimal
import java.math.BigInteger
import java.math.MathContext
 
object Hickerson {
private const val LN2 = "0.693147180559945309417232121458"
 
fun almostInteger(n: Int): Boolean {
val a = BigDecimal(LN2).pow(n + 1) * BigDecimal(2)
var nn = n
var f = n.toLong()
while (--nn > 1) f *= nn
val b = BigDecimal(f).divide(a, MathContext.DECIMAL128)
val c = b.movePointRight(1).toBigInteger() % BigInteger.TEN
return c.toString().matches(Regex("[09]"))
}
}
 
fun main(args: Array<String>) {
for (n in 1..17) println("${"%2d".format(n)} is almost integer: ${Hickerson.almostInteger(n)}")
}</syntaxhighlight>
 
{{out}}
<pre>
1 is almost integer: true
2 is almost integer: true
3 is almost integer: true
4 is almost integer: true
5 is almost integer: true
6 is almost integer: true
7 is almost integer: true
8 is almost integer: true
9 is almost integer: true
10 is almost integer: true
11 is almost integer: true
12 is almost integer: true
13 is almost integer: true
14 is almost integer: true
15 is almost integer: true
16 is almost integer: false
17 is almost integer: false
</pre>
 
=={{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 944 ⟶ 1,445:
16 5315654681981354.51308 False
17 130370767029135900.45799 False</pre>
 
=={{header|PARI/GP}}==
<lang parigp>h(n)=n!/2/log(2)^(n+1)
almost(x)=abs(x-round(x))<.1
select(n->almost(h(n)),[1..17])
</lang>
{{out}}
<pre>%1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]</pre>
 
=={{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 971 ⟶ 1,464:
;
foreach show ` iota 17;
</syntaxhighlight>
</lang>
Output
<pre>(1, 1.04068449050280389893479080186749587, true)
Line 992 ⟶ 1,485:
</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}}==
<syntaxhighlight 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>
{{out}}
<pre>%1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]</pre>
 
=={{header|Perl}}==
Line 1,001 ⟶ 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,015 ⟶ 1,566:
$n, $s, ($s =~ /\.[09]/ ? "" : " NOT");
}
</syntaxhighlight>
</lang>
{{out}}
<pre>h( 1) = 1.041 is almost an integer.
Line 1,035 ⟶ 1,586:
h(17) = 130370767029135900.458 is NOT almost an integer.</pre>
 
=={{header|Perl 6Phix}}==
<!--<syntaxhighlight lang="phix">(notonline)-->
{{works with|rakudo|2015-09-13}}
<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>
We'll use [http://doc.perl6.org/type/FatRat FatRat] values, and a series for an [http://mathworld.wolfram.com/NaturalLogarithmof2.html approximation of ln(2)].
<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>
1: 1.0407 Y
2: 3.0028 Y
3: 12.9963 Y
4: 74.9987 Y
5: 541.0015 Y
6: 4683.0012 Y
7: 47292.9987 Y
8: 545834.9979 Y
9: 7087261.0016 Y
10: 102247563.0053 Y
11: 1622632572.9976 Y
12: 28091567594.9816 Y
13: 526858348381.0012 Y
14: 10641342970443.0845 Y
15: 230283190977853.0374 Y
16: 5315654681981354.5131 N
17: 130370767029135900.4580 N
</pre>
 
=={{header|PicoLisp}}==
<lang perl6>constant ln2 = [+] (1/2.FatRat, */2 ... *) Z/ 1 .. 100;
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.
constant h = [\*] 1/2, |(1..*) X/ ln2;
<syntaxhighlight lang="picolisp">
(load "@lib/misc.l")
 
(scl 25)
use Test;
plan *;
for h[1..17] {
ok m/'.'<[09]>/, .round(0.001)
}</lang>
{{out}}
<pre>ok 1 - 1.041
ok 2 - 3.003
ok 3 - 12.996
ok 4 - 74.999
ok 5 - 541.002
ok 6 - 4683.001
ok 7 - 47292.999
ok 8 - 545834.998
ok 9 - 7087261.002
ok 10 - 102247563.005
ok 11 - 1622632572.998
ok 12 - 28091567594.982
ok 13 - 526858348381.001
ok 14 - 10641342970443.085
ok 15 - 230283190977853.037
not ok 16 - 5315654681981354.513
 
(setq LN2 0.69314718055994530941723212)
# Failed test '5315654681981354.513'
# at hickerson line 8
not ok 17 - 130370767029135900.458
 
(de almost-int? (N)
# Failed test '130370767029135900.458'
(bool (member (% (/ N 0.1) 10) (0 9))))
# at hickerson line 8</pre>
 
(de fmt4 (N)
(format (/ N 0.0001) 4))
 
(de h (N)
(*/ (factorial N) `(* 1.0 1.0) (* 2 (s** LN2 (inc N)))))
 
(de s** (A N) # scaled integer exponentiation
(let R 1.0
(for Bit (chop (bin N))
(setq R (*/ R R 1.0))
(when (= Bit "1")
(setq R (*/ A R 1.0))))
R))
 
(de factorial (N)
(cond
((lt0 N) NIL)
((=0 N) 1)
(T (apply * (range 1 N)))))
 
(for I 18
(let H (h I)
(if (almost-int? H)
(prin "yes: ")
(prin " no: "))
(prinl "h(" (align 2 I) ") = " (fmt4 H))))
(bye)
</syntaxhighlight>
{{Out}}
<pre>
yes: h( 1) = 1.0406
yes: h( 2) = 3.0027
yes: h( 3) = 12.9962
yes: h( 4) = 74.9987
yes: h( 5) = 541.0015
yes: h( 6) = 4683.0012
yes: h( 7) = 47292.9987
yes: h( 8) = 545834.9979
yes: h( 9) = 7087261.0016
yes: h(10) = 102247563.0052
yes: h(11) = 1622632572.9975
yes: h(12) = 28091567594.9815
yes: h(13) = 526858348381.0012
yes: h(14) = 10641342970443.0845
yes: h(15) = 230283190977853.0374
no: h(16) = 5315654681981354.5130
no: h(17) = 130370767029135900.4579
no: h(18) = 3385534663256845326.3904
</pre>
 
=={{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,095 ⟶ 1,709:
end;
 
end Hickerson;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,116 ⟶ 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,133 ⟶ 1,747:
put edit (' is not a near integer') (A);
end;
</syntaxhighlight>
</lang>
{{out}} Results:
<pre>
Line 1,159 ⟶ 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,176 ⟶ 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,202 ⟶ 1,816:
=={{header|Racket}}==
{{trans|Python}}
<langsyntaxhighlight Racketlang="racket">#lang racket
(require math/bigfloat)
 
Line 1,220 ⟶ 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,240 ⟶ 1,854:
16: 5315654681981354.513076743456805577995494 is not Nearly integer!
17: 130370767029135900.4579853491967736302263 is not Nearly integer!</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015-09-13}}
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" line>constant ln2 = [+] (1/2.FatRat, */2 ... *) Z/ 1 .. 100;
constant h = [\*] 1/2, |(1..*) X/ ln2;
 
use Test;
plan *;
for h[1..17] {
ok m/'.'<[09]>/, .round(0.001)
}</syntaxhighlight>
{{out}}
<pre>ok 1 - 1.041
ok 2 - 3.003
ok 3 - 12.996
ok 4 - 74.999
ok 5 - 541.002
ok 6 - 4683.001
ok 7 - 47292.999
ok 8 - 545834.998
ok 9 - 7087261.002
ok 10 - 102247563.005
ok 11 - 1622632572.998
ok 12 - 28091567594.982
ok 13 - 526858348381.001
ok 14 - 10641342970443.085
ok 15 - 230283190977853.037
not ok 16 - 5315654681981354.513
 
# Failed test '5315654681981354.513'
# at hickerson line 8
not ok 17 - 130370767029135900.458
 
# Failed test '130370767029135900.458'
# at hickerson line 8</pre>
 
=={{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,331 ⟶ 1,984:
fact=fact*i
End
Return fact</langsyntaxhighlight>
{{out}}
<pre> 1 1.0406844905 almost an integer
Line 1,354 ⟶ 2,007:
===version 2===
This REXX version can calculate the Hickerson series to any number up to the length (in decimal digits)
<br>up to the size of the number returned by the &nbsp; '''ln2''' &nbsp; function.
 
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()) - 1 length(.) /*be able to calculate big factorials. */
parse arg N . /*get optional number of values to use.*/
if N=='' then N=18 1 /*Not specified? Then use the default.*/
/* [↓] compute possible Hickerson #s. */
do j=1 for N; #= Hickerson(j) /*traipse thru a range of Hickerson #s.*/
?= right(# * 10 % 1, 1) /*get 1st decimal digit past dec. point*/
if ?==0 | ?==9 then _= 'almost integer' /*the number is, */
else _= ' ' /* or it ain't. */
say right(j,3) _ format(#, , 5) /*show number with 5 dec digs fraction.*/
end /*j*/
exit /*stick a fork in it, we're all done. */
/*─────────────────────────────────────────────────────────────────────────────────────────────────────────*/
!: procedure; parse arg x; !=1; do j=2 to x; != !*j; end; return ! /* ◄──compute X factorial*/
Hickerson: procedure; parse arg z; return !(z) / (2*ln2() ** (z+1))
ln2: return .69314718055994530941723212145817656807550013436025525412068000949339362196969471560586332699641,
|| 8687542001481020570685733685520235758130557032670751635075961930727570828371435190307038623891673471123,
|| 3501153644979552391204751726815749320651555247341395258829504530070953263666426541042391578149520437404,
|| 3038550080194417064167151864471283996817178454695702627163106454615025720740248163777338963855069526066,
|| 83411372738737229289564935470257626520988596932019650585547647033067936544325476327449512504060694381470</langsyntaxhighlight>
'''{{out|output'''|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 197 </tt> }}
<pre style="height:65ex91ex">
1 almost integer 1.04068
2 almost integer 3.00278
Line 1,580 ⟶ 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()) - 1 length(.) /*be able to calculate big factorials. */
parse arg N . /*get optional number of values to use.*/
if N=='' then N=18 1 /*Not specified? Then use the default.*/
!=1 1 /* [↓] compute possible Hickerson #s. */
do j=1 for N; #= Hickerson(j) /*traipse thru a range of Hickerson #s.*/
?= right(# * 10 % 1, 1) /*get 1st decimal digit past dec. point*/
if ?==0 | ?==9 then _= 'almost integer' /*the number is, */
else _= ' ' /* or it ain't. */
say right(j,3) _ format(#, , 5) /*show number with 5 dec digs fraction.*/
end /*j*/
exit /*stick a fork in it, we're all done. */
/*─────────────────────────────────────────────────────────────────────────────────────────────────────────*/
Hickerson: parse arg z; != ! * z; return ! / (2*ln2() ** (z+1) )
ln2: return .69314718055994530941723212145817656807550013436025525412068000949339362196969471560586332699641,
|| 8687542001481020570685733685520235758130557032670751635075961930727570828371435190307038623891673471123,
|| 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 1,627 ⟶ 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 1,650 ⟶ 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 1,669 ⟶ 2,322:
n: 15 h: 230283190977853.037436039 nearly integer
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 ♫===
<syntaxhighlight lang="scala">import scala.annotation.tailrec
 
object Hickerson extends App {
 
def almostInteger(n: Int): Boolean = {
def ln2 = BigDecimal("0.69314718055994530941723212145818")
 
def div: BigDecimal = ln2.pow(n + 1) * 2
 
def factorial(num: Int): Long = {
@tailrec
def accumulated(acc: Long, n: Long): Long =
if (n <= 0) acc else accumulated(acc * n, n - 1)
 
accumulated(1, num)
}
 
((BigDecimal(factorial(n)) / div * 10).toBigInt() % 10).toString.matches("0|9")
}
 
val aa = (1 to 17).map(n => n -> almostInteger(n))
 
println(s"Function h(n) gives a almost integer with a n of ${aa.filter(_._2).map(_._1).mkString(", ")}.")
println(s"While h(n) gives NOT an almost integer with a n of ${aa.filter(!_._2).map(_._1).mkString(", ")}.")
 
}</syntaxhighlight>
{{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 1,689 ⟶ 2,416:
stri[succ(pos(stri, '.'))] in {'0', '9'});
end for;
end func;</langsyntaxhighlight>
 
{{out}}
Line 1,713 ⟶ 2,440:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func h(n) {
n! / (2 * Math.pow(2.log, n+1));
}
 
 
17.times { |n|
"h(%2d) =  %22s is%s almost an integer.\n".printf(
n, var hn = h(n).roundfround(-3), hn.to_s ~~ /\.[09]/  ? ''  : ' NOT');
} << 1..17</syntaxhighlight>
}</lang>
{{out}}
<pre>h( 1) = 1.041 is almost an integer.
Line 1,739 ⟶ 2,466:
h(16) = 5315654681981354.513 is NOT almost an integer.
h(17) = 130370767029135900.458 is NOT almost an integer.</pre>
 
=={{header|TI-83 BASIC}}==
<lang ti83b>For(N,1,17
N!/(2ln(2)^(N+1→H
Disp N,H,"IS
round(H,1)-iPart(H)
If not(Ans=.9 or not(Ans
Disp "NOT
Disp "ALMOST INTEGER
End</lang>
(untested)
 
=={{header|Tcl}}==
{{tcllib|math::bigfloat}}
<langsyntaxhighlight lang="tcl">package require math::bigfloat
namespace import math::bigfloat::*
 
Line 1,770 ⟶ 2,486:
set almost [regexp {\.[09]} $h]
puts [format "h(%d) = %s (%salmost integer)" $n $h [expr {$almost ? "" : "not "}]]
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,790 ⟶ 2,506:
h(16) = 5315654681981354.5130767435 (not almost integer)
h(17) = 130370767029135900.457985349 (not almost integer)
</pre>
 
=={{header|TI-83 BASIC}}==
<syntaxhighlight lang="ti83b">For(N,1,17
N!/(2ln(2)^(N+1→H
Disp N,H,"IS
round(H,1)-iPart(H)
If not(Ans=.9 or not(Ans
Disp "NOT
Disp "ALMOST INTEGER
End</syntaxhighlight>
(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 = BigDec.fromBigInt(BigInt.factorial(n), 64)
var ln2 = BigDec.ln2 // precise to 64 decimal digits
return fact / (BigDec.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(1, false, true)
var k = h.count - 1
var ai = (h[k] == "0" || h[k] == "9")
Fmt.print("$2d: $20s $s", i, h, ai)
}</syntaxhighlight>
 
{{out}}
<pre>
Values of h(n), truncated to 1 dp, and whether 'almost integers' or not:
1: 1.0 true
2: 3.0 true
3: 12.9 true
4: 74.9 true
5: 541.0 true
6: 4683.0 true
7: 47292.9 true
8: 545834.9 true
9: 7087261.0 true
10: 102247563.0 true
11: 1622632572.9 true
12: 28091567594.9 true
13: 526858348381.0 true
14: 10641342970443.0 true
15: 230283190977853.0 true
16: 5315654681981354.5 false
17: 130370767029135900.4 false
</pre>
 
=={{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>
337

edits