Additive primes: Difference between revisions

Content added Content deleted
m (→‎{{header|langur}}: added langur rev. no.)
m (syntax highlighting fixup automation)
Line 21: Line 21:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F is_prime(a)
<syntaxhighlight lang=11l>F is_prime(a)
I a == 2
I a == 2
R 1B
R 1B
Line 43: Line 43:
additive_primes++
additive_primes++
print(i, end' ‘ ’)
print(i, end' ‘ ’)
print("\nFound "additive_primes‘ additive primes less than 500’)</lang>
print("\nFound "additive_primes‘ additive primes less than 500’)</syntaxhighlight>


{{out}}
{{out}}
Line 52: Line 52:
=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<lang AArch64 Assembly>
<syntaxhighlight lang=AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program additivePrime64.s */
/* program additivePrime64.s */
Line 202: Line 202:
.include "../includeARM64.inc"
.include "../includeARM64.inc"


</syntaxhighlight>
</lang>
<pre>
<pre>
Prime : 2
Prime : 2
Line 261: Line 261:
</pre>
</pre>
=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_Io;
<syntaxhighlight lang=Ada>with Ada.Text_Io;


procedure Additive_Primes is
procedure Additive_Primes is
Line 318: Line 318:
Put (" additive primes.");
Put (" additive primes.");
New_Line;
New_Line;
end Additive_Primes;</lang>
end Additive_Primes;</syntaxhighlight>
{{out}}
{{out}}
<pre>Additive primes <500:
<pre>Additive primes <500:
Line 330: Line 330:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # find additive primes - primes whose digit sum is also prime #
<syntaxhighlight lang=algol68>BEGIN # find additive primes - primes whose digit sum is also prime #
# sieve the primes to max prime #
# sieve the primes to max prime #
PR read "primes.incl.a68" PR
PR read "primes.incl.a68" PR
Line 353: Line 353:
OD;
OD;
print( ( newline, "Found ", whole( additive count, 0 ), " additive primes below ", whole( UPB prime + 1, 0 ), newline ) )
print( ( newline, "Found ", whole( additive count, 0 ), " additive primes below ", whole( UPB prime + 1, 0 ), newline ) )
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 363: Line 363:


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang algolw>begin % find some additive primes - primes whose digit sum is also prime %
<syntaxhighlight lang=algolw>begin % find some additive primes - primes whose digit sum is also prime %
% sets p( 1 :: n ) to a sieve of primes up to n %
% sets p( 1 :: n ) to a sieve of primes up to n %
procedure Eratosthenes ( logical array p( * ) ; integer value n ) ;
procedure Eratosthenes ( logical array p( * ) ; integer value n ) ;
Line 402: Line 402:
write( i_w := 1, s_w := 0, "Found ", aCount, " additive primes below ", MAX_NUMBER )
write( i_w := 1, s_w := 0, "Found ", aCount, " additive primes below ", MAX_NUMBER )
end
end
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 413: Line 413:
=={{header|APL}}==
=={{header|APL}}==


<lang APL>((+⌿(4/10)⊤P)∊P)/P←(~P∊P∘.×P)/P←1↓⍳500</lang>
<syntaxhighlight lang=APL>((+⌿(4/10)⊤P)∊P)/P←(~P∊P∘.×P)/P←1↓⍳500</syntaxhighlight>


{{out}}
{{out}}
Line 421: Line 421:


=={{header|AppleScript}}==
=={{header|AppleScript}}==
<lang applescript>on sieveOfEratosthenes(limit)
<syntaxhighlight lang=applescript>on sieveOfEratosthenes(limit)
script o
script o
property numberList : {missing value}
property numberList : {missing value}
Line 466: Line 466:


-- Task code:
-- Task code:
tell additivePrimes(499) to return {|additivePrimes<500|:it, numberThereof:count}</lang>
tell additivePrimes(499) to return {|additivePrimes<500|:it, numberThereof:count}</syntaxhighlight>


{{output}}
{{output}}
<lang applescript>{|additivePrimes<500|:{2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89, 101, 113, 131, 137, 139, 151, 157, 173, 179, 191, 193, 197, 199, 223, 227, 229, 241, 263, 269, 281, 283, 311, 313, 317, 331, 337, 353, 359, 373, 379, 397, 401, 409, 421, 443, 449, 461, 463, 467, 487}, numberThereof:54}</lang>
<syntaxhighlight lang=applescript>{|additivePrimes<500|:{2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89, 101, 113, 131, 137, 139, 151, 157, 173, 179, 191, 193, 197, 199, 223, 227, 229, 241, 263, 269, 281, 283, 311, 313, 317, 331, 337, 353, 359, 373, 379, 397, 401, 409, 421, 443, 449, 461, 463, 467, 487}, numberThereof:54}</syntaxhighlight>
=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<lang ARM Assembly>
<syntaxhighlight lang=ARM Assembly>
/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
/* program additivePrime.s */
/* program additivePrime.s */
Line 618: Line 618:
.include "../affichage.inc"
.include "../affichage.inc"


</syntaxhighlight>
</lang>
<pre>
<pre>
Prime : 2
Prime : 2
Line 678: Line 678:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>additives: select 2..500 'x -> and? prime? x prime? sum digits x
<syntaxhighlight lang=rebol>additives: select 2..500 'x -> and? prime? x prime? sum digits x


loop split.every:10 additives 'a ->
loop split.every:10 additives 'a ->
print map a => [pad to :string & 4]
print map a => [pad to :string & 4]


print ["\nFound" size additives "additive primes up to 500"]</lang>
print ["\nFound" size additives "additive primes up to 500"]</syntaxhighlight>


{{out}}
{{out}}
Line 697: Line 697:


=={{header|AWK}}==
=={{header|AWK}}==
<lang AWK>
<syntaxhighlight lang=AWK>
# syntax: GAWK -f ADDITIVE_PRIMES.AWK
# syntax: GAWK -f ADDITIVE_PRIMES.AWK
BEGIN {
BEGIN {
Line 727: Line 727:
return(sum)
return(sum)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 740: Line 740:


=={{header|BASIC}}==
=={{header|BASIC}}==
<lang basic>10 DEFINT A-Z: E=500
<syntaxhighlight lang=basic>10 DEFINT A-Z: E=500
20 DIM P(E): P(0)=-1: P(1)=-1
20 DIM P(E): P(0)=-1: P(1)=-1
30 FOR I=2 TO SQR(E)
30 FOR I=2 TO SQR(E)
Line 750: Line 750:
90 IF NOT P(S) THEN N=N+1: PRINT I,
90 IF NOT P(S) THEN N=N+1: PRINT I,
100 NEXT
100 NEXT
110 PRINT: PRINT N;" additive primes found below ";E</lang>
110 PRINT: PRINT N;" additive primes found below ";E</syntaxhighlight>
{{out}}
{{out}}
<pre> 2 3 5 7 11
<pre> 2 3 5 7 11
Line 765: Line 765:
54 additive primes found below 500</pre>
54 additive primes found below 500</pre>
==={{header|Applesoft BASIC}}===
==={{header|Applesoft BASIC}}===
<lang gwbasic> 0 E = 500
<syntaxhighlight lang=gwbasic> 0 E = 500
1 F = E - 1:L = LEN ( STR$ (F)) + 1: FOR I = 2 TO L:S$ = S$ + CHR$ (32): NEXT I: DIM P(E):P(0) = - 1:P(1) = - 1: FOR I = 2 TO SQR (F): IF NOT P(I) THEN FOR J = I * 2 TO E STEP I:P(J) = - 1: NEXT J
1 F = E - 1:L = LEN ( STR$ (F)) + 1: FOR I = 2 TO L:S$ = S$ + CHR$ (32): NEXT I: DIM P(E):P(0) = - 1:P(1) = - 1: FOR I = 2 TO SQR (F): IF NOT P(I) THEN FOR J = I * 2 TO E STEP I:P(J) = - 1: NEXT J
2 NEXT I: FOR I = B TO F: IF NOT P(I) THEN GOSUB 4
2 NEXT I: FOR I = B TO F: IF NOT P(I) THEN GOSUB 4
Line 771: Line 771:
4 S = 0: IF I THEN FOR J = I TO 0 STEP 0:J1 = INT (J / 10):S = S + (J - J1 * 10):J = J1: NEXT J
4 S = 0: IF I THEN FOR J = I TO 0 STEP 0:J1 = INT (J / 10):S = S + (J - J1 * 10):J = J1: NEXT J
5 IF NOT P(S) THEN N = N + 1: PRINT RIGHT$ (S$ + STR$ (I),L);
5 IF NOT P(S) THEN N = N + 1: PRINT RIGHT$ (S$ + STR$ (I),L);
6 RETURN</lang>
6 RETURN</syntaxhighlight>
<pre>
<pre>
2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487
2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487
Line 778: Line 778:


=={{header|BASIC256}}==
=={{header|BASIC256}}==
<lang freebasic>print "Prime", "Digit Sum"
<syntaxhighlight lang=freebasic>print "Prime", "Digit Sum"
for i = 2 to 499
for i = 2 to 499
if isprime(i) then
if isprime(i) then
Line 805: Line 805:
end while
end while
return s
return s
end function</lang>
end function</syntaxhighlight>


=={{header|BCPL}}==
=={{header|BCPL}}==
<lang bcpl>get "libhdr"
<syntaxhighlight lang=bcpl>get "libhdr"
manifest $( limit = 500 $)
manifest $( limit = 500 $)


Line 842: Line 842:
$)
$)
writef("*N*NFound %N additive primes < %N.*N", num, limit)
writef("*N*NFound %N additive primes < %N.*N", num, limit)
$)</lang>
$)</syntaxhighlight>
{{out}}
{{out}}
<pre> 2 3 5 7 11 23 29 41 43 47
<pre> 2 3 5 7 11 23 29 41 43 47
Line 854: Line 854:


=={{header|C}}==
=={{header|C}}==
<syntaxhighlight lang=C>
<lang C>
#include <stdbool.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdio.h>
Line 928: Line 928:
return 0;
return 0;
}/* main */
}/* main */
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 942: Line 942:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <iomanip>
<syntaxhighlight lang=cpp>#include <iomanip>
#include <iostream>
#include <iostream>


Line 983: Line 983:
}
}
std::cout << '\n' << count << " additive primes found.\n";
std::cout << '\n' << count << " additive primes found.\n";
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 999: Line 999:


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>% Sieve of Erastothenes
<syntaxhighlight lang=clu>% Sieve of Erastothenes
% Returns an array [1..max] marking the primes
% Returns an array [1..max] marking the primes
sieve = proc (max: int) returns (array[bool])
sieve = proc (max: int) returns (array[bool])
Line 1,041: Line 1,041:
stream$putl(po, "\nFound " || int$unparse(count) ||
stream$putl(po, "\nFound " || int$unparse(count) ||
" additive primes < " || int$unparse(max))
" additive primes < " || int$unparse(max))
end start_up</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre> 2 3 5 7 11 23 29 41 43 47
<pre> 2 3 5 7 11 23 29 41 43 47
Line 1,052: Line 1,052:


=={{header|COBOL}}==
=={{header|COBOL}}==
<lang cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang=cobol> IDENTIFICATION DIVISION.
PROGRAM-ID. ADDITIVE-PRIMES.
PROGRAM-ID. ADDITIVE-PRIMES.
Line 1,124: Line 1,124:
SIEVE-INNER-LOOP.
SIEVE-INNER-LOOP.
MOVE 'X' TO COMPOSITE-FLAG(SIEVE-COMP).</lang>
MOVE 'X' TO COMPOSITE-FLAG(SIEVE-COMP).</syntaxhighlight>
{{out}}
{{out}}
<pre> 2 3 5 7 11 23 29 41 43 47
<pre> 2 3 5 7 11 23 29 41 43 47
Line 1,136: Line 1,136:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>
<syntaxhighlight lang=lisp>
(defun sum-of-digits (n)
(defun sum-of-digits (n)
"Return the sum of the digits of a number"
"Return the sum of the digits of a number"
Line 1,158: Line 1,158:




</syntaxhighlight>
</lang>
{{out}}<pre>(dotimes (i 500) (when (additive-primep i) (princ i) (princ " ")))
{{out}}<pre>(dotimes (i 500) (when (additive-primep i) (princ i) (princ " ")))


Line 1,165: Line 1,165:


=={{header|Crystal}}==
=={{header|Crystal}}==
<lang ruby># Fast/simple way to generate primes for small values.
<syntaxhighlight lang=ruby># Fast/simple way to generate primes for small values.
# Uses P3 Prime Generator (PG) and its Prime Generator Sequence (PGS).
# Uses P3 Prime Generator (PG) and its Prime Generator Sequence (PGS).


Line 1,203: Line 1,203:
addprimes.each_with_index { |n, idx| printf "%*d ", maxdigits, n; print "\n" if idx % 10 == 9 } # more efficient
addprimes.each_with_index { |n, idx| printf "%*d ", maxdigits, n; print "\n" if idx % 10 == 9 } # more efficient
puts "\n#{addprimes.size} additive primes below #{nn}."
puts "\n#{addprimes.size} additive primes below #{nn}."
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre> 2 3 5 7 11 23 29 41 43 47
<pre> 2 3 5 7 11 23 29 41 43 47
Line 1,255: Line 1,255:


=={{header|Draco}}==
=={{header|Draco}}==
<lang draco>proc sieve([*] bool prime) void:
<syntaxhighlight lang=draco>proc sieve([*] bool prime) void:
word max, p, c;
word max, p, c;
max := dim(prime,1)-1;
max := dim(prime,1)-1;
Line 1,294: Line 1,294:
writeln();
writeln();
writeln("There are ", n, " additive primes below ", MAX)
writeln("There are ", n, " additive primes below ", MAX)
corp</lang>
corp</syntaxhighlight>
{{out}}
{{out}}
<pre> 2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151
<pre> 2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151
Line 1,302: Line 1,302:


=={{header|Erlang}}==
=={{header|Erlang}}==
<lang Erlang>
<syntaxhighlight lang=Erlang>
main(_) ->
main(_) ->
AddPrimes = [N || N <- lists:seq(2,500), isprime(N) andalso isprime(digitsum(N))],
AddPrimes = [N || N <- lists:seq(2,500), isprime(N) andalso isprime(digitsum(N))],
Line 1,319: Line 1,319:
digitsum(0, S) -> S;
digitsum(0, S) -> S;
digitsum(N, S) -> digitsum(N div 10, S + N rem 10).
digitsum(N, S) -> digitsum(N div 10, S + N rem 10).
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 1,332: Line 1,332:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<lang fsharp>
<syntaxhighlight lang=fsharp>
// Additive Primes. Nigel Galloway: March 22nd., 2021
// Additive Primes. Nigel Galloway: March 22nd., 2021
let rec fN g=function n when n<10->n+g |n->fN(g+n%10)(n/10)
let rec fN g=function n when n<10->n+g |n->fN(g+n%10)(n/10)
primes32()|>Seq.takeWhile((>)500)|>Seq.filter(fN 0>>isPrime)|>Seq.iter(printf "%d "); printfn ""
primes32()|>Seq.takeWhile((>)500)|>Seq.filter(fN 0>>isPrime)|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,344: Line 1,344:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
{{works with|Factor|0.99 2021-02-05}}
<lang factor>USING: formatting grouping io kernel math math.primes
<syntaxhighlight lang=factor>USING: formatting grouping io kernel math math.primes
prettyprint sequences ;
prettyprint sequences ;


Line 1,352: Line 1,352:
499 primes-upto [ sum-digits prime? ] filter
499 primes-upto [ sum-digits prime? ] filter
[ 9 group simple-table. nl ]
[ 9 group simple-table. nl ]
[ length "Found %d additive primes < 500.\n" printf ] bi</lang>
[ length "Found %d additive primes < 500.\n" printf ] bi</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,366: Line 1,366:


=={{header|Fermat}}==
=={{header|Fermat}}==
<lang fermat>Function Digsum(n) =
<syntaxhighlight lang=fermat>Function Digsum(n) =
digsum := 0;
digsum := 0;
while n>0 do
while n>0 do
Line 1,383: Line 1,383:
fi od;
fi od;


!!('There were ',nadd);</lang>
!!('There were ',nadd);</syntaxhighlight>
{{out}}<pre>
{{out}}<pre>
Additive primes below 500 are
Additive primes below 500 are
Line 1,444: Line 1,444:
=={{header|Forth}}==
=={{header|Forth}}==
{{works with|Gforth}}
{{works with|Gforth}}
<lang forth>: prime? ( n -- ? ) here + c@ 0= ;
<syntaxhighlight lang=forth>: prime? ( n -- ? ) here + c@ 0= ;
: notprime! ( n -- ) here + 1 swap c! ;
: notprime! ( n -- ) here + 1 swap c! ;


Line 1,483: Line 1,483:


500 print_additive_primes
500 print_additive_primes
bye</lang>
bye</syntaxhighlight>


{{out}}
{{out}}
Line 1,499: Line 1,499:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
As with the other special primes tasks, use one of the primality testing algorithms as an include.
As with the other special primes tasks, use one of the primality testing algorithms as an include.
<lang freebasic>#include "isprime.bas"
<syntaxhighlight lang=freebasic>#include "isprime.bas"


function digsum( n as uinteger ) as uinteger
function digsum( n as uinteger ) as uinteger
Line 1,520: Line 1,520:
end if
end if
end if
end if
next i</lang>
next i</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:16em">Prime Digit Sum
<pre style="height:16em">Prime Digit Sum
Line 1,582: Line 1,582:
then go through the list, sum digits and check for prime
then go through the list, sum digits and check for prime


<lang pascal>
<syntaxhighlight lang=pascal>
Program AdditivePrimes;
Program AdditivePrimes;
Const max_number = 500;
Const max_number = 500;
Line 1,644: Line 1,644:
writeln(counter,' additive primes found.');
writeln(counter,' additive primes found.');
End.
End.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,658: Line 1,658:


=={{header|Frink}}==
=={{header|Frink}}==
<lang frink>vals = toArray[select[primes[2, 500], {|x| isPrime[sum[integerDigits[x]]]}]]
<syntaxhighlight lang=frink>vals = toArray[select[primes[2, 500], {|x| isPrime[sum[integerDigits[x]]]}]]
println[formatTable[columnize[vals, 10]]]
println[formatTable[columnize[vals, 10]]]
println["\n" + length[vals] + " values found."]</lang>
println["\n" + length[vals] + " values found."]</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,674: Line 1,674:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang=go>package main


import "fmt"
import "fmt"
Line 1,733: Line 1,733:
}
}
fmt.Printf("\n\n%d additive primes found.\n", count)
fmt.Printf("\n\n%d additive primes found.\n", count)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,749: Line 1,749:


=={{header|J}}==
=={{header|J}}==
<lang J> (#~ 1 p: [:+/@|: 10&#.inv) i.&.(p:inv) 500
<syntaxhighlight lang=J> (#~ 1 p: [:+/@|: 10&#.inv) i.&.(p:inv) 500
2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487</lang>
2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487</syntaxhighlight>
=={{header|Java}}==
=={{header|Java}}==
<lang Java>public class additivePrimes {
<syntaxhighlight lang=Java>public class additivePrimes {


public static void main(String[] args) {
public static void main(String[] args) {
Line 1,789: Line 1,789:
}
}
}
}
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,800: Line 1,800:


'''Preliminaries'''
'''Preliminaries'''
<lang jq>def is_prime:
<syntaxhighlight lang=jq>def is_prime:
. as $n
. as $n
| if ($n < 2) then false
| if ($n < 2) then false
Line 1,832: Line 1,832:


def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
</syntaxhighlight>
</lang>
'''The task'''
'''The task'''
<syntaxhighlight lang=jq>
<lang jq>
# Input: a number n
# Input: a number n
# Output: an array of additive primes less than n
# Output: an array of additive primes less than n
Line 1,851: Line 1,851:
| ((nwise(10) | map(lpad(4)) | join(" ")),
| ((nwise(10) | map(lpad(4)) | join(" ")),
"\n\(length) additive primes found."))
"\n\(length) additive primes found."))
</lang>
</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,867: Line 1,867:
=={{header|Haskell}}==
=={{header|Haskell}}==
Naive solution which doesn't rely on advanced number theoretic libraries.
Naive solution which doesn't rely on advanced number theoretic libraries.
<lang haskell>import Data.List (unfoldr)
<syntaxhighlight lang=haskell>import Data.List (unfoldr)


