90 lines
2.0 KiB
Plaintext
90 lines
2.0 KiB
Plaintext
#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
|