Additive primes: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|langur}}: added langur rev. no.)
m (syntax highlighting fixup automation)
Line 21:
{{trans|Python}}
 
<langsyntaxhighlight lang=11l>F is_prime(a)
I a == 2
R 1B
Line 43:
additive_primes++
print(i, end' ‘ ’)
print("\nFound "additive_primes‘ additive primes less than 500’)</langsyntaxhighlight>
 
{{out}}
Line 52:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<langsyntaxhighlight lang=AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program additivePrime64.s */
Line 202:
.include "../includeARM64.inc"
 
</syntaxhighlight>
</lang>
<pre>
Prime : 2
Line 261:
</pre>
=={{header|Ada}}==
<langsyntaxhighlight lang=Ada>with Ada.Text_Io;
 
procedure Additive_Primes is
Line 318:
Put (" additive primes.");
New_Line;
end Additive_Primes;</langsyntaxhighlight>
{{out}}
<pre>Additive primes <500:
Line 330:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang=algol68>BEGIN # find additive primes - primes whose digit sum is also prime #
# sieve the primes to max prime #
PR read "primes.incl.a68" PR
Line 353:
OD;
print( ( newline, "Found ", whole( additive count, 0 ), " additive primes below ", whole( UPB prime + 1, 0 ), newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 363:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight 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 %
procedure Eratosthenes ( logical array p( * ) ; integer value n ) ;
Line 402:
write( i_w := 1, s_w := 0, "Found ", aCount, " additive primes below ", MAX_NUMBER )
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 413:
=={{header|APL}}==
 
<langsyntaxhighlight lang=APL>((+⌿(4/10)⊤P)∊P)/P←(~P∊P∘.×P)/P←1↓⍳500</langsyntaxhighlight>
 
{{out}}
Line 421:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang=applescript>on sieveOfEratosthenes(limit)
script o
property numberList : {missing value}
Line 466:
 
-- Task code:
tell additivePrimes(499) to return {|additivePrimes<500|:it, numberThereof:count}</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight 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}</langsyntaxhighlight>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<langsyntaxhighlight lang=ARM Assembly>
/* ARM assembly Raspberry PI */
/* program additivePrime.s */
Line 618:
.include "../affichage.inc"
 
</syntaxhighlight>
</lang>
<pre>
Prime : 2
Line 678:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang=rebol>additives: select 2..500 'x -> and? prime? x prime? sum digits x
 
loop split.every:10 additives 'a ->
print map a => [pad to :string & 4]
 
print ["\nFound" size additives "additive primes up to 500"]</langsyntaxhighlight>
 
{{out}}
Line 697:
 
=={{header|AWK}}==
<langsyntaxhighlight lang=AWK>
# syntax: GAWK -f ADDITIVE_PRIMES.AWK
BEGIN {
Line 727:
return(sum)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 740:
 
=={{header|BASIC}}==
<langsyntaxhighlight lang=basic>10 DEFINT A-Z: E=500
20 DIM P(E): P(0)=-1: P(1)=-1
30 FOR I=2 TO SQR(E)
Line 750:
90 IF NOT P(S) THEN N=N+1: PRINT I,
100 NEXT
110 PRINT: PRINT N;" additive primes found below ";E</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 11
Line 765:
54 additive primes found below 500</pre>
==={{header|Applesoft BASIC}}===
<langsyntaxhighlight 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
2 NEXT I: FOR I = B TO F: IF NOT P(I) THEN GOSUB 4
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
5 IF NOT P(S) THEN N = N + 1: PRINT RIGHT$ (S$ + STR$ (I),L);
6 RETURN</langsyntaxhighlight>
<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 778:
 
=={{header|BASIC256}}==
<langsyntaxhighlight lang=freebasic>print "Prime", "Digit Sum"
for i = 2 to 499
if isprime(i) then
Line 805:
end while
return s
end function</langsyntaxhighlight>
 
=={{header|BCPL}}==
<langsyntaxhighlight lang=bcpl>get "libhdr"
manifest $( limit = 500 $)
 
Line 842:
$)
writef("*N*NFound %N additive primes < %N.*N", num, limit)
$)</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 11 23 29 41 43 47
Line 854:
 
=={{header|C}}==
<syntaxhighlight lang=C>
<lang C>
#include <stdbool.h>
#include <stdio.h>
Line 928:
return 0;
}/* main */
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 942:
 
=={{header|C++}}==
<langsyntaxhighlight lang=cpp>#include <iomanip>
#include <iostream>
 
