' Puzzlet #019.
' Find a sequence of six 5-digit prime numbers
' a,b,c,d,e, such that b = a + 2, c = b + 4,
' ... f = e + 10. This is a span of 30 integers,
' of which only six are allowed to be prime.
' Example: 21557,21559,21563,21569,21577,21587.
' There are four other sequences to find.
' By Dave Ellis.
declare Prime(p: int )
declare PokeNewPrime(p: int)
declare InSequence()
declare PrintSequence()
def n: int
def primeArray[7]: int
openconsole
print "Searching primes ..." : print
print " a b c d e f"
print " = a+2 = b+4 = c+6 = d+8 =e+10"
for n = 10001 to 99997 #2
if Prime(n)
PokeNewPrime(n)
if InSequence()
PrintSequence()
endif
endif
next n
print: print "No more found."
print: print "Press any key to exit ..."
do: until inkey$ <> ""
closeconsole
end
sub Prime(p)
' If p is a prime number,
' returns 1, else returns 0.
def flag, i, root: int
if p = 2 | p = 3
flag = -1
else
if p % 2 = 0
flag = 0
else
root = int (sqrt(p))
i = 3
do
flag = -1
if p % i = 0
flag = 0
endif
i = i + 2
until ((flag = 0) | (i > root)))
endif
endif
return flag
sub pokeNewPrime(p)
' Shuffles primeArray contents from position
' 6 to position 2 downwards by one position,
' overwriting as it goes. Position 1 content
' is permanently lost. New prime p is poked
' into primArray[6].
def i, j: int
for i = 1 to 5
j = i + 1
primeArray[i] = primeArray[j]
next i
primeArray[6] = p
return
sub inSequence()
' If primeArray contents are in sequence as
' defined in the introductory REMs, returns
' 1, otherwise returns 0.
def flag, i: int
flag = -1
i = 1
do
if primeArray[i + 1] <> i*2 + primeArray[i]
flag = 0
endif
i = i + 1
until flag = 0 | i = 6
return flag
sub printSequence()
' Prints out the contents of primeArray.
def i: int
for i = 1 to 6
print using ("######", primeArray[i]),
next i
return
PROGRAM NOTES
The search range must start with the smallest 5-digit integer not a prime, 10001, and conclude with the largest, 99997.
Variable n is used to iterate through this range. Note that n only looks at every 2nd number, thus skipping all those even numbers - none of which can be a prime.
As each 5-digit odd integer is loaded into variable n, it's submitted to the function prime(). This function simply determines if n is a prime number. If not, no action is taken, and n is incremented to look at the next odd 5-digit integer.
If n is found to be a prime number, it's submitted to the subroutine PokeNewPrime(), whose job is to push this newly-found prime onto the top a little stack stored in the appropriately-named array PrimeArray[]. This process deliberately pushes the least-recently found prime off the bottom of the stack, so that it's always filled with the six most-recently discovered primes.
Every time a new prime is pushed onto the stack, it's necessary to take a look at the stack to determine if its contents are sequenced in exactly the way we need for a solution. The function InSequence() takes care of that task.
If InSequence() returns -1 (TRUE), we have a valid sequence of primes stored in PrimeArray[], and its contents are printed out by the subroutine PrintSequence().
The process continues until all valid 5-digit integers have been examined.
When you run the code, it will produce the inset screen.
The given example appears on the second line, and the other four examples which the Puzzlet called for are all there.
Searching primes ...
a b c d e f
= a+2 = b+4 = c+6 = d+8 =e+10
13901 13903 13907 13913 13921 13931
21557 21559 21563 21569 21577 21587
28277 28279 28283 28289 28297 28307
55661 55663 55667 55673 55681 55691
68897 68899 68903 68909 68917 68927
No more found.
Press any key to exit ...
MAIN MENU
DOWNLOAD CODE
ARCHIVES
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.