Stack overflow in relatively simple programs
- From: "robinganemccalla@xxxxxxxxx" <robinganemccalla@xxxxxxxxx>
- Date: 30 Dec 2006 17:18:07 -0800
Hi, I'm trying to write a program that will calculate large prime
numbers, I have written two separate programs that can calculate the
100th prime number but break down around the 500th, I have no idea
whether they are breaking down because the numbers are too big or
because the code is inefficient. For those interested, I'm using clisp
downloaded from http://www.gigamonkeys.com/lispbox/#download
Here's my code:
;first program, goes through all numbers incrementing numbers when it
finds a prime, when ;numbers = iterations (the number of the prime we
are looking for) we return the most recent prime ;number.
(defun new-primes (pnumber number iteration)
(cond
((= iteration number)
( - pnumber 1 ))
(t
(let ((prime 1))
;see if the number has any factors less than or equal to its square
root (ceiling rounds up a number)
(loop for x from 2 to (ceiling (sqrt pnumber))
do
(cond
((= 0 (mod pnumber x))
;if it does, set prime to 0 (does return leave the loop?)
(return
(setq prime 0)))))
(cond
((= prime 0)
;if the number is not prime, move on to next number
(new-primes (+ 1 pnumber) number iteration))
(t
(progn
;if it is prime, print it and increment the number of primes found
(format t "~A~%" pnumber)
(new-primes (+ 1 pnumber) (+ 1 number) iteration ))))))))
SECOND PROGRAM
;this uses a list of primes and sees if a number has factors in that
list, for some reason it
;did not work when I tried to add something to only check factors up to
the square root of the number
(setq prime-list '(2))
(defun primes (number iterations)
(cond
((= iterations 0)
prime-list)
(t
(progn
(let ((composite 1))
(loop for e in prime-list
do
(cond
;if the number is divisible by a prime
;we note it, and stop checking (hopefully that's what
;the return does)
((= (mod number e) 0)
(progn
(setq composite 0)
(return nil)))
;commented out square root stuff
;((> e (sqrt number))
; (return nil))))
))
;(format t "number is ~A list is ~A~%" number list)
;then we call the function again, with a larger number
;and cons the number to the prime list if appropiate.
(cond
((equal composite 1)
(progn
(setq prime-list (cons number prime-list))
(format t "~A~%" number))))
(primes (+ 1 number) (- iterations composite)))))))
Neither of these functions gets past primes in the 6000s... Am I
missing out on something tremendously inefficient in my code or is
their some problem with LISP? Any help would be greatly appreciated, I
don't want answers to the problem I just want to know why my answers
aren't working.
.
- Follow-Ups:
- Re: Stack overflow in relatively simple programs
- From: fairchild . anthony
- Re: Stack overflow in relatively simple programs
- Prev by Date: Matlab to Lisp converter?
- Next by Date: Re: Stack overflow in relatively simple programs
- Previous by thread: Matlab to Lisp converter?
- Next by thread: Re: Stack overflow in relatively simple programs
- Index(es):
Relevant Pages
|