Extra primes

From Rosetta Code
Revision as of 04:38, 2 December 2020 by rosettacode>Gerard Schildberger (→‎{{header|REXX}}: added the computer programming language REXX.)
Extra primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Definition

n is an extra prime if n is prime and its digits and sum of digits are also primes.

Task

Show the extra primes under 10000

Reference

OEIS:A062088 - Primes with every digit a prime and the sum of the digits a prime.

Related tasks


Factor

Translation of: Wren
Works with: Factor version 0.99 2020-08-14

<lang factor>USING: formatting io kernel math math.functions math.primes sequences sequences.extras ;

digit ( seq seq -- seq ) [ suffix ] cartesian-map concat ;
front ( -- seq ) { { 2 } { 3 } { 5 } { 7 } } ;
middle ( seq -- newseq ) { 2 3 5 7 } digit ;
end ( seq -- newseq ) { 3 7 } digit ;
candidates ( -- seq )
   front
   front end
   front middle end
   front middle middle end
   append append append ;
digits>number ( seq -- n )
   <reversed> 0 [ 10^ * + ] reduce-index ;

"The extra primes with up to 4 digits are:" print candidates [ sum prime? ] filter [ digits>number ] [ prime? ] map-filter [ 1 + swap "%2d: %4d\n" printf ] each-index</lang>

Output:
The extra primes with up to 4 digits are:
 1:    2
 2:    3
 3:    5
 4:    7
 5:   23
 6:  223
 7:  227
 8:  337
 9:  353
10:  373
11:  557
12:  577
13:  733
14:  757
15:  773
16: 2333
17: 2357
18: 2377
19: 2557
20: 2753
21: 2777
22: 3253
23: 3257
24: 3323
25: 3527
26: 3727
27: 5233
28: 5237
29: 5273
30: 5323
31: 5527
32: 7237
33: 7253
34: 7523
35: 7723
36: 7727

Phix

Minor reworking of Numbers_with_prime_digits_whose_sum_is_13#Phix#iterative <lang Phix>constant lim = 99999999, -- (erm, the real limit is actually (lim+1)*10)

        dgts = {2,3,5,7}

function extra_primes()

   sequence res = {}, q = Template:0,0
   integer s, -- partial digit sum
           v  -- corresponding value
   while length(q) do
       {s,v} = q[1]
       q = q[2..$]
       for i=1 to length(dgts) do
           integer d = dgts[i], {ns,nv} = {s+d,v*10+d}
           if is_prime(ns) and is_prime(nv) then res &= nv end if
           if nv<lim then q &= Template:Ns,nv end if
       end for
   end while
   return res

end function

printf(1,"Extra primes < %,d:\n",{(lim+1)*10}) sequence res = extra_primes() printf(1,"[1..37]: %s\n",ppf(res[1..37],{pp_Indent,9,pp_Maxlen,94})) printf(1,"[991..1000]: %v\n",{res[991..1000]}) integer l = length(res) printf(1,"[%d..%d]: %v\n",{l-8,l,res[l-8..l]})</lang>

Output:
Extra primes < 1,000,000,000:
[1..37]: {2,3,5,7,23,223,227,337,353,373,557,577,733,757,773,2333,2357,2377,2557,2753,2777,
          3253,3257,3323,3527,3727,5233,5237,5273,5323,5527,7237,7253,7523,7723,7727,22573}
[991..1000]: {25337353,25353227,25353373,25353577,25355227,25355333,25355377,25357333,25357357,25357757}
[9050..9058]: {777755753,777773333,777773753,777775373,777775553,777775577,777777227,777777577,777777773}

Raku

For the time being, (Doctor?), I'm going to assume that the task is really "Sequence of primes with every digit a prime and the sum of the digits a prime". Outputting my own take on a reasonable display of results, compact and easily doable but exercising it a bit.

<lang perl6>my @ppp = lazy flat 2, 3, 5, 7, 23, grep { .is-prime && .comb.sum.is-prime },

              flat (2..*).map: { flat ([X~] (2, 3, 5, 7) xx $_) X~ (3, 7) };

put 'First 20 terms: '.fmt('%34s'), @ppp[^20]; put '991st through 1000th: '.fmt('%34s'), @ppp[990 .. 999]; put 'Crossing 10th order of magnitude: ', @ppp[9055..9060];</lang>

Output:
                  First 20 terms: 2 3 5 7 23 223 227 337 353 373 557 577 733 757 773 2333 2357 2377 2557 2753
            991st through 1000th: 25337353 25353227 25353373 25353577 25355227 25355333 25355377 25357333 25357357 25357757
Crossing 10th order of magnitude: 777777227 777777577 777777773 2222222377 2222222573 2222225273

REXX

<lang rexx>/*REXX pgm finds & shows all primes whose digits are prime and the digits sum to a prime*/ parse arg HI . if HI== | HI=="," then HI= 10000 /*obtain optional argument from the CL.*/ y= 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 @.= 0;  !.= @.

     do k=1  for words(y);     p= word(y, k)    /*obtain a prime number from the list. */
     @.k= p;     !.p= 1                         /*define a prime by it's index & value.*/
     end   /*k*/
  1. = 0

