473,499 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loop, Perfect Numbers

I have to write a program that displays all of the perfect numbers less than
1000. I know I need 2 for loops and at least one if statement. What I need
to know is, is there any method in Java to test if a number is perfect?
Thanks.
Jul 17 '05 #1
8 14701
co*********@bellsouth.net wrote:
I have to write a program that displays all of the perfect numbers less than
1000. I know I need 2 for loops and at least one if statement. What I need
to know is, is there any method in Java to test if a number is perfect?
Thanks.


What do you mean "Perfect" number?
do you mean prime?
Jul 17 '05 #2
What do you mean "Perfect" number?

A perfect number is one that equals the sum of all the numbers that divide
evenly into it.

Example - 6 is perfect because 1, 2, and 3 divide evenly into it and their
sum is 6.
Jul 17 '05 #3
co*********@bellsouth.net wrote:
What do you mean "Perfect" number?

A perfect number is one that equals the sum of all the numbers that divide
evenly into it.

Example - 6 is perfect because 1, 2, and 3 divide evenly into it and their
sum is 6.


See http://home1.pacific.net.sg/~novelway/MEW2/lesson1.html

Class java.math.BigInteger has a method isProbablePrime(I)Z which would
probably work. :) However if you use Euclid's algorithm the largest number
you'll ever need to test for primality is 31, or 63 at the outside (by this
time it is obvious that the next perfect number will be bigger than 1000).
For numbers of that size almost any primality test you can think of will be
good enough ...

--
Chris Gray ch***@kiffer.eunet.be
/k/ Embedded Java Solutions

Jul 17 '05 #4
<co*********@bellsouth.net> wrote in message news:<wz*******************@bignews4.bellsouth.net >...
I have to write a program that displays all of the perfect numbers less than
1000. I know I need 2 for loops and at least one if statement. What I need
to know is, is there any method in Java to test if a number is perfect?
Thanks.


No there isn't, and that's the point of the exercise.

I won't write the program for you, but I'll show you how to go about
it. First, you need to scan through all numbers, checking if they are
perfect.

i.e.
For Each Of These (hint) candidate perfect numbers, you need to go
through all the numbers below it to see if it is a factor.

For Each Of These (hint) candidate factors, you need to check if it is
a factor, and if it is, then add it to a running total.

to follow the instructions as a computer would:

For each candidate perfect number:
start with a running total of zero

For each candidate factor:
is the candidate perfect number divisible by the candidate factor?
(you need to do some maths here)
if so, then add the factor to the running total
Go back to next candidate factor

All possible factors have now been checked,
and the running total is now the sum of all factors.
Is the running total equal to the candidate perfect number?
if so, the candidate IS a perfect number!
Advance to next candidate perfect number

I hope you see how this set of instructions would guide a
non-mathematician to find all the perfect numbers.
Jul 17 '05 #5
On Sat, 13 Mar 2004 09:08:09 -0500, coxmarshal wrote:
I have to write a program that displays all of the perfect numbers less than
1000. I know I need 2 for loops and at least one if statement. What I need
to know is, is there any method in Java to test if a number is perfect?
Thanks.


Here, Try this...
//

procedure DoYourOwnHomework( int DONE ) {
const HARDWORK=1;
for (int STUDY=0; (STUDY = STUDY + HARDWORK); STUDY < DONE)
{
OpenBook();
ReadText();
Think();
}
if (UNDERSTOOD)
{
WriteProgram();
TurnItIn()
}
else
{
Worry();
Kvetch();
DropOut();
}
}

Jul 17 '05 #6
co*********@bellsouth.net wrote:
I have to write a program that displays all of the perfect numbers less than
1000. I know I need 2 for loops and at least one if statement.

<snip>

You don't need any loops or ifs.

public class PerfectNumbers {
public static void main(String[][] a) {
System.out.println(6);
System.out.println(28);
System.out.println(496);
}
}

:-)

Stewart.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 17 '05 #7
Ian
While I agree that you should do your own homework, I assume that its
a Java assignment and not a Math one ! therefore a little hint wont
hurt. Euclid had come up with an algorithm to generate perfect
numbers. You can find more details here:

http://www-gap.dcs.st-and.ac.uk/~his...umbers.html#s1

This extract should help though:

There exists an elegant and sure method of generating these numbers,
which does not leave out any perfect numbers and which does not
include any that are not; and which is done in the following way.
First set out in order the powers of two in a line, starting from
unity, and proceeding as far as you wish: 1, 2, 4, 8, 16, 32, 64, 128,
256, 512, 1024, 2048, 4096; and then they must be totalled each time
there is a new term, and at each totalling examine the result, if you
find that it is prime and non-composite, you must multiply it by the
quantity of the last term that you added to the line, and the product
will always be perfect. If, otherwise, it is composite and not prime,
do not multiply it, but add on the next term, and again examine the
result, and if it is composite leave it aside, without multiplying it,
and add on the next term. If, on the other hand, it is prime, and
non-composite, you must multiply it by the last term taken for its
composition, and the number that results will be perfect, and so on as
far as infinity.

Good luck !

Ian
Jul 17 '05 #8
Ian wrote:

<snip>
There exists an elegant and sure method of generating these numbers,
which does not leave out any perfect numbers and which does not
include any that are not; and which is done in the following way.


You mean someone's finally solved the mystery of the odd perfect number?
Do you have a source for this?

Stewart.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
2553
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
19
4408
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an...
14
2645
by: dawnerd | last post by:
Hi, I am developing a CMS and came across something which has never happened to me before, and I re-wrote the specific script twice, both differently, and still had the same error. I'm not sure...
3
6191
by: vduber6er | last post by:
Lets say I have this structure: typedef struct numbers { double first = 0.0; double second = 0.0; double third = 0.0; } MYVALUES; and I initialize an array of MYVALUES like the following
6
1850
by: Alenik1989 | last post by:
ok i have this code that executes for all # between 0 and 1001 perfectly. it is a cod to find the factors of inputed data and then determine it as a perfect number or not, also it suppose to loop...
2
12666
by: wkid87 | last post by:
Using C# Microsft Visual C#, Console Application I'm needing help with the user entering in a number to tell if it perfect or not. I have the perfect number figure out. Here in the instruction: ...
20
3066
by: Constantine AI | last post by:
Hi I have this code which i thought was perfect maybe a bit of a mess but it works. The only problem is that the rst!PONo does not look at the table properly and generate a continuing number, which i...
12
2424
by: hipfunkyjive | last post by:
Basically my program has to find perfect numbers in a range 1- 1000 it works , kinda but it displays a few numbers that are not perfect for example the first Perfect numbers are : 6 28 496 my...
17
9291
by: nickels | last post by:
-Alright im supposed to make a program that tests for perfect numbers. I have to use 2 methods and 3 methods if you count the main method. -My first method has to return a boolean value of true...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.