# Additive Primes amongst first x Primes

• Numbers which have exactly 2 divisors are called Prime numbers.

• Numbers which are prime and their sum of digits is also a prime number are called Additive Primes

Given an integer x, compute all the additive primes amongst the first x prime numbers, with 2 being considered both the first prime and additive prime number. The numbers are represented in base 10.

## Rules:

• The output consists of all the additive primes amongst the first x primes
• 0 < x < 151, for this challenge, for functionality purposes
• Since the additive primes are all integers, decimals are not allowed (e.g.: you should output 2, not 2.0) and they must not be displayed as a fraction.

## Examples:

10 -> 2 3 5 7 11 23 29

Explanation:

The first 10 primes are 2 3 5 7 11 13 17 19 23 29, and only 2 3 5 7 11 23 29 have their sum of digits prime numbers, those being, respectively 2,3,5,7,2,5,11, so they are additive primes

Following the explanation from example 1, other test cases may be:

2 -> 2 3

25 -> 2 3 5 7 11 23 29 41 43 47 61 67 83 89

7 -> 2 3 5 7 11

NOTE: Please read the newly-edited rule 1, it brings changes to the output format slightly

Your code should be as short as possible, since this is , so the shortest answer in bytes wins. Good luck!

• That's fine. I'd recommend waiting around 24 hours though, because every time you accept the answer they get 15 rep, but they lose it when you un-accept. It's somewhat frustrating sometimes to ride the rollercoaster and continuously lose and gain rep. Mar 5, 2017 at 15:19

# Röda, 136 135 bytes

