[CSC 330] A LISP Starting Point
Andrew J. Pounds
pounds_aj at mercer.edu
Fri Sep 30 16:10:56 EDT 2022
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.
#!/usr/bin/sbcl --script
;;; Here is the function to print the prime numbers
(defun printprimes (num1 num2)
(loop for i from num1 to num2 by 1 do
( when (isprime i) (print i) )
)
(terpri)
)
;;; Here is the function to determine if a number is prime
(defun isprime ( num )
(let (( numDivisors 0))
(loop for i from 1 to num by 1 do
( when ( = (mod num i) 0 )
( setf numDivisors (+ numDivisors 1))
)
)
;;; This last function should return true for primes
(= numDivisors 2)
)
)
;;; Here is the main program
(progn
;;; Let's define variables to avoid compiler warnings
(defvar num1)
(defvar num2)
(princ "Enter the first number:")
(terpri)
(setf num1 (read))
(princ "Enter the second number:")
(terpri)
(setf num2 (read))
(printprimes num1 num2)
)
--
*/Andrew J. Pounds, Ph.D./*
/Professor of Chemistry and Computer Science/
/Director of the Computational Science Program/
/Mercer University, Macon, GA 31207 (478) 301-5627/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://theochem.mercer.edu/pipermail/csc330/attachments/20220930/3d887fd0/attachment.html>
More information about the csc330
mailing list