Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!bloom-beacon!gatech!pyr!jkr From: jkr@pyr.gatech.EDU (J. Kenneth Riviere (JoKeR)) Newsgroups: comp.lang.pascal Subject: Re: Perfect number program Message-ID: <4166@pyr.gatech.EDU> Date: Wed, 7-Oct-87 12:33:15 EDT Article-I.D.: pyr.4166 Posted: Wed Oct 7 12:33:15 1987 Date-Received: Sat, 10-Oct-87 09:52:03 EDT References: <9652@brl-adm.ARPA> Reply-To: jkr@pyr.UUCP (J. Kenneth Riviere (JoKeR)) Organization: Georgia Institute of Technology Lines: 25 In article <9652@brl-adm.ARPA> 11SSTEIN%GALLUA.BITNET@wiscvm.wisc.EDU writes: > I am working on a perfect number program and I have faced some problems. > I tried mine and it takes TOO long. I used this program: > > FOR I=2 TO 10000 DO > W=0 > FOR K=2 TO I DO > IF I/K = TRUNC (I/K) THEN W=W+(I/K) > IF W=I THEN PRINT W "IS A PERFECT NUMBER" > First of all, when you discover a divisor of I you have also discovered another divisor of I (when you find that 2 is a divisor of 28 it is clear that 28/2 means that 14 is also a divisor of 28). Thus you should be able to increment W by both K and I/K when your test is true. Furthermore, since you are checking both K and I/K it is not necessary for your inner loop to progress past the square root of I since you will only rediscover the same divisors from the other side (you would eventually find 14 again, but with my approach you shouldn't need to loop past 5 to find divisors for 28). Hope this helps. J. Kenneth Riviere (JoKeR) ISA, Georgia Tech, Atlanta, GA 30332 Internet: jkr@pyr.gatech.edu Bitnet: iadt1kr@gitvm1 uucp: ...!{decvax,linus,rutgers,ihnp4,hplabs,seismo}!gatech!gitpyr!jkr