Data update
This commit is contained in:
parent
72eb4943cb
commit
4d5544505c
2347 changed files with 62432 additions and 16731 deletions
60
Task/Additive-primes/ALGOL-60/additive-primes.alg
Normal file
60
Task/Additive-primes/ALGOL-60/additive-primes.alg
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
begin
|
||||
|
||||
integer procedure sumdigits(n);
|
||||
value n; integer n;
|
||||
begin
|
||||
integer q, sum;
|
||||
sum := 0;
|
||||
for sum := sum while n > 0 do
|
||||
begin
|
||||
q := entier(n / 10);
|
||||
sum := sum + (n - q * 10);
|
||||
n := q;
|
||||
end;
|
||||
sumdigits := sum;
|
||||
end;
|
||||
|
||||
boolean procedure isprime(n);
|
||||
value n; integer n;
|
||||
begin
|
||||
if n < 2 then
|
||||
isprime := false
|
||||
else if n = entier(n / 2) * 2 then
|
||||
isprime := (n = 2)
|
||||
else
|
||||
begin
|
||||
comment - check odd divisors up to sqrt(n);
|
||||
integer i, limit;
|
||||
boolean divisible;
|
||||
i := 3;
|
||||
limit := entier(sqrt(n));
|
||||
divisible := false;
|
||||
for i := i while i <= limit and not divisible do
|
||||
begin
|
||||
if entier(n / i) * i = n then
|
||||
divisible := true;
|
||||
i := i + 2
|
||||
end;
|
||||
isprime := not divisible;
|
||||
end;
|
||||
end;
|
||||
|
||||
integer i, count;
|
||||
outstring(1,"Looking up to 500 for additive primes\n");
|
||||
count := 0;
|
||||
for i := 2 step 1 until 500 do
|
||||
if isprime(i) then
|
||||
begin
|
||||
if isprime(sumdigits(i)) then
|
||||
begin
|
||||
outinteger(1,i);
|
||||
count := count + 1;
|
||||
if count = entier(count / 10) * 10 then
|
||||
outstring(1,"\n");
|
||||
end;
|
||||
end;
|
||||
outstring(1,"\n");
|
||||
outinteger(1,count);
|
||||
outstring(1,"were found\n");
|
||||
|
||||
end
|
||||
64
Task/Additive-primes/ALGOL-M/additive-primes.alg
Normal file
64
Task/Additive-primes/ALGOL-M/additive-primes.alg
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
BEGIN
|
||||
|
||||
% RETURN N MOD M %
|
||||
INTEGER FUNCTION MOD(N, M);
|
||||
INTEGER N, M;
|
||||
BEGIN
|
||||
MOD := N - (N / M) * M;
|
||||
END;
|
||||
|
||||
% RETURN 1 IF N IS PRIME, OTHERWISE 0 %
|
||||
INTEGER FUNCTION ISPRIME(N);
|
||||
INTEGER N;
|
||||
BEGIN
|
||||
IF N = 2 THEN
|
||||
ISPRIME := 1
|
||||
ELSE IF (N < 2) OR (MOD(N,2) = 0) THEN
|
||||
ISPRIME := 0
|
||||
ELSE % TEST ODD DIVISORS UP TO SQRT OF N %
|
||||
BEGIN
|
||||
INTEGER I, DIVISIBLE;
|
||||
I := 3;
|
||||
DIVISIBLE := 0;
|
||||
WHILE (I * I <= N) AND (DIVISIBLE = 0) DO
|
||||
BEGIN
|
||||
IF MOD(N,I) = 0 THEN DIVISIBLE := 1;
|
||||
I := I + 2;
|
||||
END;
|
||||
ISPRIME := 1 - DIVISIBLE;
|
||||
END;
|
||||
END;
|
||||
|
||||
% RETURN THE SUM OF THE DIGITS OF N %
|
||||
INTEGER FUNCTION SUMDIGITS(N);
|
||||
INTEGER N;
|
||||
BEGIN
|
||||
INTEGER SUM;
|
||||
SUM := 0;
|
||||
WHILE N > 0 DO
|
||||
BEGIN
|
||||
SUM := SUM + MOD(N, 10);
|
||||
N := N / 10;
|
||||
END;
|
||||
SUMDIGITS := SUM;
|
||||
END;
|
||||
|
||||
% LOOK FOR ADDITIVE PRIMES IN RANGE 1 TO 500 %
|
||||
INTEGER I, S, COUNT;
|
||||
COUNT := 0;
|
||||
FOR I := 1 STEP 1 UNTIL 500 DO
|
||||
BEGIN
|
||||
IF ISPRIME(I)=1 THEN
|
||||
BEGIN
|
||||
S := SUMDIGITS(I);
|
||||
IF ISPRIME(S)=1 THEN
|
||||
BEGIN
|
||||
WRITEON(I);
|
||||
COUNT := COUNT + 1;
|
||||
IF MOD(COUNT,8) = 0 THEN WRITE("");
|
||||
END;
|
||||
END;
|
||||
END;
|
||||
WRITE(COUNT," ADDITIVE PRIMES WERE FOUND");
|
||||
|
||||
END
|
||||
67
Task/Additive-primes/Ballerina/additive-primes.ballerina
Normal file
67
Task/Additive-primes/Ballerina/additive-primes.ballerina
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
import ballerina/io;
|
||||
|
||||
function sumDigits(int m) returns int {
|
||||
int n = m; // make mutable
|
||||
int sum = 0;
|
||||
while n > 0 {
|
||||
sum += n % 10;
|
||||
n /= 10;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
function isPrime(int n) returns boolean {
|
||||
if n < 2 { return false; }
|
||||
if n % 2 == 0 { return n == 2; }
|
||||
if n % 3 == 0 { return n == 3; }
|
||||
int d = 5;
|
||||
while d * d <= n {
|
||||
if n % d == 0 { return false; }
|
||||
d += 2;
|
||||
if n % d == 0 { return false; }
|
||||
d += 4;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function getPrimes(int n) returns int[] {
|
||||
if n < 2 { return []; }
|
||||
if n == 2 { return [2]; }
|
||||
int k = (n - 3) / 2 + 1;
|
||||
boolean[] marked = [];
|
||||
marked.setLength(k);
|
||||
foreach int i in 0..<k { marked[i] = true; }
|
||||
float f = (<float>n).sqrt().floor();
|
||||
int lim = (<int>f - 3) / 2 + 1;
|
||||
foreach int i in 0..<lim {
|
||||
if marked[i] {
|
||||
int p = 2 * i + 3;
|
||||
int s = (p * p - 3) / 2;
|
||||
int j = s;
|
||||
while j < k {
|
||||
marked[j] = false;
|
||||
j += p;
|
||||
}
|
||||
}
|
||||
}
|
||||
int[] primes = [2];
|
||||
foreach int i in 0..<k {
|
||||
if marked[i] { primes.push(2 * i + 3); }
|
||||
}
|
||||
return primes;
|
||||
}
|
||||
|
||||
public function main() {
|
||||
io:println("Additive primes less than 500:");
|
||||
int[] primes = getPrimes(499);
|
||||
int count = 0;
|
||||
foreach int p in primes {
|
||||
if isPrime(sumDigits(p)) {
|
||||
count += 1;
|
||||
string ps = p.toString().padStart(3);
|
||||
io:print(ps, " ");
|
||||
if count % 10 == 0 { io:println(); }
|
||||
}
|
||||
}
|
||||
io:println("\n\n", count, " additive primes found.");
|
||||
}
|
||||
|
|
@ -1,13 +1,9 @@
|
|||
func prime n .
|
||||
if n mod 2 = 0 and n > 2
|
||||
return 0
|
||||
.
|
||||
if n mod 2 = 0 and n > 2 : return 0
|
||||
i = 3
|
||||
sq = sqrt n
|
||||
while i <= sq
|
||||
if n mod i = 0
|
||||
return 0
|
||||
.
|
||||
if n mod i = 0 : return 0
|
||||
i += 2
|
||||
.
|
||||
return 1
|
||||
|
|
@ -22,9 +18,6 @@ func digsum n .
|
|||
for i = 2 to 500
|
||||
if prime i = 1
|
||||
s = digsum i
|
||||
if prime s = 1
|
||||
write i & " "
|
||||
.
|
||||
if prime s = 1 : write i & " "
|
||||
.
|
||||
.
|
||||
print ""
|
||||
|
|
|
|||
52
Task/Additive-primes/PL-I-80/additive-primes.pli
Normal file
52
Task/Additive-primes/PL-I-80/additive-primes.pli
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
additive_primes: procedure options (main);
|
||||
%replace
|
||||
search_limit by 500,
|
||||
true by '1'b,
|
||||
false by '0'b;
|
||||
dcl (i, count) fixed bin;
|
||||
put skip edit('Searching up to ', search_limit,
|
||||
' for additive primes') (a,f(3),a);
|
||||
put skip;
|
||||
count = 0;
|
||||
do i = 2 to search_limit;
|
||||
if isprime(i) then
|
||||
do;
|
||||
if isprime(sumdigits(i)) then
|
||||
do;
|
||||
put edit(i) (f(5));
|
||||
count = count + 1;
|
||||
if mod(count,8) = 0 then put skip;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
put skip edit(count, ' were found') (f(3), a);
|
||||
|
||||
/* return true if n is prime */
|
||||
isprime: proc(n) returns (bit(1));
|
||||
dcl
|
||||
(n, i, limit) fixed bin;
|
||||
if n < 2 then return (false);
|
||||
if mod(n, 2) = 0 then return (n = 2);
|
||||
limit = floor(sqrt(n));
|
||||
i = 3;
|
||||
do while ((i <= limit) & (mod(n, i) ^= 0));
|
||||
i = i + 2;
|
||||
end;
|
||||
return (i > limit);
|
||||
end isprime;
|
||||
|
||||
/* return the sum of the digits of n */
|
||||
sumdigits: proc(n) returns (fixed bin);
|
||||
dcl
|
||||
(n, nn, sum) fixed bin;
|
||||
/* use copy, since n is passed by reference */
|
||||
nn = n;
|
||||
sum = 0;
|
||||
do while (nn > 0);
|
||||
sum = sum + mod(nn, 10);
|
||||
nn = nn / 10;
|
||||
end;
|
||||
return (sum);
|
||||
end sumdigits;
|
||||
|
||||
end additive_primes;
|
||||
4
Task/Additive-primes/PascalABC.NET/additive-primes.pas
Normal file
4
Task/Additive-primes/PascalABC.NET/additive-primes.pas
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
## uses School;//поиск аддитивных простых чисел
|
||||
var AdditivePrimes := Primes(500).Where(n -> n.Digits.Sum.IsPrime).ToArray;
|
||||
Print('Additive Primes:'); AdditivePrimes.Println;
|
||||
Println('Additive Primes Count:', AdditivePrimes.Count);
|
||||
|
|
@ -1,61 +1,28 @@
|
|||
-- 22 Mar 2025
|
||||
include Settings
|
||||
|
||||
say version; say 'Additive primes'; say
|
||||
say 'ADDITIVE PRIMES'
|
||||
say version
|
||||
say
|
||||
arg n
|
||||
numeric digits 16
|
||||
if n = '' then
|
||||
n = -500
|
||||
n = 500
|
||||
show = (n > 0); n = Abs(n)
|
||||
a = Additiveprimes(n)
|
||||
a = Additives(n)
|
||||
if show then do
|
||||
do i = 1 to a
|
||||
call Charout ,Right(addi.additiveprime.i,8)' '
|
||||
call Charout ,Right(addi.i,8)' '
|
||||
if i//10 = 0 then
|
||||
say
|
||||
end
|
||||
say
|
||||
end
|
||||
say a 'additive Primes found below' n
|
||||
say Time('e') 'seconds'
|
||||
say a 'additive primes found below' n
|
||||
say Time('e')/1 'seconds'
|
||||
exit
|
||||
|
||||
Additiveprimes:
|
||||
/* Additive prime numbers */
|
||||
procedure expose addi. prim.
|
||||
arg x
|
||||
/* Init */
|
||||
addi. = 0
|
||||
/* Fast values */
|
||||
if x < 2 then
|
||||
return 0
|
||||
if x < 101 then do
|
||||
a = '2 3 5 7 11 23 29 41 43 47 61 67 83 89 999'
|
||||
do n = 1 to Words(a)
|
||||
w = Word(a,n)
|
||||
if w > x then
|
||||
leave
|
||||
addi.additiveprime.n = w
|
||||
end
|
||||
n = n-1; addi.0 = n
|
||||
return n
|
||||
end
|
||||
/* Get primes */
|
||||
p = Primes(x)
|
||||
/* Collect additive primes */
|
||||
n = 0
|
||||
do i = 1 to p
|
||||
q = prim.Prime.i; s = 0
|
||||
do j = 1 to Length(q)
|
||||
s = s+Substr(q,j,1)
|
||||
end
|
||||
if Prime(s) then do
|
||||
n = n+1; addi.additiveprime.n = q
|
||||
end
|
||||
end
|
||||
/* Return number of additive primes */
|
||||
return n
|
||||
|
||||
include Functions
|
||||
include Numbers
|
||||
include Sequences
|
||||
include Numbers
|
||||
include Functions
|
||||
include Abend
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
unit sub MAIN ($limit = 500);
|
||||
say "{+$_} additive primes < $limit:\n{$_».fmt("%" ~ $limit.chars ~ "d").batch(10).join("\n")}",
|
||||
with ^$limit .grep: { .is-prime and .comb.sum.is-prime }
|
||||
with ^$limit .grep: { .is-prime && .comb.sum.is-prime }
|
||||
|
|
|
|||
55
Task/Additive-primes/S-BASIC/additive-primes.basic
Normal file
55
Task/Additive-primes/S-BASIC/additive-primes.basic
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
$constant true = 0FFFFH
|
||||
$constant false = 0
|
||||
$constant limit = 500
|
||||
|
||||
function sum.of.digits(n = integer) = integer
|
||||
var i, sum = integer
|
||||
var s = string
|
||||
var ch = char
|
||||
s = str$(n)
|
||||
sum = 0
|
||||
for i = 2 to len(s)
|
||||
ch = mid(s,i,1)
|
||||
sum = sum + (ch - '0')
|
||||
next i
|
||||
end = sum
|
||||
|
||||
function mod(n, m = integer) = integer
|
||||
end = n - (n / m) * m
|
||||
|
||||
comment
|
||||
build a table of prime numbers using
|
||||
the classic sieve of Erathosthenes
|
||||
end
|
||||
dim integer prime(limit)
|
||||
var i, j, count = integer
|
||||
prime(1) = false
|
||||
for i = 2 to limit
|
||||
prime(i) = true
|
||||
next i
|
||||
rem - strike out multiples of each prime found
|
||||
for i = 2 to sqr(limit)
|
||||
if prime(i) then
|
||||
begin
|
||||
for j = i + i to limit step i
|
||||
prime(j) = false
|
||||
next j
|
||||
end
|
||||
next i
|
||||
|
||||
rem - use the table for the search
|
||||
print "Searching up to"; limit; " for additive primes"
|
||||
count = 0
|
||||
for i = 2 to limit
|
||||
if prime(i) then
|
||||
if prime(sum.of.digits(i)) then
|
||||
begin
|
||||
print using "### "; i;
|
||||
count = count + 1
|
||||
if mod(count, 10) = 0 then print
|
||||
end
|
||||
next i
|
||||
print
|
||||
print count;" were found"
|
||||
|
||||
end
|
||||
29
Task/Additive-primes/TypeScript/additive-primes.ts
Normal file
29
Task/Additive-primes/TypeScript/additive-primes.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
function isPrime(n: number): boolean {
|
||||
if (n < 2) return false;
|
||||
if (n < 4) return true;
|
||||
if (n % 2 == 0 || n % 3 == 0) return false;
|
||||
for (let i = 5; i <= n ** 0.5; i += 6) {
|
||||
if (n % i == 0 || n % (i+2) == 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function sumDigits(x: number): number {
|
||||
let sum = 0;
|
||||
while (x > 0) {
|
||||
sum = sum + (x % 10);
|
||||
x = Math.floor(x / 10);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
const additivePrimes: number[] = [];
|
||||
|
||||
for (let i = 2; i < 10**7; i++) {
|
||||
if (isPrime(i) && isPrime(sumDigits(i))) {
|
||||
additivePrimes.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(additivePrimes);
|
||||
console.log(`Found ${additivePrimes.length} values`);
|
||||
Loading…
Add table
Add a link
Reference in a new issue