Prime numbers which sum of digits is also prime

From Rosetta Code
Redirect page

Redirect to:

Task
Prime numbers n which sum of digits is also prime, where n < 500



Ring

<lang ring> load "stdlib.ring" see "working..." + nl see "Prime numbers which sum of digits is also prime:" + nl

row = 0 limit = 500

for n = 1 to limit

   flag = 0
   sum = 0
   strn = string(n)
   if isprime(n)
      for m = 1 to len(strn)
          sum = sum + strn[m]
      next
      if isprime(sum)
         flag = 1
      ok
   ok
   if flag = 1    
      see "" + n + " "
      row = row + 1
      if row%5 = 0
         see nl
      ok
   ok

next

see nl + "Found " + row + " numbers" + nl see "done..." + nl </lang>

Output:
working...
Prime numbers which sum of digits is also prime:
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 
Found 54 numbers
done...

XPL0

<lang XPL0>func IsPrime(N); \Return 'true' if N is a prime number int N, I; [if N <= 1 then return false; for I:= 2 to sqrt(N) do

   if rem(N/I) = 0 then return false;

return true; ];

func SumDigits(N); \Return the sum of the digits in N int N, Sum; [Sum:= 0; repeat N:= N/10;

       Sum:= Sum + rem(0);

until N=0; return Sum; ];

int Count, N; [Count:= 0; for N:= 0 to 500-1 do

   if IsPrime(N) & IsPrime(SumDigits(N)) then
       [IntOut(0, N);
       Count:= Count+1;
       if rem(Count/10) = 0 then CrLf(0) else ChOut(0, 9\tab\);
       ];

CrLf(0); IntOut(0, Count); Text(0, " additive primes found below 500. "); ]</lang>

Output:
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     
54 additive primes found below 500.