<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<p><font face="serif">Based on conversations I've had today several
of you have a lot of coding to do to finish off the Happy
Numbers project. It's also evident that many of you haven't
even cracked open LISP. While we did a simple exercise in
class using LISP using arrays, we did not code the prime number
finder that we did in several other languages. I am providing
you here one I wrote a few minutes ago that uses the same
"logic" we used in some of the other languages. I wrote this so
it uses a main program, a function to print the prime numbers,
and a function to determine if a number is prime. It, along
with the prior LISP example, should provide you a good starting
point on your Happy.lisp code.</font></p>
<p><font face="monospace">#!/usr/bin/sbcl --script<br>
;;; Here is the function to print the prime numbers<br>
(defun printprimes (num1 num2)<br>
(loop for i from num1 to num2 by 1 do<br>
( when (isprime i) (print i) )<br>
)<br>
(terpri)<br>
)<br>
<br>
;;; Here is the function to determine if a number is prime<br>
(defun isprime ( num )<br>
(let (( numDivisors 0))<br>
(loop for i from 1 to num by 1 do<br>
( when ( = (mod num i) 0 )<br>
( setf numDivisors (+ numDivisors 1))<br>
)<br>
)<br>
;;; This last function should return true for primes<br>
(= numDivisors 2)<br>
)<br>
)<br>
<br>
<br>
;;; Here is the main program<br>
(progn<br>
;;; Let's define variables to avoid compiler warnings<br>
(defvar num1)<br>
(defvar num2)<br>
(princ "Enter the first number:")<br>
(terpri)<br>
(setf num1 (read))<br>
(princ "Enter the second number:")<br>
(terpri)<br>
(setf num2 (read))<br>
(printprimes num1 num2)<br>
)<br>
<br>
</font><br>
</p>
<div class="moz-signature">-- <br>
<b><i>Andrew J. Pounds, Ph.D.</i></b><br>
<i>Professor of Chemistry and Computer Science</i><br>
<i>Director of the Computational Science Program</i><br>
<i>Mercer University, Macon, GA 31207 (478) 301-5627</i></div>
</body>
</html>