comp.lang.ada
 help / color / mirror / Atom feed
From: "Robert I. Eachus" <eachus@mitre.org>
Subject: Re: Prime number program?
Date: 1999/12/13
Date: 1999-12-13T22:55:15+00:00	[thread overview]
Message-ID: <38557AEE.B35C157D@mitre.org> (raw)
In-Reply-To: 82bk0d$55b$1@saturn.bton.ac.uk


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...




      parent reply	other threads:[~1999-12-13  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-12-04  0:00 Prime number program? Oly
1999-12-04  0:00 ` Robert L. Klungle
1999-12-04  0:00 ` Larry Kilgallen
1999-12-04  0:00 ` E. Robert Tisdale
1999-12-06  0:00 ` skamn
1999-12-13  0:00 ` Robert I. Eachus [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox