Find prime numbers of the form n*n*n+2

From Rosetta Code
Revision as of 14:54, 15 March 2021 by Chunes (talk | contribs) (Add Factor)
Find prime numbers of the form n*n*n+2 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.
Task
Find prime numbers of form   n3+2,   where 0 < n < 200


Factor

Using the parity optimization from the Wren entry:

Works with: Factor version 0.99 2021-02-05

<lang factor>USING: formatting kernel math math.functions math.primes math.ranges sequences tools.memory.private ;

1 199 2 <range> [

   dup 3 ^ 2 + dup prime?
   [ commas "n = %3d => n³ + 2 = %9s\n" printf ] [ 2drop ] if

] each</lang> Or, using local variables:

Translation of: Wren
Works with: Factor version 0.99 2021-02-05

<lang factor>USING: formatting kernel math math.primes math.ranges sequences tools.memory.private ;

[let

   199 :> limit
   1 limit 2 <range> [| n |
       n n n * * 2 + :> p
       p prime?
       [ n p commas "n = %3d => n³ + 2 = %9s\n" printf ] when
   ] each

]</lang>

Output:
n =   1 => n³ + 2 =         3
n =   3 => n³ + 2 =        29
n =   5 => n³ + 2 =       127
n =  29 => n³ + 2 =    24,391
n =  45 => n³ + 2 =    91,127
n =  63 => n³ + 2 =   250,049
n =  65 => n³ + 2 =   274,627
n =  69 => n³ + 2 =   328,511
n =  71 => n³ + 2 =   357,913
n =  83 => n³ + 2 =   571,789
n = 105 => n³ + 2 = 1,157,627
n = 113 => n³ + 2 = 1,442,899
n = 123 => n³ + 2 = 1,860,869
n = 129 => n³ + 2 = 2,146,691
n = 143 => n³ + 2 = 2,924,209
n = 153 => n³ + 2 = 3,581,579
n = 171 => n³ + 2 = 5,000,213
n = 173 => n³ + 2 = 5,177,719
n = 189 => n³ + 2 = 6,751,271

Ring

<lang ring> load "stdlib.ring"

see "working..." + nl

for n = 1 to 200 step 2

   pr = pow(n,3)+2
   if isprime(pr)
      see "n: " + n + " n³+2 : " + pr + nl
   ok

next

see "done..." + nl </lang>

Output:
working...
n: 1 n³+2 : 3
n: 3 n³+2 : 29
n: 5 n³+2 : 127
n: 29 n³+2 : 24391
n: 45 n³+2 : 91127
n: 63 n³+2 : 250049
n: 65 n³+2 : 274627
n: 69 n³+2 : 328511
n: 71 n³+2 : 357913
n: 83 n³+2 : 571789
n: 105 n³+2 : 1157627
n: 113 n³+2 : 1442899
n: 123 n³+2 : 1860869
n: 129 n³+2 : 2146691
n: 143 n³+2 : 2924209
n: 153 n³+2 : 3581579
n: 171 n³+2 : 5000213
n: 173 n³+2 : 5177719
n: 189 n³+2 : 6751271
done...

Wren

Library: Wren-math
Library: Wren-trait
Library: Wren-fmt

If n is even then n³ + 2 is also even, so we only need to examine odd values of n here. <lang ecmascript>import "/math" for Int import "/trait" for Stepped import "/fmt" for Fmt

var limit = 200 for (n in Stepped.new(1...limit, 2)) {

   var p = n*n*n + 2
   if (Int.isPrime(p)) Fmt.print("n = $3d => n³ + 2 = $,9d", n, p)

}</lang>

Output:
n =   1 => n³ + 2 =         3
n =   3 => n³ + 2 =        29
n =   5 => n³ + 2 =       127
n =  29 => n³ + 2 =    24,391
n =  45 => n³ + 2 =    91,127
n =  63 => n³ + 2 =   250,049
n =  65 => n³ + 2 =   274,627
n =  69 => n³ + 2 =   328,511
n =  71 => n³ + 2 =   357,913
n =  83 => n³ + 2 =   571,789
n = 105 => n³ + 2 = 1,157,627
n = 113 => n³ + 2 = 1,442,899
n = 123 => n³ + 2 = 1,860,869
n = 129 => n³ + 2 = 2,146,691
n = 143 => n³ + 2 = 2,924,209
n = 153 => n³ + 2 = 3,581,579
n = 171 => n³ + 2 = 5,000,213
n = 173 => n³ + 2 = 5,177,719
n = 189 => n³ + 2 = 6,751,271