Line 983:
}
std::cout << '\n' << count << " additive primes found.\n";
}</langsyntaxhighlight>
 
{{out}}
Line 999:
 
=={{header|CLU}}==
<langsyntaxhighlight lang=clu>% Sieve of Erastothenes
% Returns an array [1..max] marking the primes
sieve = proc (max: int) returns (array[bool])
Line 1,041:
stream$putl(po, "\nFound " || int$unparse(count) ||
" additive primes < " || int$unparse(max))
end start_up</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 11 23 29 41 43 47
Line 1,052:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang=cobol> IDENTIFICATION DIVISION.
PROGRAM-ID. ADDITIVE-PRIMES.
Line 1,124:
SIEVE-INNER-LOOP.
MOVE 'X' TO COMPOSITE-FLAG(SIEVE-COMP).</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 11 23 29 41 43 47
Line 1,136:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang=lisp>
(defun sum-of-digits (n)
"Return the sum of the digits of a number"
Line 1,158:
 
 
</syntaxhighlight>
</lang>
{{out}}<pre>(dotimes (i 500) (when (additive-primep i) (princ i) (princ " ")))
 
Line 1,165:
 
=={{header|Crystal}}==
<langsyntaxhighlight lang=ruby># Fast/simple way to generate primes for small values.
# Uses P3 Prime Generator (PG) and its Prime Generator Sequence (PGS).
 
Line 1,203:
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}."
</syntaxhighlight>
</lang>
{{out}}
<pre> 2 3 5 7 11 23 29 41 43 47
Line 1,255:
 
=={{header|Draco}}==
<langsyntaxhighlight lang=draco>proc sieve([*] bool prime) void:
word max, p, c;
max := dim(prime,1)-1;
Line 1,294:
writeln();
writeln("There are ", n, " additive primes below ", MAX)
corp</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 11 23 29 41 43 47 61 67 83 89 101 113 131 137 139 151
Line 1,302:
 
=={{header|Erlang}}==
<langsyntaxhighlight lang=Erlang>
main(_) ->
AddPrimes = [N || N <- lists:seq(2,500), isprime(N) andalso isprime(digitsum(N))],
Line 1,319:
digitsum(0, S) -> S;
digitsum(N, S) -> digitsum(N div 10, S + N rem 10).
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,332:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang=fsharp>
// 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)
primes32()|>Seq.takeWhile((>)500)|>Seq.filter(fN 0>>isPrime)|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,344:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang=factor>USING: formatting grouping io kernel math math.primes
prettyprint sequences ;
 
Line 1,352:
499 primes-upto [ sum-digits prime? ] filter
[ 9 group simple-table. nl ]
[ length "Found %d additive primes < 500.\n" printf ] bi</langsyntaxhighlight>
{{out}}
<pre>
Line 1,366:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang=fermat>Function Digsum(n) =
digsum := 0;
while n>0 do
Line 1,383:
fi od;
 
!!('There were ',nadd);</langsyntaxhighlight>
{{out}}<pre>
Additive primes below 500 are
Line 1,444:
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang=forth>: prime? ( n -- ? ) here + c@ 0= ;
: notprime! ( n -- ) here + 1 swap c! ;
 
Line 1,483:
 
500 print_additive_primes
bye</langsyntaxhighlight>
 
{{out}}
Line 1,499:
=={{header|FreeBASIC}}==
As with the other special primes tasks, use one of the primality testing algorithms as an include.
<langsyntaxhighlight lang=freebasic>#include "isprime.bas"
 
function digsum( n as uinteger ) as uinteger
Line 1,520:
end if
end if
next i</langsyntaxhighlight>
{{out}}
<pre style="height:16em">Prime Digit Sum
Line 1,582:
then go through the list, sum digits and check for prime
 
<langsyntaxhighlight lang=pascal>
Program AdditivePrimes;
Const max_number = 500;
Line 1,644:
writeln(counter,' additive primes found.');
End.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,658:
 
=={{header|Frink}}==
<langsyntaxhighlight lang=frink>vals = toArray[select[primes[2, 500], {|x| isPrime[sum[integerDigits[x]]]}]]
println[formatTable[columnize[vals, 10]]]
println["\n" + length[vals] + " values found."]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,674:
 
=={{header|Go}}==
<langsyntaxhighlight lang=go>package main
 
import "fmt"
Line 1,733:
}
fmt.Printf("\n\n%d additive primes found.\n", count)
}</langsyntaxhighlight>
 
