i have to write a program computes the sum of the divisors for each integer from one to N.

now i have 3 types of number.

perfect ---- 1+2+3 = 6.

abundant----1+2+3+4+6>12

deficient ---- 1+2+4+8<8.

i would know how to write the program. but i dont know how the math works in this case.

if i input 50.

i would need to create a counter that goes up to 50 and that evaluates each number. that smells like a for loop to me, however, any ideas how i would be able to separate the 3 types of numbers. because if there is any of all 3 types in 50, i need to find them and print them out.

I would recommend creating a method that accepts as input the number and returns the sum of its divisors.

[ April 04, 2006: Message edited by: Keith Lynn ]

lets say they enter 100.

i order for me to know which numbers are perfect. which numbers are abundant and which are deficient, i need a formula. i dont know how to get those numbers.

or lets say i create a counter that goes from 1 to 100 (100=N).

how do i check each and every number?

Originally posted by Ryan McGuire:

Well...

Maybe from1to number/2

Oops. you're right.

I was thinking about testing for divisibility and forgot 1 will automatically divide.

The pseudcode would be something like this

If sum = number, then number is perfect.

If sum < number, then number is deficient.

Otherwise number is abundant.

**Keith Lynn: I would recommend creating a method that accepts as input the number and returns the sum of its divisors.**

This sounds like a good starting point to me.

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter

Originally posted by apollo abel:

...the code is not working at all...

Suppose userInput is 6, and consider the statement

**if(num%userInput == 0)**as num goes from 1 to 6:

1 divided by 6 is 0 with a remainder of 1, so num%userInput is 1.

2 divided by 6 is 0 with a remainder of 2, so num%userInput is 2.

etc.

Do you see what's wrong with this?

Now, as to why userInput/2 would work as an upper limit for testing, consider this: If one number is

*more than half*of another (that is, greater than userInput/2), then is it possible for that number to be a proper divisor?

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org

so if 6%2=0, then 2 is a divisor. however,

6%1=0 --good,

6%2=0 --good,

6%3=0 --good,

6%4!=0 --not good,

6%5!=0 -- not good,

note: i cant do 6%6.

so then i need to get the good ones, and add them up, and if the sum of them = 6 then is a perfect number.

any ideas on how to solve it??

[ April 04, 2006: Message edited by: apollo abel ]

Originally posted by apollo abel:

...

6%1=0 --good,

6%2=0 --good,

6%3=0 --good,

6%4!=0 --not good,

6%5!=0 -- not good,...

Right, but this is

*not*what your code is doing. Your code will do this:

1%6 != 0 --not good

2%6 != 0 --not good

3%6 != 0 --not good

4%6 != 0 --not good

5%6 != 0 --not good

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org

Originally posted by apollo abel:

...

if(num%userInput == 0);

perfect = num%userInput;

...

I should also point out that there's a syntax problem (or maybe just a typo) in the line

**if(num%userInput == 0);**

If the boolean expression in an "if" statement evaluates to true, then the statement

*immediately following the parentheses*will execute. Normally, this is a statement

*block*denoted with braces, although it

*could*be a single-line statement without braces. But in your code, there is NO statement following the parentheses because you cut it off with a semicolon. The line following that will execute either way.

Also, assuming that the "if" condition was working, then you would be assigning a value to "perfect"

*only if that value is zero.*And perfect (zero) is what the method returns. I don't think that's what you really want. (

**Hint:**Take another look at Keith's pseudocode above.)

[ April 04, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org

i know the first part of the loop.

for(number=1; number <=userInput; number++)

so far, i see it will go one by one till it reaches the userInput, am i right??

Now, i have the concept, but i dont know how to put it in java language.

for(number=1; number<=userInput; numer++)

{

if(number%userInput=0)

/*

here i want to know which number i did the test for?? number one? and if i did test for number one, (since 6%1=0) how do i test for that and then get one apart from the others.......im not good with for loops this is really confusing, any help will be more apreciated.

*/

Originally posted by apollo abel:

...if(number%userInput=0)...

here i want to know which number i did the test for?? number one? and if i did test for number one, (since 6%1=0) ...

Again, this code does

*not*evaluate 6%1. Instead, it is evaluating 1%6. Do you see why?

The loop is constructed correctly. It's the logic inside of the loop that needs to be fixed.

(Note: The typo of = should be replaced with ==.)

*~Joe Strummer*

sscce.org

Also note that the logic here is wrong. A perfect number is one in which all the numbers factors added together add up to equal the number itself. In the case of the perfect number 6 in your first example the factors of 6 are {1, 2, 3}

1 + 2 + 3 == 6; <-- Therefore 6 is a perfect number.

You'll have to come up with some logic for adding all the factors together, and checking that sum against the original number.

because if there is any of all 3 types in 50, i need to find them and print them out.

Also note that every integer can be described as either perfect, abundant, or deficient.

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter

class abc

{

public static void main(String [] vc)

{

int num =50;

val=0;

for(i=1;i<=num/2;i++)

{

if(num%i==0)

val=val+i;

}

if(val==num)

System.out.prinln("perfect ");

else if(val>num)

System.out.prinln("abundant");

else

System.out.prinln("deficient ");

} // end of main

}// end of class

Originally posted by anjireddy pulicharla:

I think this is the solution ..............

Any one of us could have given away the solution hours ago if that was the aim of this site.

**It is not!**People here at the 'Ranch enjoy helping people learn. Doing someones homework doesn't help anyone learn anything.

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter