Hickerson series of almost integers: Difference between revisions

Ada version
(Add Factor example)
(Ada version)
Line 15:
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...
 
=={{header|Ada}}==
<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;</lang>
 
{{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}}==
211

edits