Neighbour primes: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created page with "{{Draft task}} ;Task: Find and show primes p such that p*q+2 is prime, where q is next prime after p, where '''p < 500''' <br><br> =={{header|Ring}}== <lang ring> load "stdl...")
 
Line 9: Line 9:
load "stdlib.ring"
load "stdlib.ring"
see "working..." + nl
see "working..." + nl
see "Neighbour primes are:" + nl
see "prime1 prime2 prime3" + nl
see "prime1 prime2 prime3" + nl


row = 0
num = 0
num = 0
pr = 0
pr = 0
Line 30: Line 32:
prim = Primes[n]*Primes[n+1]+2
prim = Primes[n]*Primes[n+1]+2
if isprime(prim)
if isprime(prim)
row = row + 1
see "" + Primes[n] + " " + Primes[n+1] + " " + prim + nl
see "" + Primes[n] + " " + Primes[n+1] + " " + prim + nl
ok
ok
next
next


see "Found " + row + " neighbour primes" + nl
see "done..." + nl
see "done..." + nl
</lang>
</lang>
Line 39: Line 43:
<pre>
<pre>
working...
working...
Neighbour primes are:
prime1 prime2 prime3
prime1 prime2 prime3
3 5 17
3 5 17
Line 60: Line 65:
439 443 194479
439 443 194479
487 491 239119
487 491 239119
Found 20 neighbour primes
done...
done...
</pre>
</pre>

Revision as of 04:47, 13 April 2021

Neighbour 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.
Task

Find and show primes p such that p*q+2 is prime, where q is next prime after p, where p < 500

Ring

<lang ring> load "stdlib.ring" see "working..." + nl see "Neighbour primes are:" + nl see "prime1 prime2 prime3" + nl

row = 0 num = 0 pr = 0 limit = 100 Primes = []

while true

   pr = pr + 1
   if isprime(pr)
      add(Primes,pr)
      num = num + 1
      if num = limit 
         exit
      ok
   ok

end

for n = 1 to limit-1

   prim = Primes[n]*Primes[n+1]+2
   if isprime(prim)
      row = row + 1
      see "" + Primes[n] + " " + Primes[n+1] + " " + prim + nl
   ok

next

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

Output:
working...
Neighbour primes are:
prime1 prime2 prime3
3 5 17
5 7 37
7 11 79
13 17 223
19 23 439
67 71 4759
149 151 22501
179 181 32401
229 233 53359
239 241 57601
241 251 60493
269 271 72901
277 281 77839
307 311 95479
313 317 99223
397 401 159199
401 409 164011
419 421 176401
439 443 194479
487 491 239119
Found 20 neighbour primes
done...