Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,58 @@
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

@ -0,0 +1,73 @@
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

@ -0,0 +1,13 @@
link factors
procedure main(A)
limit := \A[1] | 500
every p := prime() do
if p < 500 then writes(" ",additive(p)) else break
write()
end
procedure additive(a)
a ? (sum := 0, while sum +:= move(1))
return (isprime(sum),a)
end

View file

@ -1,9 +1,9 @@
val isPrime = fn(i) {
i == 2 or i > 2 and
not any(series(2 .. i ^/ 2, asconly=true), by=fn x:i div x)
val isPrime = fn(i number) {
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{+})
val sumDigits = fn(i) { fold(s2n(string(i)), by=fn{+}) }
writeln "Additive primes less than 500:"

View file

@ -0,0 +1,14 @@
local int = require "int"
local fmt = require "fmt"
print("Additive primes less than 500:")
local primes = int.primes(499)
local count = 0
for primes as p do
if int.isprime(int.digstat(p)[2]) do
count += 1
fmt.write("%3d ", p)
if count % 10 == 0 then print() end
end
end
print($"\n\n{count} additive primes found.")

View file

@ -0,0 +1,42 @@
Rebol [
title: "Rosetta code: Additive primes"
file: %Additive_primes.r3
url: https://rosettacode.org/wiki/Additive_primes
note: "Based on Red language solution"
]
primes: function [n [integer!]][
;; See: https://rosettacode.org/wiki/Sieve_of_Eratosthenes#Rebol
poke prim: make bitset! n 1 true
r: 2 while [r * r <= n][repeat q n / r - 1 [poke prim q + 1 * r true]
until [not pick prim r: r + 1]]
collect [repeat i n [unless prim/:i [keep i]]]
]
;; Compute the digit sum (cross-sum) of an integer n
cross-sum: function [n][
out: 0 ;; accumulator for sum of digits
foreach m form n [ ;; iterate over the characters of n's string form
out: out - 48 + m ;; convert char to integer, add to out
]
out
]
;; Return all additive primes <= n:
;; A prime p is "additive" if its digit-sum is also prime.
additive-primes: function [n][
collect [
foreach p ps: primes n [ ;; generate primes up to n and iterate over them
if find ps cross-sum p [ ;; check if digit-sum of p is itself in the prime set
keep p ;; keep p if digit-sum is prime
]
]
]
]
;; Generate additive primes up to 500
result: additive-primes 500
;; Format nicely by rows of 10 (with newlines)
print [length? result "additive primes < 500:"]
forall result [
prin pad result/1 -4
if zero? ((index? result) % 10) [print ""]
]
print ""

View file

@ -0,0 +1,2 @@
Prime ← =₁⧻°/×
⊸⧻▽⊸≡(×Prime/+⊥₁₀⟜Prime) ↘2⇡500

View file

@ -0,0 +1 @@
Prime ← =₁⧻°/×

View file

@ -0,0 +1 @@
↘2⇡500

View file

@ -0,0 +1 @@
≡(×Prime/+⊥₁₀⟜Prime)

View file

@ -0,0 +1 @@
⊸⧻▽⊸

View file

@ -1,56 +1,34 @@
fn is_prime(n int) bool {
if n < 2 {
return false
} else if n%2 == 0 {
return n == 2
} else if n%3 == 0 {
return n == 3
} else {
mut d := 5
for d*d <= n {
if n%d == 0 {
return false
}
d += 2
if n%d == 0 {
return false
}
d += 4
}
return true
}
}
import strconv
fn sum_digits(nn int) int {
mut n := nn
mut sum := 0
for n > 0 {
sum += n % 10
n /= 10
fn is_prime(n int) bool {
if n <= 1 { return false }
for i := 2; i * i <= n; i++ {
if n % i == 0 { return false }
}
return sum
return true
}
fn main() {
println("Additive primes less than 500:")
mut i := 2
mut count := 0
for {
if is_prime(i) && is_prime(sum_digits(i)) {
count++
print("${i:3} ")
if count%10 == 0 {
println('')
mut row := 0
limit := 500
println("working...")
println("Additive primes are:")
for n := 1; n <= limit; n++ {
if is_prime(n) {
strn := n.str()
mut num := 0
for ch in strn {
digit := strconv.atoi(ch.ascii_str()) or { exit(1) }
num += digit
}
if is_prime(num) {
row++
print("$n ")
if row % 10 == 0 { println("") }
}
}
if i > 2 {
i += 2
} else {
i++
}
if i > 499 {
break
}
}
println("\n\n$count additive primes found.")
println("")
println("found $row additive primes.")
println("done...")
}