f n{P=[2]S=[2]seq 3,863|{|i|{P|{P+=i;s=0;((""..i)/"")|parseInteger _|s+=_;S+=i if[s in P and not(i in S)]}if{|p|[i%p>0]}_}if[#P<n]}_;S}

Try it online!

It's a function that returns the requested additive primes.

Usage: main { f(25) | print ap for ap } The code uses version 0.12, which is in branch roda-0.12.

Ungolfed:

function f(n) {
primes := [2]
ultraprimes := [2]
seq(3, 863) | for i do
break if [ #primes = n ]
if [ i%p != 0 ] for p in primes do
primes += i
sum := 0
((""..i)/"") | parseInteger _ | sum += digit for digit
ultraprimes += i if [ sum in primes and not (i in ultraprimes) ]
done
done
ultraprimes
}
• Nice language! You made this yourself a long time ago it looks like? 10/10, looks pretty cool. Mar 5, 2017 at 16:14
• Neat language! How do you run the program? Mar 5, 2017 at 16:26
• Was just about to ask the same thing. Although I looked over the documentation, I cannot run or compile the source at all. What is your approach? Mar 5, 2017 at 16:40
• @KritixiLithos @Xcoder123 It requires Java 8 and Gradle. The version I use in this answer is 0.12 (in its own branch). The repository must be cloned recursively. To make a runnable jar, invoke gradle fatJar. Do you get any errors when compiling? Mar 5, 2017 at 16:43
• @fergusq Running gradle fatJar doesn't seem to create a jar for me Mar 5, 2017 at 16:45

## Pyke, 9 7 bytes

~p>#Yss

Try it online!

The single byte is_prime was only pushed 3 hours ago. Github commit.

~p      -    All the prime numbers
>     -   first input of them
#Yss -  filter(^)
Y   -     digits(^)
s  -    sum(^)
s -   is_prime(^)
• Did you just edit your language to suit this challenge? :D Mar 5, 2017 at 17:43
• so... s means is_prime on numbers, and sum on lists? Mar 5, 2017 at 20:15
• @ConorO'Brien yes, I overload it for lists and integers
– Blue
Mar 5, 2017 at 21:36
• @Džuris no, I've been meaning to for a while because I haven't had a single node for doing prime checking, only factorising into primes and divisors. Before I would have had to do _P which is 1 byte longer in this case
– Blue
Mar 5, 2017 at 21:38
• a new contender for "youngest language feature to win a challenge"? under the wire by ~2 hours? Mar 5, 2017 at 22:06

## Python 2, 124 118 bytes

With help from Riker:

n,f,P=input(),filter,lambda n:all(n%i for i in range(2,n))
f(lambda x:P(sum(map(int,x)))&P(x),f(P,range(2,n*n))[:n])

Original:

n,o,c,P=input(),0,2,lambda n:all(n%i for i in range(2,n))
while o<n:
o+=P(c)
if P(sum(map(int,c)))and P(c):print c
c+=1

Checking primality in Python ain't fun.

• I (read: got conor to write cool J code for me) have tested this with 9n, doesn't work. :/ n**2 does work, but at a cost of 1 byte. Mar 5, 2017 at 17:58
• Try n*n for n**2 Mar 5, 2017 at 17:59

# Perl 6, 53 bytes

{grep *.comb.sum.is-prime,grep(*.is-prime,0..*)[^$_]} Try it ## Expanded: { grep *.comb.sum.is-prime, # find the ultra primes from: grep( *.is-prime, # find the primes 0..* # from all integers )[ ^$_ ]             # grab only the first x primes
}

If this challenge were changed so that you took the first x ultraprimes this could be shortened to just

{grep({($_&.comb.sum).is-prime},0..*)[^$_]}

# Python 2, 96 87 bytes

p=-input(),0;m=k=1
while sum(p):
m*=k*k;k+=1;p+=m%k,
if m%k*p[int(k,36)%35]:print k

Thanks to @xnor for golfing off 9 bytes!

Try it online!

• Looks like using a list of indicator variables is shorter.
– xnor
Mar 6, 2017 at 1:52
• The digit sum can be done shorter as is int(k,36)%35. All the inputs will be small enough that this suffices.
– xnor
Mar 6, 2017 at 2:12
• More golfing
– xnor
Mar 6, 2017 at 2:23
• Wow! I'm not sure how I thought of a Boolean dict but not a Boolean tuple (hindsight is 20/20), but sum(p) and int(k,36)%35 are something else... Thanks! Mar 6, 2017 at 2:36

## Mathematica, 61 47 bytes

Prime@Range@#~Select~PrimeQ@*Tr@*IntegerDigits&
• Not entirely familiar with Mathematica's shorthand syntaxes - what's that @*? The * doesn't look like it's in the right place to be multiplication? Mar 6, 2017 at 3:23
• @numbermaniac it's function composition. f@*g is essentially f@g@##&. Mar 6, 2017 at 7:08

# Jelly, 10 bytes

ÆNDS$€ĖÆPM Try it online! ### How? A slightly different approach... ÆNDS$€ĖÆPM - Main link: n (>0)           e.g. 10
ÆN         - nth prime number                 29
€     - for each in range(1,nth prime)   [1,    2,    3,   ..., 27,    28,     29]
$- last two links as a monad D - decimal digit list [[1], [2], [3], ...,[2,7], [2,8], [2,9]] S - sum [1, 2, 3, ..., 9, 10, 11] Ė - enumerate [[1,1],[2,2],[3,3],...,[9,27],[10,28],[11,29]] ÆP - is prime? (vectorises) [[0,0],[1,1],[1,1],...,[0,1], [0,0], [1,1]] M - indices of maximal elements [ 2, 3, ..., 29] • Nice use of Ė. Mar 5, 2017 at 16:12 # 05AB1E, 9 bytes ÝØ¨vySOp— Uses the CP-1252 encoding. Try it online! # Jelly, 11 bytes ÆN€DS$ÆP$Ðf Try it online! Explanation: ÆN€DS$ÆP$Ðf Main link (args: z) ÆN€ Generate first z primes. DS$      Take the digital sum.
ÆP    Check if it's prime.
$Join last two links and make a monad. Ðf Only keep elements which conform to the criterion above. I got outgolfed. # MATL, 15 13 bytes 2 bytes saved thanks to @Luis :Yq"@V!UsZp?@ Try it at MATL Online Explanation % Implicitly grab input as a number (N) : % Create an array [1...N] Yq % Get the k-th prime for each element k in that array " % For each element in this list @ % Get the current element V!U % Break it into digits s % Sum up the digits Zp % Determine if this is a prime number ?@ % If it is, push the value to the stack % Implicit end of for loop and implicit display of the stack • @LuisMendo Ah! I knew there was some way to shorten that first part. Thanks Mar 11, 2017 at 0:30 # Fig, $$\12\log_{256}(96)\approx\$$ 9.877 bytes FtxFmC@p'pSf Try it online! Explanation: t # take the first x # x items from mC # all positive integers F # filtered by @p # is prime, F ' # then filter those by S # the sum of f # the digits p # is prime # Bash + coreutils, 97 bytes p()(factor$1|wc -w)
for((;++n,c<$1;)){((p$n-2||(c++,p $[n%10+n/10%10+n/100]-2)))||echo$n;}

Try it online!

# Ohm, 10 bytes (CP437)

@▓_π;░_}Σp

This would be much shorter if I had vectorization or a component for the first N primes, but alas, I did not before this challenge (but I do now!).

Explanation:

@▓_π;░_}Σp    Main wire, arguments: a

@▓  ;         Map...over the range (1..n)
_π            nth prime
░        Select from ToS where...
_}Σ       The sum of all digits
p      is prime

