• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A problem

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you will need a loop. The loop should step through the integers between 2 and the number/2 and sum those integers that evenly divide the number. Based on that sum you will be able to tell if the number is perfect, deficient, or abundant.

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 ]
 
apollo abel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that i dont understand the math behind this program.
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?
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The definition of perfect, abundant, and deficient provides you with the "formula" that you need. The important thing is adding up the numbers which are divisors of the number.
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The % operator gives the remainder from integer division.

For instance, 52 % 7 is 3.

That should help you determine if one number is a divsor of another.
[ April 04, 2006: Message edited by: Ryan McGuire ]
 
Ryan McGuire
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:
Yes you will need a loop. The loop should step through the integers between 2 and the number/2 and sum those integers that evenly divide the number.



Well...
Maybe from 1 to number/2
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ryan McGuire:


Well...
Maybe from 1 to number/2



Oops. you're right.

I was thinking about testing for divisibility and forgot 1 will automatically divide.
 
apollo abel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i know how to use the %. like A is a divisor of B if B5A == 0.
so lets say i create a loop:
for(int num = 1, num <=userInput, num++)
{
HERE I HAVE TO EVALUATE NUMBER??
}
how do i evaluate to get perfect, abundant or deficient.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You aren't going to be able to tell whether a number is perfect, deficient, or abundant until you have run through the whole loop.

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.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
apollo abel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an idea of how i see it could work:

what you guys think??
NOTE: the code is not working at all...
BTW, why number divided by 2??
thanks for the support.
[ April 04, 2006: Message edited by: apollo abel ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
apollo abel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, the approach i was getting to, is if an integer division doesnt leave any reminder then it is a divisor,
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 ]
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you're asking. You have almost everything done to solve the problem.

Note that there's no reason to test past half the number because half the number is the largest possible divisor.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
apollo abel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried and tried but i dont get anywhere.
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.
*/
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ==.)
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this is the solution ..............

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
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
anji_java
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for Ur suggestion Garrett. I am new to this site........
reply
    Bookmark Topic Watch Topic
  • New Topic