To show how to check primality with the Lucas sequence, let’s look at an example. Consider the first few numbers in the Lucas sequence: 1,3,4,7,11,18,29,47,76,123,...
If we are interested to see if a particular number, say 7, is prime, we take the 7th number in the Lucas sequence and subtract 1. The 7th Lucas number is 29. Subtract 1 and we have 28. Now, if this result is evenly divisible by 7, 7 is probably prime. In our example, 28 is divisible by 7, so it is probably prime. We call these numbers Lucas pseudoprimes.
If we were interested in another number, say 8, we would take the 8th Lucas number and subtract 1. In this case, we get 46, which is not evenly divisible by 8. So, 8 is definitely not prime. This method is useful for filtering out composite numbers.
So, the general procedure to determine if an integer p could possibly be prime is as follows:
- Find the pth Lucas number
- Subtract 1
- Check to see if p divides the result evenly
Define a SCHEME function, named
(lucas-pseudoprime p)
which uses this approach to determine if an integer p is a Lucas pseudoprime.
I have this so far:
(define (lucas-pseudoprime p)
(define (helper-lucas goal current next)
(if (= goal current)
(head next)
(helper-lucas goal (+ current 1) next))
(let ((solution (- p 1))
(if (= (module solution p) 0) #t
#f)))))
Not sure what's going wrong with this.