Data update
This commit is contained in:
parent
81fd053722
commit
52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions
75
Task/Factorial-primes/ALGOL-W/factorial-primes.alg
Normal file
75
Task/Factorial-primes/ALGOL-W/factorial-primes.alg
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
begin % find some factorial primes - primes that are f - 1 or f + 1 %
|
||||
% for some factorial f %
|
||||
|
||||
% returns true if p is prime, false otherwise %
|
||||
logical procedure isPrime ( integer value p ) ;
|
||||
if p <= 1 or p rem 2 = 0 then p = 2
|
||||
else begin
|
||||
logical prime;
|
||||
integer rootP, i;
|
||||
prime := true;
|
||||
i := 3;
|
||||
rootP := entier( sqrt( p ) );
|
||||
while i <= rootP and prime do begin
|
||||
prime := p rem i not = 0;
|
||||
i := i + 2
|
||||
end;
|
||||
prime
|
||||
end isPrime ;
|
||||
|
||||
% returns true if p is prime, false otherwise %
|
||||
logical procedure isPrimeLongReal ( long real value p ) ;
|
||||
if p <= MAXINTEGER then begin
|
||||
% p is small enough to test using integer arithmetic %
|
||||
isPrime( entier( p ) )
|
||||
end
|
||||
else begin
|
||||
% p is too large for integer primality testing %
|
||||
logical prime;
|
||||
integer rootP, i;
|
||||
prime := true;
|
||||
i := 2;
|
||||
rootP := entier( longsqrt( p ) );
|
||||
while i <= rootP and prime do begin
|
||||
long real pOverI;
|
||||
pOverI := p / i;
|
||||
prime := roundToReal( pOverI ) not = pOverI;
|
||||
i := if i < 3 then 3 else i + 2
|
||||
end;
|
||||
prime
|
||||
end isPrime ;
|
||||
|
||||
begin
|
||||
long real f;
|
||||
integer fpCount, n;
|
||||
fpCount := 0;
|
||||
n := 0;
|
||||
f := 1;
|
||||
while fpCount < 10 do begin
|
||||
long real fp;
|
||||
string(1) fpOp;
|
||||
n := n + 1;
|
||||
f := f * n;
|
||||
fpOp := "-";
|
||||
for offset := -1 step 2 until 1 do begin
|
||||
fp := f + offset;
|
||||
if fp < MAXINTEGER then begin
|
||||
if isPrime( entier( fp ) ) then begin
|
||||
fpCount := fpCount + 1;
|
||||
write( s_w := 0, i_w := 2, fpCount, ":" );
|
||||
writeon( s_w := 0, i_w := 4, n, "! ", fpOp, " 1 = " );
|
||||
writeon( s_w := 0, i_w := 1, entier( fp ) )
|
||||
end if_isPrime__entier__fp
|
||||
end
|
||||
else if isPrimeLongReal( fp ) then begin
|
||||
fpCount := fpCount + 1;
|
||||
write( s_w := 0, i_w := 2, fpCount, ":" );
|
||||
writeon( s_w := 0, i_w := 4, n, "! ", fpOp, " 1 = " );
|
||||
writeon( r_format := "A", r_w := 1, r_d := 0, fp )
|
||||
end if_isPrime__fp__isPrimeLongReal__fp ;
|
||||
fpOp := "+"
|
||||
end for_fp
|
||||
end while_fpCount_lt_10
|
||||
end
|
||||
|
||||
end.
|
||||
95
Task/Factorial-primes/Ada/factorial-primes.ada
Normal file
95
Task/Factorial-primes/Ada/factorial-primes.ada
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
-- Rosetta Code Task written in Ada
|
||||
-- Factorial primes
|
||||
-- https://rosettacode.org/wiki/Factorial_primes
|
||||
-- August 2024, R. B. E.
|
||||
-- Using GNAT Big Integers, GNAT version 14.1, MacOS 14.6.1, M1 chip
|
||||
|
||||
pragma Ada_2022;
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
|
||||
with Ada.Numerics.Big_Numbers.Big_Integers; use Ada.Numerics.Big_Numbers.Big_Integers;
|
||||
|
||||
procedure Factorial_Primes is
|
||||
|
||||
function Is_Prime (N : in Big_Integer) return Boolean is
|
||||
Big_0 : Big_Natural := To_Big_Integer (0);
|
||||
Big_2 : Big_Natural := To_Big_Integer (2);
|
||||
Big_3 : Big_Natural := To_Big_Integer (3);
|
||||
Big_Temp : Big_Natural := To_Big_Integer (5);
|
||||
begin
|
||||
if N < Big_2 then
|
||||
return False;
|
||||
end if;
|
||||
if N mod Big_2 = Big_0 then
|
||||
return N = Big_2;
|
||||
end if;
|
||||
if N mod Big_3 = Big_0 then
|
||||
return N = Big_3;
|
||||
end if;
|
||||
while Big_Temp * Big_Temp <= N loop
|
||||
if N mod Big_Temp = Big_0 then
|
||||
return False;
|
||||
end if;
|
||||
Big_Temp := Big_Temp + Big_2;
|
||||
if N mod Big_Temp = Big_0 then
|
||||
return False;
|
||||
end if;
|
||||
Big_Temp := Big_Temp + 4;
|
||||
end loop;
|
||||
return True;
|
||||
end Is_Prime;
|
||||
|
||||
function Factorial (N : Positive) return Big_Integer is
|
||||
type Factorial_Array is array (1..12) of Big_Integer;
|
||||
First12_Facts : Factorial_Array;
|
||||
Result : Big_Integer;
|
||||
begin
|
||||
First12_Facts (1) := To_Big_Integer (1);
|
||||
for I in 2..12 loop
|
||||
First12_Facts (I) := First12_Facts (I-1) * To_Big_Integer (I);
|
||||
end loop;
|
||||
if (N <= 12) then
|
||||
return First12_Facts (N);
|
||||
else
|
||||
Result := First12_Facts (12);
|
||||
for I in 13..N loop
|
||||
Result := Result * To_Big_Integer (I);
|
||||
end loop;
|
||||
end if;
|
||||
return Result;
|
||||
end Factorial;
|
||||
|
||||
Fact : Big_Integer;
|
||||
Fact_Plus_One : Big_Integer;
|
||||
Fact_Minus_One : Big_Integer;
|
||||
Big_One : constant Big_Integer := To_Big_Integer (1);
|
||||
I, Count : Natural := 0;
|
||||
Limit : constant Positive := 10;
|
||||
|
||||
begin
|
||||
loop
|
||||
I := I + 1;
|
||||
Fact := Factorial (I);
|
||||
if (is_Prime (Fact - Big_One)) then
|
||||
Count := Count + 1;
|
||||
Put (Count, 3);
|
||||
Put (": ");
|
||||
Put (I, 5);
|
||||
Put ("! - 1 ");
|
||||
Fact_Minus_One := Fact - Big_One;
|
||||
Put (To_String (Arg => Fact_Minus_One, Width => 40));
|
||||
New_Line;
|
||||
end if;
|
||||
if (is_Prime (Fact + Big_One)) then
|
||||
Count := Count + 1;
|
||||
Put (Count, 3);
|
||||
Put (": ");
|
||||
Put (I, 5);
|
||||
Put ("! + 1 ");
|
||||
Fact_Plus_One := Fact + Big_One;
|
||||
Put (To_String (Arg => Fact_Plus_One, Width => 40));
|
||||
New_Line;
|
||||
end if;
|
||||
exit when Count >= Limit;
|
||||
end loop;
|
||||
end Factorial_Primes;
|
||||
Loading…
Add table
Add a link
Reference in a new issue