Data update

This commit is contained in:
Ingy döt Net 2026-02-01 16:33:20 -08:00
parent 5150844a7d
commit 4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions

View file

@ -1,58 +0,0 @@
with Ada.Text_Io;
procedure Additive_Primes is
Last : constant := 499;
Columns : constant := 12;
type Prime_List is array (2 .. Last) of Boolean;
function Get_Primes return Prime_List is
Prime : Prime_List := (others => True);
begin
for P in Prime'Range loop
if Prime (P) then
for N in 2 .. Positive'Last loop
exit when N * P not in Prime'Range;
Prime (N * P) := False;
end loop;
end if;
end loop;
return Prime;
end Get_Primes;
function Sum_Of (N : Natural) return Natural is
Image : constant String := Natural'Image (N);
Sum : Natural := 0;
begin
for Char of Image loop
Sum := Sum + (if Char in '0' .. '9'
then Natural'Value ("" & Char)
else 0);
end loop;
return Sum;
end Sum_Of;
package Natural_Io is new Ada.Text_Io.Integer_Io (Natural);
use Ada.Text_Io, Natural_Io;
Prime : constant Prime_List := Get_Primes;
Count : Natural := 0;
begin
Put_Line ("Additive primes <500:");
for N in Prime'Range loop
if Prime (N) and then Prime (Sum_Of (N)) then
Count := Count + 1;
Put (N, Width => 5);
if Count mod Columns = 0 then
New_Line;
end if;
end if;
end loop;
New_Line;
Put ("There are ");
Put (Count, Width => 2);
Put (" additive primes.");
New_Line;
end Additive_Primes;

View file

@ -1,73 +0,0 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. ADDITIVE-PRIMES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VARIABLES.
03 MAXIMUM PIC 999.
03 AMOUNT PIC 999.
03 CANDIDATE PIC 999.
03 DIGIT PIC 9 OCCURS 3 TIMES,
REDEFINES CANDIDATE.
03 DIGITSUM PIC 99.
01 PRIME-DATA.
03 COMPOSITE-FLAG PIC X OCCURS 500 TIMES.
88 PRIME VALUE ' '.
03 SIEVE-PRIME PIC 999.
03 SIEVE-COMP-START PIC 999.
03 SIEVE-COMP PIC 999.
03 SIEVE-MAX PIC 999.
01 OUT-FMT.
03 NUM-FMT PIC ZZZ9.
03 OUT-LINE PIC X(40).
03 OUT-PTR PIC 99.
PROCEDURE DIVISION.
BEGIN.
MOVE 500 TO MAXIMUM.
MOVE 1 TO OUT-PTR.
PERFORM SIEVE.
MOVE ZERO TO AMOUNT.
PERFORM TEST-NUMBER
VARYING CANDIDATE FROM 2 BY 1
UNTIL CANDIDATE IS GREATER THAN MAXIMUM.
DISPLAY OUT-LINE.
DISPLAY SPACES.
MOVE AMOUNT TO NUM-FMT.
DISPLAY 'Amount of additive primes found: ' NUM-FMT.
STOP RUN.
TEST-NUMBER.
ADD DIGIT(1), DIGIT(2), DIGIT(3) GIVING DIGITSUM.
IF PRIME(CANDIDATE) AND PRIME(DIGITSUM),
ADD 1 TO AMOUNT,
PERFORM WRITE-NUMBER.
WRITE-NUMBER.
MOVE CANDIDATE TO NUM-FMT.
STRING NUM-FMT DELIMITED BY SIZE INTO OUT-LINE
WITH POINTER OUT-PTR.
IF OUT-PTR IS GREATER THAN 40,
DISPLAY OUT-LINE,
MOVE SPACES TO OUT-LINE,
MOVE 1 TO OUT-PTR.
SIEVE.
MOVE SPACES TO PRIME-DATA.
DIVIDE MAXIMUM BY 2 GIVING SIEVE-MAX.
PERFORM SIEVE-OUTER-LOOP
VARYING SIEVE-PRIME FROM 2 BY 1
UNTIL SIEVE-PRIME IS GREATER THAN SIEVE-MAX.
SIEVE-OUTER-LOOP.
IF PRIME(SIEVE-PRIME),
MULTIPLY SIEVE-PRIME BY 2 GIVING SIEVE-COMP-START,
PERFORM SIEVE-INNER-LOOP
VARYING SIEVE-COMP
FROM SIEVE-COMP-START BY SIEVE-PRIME
UNTIL SIEVE-COMP IS GREATER THAN MAXIMUM.
SIEVE-INNER-LOOP.
MOVE 'X' TO COMPOSITE-FLAG(SIEVE-COMP).

View file

@ -28,11 +28,11 @@ var T: integer;
begin
Result:=0;
repeat
begin
T:=N mod 10;
N:=N div 10;
Result:=Result+T;
end
begin
T:=N mod 10;
N:=N div 10;
Result:=Result+T;
end
until N<1;
end;
@ -48,15 +48,15 @@ Cnt:=0;
S:='';
for N:=1 to 500-1 do
if IsPrime(N) then
begin
Sum:=SumDigits(N);
if IsPrime(Sum) then
begin
Inc(Cnt);
S:=S+Format('%6d',[N]);
if (Cnt mod 8)=0 then S:=S+CRLF;
end;
end;
begin
Sum:=SumDigits(N);
if IsPrime(Sum) then
begin
Inc(Cnt);
S:=S+Format('%6d',[N]);
if (Cnt mod 8)=0 then S:=S+CRLF;
end;
end;
Memo.Lines.Add(S);
Memo.Lines.Add('Count = '+IntToStr(Cnt));
end;