# PowerShell, 120 bytes

for($n=$args[0];$n){for(;'1'*++$i-notmatch($s='^(?!(..+)\1+$)..')){}if('1'*([char[]]"$i"-join'+'|iex)-match$s){$i};$n--}

Try it online!

Prime checking in PowerShell sucks.

# Pari/GP, 59 bytes

f(x)=[p|p<-vector(x,i,prime(i)),isprime(vecsum(digits(p)))]

Try it online!

# Vyxal, 5 bytes

ʁǎ'∑æ

Explanation:

ʁ      # Range from 0 to n-1
ǎ     # Nth prime (vectorising)
'    # Filtered by:
∑   #   The digit sum
æ  #   Is prime

# MathGolf, 9 bytes

♪╒g¶<gÅΣ¶

Try it online.

Explanation:

Since the input is guaranteed to be within the range $$\[1,150]\$$, where the $$\150^{th}\$$ prime is $$\877\$$, we'll use this to our advantage. (Although using ó ($$\2^n\$$ builtin using the implicit input) instead of would have worked as well, but would be way too slow for larger inputs.)

♪╒         # Push a list in the range [1,1000]
g        # Filter it by:
¶       #  Check if the number is a prime
<      # Then only keep the first (implicit) input amount of prime numbers
g     # Filter it again,
Å    # using 2 characters as inner code-block:
Σ   #  Get the digit-sum of the current integer
¶  #  Check if it's also a prime
# (after which the entire stack is output implicitly as result)

fȯṗΣd↑İp

Try it online!

Èj}jU fÈìx j

Try it

# Factor + math.primes math.unicode math.text.utils, 47 bytes

[ nprimes [ 1 digit-groups Σ prime? ] filter ]

Try it online!

## Explanation:

It's a quotation (anonymous function) that takes an integer from the data stack as input and leaves a sequence of integers on the data stack as output.

• nprimes Obtain a list of the first n primes.
• [ ... ] filter Take elements from the list that match a predicate.
• 1 digit-groups Obtain a list of digits from an integer.
• Σ Sum a sequence.
• prime? Is it prime?

# 05AB1E, 6 bytes

ÅpʒSOp

Explanation:

Åp      # Get a list of the first (implicit) input amount of primes
ʒ     # Filter this list by:
S    #  Convert the prime to a list of digits
O   #  Sum them together
p  #  Check if this sum is a prime
# (after which the filtered list is output implicitly as result)

# MATL, 13 bytes

:YqtFYA!XsZp)

Try it at MATL Online!

### Explanation

:      % Range [1 2 ... n], where n is implicit input
Yq     % Array of first n prime numbers
t      % Duplicate
FYA    % Convert to decimal digits. Gives a matrix, where each original
% number corresponds to a row. Left-pads with zeros if needed
!Xs    % Sum of rows
Zp     % Is prime? (element-wise)
)      % Use as logical index into the array of the first n prime numbers
% Implicitly display