From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4117034ae215d480 X-Google-Attributes: gid103376,public From: "Robert I. Eachus" Subject: Re: Prime number program? Date: 1999/12/13 Message-ID: <38557AEE.B35C157D@mitre.org>#1/1 X-Deja-AN: 560283840 Content-Transfer-Encoding: 7bit References: <82bk0d$55b$1@saturn.bton.ac.uk> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@news.mitre.org X-Trace: top.mitre.org 945125715 23953 129.83.41.77 (13 Dec 1999 22:55:15 GMT) Organization: The MITRE Corporation Mime-Version: 1.0 NNTP-Posting-Date: 13 Dec 1999 22:55:15 GMT Newsgroups: comp.lang.ada Date: 1999-12-13T22:55:15+00:00 List-Id: 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...