0

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:

  1. Find the pth Lucas number
  2. Subtract 1
  3. 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.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • 1
    For starters, your function is completely empty and only defines a `helper-lucas` function. This appears to be a homework question. Why don't you start by explaining your thought process. Also it would help if you could format your question so your code snippets show up properly. – Leif Andersen Apr 17 '18 at 15:41
  • Solve item 1 first - write a (separate) function that returns the p:th Lucas number. This is very similar to finding the p:th Fibonacci number, which you've probably already implemented (or at least seen). – molbdnilo Apr 18 '18 at 08:14
  • You should also subtract 1 from the `p`:th Lucas number, not from `p`. – molbdnilo Apr 18 '18 at 08:16
  • I notice not one of your questions has an accepted answer. When you accept the most helpful (for you) answer you get a +2 reputation points. when you reach just 15 rep you get the power to upvote even more answers that you find good and helpful. maybe more people will be inclined to answer your new questions then... if they are not just copy-pastes of the assignments you get, of course. like this one looks like. for instance, what is `next`? what is `head`? `goal`? `current`? If you wrote this code, you ought to know. If not, try writing something *yourself*, and edit it in. You can do it! – Will Ness Apr 22 '18 at 11:11
  • also, [mcve], always. – Will Ness Apr 22 '18 at 11:23

0 Answers0