-- infinite list of primes
-- infinite list of primes
Line 1,884: Line 1,884:
-- test for an additive prime
-- test for an additive prime
isAdditivePrime n = isPrime n && (isPrime . sum . digits) n
isAdditivePrime n = isPrime n && (isPrime . sum . digits) n
</syntaxhighlight>
</lang>


The task
The task
Line 1,903: Line 1,903:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Primes
<syntaxhighlight lang=julia>using Primes


let
let
Line 1,917: Line 1,917:
println("\n\n$pcount additive primes found.")
println("\n\n$pcount additive primes found.")
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Erdős primes under 500:
Erdős primes under 500:
Line 1,929: Line 1,929:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Python}}
{{trans|Python}}
<lang kotlin>fun isPrime(n: Int): Boolean {
<syntaxhighlight lang=kotlin>fun isPrime(n: Int): Boolean {
if (n <= 3) return n > 1
if (n <= 3) return n > 1
if (n % 2 == 0 || n % 3 == 0) return false
if (n % 2 == 0 || n % 3 == 0) return false
Line 1,959: Line 1,959:
}
}
println("\nFound $additivePrimes additive primes less than 500")
println("\nFound $additivePrimes additive primes less than 500")
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487
<pre>2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487
Line 1,965: Line 1,965:


=={{header|Ksh}}==
=={{header|Ksh}}==
<lang ksh>#!/bin/ksh
<syntaxhighlight lang=ksh>#!/bin/ksh


