Additive primes: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
||
Line 21: | Line 21: | ||
{{trans|Python}} |
{{trans|Python}} |
||
<syntaxhighlight lang=11l>F is_prime(a) |
<syntaxhighlight lang="11l">F is_prime(a) |
||
I a == 2 |
I a == 2 |
||
R 1B |
R 1B |
||
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 }} |
||
<syntaxhighlight lang= |
<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 261: | Line 261: | ||
</pre> |
</pre> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="ada">with Ada.Text_Io; |
||
procedure Additive_Primes is |
procedure Additive_Primes is |
||
Line 330: | Line 330: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{libheader|ALGOL 68-primes}} |
{{libheader|ALGOL 68-primes}} |
||
<syntaxhighlight 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 363: | Line 363: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
<syntaxhighlight 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 413: | Line 413: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
<syntaxhighlight 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}}== |
||
<syntaxhighlight lang=applescript>on sieveOfEratosthenes(limit) |
<syntaxhighlight lang="applescript">on sieveOfEratosthenes(limit) |
||
script o |
script o |
||
property numberList : {missing value} |
property numberList : {missing value} |
||
Line 469: | Line 469: | ||
{{output}} |
{{output}} |
||
<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> |
<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}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="arm assembly"> |
||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
/* program additivePrime.s */ |
/* program additivePrime.s */ |
||
Line 678: | Line 678: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
<syntaxhighlight 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 -> |
||
Line 697: | Line 697: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="awk"> |
||
# syntax: GAWK -f ADDITIVE_PRIMES.AWK |
# syntax: GAWK -f ADDITIVE_PRIMES.AWK |
||
BEGIN { |
BEGIN { |
||
Line 740: | Line 740: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
<syntaxhighlight 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 765: | Line 765: | ||
54 additive primes found below 500</pre> |
54 additive primes found below 500</pre> |
||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
<syntaxhighlight 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 778: | Line 778: | ||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
<syntaxhighlight 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 808: | Line 808: | ||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
<syntaxhighlight lang=bcpl>get "libhdr" |
<syntaxhighlight lang="bcpl">get "libhdr" |
||
manifest $( limit = 500 $) |
manifest $( limit = 500 $) |
||
Line 854: | Line 854: | ||
=={{header|C}}== |
=={{header|C}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="c"> |
||
#include <stdbool.h> |
#include <stdbool.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 942: | Line 942: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
<syntaxhighlight lang=cpp>#include <iomanip> |
<syntaxhighlight lang="cpp">#include <iomanip> |
||
#include <iostream> |
#include <iostream> |
||
Line 999: | Line 999: | ||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
<syntaxhighlight 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,052: | Line 1,052: | ||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
<syntaxhighlight lang=cobol> IDENTIFICATION DIVISION. |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
PROGRAM-ID. ADDITIVE-PRIMES. |
PROGRAM-ID. ADDITIVE-PRIMES. |
||
Line 1,136: | Line 1,136: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
<syntaxhighlight 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,165: | Line 1,165: | ||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
<syntaxhighlight 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,255: | Line 1,255: | ||
=={{header|Draco}}== |
=={{header|Draco}}== |
||
<syntaxhighlight 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,302: | Line 1,302: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang= |
<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,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#)] |
||
<syntaxhighlight 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) |
||
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}} |
||
<syntaxhighlight 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,366: | Line 1,366: | ||
=={{header|Fermat}}== |
=={{header|Fermat}}== |
||
<syntaxhighlight 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,444: | Line 1,444: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
{{works with|Gforth}} |
{{works with|Gforth}} |
||
<syntaxhighlight 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,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. |
||
<syntaxhighlight 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,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 |
||
<syntaxhighlight lang=pascal> |
<syntaxhighlight lang="pascal"> |
||
Program AdditivePrimes; |
Program AdditivePrimes; |
||
Const max_number = 500; |
Const max_number = 500; |
||
Line 1,658: | Line 1,658: | ||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
<syntaxhighlight 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."]</syntaxhighlight> |
println["\n" + length[vals] + " values found."]</syntaxhighlight> |
||
Line 1,674: | Line 1,674: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
<syntaxhighlight lang=go>package main |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,749: | Line 1,749: | ||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang= |
<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</syntaxhighlight> |
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}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="java">public class additivePrimes { |
||
public static void main(String[] args) { |
public static void main(String[] args) { |
||
Line 1,800: | Line 1,800: | ||
'''Preliminaries''' |
'''Preliminaries''' |
||
<syntaxhighlight 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,834: | Line 1,834: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
'''The task''' |
'''The task''' |
||
<syntaxhighlight lang=jq> |
<syntaxhighlight 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,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. |
||
<syntaxhighlight lang=haskell>import Data.List (unfoldr) |
<syntaxhighlight lang="haskell">import Data.List (unfoldr) |
||
-- infinite list of primes |
-- infinite list of primes |
||
Line 1,903: | Line 1,903: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<syntaxhighlight lang=julia>using Primes |
<syntaxhighlight lang="julia">using Primes |
||
let |
let |
||
Line 1,929: | Line 1,929: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
<syntaxhighlight 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,965: | Line 1,965: | ||
=={{header|Ksh}}== |
=={{header|Ksh}}== |
||
<syntaxhighlight 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,018: | Line 2,018: | ||
=={{header|langur}}== |
=={{header|langur}}== |
||
{{works with|langur|0.10}} |
{{works with|langur|0.10}} |
||
<syntaxhighlight 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,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]] |
||
<syntaxhighlight 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,069: | Line 2,069: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
<syntaxhighlight lang= |
<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]</syntaxhighlight> |
Select[Range[500], AdditivePrimeQ]</syntaxhighlight> |
||
Line 2,076: | Line 2,076: | ||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
<syntaxhighlight lang=modula2>MODULE AdditivePrimes; |
<syntaxhighlight lang="modula2">MODULE AdditivePrimes; |
||
FROM InOut IMPORT WriteString, WriteCard, WriteLn; |
FROM InOut IMPORT WriteString, WriteCard, WriteLn; |
||
Line 2,140: | Line 2,140: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="nim">import math, strutils |
||
const N = 499 |
const N = 499 |
||
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. |
||
<syntaxhighlight 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,197: | Line 2,197: | ||
=={{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. |
||
<syntaxhighlight 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,360: | Line 2,360: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
<syntaxhighlight lang=perl>use strict; |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use ntheory 'is_prime'; |
use ntheory 'is_prime'; |
||
Line 2,383: | Line 2,383: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--<syntaxhighlight lang= |
<!--<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> |
||
Line 2,395: | Line 2,395: | ||
=={{header|Phixmonti}}== |
=={{header|Phixmonti}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="phixmonti">/# Rosetta Code problem: http://rosettacode.org/wiki/Additive_primes |
||
by Galileo, 05/2022 #/ |
by Galileo, 05/2022 #/ |
||
Line 2,427: | Line 2,427: | ||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picat">main => |
||
PCount = 0, |
PCount = 0, |
||
foreach (I in 2..499) |
foreach (I in 2..499) |
||
Line 2,449: | Line 2,449: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picolisp">(de prime? (N) |
||
(let D 0 |
(let D 0 |
||
(or |
(or |
||
Line 2,477: | Line 2,477: | ||
=={{header|PILOT}}== |
=={{header|PILOT}}== |
||
<syntaxhighlight lang=pilot>C :z=2 |
<syntaxhighlight lang="pilot">C :z=2 |
||
:c=0 |
:c=0 |
||
:max=500 |
:max=500 |
||
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. |
||
<syntaxhighlight 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,688: | Line 2,688: | ||
=={{header|Processing}}== |
=={{header|Processing}}== |
||
<syntaxhighlight lang=processing>IntList primes = new IntList(); |
<syntaxhighlight lang="processing">IntList primes = new IntList(); |
||
void setup() { |
void setup() { |
||
Line 2,729: | Line 2,729: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
<syntaxhighlight lang= |
<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,755: | Line 2,755: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="python">def is_prime(n: int) -> bool: |
||
if n <= 3: |
if n <= 3: |
||
return n > 1 |
return n > 1 |
||
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]]. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="quackery"> 500 eratosthenes |
||
[] |
[] |
||
Line 2,814: | Line 2,814: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang=racket>#lang racket |
<syntaxhighlight lang="racket">#lang racket |
||
(require math/number-theory) |
(require math/number-theory) |
||
Line 2,835: | Line 2,835: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="raku" line>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 }</syntaxhighlight> |
with ^$limit .grep: { .is-prime and .comb.sum.is-prime }</syntaxhighlight> |
||
Line 2,848: | Line 2,848: | ||
=={{header|Red}}== |
=={{header|Red}}== |
||
<syntaxhighlight lang= |
<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,866: | Line 2,866: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<syntaxhighlight 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,926: | Line 2,926: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
<syntaxhighlight lang=ring> |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
Line 2,969: | Line 2,969: | ||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<syntaxhighlight 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,988: | Line 2,988: | ||
===Flat implementation=== |
===Flat implementation=== |
||
<syntaxhighlight 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,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) |
||
<syntaxhighlight lang=fsharp>// [dependencies] |
<syntaxhighlight lang="fsharp">// [dependencies] |
||
// primal = "0.3.0" |
// primal = "0.3.0" |
||
Line 3,053: | Line 3,053: | ||
=={{header|Sage}}== |
=={{header|Sage}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="sagemath"> |
||
limit = 500 |
limit = 500 |
||
additivePrimes = list(filter(lambda x: x > 0, |
additivePrimes = list(filter(lambda x: x > 0, |
||
Line 3,066: | Line 3,066: | ||
</pre> |
</pre> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
<syntaxhighlight 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,125: | Line 3,125: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
<syntaxhighlight 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,143: | Line 3,143: | ||
=={{header|TSE SAL}}== |
=={{header|TSE SAL}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="tsesal"> |
||
INTEGER PROC FNMathGetSquareRootI( INTEGER xI ) |
INTEGER PROC FNMathGetSquareRootI( INTEGER xI ) |
||
Line 3,315: | Line 3,315: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
<syntaxhighlight lang=swift>import Foundation |
<syntaxhighlight lang="swift">import Foundation |
||
func isPrime(_ n: Int) -> Bool { |
func isPrime(_ n: Int) -> Bool { |
||
Line 3,376: | Line 3,376: | ||
=={{header|uBasic/4tH}}== |
=={{header|uBasic/4tH}}== |
||
{{trans|BASIC256}} |
{{trans|BASIC256}} |
||
<lang>print "Prime", "Digit Sum" |
<syntaxhighlight lang="text">print "Prime", "Digit Sum" |
||
for i = 2 to 499 |
for i = 2 to 499 |
||
if func(_isPrime(i)) then |
if func(_isPrime(i)) then |
||
Line 3,471: | Line 3,471: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|go}} |
{{trans|go}} |
||
<syntaxhighlight 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,542: | Line 3,542: | ||
=={{header|VTL-2}}== |
=={{header|VTL-2}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="vtl2">10 M=499 |
||
20 :1)=1 |
20 :1)=1 |
||
30 P=2 |
30 P=2 |
||
Line 3,588: | Line 3,588: | ||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
<syntaxhighlight lang=ecmascript>import "/math" for Int |
<syntaxhighlight lang="ecmascript">import "/math" for Int |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 3,626: | Line 3,626: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
<syntaxhighlight lang= |
<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,669: | Line 3,669: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Additive_primes |
||
// by Galileo, 06/2022 |
// by Galileo, 06/2022 |
||