From: Grant Henderson
Subject: Prime numbers program problems
Date: 
Message-ID: <_6866.15704$0x5.1329720@news3.cableinet.net>
Hi,

I've spent the last few hours knocking this up, but in my
short-sightedness I can't see why it doesn't work.
Its supposed to loop through all the numbers between 2 and the given
integer.
If the remainder is zero then its not a prime number.


(defun prime(n)
 (setq i 2)                    ;Set i as 2
 (prime2 i)                ;Pass i into the prime function
)
(defun prime2(i)
 if (eq (mod n i) 0)('NoPrime)        'If the remainder of the integer
divided by i is zero then print its not a prime
 (prime2(+ i 1))        ;Loop again incrementing i by 1

 if(= n i)('Exit)            ;Exit if i is equal to the integer being
checked
)

Thanks in advance
Grant

From: Peder O. Klingenberg
Subject: Re: Prime numbers program problems
Date: 
Message-ID: <uju7l46ihcr.fsf@false.linpro.no>
"Grant Henderson" <·········@cableinet.co.uk> writes:

> (defun prime2(i)
>  if (eq (mod n i) 0)('NoPrime)        'If the remainder of the integer
> divided by i is zero then print its not a prime
>  (prime2(+ i 1))        ;Loop again incrementing i by 1
> 
>  if(= n i)('Exit)            ;Exit if i is equal to the integer being
> checked
> )

You need to check the exit condition before you recurse, methinks.

...Peder...
-- 
No .sig for you
From: Janis Dzerins
Subject: Re: Prime numbers program problems
Date: 
Message-ID: <87u27agk72.fsf@asaka.latnet.lv>
"Grant Henderson" <·········@cableinet.co.uk> writes:

> Hi,
> 
> I've spent the last few hours knocking this up, but in my
> short-sightedness I can't see why it doesn't work.
> Its supposed to loop through all the numbers between 2 and the given
> integer.

Use LOOP.

> If the remainder is zero then its not a prime number.
> 
> 
> (defun prime(n)
>  (setq i 2)                    ;Set i as 2
>  (prime2 i)                ;Pass i into the prime function
> )

N is not used. Why do you (setq i 2) if all you do is to pass it to
PRIME2?

> (defun prime2(i)
>  if (eq (mod n i) 0)('NoPrime)        'If the remainder of the integer

(if (eq ...)
    ...
  ...)

'NoPrime is not a function object. If you want to return 'NoPrime,
then return it. You're trying to invoke it as a function.

The comment is not a comment at all.

> divided by i is zero then print its not a prime
>  (prime2(+ i 1))        ;Loop again incrementing i by 1
> 
>  if(= n i)('Exit)            ;Exit if i is equal to the integer being
> checked

This should be: (if (= n i) 'Exit)

> )

Man! You definitely should read some introduction to Lisp before you
attempt to write something in it. Looks like you don't understand the
very basics at all. I'd suggest reading some tutorials avaliable
online (some even with the ability to type and evaluate Lisp
expressions interactively).

Janis Dzerins
-- 
  If million people say a stupid thing it's still a stupid thing.
From: Hannah Schroeter
Subject: Re: Prime numbers program problems
Date: 
Message-ID: <93cop5$knm$1@c3po.schlund.de>
Hello!

In article <·······················@news3.cableinet.net>,
Grant Henderson <·········@cableinet.co.uk> wrote:
>Hi,

>I've spent the last few hours knocking this up, but in my
>short-sightedness I can't see why it doesn't work.
>Its supposed to loop through all the numbers between 2 and the given
>integer.

It'd be enough to loop to (ceiling (sqrt n)).

>If the remainder is zero then its not a prime number.

>(defun prime(n)
> (setq i 2)                    ;Set i as 2

Dirty: You touch a global variable i which isn't used any more below that.

> (prime2 i)                ;Pass i into the prime function
>)
>(defun prime2(i)
> if (eq (mod n i) 0)('NoPrime)        'If the remainder of the integer
  ^^ '(' missing before
      ^^ use eql
              ^ n isn't visible here.
                      ^^^^^^^^ No function called (quote noprime)
                               Why not return nil for false?
                                       ^^^ To-EOL-Comments are initiated
                                       with a semicolon (;)
>divided by i is zero then print its not a prime
> (prime2(+ i 1))        ;Loop again incrementing i by 1
Okay, tail recurse.

> if(= n i)('Exit)            ;Exit if i is equal to the integer being
            ^^^^^ Use t or any other *useful* non-nil value, not just
            a symbol unnecessarily polluting the namespace
>checked

You must move that check above the (if (eql (mod n i) 0) ...),
as n is always divisible by itself, i.e. for i = n, (eql (mod n i) 0)
is true and you return that n is no prime, even if it really is (n = 2
or n = 3, for example).

>)

Kind regards,

Hannah.