View file

@ -2,35 +2,35 @@
# Prime numbers for which the sum of their decimal digits are also primes
# # Variables:
# # Variables:
#
integer MAX_n=500
# # Functions:
# # Functions:
#
# # Function _isprime(n) return 1 for prime, 0 for not prime
# # Function _isprime(n) return 1 for prime, 0 for not prime
#
function _isprime {
typeset _n ; integer _n=$1
typeset _i ; integer _i
typeset _n ; integer _n=$1
typeset _i ; integer _i
(( _n < 2 )) && return 0
for (( _i=2 ; _i*_i<=_n ; _i++ )); do
(( ! ( _n % _i ) )) && return 0
done
return 1
(( _n < 2 )) && return 0
for (( _i=2 ; _i*_i<=_n ; _i++ )); do
(( ! ( _n % _i ) )) && return 0
done
return 1
}
# # Function _sumdigits(n) return sum of n's digits
# # Function _sumdigits(n) return sum of n's digits
#
function _sumdigits {
typeset _n ; _n=$1
typeset _i _sum ; integer _i _sum=0
typeset _n ; _n=$1
typeset _i _sum ; integer _i _sum=0
for ((_i=0; _i<${#_n}; _i++)); do
(( _sum+=${_n:${_i}:1} ))
done
echo ${_sum}
for ((_i=0; _i<${#_n}; _i++)); do
(( _sum+=${_n:${_i}:1} ))
done
echo ${_sum}
}
######
@ -39,9 +39,9 @@ function _sumdigits {
integer i digsum
for ((i=2; i<MAX_n; i++)); do
_isprime ${i} && (( ! $? )) && continue
_isprime ${i} && (( ! $? )) && continue
digsum=$(_sumdigits ${i})
_isprime ${digsum} ; (( $? )) && printf "%4d " ${i}
digsum=$(_sumdigits ${i})
_isprime ${digsum} ; (( $? )) && printf "%4d " ${i}
done
print

View file

@ -1,6 +1,6 @@
val isPrime = fn(i) {
i == 2 or i > 2 and
not any(series(2 .. i ^/ 2, asconly=true), by=fn x:i div x)
i == 2 or i > 2 and
not any(series(2 .. i ^/ 2, asconly=true), by=fn x:i div x)
}
val sumDigits = fn i: fold(s2n(string(i)), by=fn{+})

View file

@ -1,28 +1,28 @@
isPrime = function(n)
if n <= 3 then return n > 1
if n % 2 == 0 or n % 3 == 0 then return false
i = 5
while i ^ 2 <= n
if n % i == 0 or n % (i + 2) == 0 then return false
i += 6
end while
return true
if n <= 3 then return n > 1
if n % 2 == 0 or n % 3 == 0 then return false
i = 5
while i ^ 2 <= n
if n % i == 0 or n % (i + 2) == 0 then return false
i += 6
end while
return true
end function
digitSum = function(n)
sum = 0
while n > 0
sum += n % 10
n = floor(n / 10)
end while
return sum
sum = 0
while n > 0
sum += n % 10
n = floor(n / 10)
end while
return sum
end function
additive = []
for i in range(2, 500)
if isPrime(i) and isPrime(digitSum(i)) then additive.push(i)
if isPrime(i) and isPrime(digitSum(i)) then additive.push(i)
end for
print "There are " + additive.len + " additive primes under 500."
print additive

View file

@ -1,7 +1,7 @@
MODULE AdditivePrimes;
IMPORT
Out;
Out, Primes;
CONST
Max = 500;
@ -20,24 +20,8 @@ PROCEDURE DigitSum( n :INTEGER ):INTEGER;
RETURN result
END DigitSum;
PROCEDURE Sieve;
VAR i, j :INTEGER;
BEGIN
Prime[ 0 ] := FALSE; Prime[ 1 ] := FALSE;
FOR i := 2 TO Max DO Prime[ i ] := TRUE END;
FOR i := 2 TO Max DIV 2 DO
IF Prime[ i ] THEN
j := i * 2;
WHILE j <= Max DO
Prime[ j ] := FALSE;
INC( j, i )
END
END
END
END Sieve;
BEGIN
Sieve;
Primes.sieve( Prime );
FOR n := 2 TO Max DO
IF Prime[ n ] & Prime[ DigitSum( n ) ] THEN
Out.Int( n, 4 );

View file

@ -1,6 +1,4 @@
(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">additive</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)))</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">500</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">),</span><span style="color: #000000;">additive</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;">"%d additive primes found: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">))})</span>
<!--
with javascript_semantics
function additive(string p) return is_prime(sum(sq_sub(p,'0'))) end function
sequence res = filter(apply(get_primes_le(500),sprint),additive)
printf(1,"%d additive primes found: %s\n",{length(res),join(shorten(res,"",6))})

View file

@ -1,5 +1,5 @@
-- 22 Mar 2025
include Settings
-- 23 Aug 2025
include Setting
say 'ADDITIVE PRIMES'
say version
@ -22,7 +22,4 @@ say a 'additive primes found below' n
say Time('e')/1 'seconds'
exit
include Sequences
include Numbers
include Functions
include Abend
include Math