{{out}}
Line 1,749:
 
=={{header|J}}==
<langsyntaxhighlight 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</langsyntaxhighlight>
=={{header|Java}}==
<langsyntaxhighlight lang=Java>public class additivePrimes {
 
public static void main(String[] args) {
Line 1,789:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,800:
 
'''Preliminaries'''
<langsyntaxhighlight lang=jq>def is_prime:
. as $n
| if ($n < 2) then false
Line 1,832:
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
</syntaxhighlight>
</lang>
'''The task'''
<syntaxhighlight lang=jq>
<lang jq>
# Input: a number n
# Output: an array of additive primes less than n
Line 1,851:
| ((nwise(10) | map(lpad(4)) | join(" ")),
"\n\(length) additive primes found."))
</langsyntaxhighlight>
{{out}}
<pre>
Line 1,867:
=={{header|Haskell}}==
Naive solution which doesn't rely on advanced number theoretic libraries.
<langsyntaxhighlight lang=haskell>import Data.List (unfoldr)
 
-- infinite list of primes
Line 1,884:
-- test for an additive prime
isAdditivePrime n = isPrime n && (isPrime . sum . digits) n
</syntaxhighlight>
</lang>
 
The task
Line 1,903:
 
=={{header|Julia}}==
<langsyntaxhighlight lang=julia>using Primes
 
let
Line 1,917:
println("\n\n$pcount additive primes found.")
end
</langsyntaxhighlight>{{out}}
<pre>
Erdős primes under 500:
Line 1,929:
=={{header|Kotlin}}==
{{trans|Python}}
<langsyntaxhighlight lang=kotlin>fun isPrime(n: Int): Boolean {
if (n <= 3) return n > 1
if (n % 2 == 0 || n % 3 == 0) return false
Line 1,959:
}
println("\nFound $additivePrimes additive primes less than 500")
}</langsyntaxhighlight>
{{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
Line 1,965:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang=ksh>#!/bin/ksh
 
# Prime numbers for which the sum of their decimal digits are also primes
Line 2,011:
_isprime ${digsum} ; (( $? )) && printf "%4d " ${i}
done
print</langsyntaxhighlight>
{{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>
Line 2,018:
=={{header|langur}}==
{{works with|langur|0.10}}
<langsyntaxhighlight 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
Line 2,035:
 
writeln $"\n\n\.count; additive primes found.\n"
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,051:
=={{header|Lua}}==
This task uses <code>primegen</code> from: [[Extensible_prime_generator#Lua]]
<langsyntaxhighlight lang=lua>function sumdigits(n)
local sum = 0
while n > 0 do
Line 2,063:
aprimes = primegen:filter(function(n) return primegen.tbd(sumdigits(n)) end)
print(table.concat(aprimes, " "))
print("Count:", #aprimes)</langsyntaxhighlight>
{{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
Line 2,069:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight lang=Mathematica>ClearAll[AdditivePrimeQ]
AdditivePrimeQ[n_Integer] := PrimeQ[n] \[And] PrimeQ[Total[IntegerDigits[n]]]
Select[Range[500], AdditivePrimeQ]</langsyntaxhighlight>
{{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>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang=modula2>MODULE AdditivePrimes;
FROM InOut IMPORT WriteString, WriteCard, WriteLn;
 
Line 2,129:
WriteString('.');
WriteLn();
END AdditivePrimes.</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 11 23 29 41 43 47
Line 2,140:
 
=={{header|Nim}}==
<langsyntaxhighlight lang=Nim>import math, strutils
 
const N = 499
Line 2,170:
echo()
 
echo "\nNumber of additive primes found: ", count</langsyntaxhighlight>
 
{{out}}
Line 2,185:
=={{header|Pari/GP}}==
This is a good task for demonstrating several different ways to approach a simple problem.
<langsyntaxhighlight lang=parigp>hasPrimeDigitsum(n)=isprime(sumdigits(n)); \\ see A028834 in the OEIS
 
v1 = select(isprime, select(hasPrimeDigitsum, [1..499]));
Line 2,192:
 
s=0; forprime(p=2,499, if(hasPrimeDigitsum(p), s++)); s;
[#v1, #v2, #v3, s]</langsyntaxhighlight>
{{out}}
<pre>%1 = [54, 54, 54, 54]</pre>
=={{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.
<langsyntaxhighlight lang=pascal>program AdditivePrimes;
{$IFDEF FPC}
{$MODE DELPHI}{$CODEALIGN proc=16}
Line 2,345:
writeln(cnt, ' additive primes found.');
END.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,360:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang=perl>use strict;
use warnings;
use ntheory 'is_prime';
Line 2,374:
is_prime($_) and is_prime(sum(split '',$_)) and push @ap, $_ for 1..$limit;
 
print @ap . " additive primes < $limit:\n" . pp(@ap);</langsyntaxhighlight>
{{out}}
<pre>54 additive primes < 500:
Line 2,383:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight lang=Phix>(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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,395:
 
=={{header|Phixmonti}}==
<langsyntaxhighlight lang=Phixmonti>/# Rosetta Code problem: http://rosettacode.org/wiki/Additive_primes
by Galileo, 05/2022 #/
 
Line 2,421:
 
"Additive primes found: " print print
</syntaxhighlight>
</lang>
{{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
Line 2,427:
 
=={{header|Picat}}==
<langsyntaxhighlight lang=Picat>main =>
PCount = 0,
foreach (I in 2..499)
Line 2,439:
sum_digits(N) = S =>
S = sum([ord(C)-ord('0') : C in to_string(N)]).
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,449:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight lang=PicoLisp>(de prime? (N)
(let D 0
(or
Line 2,469:
(inc 'C) ) )
(prinl)
(prinl "Total count: " C) )</langsyntaxhighlight>
{{out}}
<pre>
Line 2,477:
 
=={{header|PILOT}}==
<langsyntaxhighlight lang=pilot>C :z=2
:c=0
:max=500
Line 2,519:
:i=j
J (i>0):*digit
E :</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'>2
Line 2,589:
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.
<langsyntaxhighlight lang=pli>/* FIND ADDITIVE PRIMES - PRIMES WHOSE DIGIT SUM IS ALSO PRIME */
additive_primes_100H: procedure options (main);
 
Line 2,676:
CALL PRNL;
 
EOF: end additive_primes_100H;</langsyntaxhighlight>
{{out}}
<pre>
Line 2,688:
 
=={{header|Processing}}==
<langsyntaxhighlight lang=processing>IntList primes = new IntList();
 
void setup() {
Line 2,723:
}
}
}</langsyntaxhighlight>
{{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
Line 2,729:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang=PureBasic>#MAX=500
Global Dim P.b(#MAX) : FillMemory(@P(),#MAX,1,#PB_Byte)
If OpenConsole()=0 : End 1 : EndIf
Line 2,743:
Next
PrintN(~"\n\n"+Str(c)+" additive primes below 500.")
Input()</langsyntaxhighlight>
{{out}}
<pre> 2 3 5 7 11 23 29 41 43 47
Line 2,755:
 
=={{header|Python}}==
<langsyntaxhighlight lang=Python>def is_prime(n: int) -> bool:
if n <= 3:
return n > 1
Line 2,783:
 
if __name__ == "__main__":
main()</langsyntaxhighlight>
{{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
Line 2,794:
<code>digitsum</code> is defined at [[Sum digits of an integer#Quackery]].
 
<langsyntaxhighlight lang=Quackery> 500 eratosthenes
[]
Line 2,803:
[ i^ join ] ] ]
dup echo cr cr
size echo say " additive primes found."</langsyntaxhighlight>
 
{{out}}
Line 2,814:
=={{header|Racket}}==
 
<langsyntaxhighlight lang=racket>#lang racket
 
(require math/number-theory)
Line 2,827:
(define additive-primes<500 (filter additive-prime? (range 1 500)))
(printf "There are ~a additive primes < 500~%" (length additive-primes<500))
(printf "They are: ~a~%" additive-primes<500)</langsyntaxhighlight>
 
{{out}}
Line 2,835:
 
=={{header|Raku}}==
<langsyntaxhighlight lang=perl6>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 }</langsyntaxhighlight>
{{out}}
<pre>54 additive primes < 500:
Line 2,848:
 
=={{header|Red}}==
<langsyntaxhighlight lang=Red>
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]]]]
Line 2,862:
]
== 54
</syntaxhighlight>
</lang>
Uses <code>primes</code> defined in https://rosettacode.org/wiki/Sieve_of_Eratosthenes#Red.
 
=={{header|REXX}}==
<langsyntaxhighlight 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*/
if n=='' | n=="," then n= 500 /*Not specified? Then assume default.*/
Line 2,909:
end /*k*/ /* [↑] only divide up to √ J */
#= # + 1; @.#= j; sq.#= j*j; !.j= 1 /*bump prime count; assign prime & flag*/
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,926:
 
=={{header|Ring}}==
<langsyntaxhighlight lang=ring>
load "stdlib.ring"
 
Line 2,954:
see nl + "found " + row + " additive primes." + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,969:
</pre>
=={{header|Ruby}}==
<langsyntaxhighlight lang=ruby>require "prime"
 
additive_primes = Prime.lazy.select{|prime| prime.digits.sum.prime? }
Line 2,977:
puts res.join(" ")
puts "\n#{res.size} additive primes below #{N}."
</syntaxhighlight>
</lang>
{{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
Line 2,988:
 
===Flat implementation===
<langsyntaxhighlight lang=fsharp>fn main() {
let limit = 500;
let column_w = limit.to_string().len() + 1;
Line 3,004:
}
println!("\n---\nFound {count} additive primes less than {limit}");
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,020:
primal implements the sieve of Eratosthenes with optimizations (10+ times faster for large limits)
 
<langsyntaxhighlight lang=fsharp>// [dependencies]
// primal = "0.3.0"
 
Line 3,038:
.count();
println!("\n---\nFound {count} additive primes less than {limit}");
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,053:
 
=={{header|Sage}}==
<langsyntaxhighlight lang=SageMath>
limit = 500
additivePrimes = list(filter(lambda x: x > 0,
Line 3,059:
list(map(str,list(primes(1,limit))))))))
print(f"{additivePrimes}\nFound {len(additivePrimes)} additive primes less than {limit}")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,066:
</pre>
=={{header|Seed7}}==
<langsyntaxhighlight lang=seed7>$ include "seed7_05.s7i";
 
const func boolean: isPrime (in integer: number) is func
Line 3,111:
end for;
writeln("\nFound " <& count <& " additive primes < 500.");
end func;</langsyntaxhighlight>
{{out}}
<pre>
Line 3,125:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang=ruby>func additive_primes(upto, base = 10) {
upto.primes.grep { .sumdigits(base).is_prime }
}
Line 3,131:
additive_primes(500).each_slice(10, {|*a|
a.map { '%3s' % _ }.join(' ').say
})</langsyntaxhighlight>
{{out}}
<pre>
Line 3,143:
 
=={{header|TSE SAL}}==
<langsyntaxhighlight lang=TSESAL>
 
INTEGER PROC FNMathGetSquareRootI( INTEGER xI )
Line 3,253:
END
 
</syntaxhighlight>
</lang>
 
{{out}} <pre>
Line 3,315:
 
=={{header|Swift}}==
<langsyntaxhighlight lang=swift>import Foundation
 
func isPrime(_ n: Int) -> Bool {
Line 3,360:
}
}
print("\n\(count) additive primes found.")</langsyntaxhighlight>
 
{{out}}
Line 3,410:
a@ = a@ / 10
loop
return (b@)</langsyntaxhighlight>
{{Out}}
<pre>Prime Digit Sum
Line 3,471:
=={{header|Vlang}}==
{{trans|go}}
<langsyntaxhighlight lang=vlang>fn is_prime(n int) bool {
if n < 2 {
return false
Line 3,526:
}
println("\n\n$count additive primes found.")
}</langsyntaxhighlight>
 
{{out}}
Line 3,542:
 
=={{header|VTL-2}}==
<langsyntaxhighlight lang=VTL2>10 M=499
20 :1)=1
30 P=2
Line 3,575:
330 ?=N
340 ?=" additive primes below ";
350 ?=M+1</langsyntaxhighlight>
{{out}}
<pre>2 3 5 7 11 23 29 41 43 47
Line 3,588:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang=ecmascript>import "/math" for Int
import "/fmt" for Fmt
 
Line 3,610:
}
}
System.print("\n\n%(count) additive primes found.")</langsyntaxhighlight>
 
{{out}}
Line 3,626:
 
=={{header|XPL0}}==
<langsyntaxhighlight lang=XPL0>func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
Line 3,655:
Text(0, " additive primes found below 500.
");
]</langsyntaxhighlight>
 
{{out}}
Line 3,669:
 
=={{header|Yabasic}}==
<langsyntaxhighlight lang=Yabasic>// Rosetta Code problem: http://rosettacode.org/wiki/Additive_primes
// by Galileo, 06/2022
 
Line 3,696:
next
 
print "\nFound: ", count</langsyntaxhighlight>
{{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
10,327

edits