# Prime numbers for which the sum of their decimal digits are also primes
# Prime numbers for which the sum of their decimal digits are also primes
Line 2,011: Line 2,011:
_isprime ${digsum} ; (( $? )) && printf "%4d " ${i}
_isprime ${digsum} ; (( $? )) && printf "%4d " ${i}
done
done
print</lang>
print</syntaxhighlight>
{{out}}
{{out}}
<pre> 2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487 </pre>
<pre> 2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487 </pre>
Line 2,018: Line 2,018:
=={{header|langur}}==
=={{header|langur}}==
{{works with|langur|0.10}}
{{works with|langur|0.10}}
<lang langur>val .isPrime = f .i == 2 or .i > 2 and not any f(.x) .i div .x, pseries 2 to .i ^/ 2
<syntaxhighlight lang=langur>val .isPrime = f .i == 2 or .i > 2 and not any f(.x) .i div .x, pseries 2 to .i ^/ 2


val .sumDigits = f fold f{+}, s2n toString .i
val .sumDigits = f fold f{+}, s2n toString .i
Line 2,035: Line 2,035:


writeln $"\n\n\.count; additive primes found.\n"
writeln $"\n\n\.count; additive primes found.\n"
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,051: Line 2,051:
=={{header|Lua}}==
=={{header|Lua}}==
This task uses <code>primegen</code> from: [[Extensible_prime_generator#Lua]]
This task uses <code>primegen</code> from: [[Extensible_prime_generator#Lua]]
<lang lua>function sumdigits(n)
<syntaxhighlight lang=lua>function sumdigits(n)
local sum = 0
local sum = 0
while n > 0 do
while n > 0 do
Line 2,063: Line 2,063:
aprimes = primegen:filter(function(n) return primegen.tbd(sumdigits(n)) end)
aprimes = primegen:filter(function(n) return primegen.tbd(sumdigits(n)) end)
print(table.concat(aprimes, " "))
print(table.concat(aprimes, " "))
print("Count:", #aprimes)</lang>
print("Count:", #aprimes)</syntaxhighlight>
{{out}}
{{out}}
<pre>2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487
<pre>2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487
Line 2,069: Line 2,069:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[AdditivePrimeQ]
<syntaxhighlight lang=Mathematica>ClearAll[AdditivePrimeQ]
AdditivePrimeQ[n_Integer] := PrimeQ[n] \[And] PrimeQ[Total[IntegerDigits[n]]]
AdditivePrimeQ[n_Integer] := PrimeQ[n] \[And] PrimeQ[Total[IntegerDigits[n]]]
Select[Range[500], AdditivePrimeQ]</lang>
Select[Range[500], AdditivePrimeQ]</syntaxhighlight>
{{out}}
{{out}}
<pre>{2,3,5,7,11,23,29,41,43,47,61,67,83,89,101,113,131,137,139,151,157,173,179,191,193,197,199,223,227,229,241,263,269,281,283,311,313,317,331,337,353,359,373,379,397,401,409,421,443,449,461,463,467,487}</pre>
<pre>{2,3,5,7,11,23,29,41,43,47,61,67,83,89,101,113,131,137,139,151,157,173,179,191,193,197,199,223,227,229,241,263,269,281,283,311,313,317,331,337,353,359,373,379,397,401,409,421,443,449,461,463,467,487}</pre>


=={{header|Modula-2}}==
=={{header|Modula-2}}==
<lang modula2>MODULE AdditivePrimes;
<syntaxhighlight lang=modula2>MODULE AdditivePrimes;
FROM InOut IMPORT WriteString, WriteCard, WriteLn;
FROM InOut IMPORT WriteString, WriteCard, WriteLn;


Line 2,129: Line 2,129:
WriteString('.');
WriteString('.');
WriteLn();
WriteLn();
END AdditivePrimes.</lang>
END AdditivePrimes.</syntaxhighlight>
{{out}}
{{out}}
<pre> 2 3 5 7 11 23 29 41 43 47
<pre> 2 3 5 7 11 23 29 41 43 47
Line 2,140: Line 2,140:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import math, strutils
<syntaxhighlight lang=Nim>import math, strutils


const N = 499
const N = 499
Line 2,170: Line 2,170:
echo()
echo()


echo "\nNumber of additive primes found: ", count</lang>
echo "\nNumber of additive primes found: ", count</syntaxhighlight>


{{out}}
{{out}}
Line 2,185: Line 2,185:
=={{header|Pari/GP}}==
=={{header|Pari/GP}}==
This is a good task for demonstrating several different ways to approach a simple problem.
This is a good task for demonstrating several different ways to approach a simple problem.
<lang parigp>hasPrimeDigitsum(n)=isprime(sumdigits(n)); \\ see A028834 in the OEIS
<syntaxhighlight lang=parigp>hasPrimeDigitsum(n)=isprime(sumdigits(n)); \\ see A028834 in the OEIS


v1 = select(isprime, select(hasPrimeDigitsum, [1..499]));
v1 = select(isprime, select(hasPrimeDigitsum, [1..499]));
Line 2,192: Line 2,192:


s=0; forprime(p=2,499, if(hasPrimeDigitsum(p), s++)); s;
s=0; forprime(p=2,499, if(hasPrimeDigitsum(p), s++)); s;
[#v1, #v2, #v3, s]</lang>
[#v1, #v2, #v3, s]</syntaxhighlight>
{{out}}
{{out}}
<pre>%1 = [54, 54, 54, 54]</pre>
<pre>%1 = [54, 54, 54, 54]</pre>
=={{header|Pascal}}==
=={{header|Pascal}}==
{{works with|Free Pascal}}{{works with|Delphi}} checking isPrime(sum of digits) before testimg isprime(num) improves speed.<br>Tried to speed up calculation of sum of digits.
{{works with|Free Pascal}}{{works with|Delphi}} checking isPrime(sum of digits) before testimg isprime(num) improves speed.<br>Tried to speed up calculation of sum of digits.
<lang pascal>program AdditivePrimes;
<syntaxhighlight lang=pascal>program AdditivePrimes;
{$IFDEF FPC}
{$IFDEF FPC}
{$MODE DELPHI}{$CODEALIGN proc=16}
{$MODE DELPHI}{$CODEALIGN proc=16}
Line 2,345: Line 2,345:
writeln(cnt, ' additive primes found.');
writeln(cnt, ' additive primes found.');
END.
END.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,360: Line 2,360:
=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang=perl>use strict;
use warnings;
use warnings;
use ntheory 'is_prime';
use ntheory 'is_prime';
Line 2,374: Line 2,374:
is_prime($_) and is_prime(sum(split '',$_)) and push @ap, $_ for 1..$limit;
is_prime($_) and is_prime(sum(split '',$_)) and push @ap, $_ for 1..$limit;


print @ap . " additive primes < $limit:\n" . pp(@ap);</lang>
print @ap . " additive primes < $limit:\n" . pp(@ap);</syntaxhighlight>
{{out}}
{{out}}
<pre>54 additive primes < 500:
<pre>54 additive primes < 500:
Line 2,383: Line 2,383:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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: #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: #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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,395: Line 2,395:


=={{header|Phixmonti}}==
=={{header|Phixmonti}}==
<lang Phixmonti>/# Rosetta Code problem: http://rosettacode.org/wiki/Additive_primes
<syntaxhighlight lang=Phixmonti>/# Rosetta Code problem: http://rosettacode.org/wiki/Additive_primes
by Galileo, 05/2022 #/
by Galileo, 05/2022 #/


Line 2,421: Line 2,421:


"Additive primes found: " print print
"Additive primes found: " print print
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487 Additive primes found: 54
<pre>2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487 Additive primes found: 54
Line 2,427: Line 2,427:


=={{header|Picat}}==
=={{header|Picat}}==
<lang Picat>main =>
<syntaxhighlight lang=Picat>main =>
PCount = 0,
PCount = 0,
foreach (I in 2..499)
foreach (I in 2..499)
Line 2,439: Line 2,439:
sum_digits(N) = S =>
sum_digits(N) = S =>
S = sum([ord(C)-ord('0') : C in to_string(N)]).
S = sum([ord(C)-ord('0') : C in to_string(N)]).
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,449: Line 2,449:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(de prime? (N)
<syntaxhighlight lang=PicoLisp>(de prime? (N)
(let D 0
(let D 0
(or
(or
Line 2,469: Line 2,469:
(inc 'C) ) )
(inc 'C) ) )
(prinl)
(prinl)
(prinl "Total count: " C) )</lang>
(prinl "Total count: " C) )</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,477: Line 2,477:


=={{header|PILOT}}==
=={{header|PILOT}}==
<lang pilot>C :z=2
<syntaxhighlight lang=pilot>C :z=2
:c=0
:c=0
:max=500
:max=500
Line 2,519: Line 2,519:
:i=j
:i=j
J (i>0):*digit
J (i>0):*digit
E :</lang>
E :</syntaxhighlight>
{{out}}
{{out}}
<pre style='height:50ex;'>2
<pre style='height:50ex;'>2
Line 2,589: Line 2,589:
The PL/I include file "pg.inc" can be found on the [[Polyglot:PL/I and PL/M]] page.
The PL/I include file "pg.inc" can be found on the [[Polyglot:PL/I and PL/M]] page.
Note the use of text in column 81 onwards to hide the PL/I specifics from the PL/M compiler.
Note the use of text in column 81 onwards to hide the PL/I specifics from the PL/M compiler.
<lang pli>/* FIND ADDITIVE PRIMES - PRIMES WHOSE DIGIT SUM IS ALSO PRIME */
<syntaxhighlight lang=pli>/* FIND ADDITIVE PRIMES - PRIMES WHOSE DIGIT SUM IS ALSO PRIME */
additive_primes_100H: procedure options (main);
additive_primes_100H: procedure options (main);


Line 2,676: Line 2,676:
CALL PRNL;
CALL PRNL;


EOF: end additive_primes_100H;</lang>
EOF: end additive_primes_100H;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,688: Line 2,688:


=={{header|Processing}}==
=={{header|Processing}}==
<lang processing>IntList primes = new IntList();
<syntaxhighlight lang=processing>IntList primes = new IntList();


void setup() {
void setup() {
Line 2,723: Line 2,723:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487
<pre>2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487
Line 2,729: Line 2,729:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>#MAX=500
<syntaxhighlight lang=PureBasic>#MAX=500
Global Dim P.b(#MAX) : FillMemory(@P(),#MAX,1,#PB_Byte)
Global Dim P.b(#MAX) : FillMemory(@P(),#MAX,1,#PB_Byte)
If OpenConsole()=0 : End 1 : EndIf
If OpenConsole()=0 : End 1 : EndIf
Line 2,743: Line 2,743:
Next
Next
PrintN(~"\n\n"+Str(c)+" additive primes below 500.")
PrintN(~"\n\n"+Str(c)+" additive primes below 500.")
Input()</lang>
Input()</syntaxhighlight>
{{out}}
{{out}}
<pre> 2 3 5 7 11 23 29 41 43 47
<pre> 2 3 5 7 11 23 29 41 43 47
Line 2,755: Line 2,755:


=={{header|Python}}==
=={{header|Python}}==
<lang Python>def is_prime(n: int) -> bool:
<syntaxhighlight lang=Python>def is_prime(n: int) -> bool:
if n <= 3:
if n <= 3:
return n > 1
return n > 1
Line 2,783: Line 2,783:


if __name__ == "__main__":
if __name__ == "__main__":
main()</lang>
main()</syntaxhighlight>
{{out}}
{{out}}
<pre>2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487
<pre>2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487
Line 2,794: Line 2,794:
<code>digitsum</code> is defined at [[Sum digits of an integer#Quackery]].
<code>digitsum</code> is defined at [[Sum digits of an integer#Quackery]].


<lang Quackery> 500 eratosthenes
<syntaxhighlight lang=Quackery> 500 eratosthenes
[]
[]
Line 2,803: Line 2,803:
[ i^ join ] ] ]
[ i^ join ] ] ]
dup echo cr cr
dup echo cr cr
size echo say " additive primes found."</lang>
size echo say " additive primes found."</syntaxhighlight>


{{out}}
{{out}}
Line 2,814: Line 2,814:
=={{header|Racket}}==
=={{header|Racket}}==


<lang racket>#lang racket
<syntaxhighlight lang=racket>#lang racket


(require math/number-theory)
(require math/number-theory)
Line 2,827: Line 2,827:
(define additive-primes<500 (filter additive-prime? (range 1 500)))
(define additive-primes<500 (filter additive-prime? (range 1 500)))
(printf "There are ~a additive primes < 500~%" (length additive-primes<500))
(printf "There are ~a additive primes < 500~%" (length additive-primes<500))
(printf "They are: ~a~%" additive-primes<500)</lang>
(printf "They are: ~a~%" additive-primes<500)</syntaxhighlight>


{{out}}
{{out}}
Line 2,835: Line 2,835:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>unit sub MAIN ($limit = 500);
<syntaxhighlight lang=perl6>unit sub MAIN ($limit = 500);
say "{+$_} additive primes < $limit:\n{$_».fmt("%" ~ $limit.chars ~ "d").batch(10).join("\n")}",
say "{+$_} additive primes < $limit:\n{$_».fmt("%" ~ $limit.chars ~ "d").batch(10).join("\n")}",
with ^$limit .grep: { .is-prime and .comb.sum.is-prime }</lang>
with ^$limit .grep: { .is-prime and .comb.sum.is-prime }</syntaxhighlight>
{{out}}
{{out}}
<pre>54 additive primes < 500:
<pre>54 additive primes < 500:
Line 2,848: Line 2,848:


=={{header|Red}}==
=={{header|Red}}==
<lang Red>
<syntaxhighlight lang=Red>
cross-sum: function [n][out: 0 foreach m form n [out: out + to-integer to-string m]]
cross-sum: function [n][out: 0 foreach m form n [out: out + to-integer to-string m]]
additive-primes: function [n][collect [foreach p ps: primes n [if find ps cross-sum p [keep p]]]]
additive-primes: function [n][collect [foreach p ps: primes n [if find ps cross-sum p [keep p]]]]
Line 2,862: Line 2,862:
]
]
== 54
== 54
</syntaxhighlight>
</lang>
Uses <code>primes</code> defined in https://rosettacode.org/wiki/Sieve_of_Eratosthenes#Red.
Uses <code>primes</code> defined in https://rosettacode.org/wiki/Sieve_of_Eratosthenes#Red.


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program counts/displays the number of additive primes under a specified number N.*/
<syntaxhighlight lang=rexx>/*REXX program counts/displays the number of additive primes under a specified number N.*/
parse arg n cols . /*get optional number of primes to find*/
parse arg n cols . /*get optional number of primes to find*/
if n=='' | n=="," then n= 500 /*Not specified? Then assume default.*/
if n=='' | n=="," then n= 500 /*Not specified? Then assume default.*/
Line 2,909: Line 2,909:
end /*k*/ /* [↑] only divide up to √ J */
end /*k*/ /* [↑] only divide up to √ J */
#= # + 1; @.#= j; sq.#= j*j; !.j= 1 /*bump prime count; assign prime & flag*/
#= # + 1; @.#= j; sq.#= j*j; !.j= 1 /*bump prime count; assign prime & flag*/
end /*j*/; return</lang>
end /*j*/; return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 2,926: Line 2,926:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang=ring>
load "stdlib.ring"
load "stdlib.ring"


Line 2,954: Line 2,954:
see nl + "found " + row + " additive primes." + nl
see nl + "found " + row + " additive primes." + nl
see "done..." + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,969: Line 2,969:
</pre>
</pre>
=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>require "prime"
<syntaxhighlight lang=ruby>require "prime"


additive_primes = Prime.lazy.select{|prime| prime.digits.sum.prime? }
additive_primes = Prime.lazy.select{|prime| prime.digits.sum.prime? }
Line 2,977: Line 2,977:
puts res.join(" ")
puts res.join(" ")
puts "\n#{res.size} additive primes below #{N}."
puts "\n#{res.size} additive primes below #{N}."
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487
<pre>2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487
Line 2,988: Line 2,988:


===Flat implementation===
===Flat implementation===
<lang fsharp>fn main() {
<syntaxhighlight lang=fsharp>fn main() {
let limit = 500;
let limit = 500;
let column_w = limit.to_string().len() + 1;
let column_w = limit.to_string().len() + 1;
Line 3,004: Line 3,004:
}
}
println!("\n---\nFound {count} additive primes less than {limit}");
println!("\n---\nFound {count} additive primes less than {limit}");
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,020: Line 3,020:
primal implements the sieve of Eratosthenes with optimizations (10+ times faster for large limits)
primal implements the sieve of Eratosthenes with optimizations (10+ times faster for large limits)


<lang fsharp>// [dependencies]
<syntaxhighlight lang=fsharp>// [dependencies]
// primal = "0.3.0"
// primal = "0.3.0"


Line 3,038: Line 3,038:
.count();
.count();
println!("\n---\nFound {count} additive primes less than {limit}");
println!("\n---\nFound {count} additive primes less than {limit}");
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,053: Line 3,053:


=={{header|Sage}}==
=={{header|Sage}}==
<lang SageMath>
<syntaxhighlight lang=SageMath>
limit = 500
limit = 500
additivePrimes = list(filter(lambda x: x > 0,
additivePrimes = list(filter(lambda x: x > 0,
Line 3,059: Line 3,059:
list(map(str,list(primes(1,limit))))))))
list(map(str,list(primes(1,limit))))))))
print(f"{additivePrimes}\nFound {len(additivePrimes)} additive primes less than {limit}")
print(f"{additivePrimes}\nFound {len(additivePrimes)} additive primes less than {limit}")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 3,066: Line 3,066:
</pre>
</pre>
=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i";


const func boolean: isPrime (in integer: number) is func
const func boolean: isPrime (in integer: number) is func
Line 3,111: Line 3,111:
end for;
end for;
writeln("\nFound " <& count <& " additive primes < 500.");
writeln("\nFound " <& count <& " additive primes < 500.");
end func;</lang>
end func;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,125: Line 3,125:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func additive_primes(upto, base = 10) {
<syntaxhighlight lang=ruby>func additive_primes(upto, base = 10) {
upto.primes.grep { .sumdigits(base).is_prime }
upto.primes.grep { .sumdigits(base).is_prime }
}
}
Line 3,131: Line 3,131:
additive_primes(500).each_slice(10, {|*a|
additive_primes(500).each_slice(10, {|*a|
a.map { '%3s' % _ }.join(' ').say
a.map { '%3s' % _ }.join(' ').say
})</lang>
})</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,143: Line 3,143:


=={{header|TSE SAL}}==
=={{header|TSE SAL}}==
<lang TSESAL>
<syntaxhighlight lang=TSESAL>


INTEGER PROC FNMathGetSquareRootI( INTEGER xI )
INTEGER PROC FNMathGetSquareRootI( INTEGER xI )
Line 3,253: Line 3,253:
END
END


</syntaxhighlight>
</lang>


{{out}} <pre>
{{out}} <pre>
Line 3,315: Line 3,315:


=={{header|Swift}}==
=={{header|Swift}}==
<lang swift>import Foundation
<syntaxhighlight lang=swift>import Foundation


func isPrime(_ n: Int) -> Bool {
func isPrime(_ n: Int) -> Bool {
Line 3,360: Line 3,360:
}
}
}
}
print("\n\(count) additive primes found.")</lang>
print("\n\(count) additive primes found.")</syntaxhighlight>


{{out}}
{{out}}
Line 3,410: Line 3,410:
a@ = a@ / 10
a@ = a@ / 10
loop
loop
return (b@)</lang>
return (b@)</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Prime Digit Sum
<pre>Prime Digit Sum
Line 3,471: Line 3,471:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|go}}
{{trans|go}}
<lang vlang>fn is_prime(n int) bool {
<syntaxhighlight lang=vlang>fn is_prime(n int) bool {
if n < 2 {
if n < 2 {
return false
return false
Line 3,526: Line 3,526:
}
}
println("\n\n$count additive primes found.")
println("\n\n$count additive primes found.")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,542: Line 3,542:


=={{header|VTL-2}}==
=={{header|VTL-2}}==
<lang VTL2>10 M=499
<syntaxhighlight lang=VTL2>10 M=499
20 :1)=1
20 :1)=1
30 P=2
30 P=2
Line 3,575: Line 3,575:
330 ?=N
330 ?=N
340 ?=" additive primes below ";
340 ?=" additive primes below ";
350 ?=M+1</lang>
350 ?=M+1</syntaxhighlight>
{{out}}
{{out}}
<pre>2 3 5 7 11 23 29 41 43 47
<pre>2 3 5 7 11 23 29 41 43 47
Line 3,588: Line 3,588:
{{libheader|Wren-math}}
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/math" for Int
<syntaxhighlight lang=ecmascript>import "/math" for Int
import "/fmt" for Fmt
import "/fmt" for Fmt


Line 3,610: Line 3,610:
}
}
}
}
System.print("\n\n%(count) additive primes found.")</lang>
System.print("\n\n%(count) additive primes found.")</syntaxhighlight>


{{out}}
{{out}}
Line 3,626: Line 3,626:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func IsPrime(N); \Return 'true' if N is a prime number
<syntaxhighlight lang=XPL0>func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
int N, I;
[if N <= 1 then return false;
[if N <= 1 then return false;
Line 3,655: Line 3,655:
Text(0, " additive primes found below 500.
Text(0, " additive primes found below 500.
");
");
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 3,669: Line 3,669:


=={{header|Yabasic}}==
=={{header|Yabasic}}==
<lang Yabasic>// Rosetta Code problem: http://rosettacode.org/wiki/Additive_primes
<syntaxhighlight lang=Yabasic>// Rosetta Code problem: http://rosettacode.org/wiki/Additive_primes
// by Galileo, 06/2022
// by Galileo, 06/2022


Line 3,696: Line 3,696:
next
next


print "\nFound: ", count</lang>
print "\nFound: ", count</syntaxhighlight>
{{out}}
{{out}}
<pre>2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487
<pre>2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151 157 173 179 191 193 197 199 223 227 229 241 263 269 281 283 311 313 317 331 337 353 359 373 379 397 401 409 421 443 449 461 463 467 487