473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 14689
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.