$= /*a list that holds "extra" primes. */

     do j=1  while j<HI                         /*search for numbers in this range.    */
     if verify(j, 2357) \== 0  then iterate     /*J  must be comprised of prime digits.*/
     s= left(j, 1)
                   do k=2  to length(j)         /*only need to sum #s with #digits ≥ 4 */
                   s= s + substr(j, k, 1)       /*sum some middle decimal digits of  J.*/
                   end   /*k*/
     if \!.s  then iterate                      /*Is the sum not equal to prime?  Skip.*/
                   do p=1  while @.p**2<=j      /*perform division up to the sqrt of J.*/
                   if j//@.p==0  then iterate j /*J divisible by a prime?  Then ¬ prime*/
                   end   /*p*/
     #= # + 1;                        $= $ j    /*bump # count; append J to the $ list.*/
     end   /*j*/

say # ' primes found whose digits are prime and the digits sum to a prime' ,

        "and which are less than "    HI":"

say strip($) /*display the output list to the term. */</lang>

output   when using the default input:
36  primes found whose digits are prime and the digits sum to a prime and which are less than  10000:
2 3 5 7 23 223 227 337 353 373 557 577 733 757 773 2333 2357 2377 2557 2753 2777 3253 3257 3323 3527 3727 5233 5237 5273 5323 5527 7237 7253 7523 7723 7727

Ring

<lang ring> load "stdlib.ring"

limit = 10000 num = 0 for n = 1 to limit

   x1 = prime1(n)
   x2 = prime2(n)
   x3 = isprime(n)
   if x1 = 1 and x2 = 1 and x3
      num = num + 1
      see "The " + num + "th Extra Prime is: " + n + nl
   ok

next

func prime1(x)

    pstr = string(x)
    len = len(pstr)
    count = 0
    for n = 1 to len 
        if isprime(number(pstr[n]))
           count = count + 1
        ok
    next
    if count = len 
       return 1
    else
       return 0
    ok

func prime2(x)

    pstr = string(x)
    len = len(pstr)
    sum = 0
    for n = 1 to len
        sum = sum + number(pstr[n])
    next
    if isprime(sum)
       return 1
    else
       return 0
    ok

</lang> Output:

The 1th Extra Prime is: 2
The 2th Extra Prime is: 3
The 3th Extra Prime is: 5
The 4th Extra Prime is: 7
The 5th Extra Prime is: 23
The 6th Extra Prime is: 223
The 7th Extra Prime is: 227
The 8th Extra Prime is: 337
The 9th Extra Prime is: 353
The 10th Extra Prime is: 373
The 11th Extra Prime is: 557
The 12th Extra Prime is: 577
The 13th Extra Prime is: 733
The 14th Extra Prime is: 757
The 15th Extra Prime is: 773
The 16th Extra Prime is: 2333
The 17th Extra Prime is: 2357
The 18th Extra Prime is: 2377
The 19th Extra Prime is: 2557
The 20th Extra Prime is: 2753
The 21th Extra Prime is: 2777
The 22th Extra Prime is: 3253
The 23th Extra Prime is: 3257
The 24th Extra Prime is: 3323
The 25th Extra Prime is: 3527
The 26th Extra Prime is: 3727
The 27th Extra Prime is: 5233
The 28th Extra Prime is: 5237
The 29th Extra Prime is: 5273
The 30th Extra Prime is: 5323
The 31th Extra Prime is: 5527
The 32th Extra Prime is: 7237
The 33th Extra Prime is: 7253
The 34th Extra Prime is: 7523
The 35th Extra Prime is: 7723
The 36th Extra Prime is: 7727

Wren

Library: Wren-math
Library: Wren-fmt

Unsure of the task - see talk page. <lang ecmascript>import "/math" for Int import "/fmt" for Fmt

var digits = [2, 3, 5, 7] // the only digits which are primes var digits2 = [3, 7] // a prime > 5 can't end in 2 or 5 var candidates = [[2, 2], [3, 3], [5, 5], [7, 7]] // [number, sum of its digits]

for (a in digits) {

   for (b in digits2) candidates.add([10*a + b, a + b])

}

for (a in digits) {

   for (b in digits) {
      for (c in digits2) candidates.add([100*a + 10*b + c, a + b + c])
   }

}

for (a in digits) {

   for (b in digits) {
       for (c in digits) {
           for (d in digits2) candidates.add([1000*a + 100*b + 10*c + d, a + b + c + d])
       }
   }

}

System.print("The extra primes with up to 4 digits are:") var count = 0 for (cand in candidates) {

  if (Int.isPrime(cand[0]) && Int.isPrime(cand[1])) {
     count = count + 1
     Fmt.print("$2d: $4d", count, cand[0])
  }

}</lang>

Output:
The extra primes with up to 4 digits are:
 1:    2
 2:    3
 3:    5
 4:    7
 5:   23
 6:  223
 7:  227
 8:  337
 9:  353
10:  373
11:  557
12:  577
13:  733
14:  757
15:  773
16: 2333
17: 2357
18: 2377
19: 2557
20: 2753
21: 2777
22: 3253
23: 3257
24: 3323
25: 3527
26: 3727
27: 5233
28: 5237
29: 5273
30: 5323
31: 5527
32: 7237
33: 7253
34: 7523
35: 7723
36: 7727