Successive prime differences: Difference between revisions
Content added Content deleted
(add FreeBASIC) |
|||
Line 1,025: | Line 1,025: | ||
Last group: { 997141 997147 997151 997153 } |
Last group: { 997141 997147 997151 997153 } |
||
Count: 306 |
Count: 306 |
||
</pre> |
|||
=={{header|FreeBASIC}}== |
|||
<lang freebasic>#include "isprime.bas" |
|||
function nextprime( n as uinteger ) as uinteger |
|||
'finds the next prime after n |
|||
if n = 0 then return 2 |
|||
if n < 3 then return n + 1 |
|||
dim as integer q = n + 2 |
|||
while not isprime(q) |
|||
q+=2 |
|||
wend |
|||
return q |
|||
end function |
|||
function spd( byval n as integer, d() as integer ) as boolean |
|||
if not isprime(n) then return false |
|||
for i as integer = lbound(d) to ubound(d) |
|||
if not nextprime(n) = n + d(i) then return false |
|||
n+=d(i) |
|||
next i |
|||
return true |
|||
end function |
|||
sub print_set( byval n as uinteger, d() as uinteger ) |
|||
print "( ";n;" "; |
|||
for i as integer = lbound(d) to ubound(d) |
|||
print n+d(i);" "; |
|||
n+=d(i) |
|||
next i |
|||
print ")" |
|||
end sub |
|||
function count_below( max as uinteger, d() as uinteger ) as uinteger |
|||
dim as uinteger c = 0, last = 0 |
|||
for n as uinteger = 2 to max-d(ubound(d)) |
|||
if spd(n, d()) then |
|||
c+=1 |
|||
if c=1 then print_set( n, d() ) |
|||
last = n |
|||
end if |
|||
next n |
|||
print_set(last, d()) |
|||
return c |
|||
end function |
|||
dim as integer n, c |
|||
'example 1, differences of 2 |
|||
redim as uinteger d(0) |
|||
d(0) = 2 |
|||
print "Differences of 2 (the twin primes)" |
|||
c = count_below(1000000, d()) |
|||
print "Number of occurrences: ", c |
|||
'example 2, difference of 1 |
|||
d(0) = 1 |
|||
print |
|||
print "Differences of 1" |
|||
c = count_below(1000000, d()) |
|||
print "Number of occurrences: ", c |
|||
'example 3, differences of 2,2 |
|||
redim as uinteger d(1) |
|||
d(0) = 2 : d(1) = 2 |
|||
print |
|||
print "Differences of 2, 2" |
|||
c = count_below(1000000, d()) |
|||
print "Number of occurrences: ", c |
|||
'example 4, differences of 2,4 |
|||
d(1) = 4 |
|||
print |
|||
print "Differences of 2, 4" |
|||
c = count_below(1000000, d()) |
|||
print "Number of occurrences: ", c |
|||
'example 5, differences of 2,2 |
|||
d(0) = 4 : d(1) = 2 |
|||
print |
|||
print "Differences of 4, 2" |
|||
c = count_below(1000000, d()) |
|||
print "Number of occurrences: ", c |
|||
'example 6, differences of 6,4,2 |
|||
redim as uinteger d(2) |
|||
d(0) = 6 : d(1) = 4 : d(2) = 2 |
|||
print |
|||
print "Differences of 6, 4, 2" |
|||
c = count_below(1000000, d()) |
|||
print "Number of occurrences: ", c</lang> |
|||
{{out}}<pre> |
|||
Differences of 2 (the twin primes) |
|||
( 3 5 ) |
|||
( 999959 999961 ) |
|||
Number of occurrences: 8169 |
|||
Differences of 1 |
|||
( 2 3 ) |
|||
( 2 3 ) |
|||
Number of occurrences: 1 |
|||
Differences of 2, 2 |
|||
( 3 5 7 ) |
|||
( 3 5 7 ) |
|||
Number of occurrences: 1 |
|||
Differences of 2, 4 |
|||
( 5 7 11 ) |
|||
( 999431 999433 999437 ) |
|||
Number of occurrences: 1393 |
|||
Differences of 4, 2 |
|||
( 7 11 13 ) |
|||
( 997807 997811 997813 ) |
|||
Number of occurrences: 1444 |
|||
Differences of 6, 4, 2 |
|||
( 31 37 41 43 ) |
|||
( 997141 997147 997151 997153 ) |
|||
Number of occurrences: 306 |
|||
</pre> |
</pre> |
||
Line 1,132: | Line 1,254: | ||
Number found = 306 |
Number found = 306 |
||
</pre> |
</pre> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Uses primes library: http://hackage.haskell.org/package/primes-0.2.1.0/docs/Data-Numbers-Primes.html |
Uses primes library: http://hackage.haskell.org/package/primes-0.2.1.0/docs/Data-Numbers-Primes.html |