Neighbour primes: Difference between revisions
(add FreeBASIC) |
|||
Line 5: | Line 5: | ||
<br><br> |
<br><br> |
||
=={{header|FreeBASIC}}== |
|||
<lang freebasic>#include "isprime.bas" |
|||
dim as uinteger q |
|||
print "p q pq+2" |
|||
print "--------------------------------" |
|||
for p as uinteger = 2 to 499 |
|||
if not isprime(p) then continue for |
|||
q = p + 1 |
|||
while not isprime(q) |
|||
q+=1 |
|||
wend |
|||
if not isprime( 2 + p*q ) then continue for |
|||
print p,q,2+p*q |
|||
next p</lang> |
|||
{{out}} |
|||
<pre> |
|||
p q pq+2 |
|||
-------------------------------- |
|||
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 |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Line 12: | Line 54: | ||
println(filter(isneiprime, primes(500))) |
println(filter(isneiprime, primes(500))) |
||
</lang>{{out}}<pre>[3, 5, 7, 13, 19, 67, 149, 179, 229, 239, 241, 269, 277, 307, 313, 397, 401, 419, 439, 487]</pre> |
</lang>{{out}}<pre>[3, 5, 7, 13, 19, 67, 149, 179, 229, 239, 241, 269, 277, 307, 313, 397, 401, 419, 439, 487]</pre> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
Revision as of 09:42, 13 April 2021
- Task
Find and show primes p such that p*q+2 is prime, where q is next prime after p and p < 500
FreeBASIC
<lang freebasic>#include "isprime.bas"
dim as uinteger q
print "p q pq+2" print "--------------------------------" for p as uinteger = 2 to 499
if not isprime(p) then continue for q = p + 1 while not isprime(q) q+=1 wend if not isprime( 2 + p*q ) then continue for print p,q,2+p*q
next p</lang>
- Output:
p q pq+2 -------------------------------- 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
Julia
<lang julia>using Primes
isneiprime(known) = isprime(known) && isprime(known * nextprime(known + 1) + 2) println(filter(isneiprime, primes(500)))
</lang>
- Output:
[3, 5, 7, 13, 19, 67, 149, 179, 229, 239, 241, 269, 277, 307, 313, 397, 401, 419, 439, 487]
Raku
<lang perl6>my @primes = grep &is-prime, ^Inf; my $last_p = @primes.first: :k, * >= 500; my $last_q = $last_p + 1;
my @cousins = @primes.head( $last_q )
.rotor( 2 => -1 ) .map(-> (\p, \q) { p, q, p*q+2 } ) .grep( *.[2].is-prime );
say .fmt('%6d') for @cousins;</lang>
- Output:
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
Ring
<lang ring> load "stdlib.ring" see "working..." + nl see "Neighbour primes are:" + nl see "p q p*q+2" + 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: p q p*q+2 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...