Additive primes: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 21:
{{trans|Python}}
 
<syntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
Line 52:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang=AArch64"aarch64 Assemblyassembly">
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program additivePrime64.s */
Line 261:
</pre>
=={{header|Ada}}==
<syntaxhighlight lang=Ada"ada">with Ada.Text_Io;
 
procedure Additive_Primes is
Line 330:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<syntaxhighlight 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 363:
 
=={{header|ALGOL W}}==
<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 %
procedure Eratosthenes ( logical array p( * ) ; integer value n ) ;
Line 413:
=={{header|APL}}==
 
<syntaxhighlight lang=APL"apl">((+⌿(4/10)⊤P)∊P)/P←(~P∊P∘.×P)/P←1↓⍳500</syntaxhighlight>
 
{{out}}
Line 421:
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">on sieveOfEratosthenes(limit)
script o
property numberList : {missing value}
Line 469:
 
{{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>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang=ARM"arm Assemblyassembly">
/* ARM assembly Raspberry PI */
/* program additivePrime.s */
Line 678:
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">additives: select 2..500 'x -> and? prime? x prime? sum digits x
 
loop split.every:10 additives 'a ->
Line 697:
 
=={{header|AWK}}==
<syntaxhighlight lang=AWK"awk">
# syntax: GAWK -f ADDITIVE_PRIMES.AWK
BEGIN {
Line 740:
 
=={{header|BASIC}}==
<syntaxhighlight 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 765:
54 additive primes found below 500</pre>
==={{header|Applesoft BASIC}}===
<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
2 NEXT I: FOR I = B TO F: IF NOT P(I) THEN GOSUB 4
Line 778:
 
=={{header|BASIC256}}==
<syntaxhighlight lang="freebasic">print "Prime", "Digit Sum"
for i = 2 to 499
if isprime(i) then
Line 808:
 
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
manifest $( limit = 500 $)
 
Line 854:
 
=={{header|C}}==
<syntaxhighlight lang=C"c">
#include <stdbool.h>
#include <stdio.h>
Line 942:
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
 
Line 999:
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% Sieve of Erastothenes
% Returns an array [1..max] marking the primes
sieve = proc (max: int) returns (array[bool])
Line 1,052:
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. ADDITIVE-PRIMES.
Line 1,136:
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
(defun sum-of-digits (n)
"Return the sum of the digits of a number"
Line 1,165:
 
=={{header|Crystal}}==
<syntaxhighlight lang="ruby"># Fast/simple way to generate primes for small values.
# Uses P3 Prime Generator (PG) and its Prime Generator Sequence (PGS).
 
Line 1,255:
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc sieve([*] bool prime) void:
word max, p, c;
max := dim(prime,1)-1;
Line 1,302:
 
=={{header|Erlang}}==
<syntaxhighlight lang=Erlang"erlang">
main(_) ->
AddPrimes = [N || N <- lists:seq(2,500), isprime(N) andalso isprime(digitsum(N))],
Line 1,332:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<syntaxhighlight 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)
Line 1,344:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<syntaxhighlight lang="factor">USING: formatting grouping io kernel math math.primes
prettyprint sequences ;
 
Line 1,366:
 
=={{header|Fermat}}==
<syntaxhighlight lang="fermat">Function Digsum(n) =
digsum := 0;
while n>0 do
Line 1,444:
=={{header|Forth}}==
{{works with|Gforth}}
<syntaxhighlight lang="forth">: prime? ( n -- ? ) here + c@ 0= ;
: notprime! ( n -- ) here + 1 swap c! ;
 
Line 1,499:
=={{header|FreeBASIC}}==
As with the other special primes tasks, use one of the primality testing algorithms as an include.
<syntaxhighlight lang="freebasic">#include "isprime.bas"
 
function digsum( n as uinteger ) as uinteger
Line 1,582:
then go through the list, sum digits and check for prime
 
<syntaxhighlight lang="pascal">
Program AdditivePrimes;
Const max_number = 500;
Line 1,658:
 
=={{header|Frink}}==
<syntaxhighlight lang="frink">vals = toArray[select[primes[2, 500], {|x| isPrime[sum[integerDigits[x]]]}]]
println[formatTable[columnize[vals, 10]]]
println["\n" + length[vals] + " values found."]</syntaxhighlight>
Line 1,674:
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import "fmt"
Line 1,749:
 
=={{header|J}}==
<syntaxhighlight lang=J"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>
=={{header|Java}}==
<syntaxhighlight lang=Java"java">public class additivePrimes {
 
public static void main(String[] args) {
Line 1,800:
 
'''Preliminaries'''
<syntaxhighlight lang="jq">def is_prime:
. as $n
| if ($n < 2) then false
Line 1,834:
</syntaxhighlight>
'''The task'''
<syntaxhighlight lang="jq">
# Input: a number n
# Output: an array of additive primes less than n
Line 1,867:
=={{header|Haskell}}==
Naive solution which doesn't rely on advanced number theoretic libraries.
<syntaxhighlight lang="haskell">import Data.List (unfoldr)
 
-- infinite list of primes
Line 1,903:
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Primes
 
let
Line 1,929:
=={{header|Kotlin}}==
{{trans|Python}}
<syntaxhighlight lang="kotlin">fun isPrime(n: Int): Boolean {
if (n <= 3) return n > 1
if (n % 2 == 0 || n % 3 == 0) return false
Line 1,965:
 
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">#!/bin/ksh
 
# Prime numbers for which the sum of their decimal digits are also primes
Line 2,018:
=={{header|langur}}==
{{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
 
val .sumDigits = f fold f{+}, s2n toString .i
Line 2,051:
=={{header|Lua}}==
This task uses <code>primegen</code> from: [[Extensible_prime_generator#Lua]]
<syntaxhighlight lang="lua">function sumdigits(n)
local sum = 0
while n > 0 do
Line 2,069:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">ClearAll[AdditivePrimeQ]
AdditivePrimeQ[n_Integer] := PrimeQ[n] \[And] PrimeQ[Total[IntegerDigits[n]]]
Select[Range[500], AdditivePrimeQ]</syntaxhighlight>
Line 2,076:
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE AdditivePrimes;
FROM InOut IMPORT WriteString, WriteCard, WriteLn;
 
Line 2,140:
 
=={{header|Nim}}==
<syntaxhighlight lang=Nim"nim">import math, strutils
 
const N = 499
Line 2,185:
=={{header|Pari/GP}}==
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
 
v1 = select(isprime, select(hasPrimeDigitsum, [1..499]));
Line 2,197:
=={{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.
<syntaxhighlight lang="pascal">program AdditivePrimes;
{$IFDEF FPC}
{$MODE DELPHI}{$CODEALIGN proc=16}
Line 2,360:
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl">use strict;
use warnings;
use ntheory 'is_prime';
Line 2,383:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix"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>
Line 2,395:
 
=={{header|Phixmonti}}==
<syntaxhighlight lang=Phixmonti"phixmonti">/# Rosetta Code problem: http://rosettacode.org/wiki/Additive_primes
by Galileo, 05/2022 #/
 
Line 2,427:
 
=={{header|Picat}}==
<syntaxhighlight lang=Picat"picat">main =>
PCount = 0,
foreach (I in 2..499)
Line 2,449:
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">(de prime? (N)
(let D 0
(or
Line 2,477:
 
=={{header|PILOT}}==
<syntaxhighlight lang="pilot">C :z=2
:c=0
:max=500
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.
<syntaxhighlight lang="pli">/* FIND ADDITIVE PRIMES - PRIMES WHOSE DIGIT SUM IS ALSO PRIME */
additive_primes_100H: procedure options (main);
 
Line 2,688:
 
=={{header|Processing}}==
<syntaxhighlight lang="processing">IntList primes = new IntList();
 
void setup() {
Line 2,729:
 
=={{header|PureBasic}}==
<syntaxhighlight lang=PureBasic"purebasic">#MAX=500
Global Dim P.b(#MAX) : FillMemory(@P(),#MAX,1,#PB_Byte)
If OpenConsole()=0 : End 1 : EndIf
Line 2,755:
 
=={{header|Python}}==
<syntaxhighlight lang=Python"python">def is_prime(n: int) -> bool:
if n <= 3:
return n > 1
Line 2,794:
<code>digitsum</code> is defined at [[Sum digits of an integer#Quackery]].
 
<syntaxhighlight lang=Quackery"quackery"> 500 eratosthenes
[]
Line 2,814:
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">#lang racket
 
(require math/number-theory)
Line 2,835:
 
=={{header|Raku}}==
<syntaxhighlight lang=perl6"raku" line>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 }</syntaxhighlight>
Line 2,848:
 
=={{header|Red}}==
<syntaxhighlight lang=Red"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,866:
 
=={{header|REXX}}==
<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*/
if n=='' | n=="," then n= 500 /*Not specified? Then assume default.*/
Line 2,926:
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 2,969:
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require "prime"
 
additive_primes = Prime.lazy.select{|prime| prime.digits.sum.prime? }
Line 2,988:
 
===Flat implementation===
<syntaxhighlight lang="fsharp">fn main() {
let limit = 500;
let column_w = limit.to_string().len() + 1;
Line 3,020:
primal implements the sieve of Eratosthenes with optimizations (10+ times faster for large limits)
 
<syntaxhighlight lang="fsharp">// [dependencies]
// primal = "0.3.0"
 
Line 3,053:
 
=={{header|Sage}}==
<syntaxhighlight lang=SageMath"sagemath">
limit = 500
additivePrimes = list(filter(lambda x: x > 0,
Line 3,066:
</pre>
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func boolean: isPrime (in integer: number) is func
Line 3,125:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func additive_primes(upto, base = 10) {
upto.primes.grep { .sumdigits(base).is_prime }
}
Line 3,143:
 
=={{header|TSE SAL}}==
<syntaxhighlight lang=TSESAL"tsesal">
 
INTEGER PROC FNMathGetSquareRootI( INTEGER xI )
Line 3,315:
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">import Foundation
 
func isPrime(_ n: Int) -> Bool {
Line 3,376:
=={{header|uBasic/4tH}}==
{{trans|BASIC256}}
<syntaxhighlight lang="text">print "Prime", "Digit Sum"
for i = 2 to 499
if func(_isPrime(i)) then
Line 3,471:
=={{header|Vlang}}==
{{trans|go}}
<syntaxhighlight lang="vlang">fn is_prime(n int) bool {
if n < 2 {
return false
Line 3,542:
 
=={{header|VTL-2}}==
<syntaxhighlight lang=VTL2"vtl2">10 M=499
20 :1)=1
30 P=2
Line 3,588:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
 
Line 3,626:
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"xpl0">func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
Line 3,669:
 
=={{header|Yabasic}}==
<syntaxhighlight lang=Yabasic"yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Additive_primes
// by Galileo, 06/2022
 
10,333

edits