Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Prime number program?

237 views
Skip to first unread message

Oly

unread,
Dec 4, 1999, 3:00:00 AM12/4/99
to
Please help!

I have to build an Ada program which tells the user if the number which they
have entered is a prime number or not.
I am still a newbie at Ada (and crap at maths)..and have spent many wasted
and frustring hours trying to solve the problem.

Please help!


(o_...@hotmail.com);

Robert L. Klungle

unread,
Dec 4, 1999, 3:00:00 AM12/4/99
to
Oly wrote:

Hint: a prime number is any positive integer which is only divisible,
with no remainder, by one (1) and itself.


Larry Kilgallen

unread,
Dec 4, 1999, 3:00:00 AM12/4/99
to
In article <82bk0d$55b$1...@saturn.bton.ac.uk>, "Oly" <o....@bton.ac.uk> writes:
> Please help!
>
> I have to build an Ada program which tells the user if the number which they
> have entered is a prime number or not.
> I am still a newbie at Ada (and crap at maths)..and have spent many wasted
> and frustring hours trying to solve the problem.
>
> Please help!

If you don't know the math regarding prime numbers, you cannot
succeed on such an endeavor no matter what programming language
you use.

Thus, I think this is not really an Ada question, and something
more appropriate to some math discussion forum.

When you resolve your math question, any introductory Ada book
should show you how basics like addition, subtraction, etc. work.

Larry Kilgallen

E. Robert Tisdale

unread,
Dec 4, 1999, 3:00:00 AM12/4/99
to
Oly wrote:

> I have to build an Ada program which tells the user
> if the number which they have entered is a prime number or not.
> I am still a newbie at Ada (and crap at maths)

> and have spent many wasted and frustrating hours


> trying to solve the problem.

I used Lycos to search for +prime +number +algorithm
and I found lots of stuff including

http://www.utm.edu/research/primes/

It is very hard to determine whether or not a number n is prime
and that is one of the reasons why prime number figure
very prominently in encryption algorithms.
Suppose that the square root of n is not an integer.
Then you will need to test whether n/p is an integer
for every prime number p < sqrt(n)
which means that you need a list
of all the prime numbers p < sqrt(n).
Programs which find all the prime numbers
in a range of integers are called prime number sieves.
The one on my computer is called "primes."

$ primes 0 10
2
3
5
7
$

Hope this helps, E. Robert Tisdale <ed...@netwood.net>


sk...@my-deja.com

unread,
Dec 6, 1999, 3:00:00 AM12/6/99
to
In article <82bk0d$55b$1...@saturn.bton.ac.uk>,
"Oly" <o....@bton.ac.uk> wrote:
> Please help!

>
> I have to build an Ada program which tells the
user if the number which they
> have entered is a prime number or not.
> I am still a newbie at Ada (and crap at
maths)..and have spent many wasted
> and frustring hours trying to solve the problem.
>
> Please help!
>
> (o_...@hotmail.com);
>
>
Here is a simple procedure in C that returns if
a number is prime. I had a similar Ada program
but I can't find it. Hope this helps.
/* return if a number is prime or not */
int IsPrime( int y) {
int rema;
int counter = 0;
int n;
for (n =1; n <= y; n++) {
rema = y%n;
if (rema == 0)
counter = counter + 1;
}
if (counter <= 2)
return 0;
else
return 1;


Sent via Deja.com http://www.deja.com/
Before you buy.

Robert I. Eachus

unread,
Dec 13, 1999, 3:00:00 AM12/13/99
to

Oly wrote:

> I have to build an Ada program which tells the user if the number which they
> have entered is a prime number or not.
> I am still a newbie at Ada (and crap at maths)..and have spent many wasted
> and frustring hours trying to solve the problem.

I have always liked this method for testing primality. But I suggest
you study both Ada and Number Theory before handing this example in as
your own work...

with Ada.Text_IO, Ada.Integer_Text_IO;
procedure Prime is
-- a program using Wilson's Theorem to test for primality. May fail for
-- numbers greater than 2**(Natural'Size/2).
-- Enter zero or a negative value to exit.
P: Integer;
Temp: Integer;
use Ada.Text_IO, Ada.Integer_Text_IO;
begin
loop
Put_Line("Enter candidate:");
Get(P);
Skip_Line;
exit when P <= 0;
Temp := 1;
for I in 2..P-1 loop
Temp := (Temp*I) mod P;
end loop;
if Temp = P-1
then Put(P,12); Put_Line(" is prime.");
else Put(P,12); Put_Line(" is not a prime.");
end if;
end loop;
Put_Line(" All done.");
exception
when others => Put_Line(" Ooops!");
end Prime;

--

Robert I. Eachus

with Standard_Disclaimer;
use Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...

0 new messages