Environment: eclipse

I have reached a snag with finding the perfect numbers from 0 - 1000 while using methods with multiple perimeters, one project, tow classes. There is an example on this site from quite a while ago within one single class. That is in a later chapter in my book, How to Program 8th edition and I have not learned it yet. The purpose of this exercise is to use declare and use multiple methods to find perfect numbers.

This is what i have so far:

import java.util.Scanner;

public class perfFinder {
    public void doPerfNum() {
        // find all the perfect numbers 1-upper range

        int j=1;
        boolean ans;
        Scanner inp = new Scanner( System.in );

        System.out.print("Enter an upper range: ");
        int upperRange = inp.nextInt();

        while(j<=upperRange) {
            ans = isPerf(j);
            if(ans)
                System.out.println("Yes, it is perf "+j);
            j++;
        }//end while    

    }//end main

    public boolean isPerf(int n){
        if( n==6 || n==28 )
            return true;
        else
            return false;
    }// end isPerf

}//end class perfNumEx

Driver class:

import java.util.*;
public class perfNumEx {

    public static void main(String[] args) {
        // create an object to find perfect numbers
        perfFinder findThem = new perfFinder();
        //call the method to "get" the process started
        findThem.doPerfNum();
    }//end main
}//end class perfNumEx

This was given as an example and I must modify/recreate it to calculate the other perfect number, 496 using an algorithm. My question is what equation do I use to find it and where does it go?

Thanks in advance

Quoth wikipedia:

"In mathematics, a perfect number is a positive integer that is the sum of its proper positive divisors, that is, the sum of the positive divisors excluding the number itself."

So, if you had a method to find the proper positive divisors of a number, you could add up the results and check them against your original number.

So there's your answer:

boolean isPerfect(int n)
{
  int sum;
  for (int i: properDivisors(n))
  {
     sum+= i;
  }
  if (sum == n) return true;
  else return false;
}

That just leaves you to write "properDivisors(int n)" which returns an array of int.

commented: Nice! +4

Where does that code fragment go?

If Im calculating the perfect numbers do i still need this:


public boolean isPerf(int n){
if( n==6 || n==28 )
return true;
else
return false;
}// end isPe

Well, that's just a stub - it's there to give show you where your code fits into the larger program. It'll return correct results by checking to see if the value submitted as a parameter is one of the values it knows about - if not, it returns "false". This is true for many cases, but it's not a calculation.

So you can replace the code in that method with the code I gave you for "isPerfect()" and it'll work - after you write a method to determine the proper divisors of a given integer.

hello there :)
i'll be joining a programming competition next week.. so wish me luck :3
anyways, during the eliminations in our school which i won,
this PERFECT NUMBER problem came out.
and here was my solution:

import java.util.Scanner;
public class Perfect {
	public static void main(String[] args) {
		Scanner en = new Scanner(System.in);
		System.out.println("Enter a number and let's see if it's perfect:");
		int num = en.nextInt();
		int sum = 0;
		for(int i=1; i<num; i++){//A
			if(num%i==0){ //B
				sum+=i; //C
			}
		}
		//D
		System.out.println((num==sum)? "PERFECT!": "NOT PERFECT.");
}
}

It has four points of interest:
in //A, i want to loop from 1 up to the inputted number - 1.
it's because i want to test all these numbers if they are divisors of the inputted number.
this test can be seen in //B.
all numbers that pass the test in //B are all divisors of the input number.
thus in //C, those numbers that passed the test are all summed up together.
and lastly in //D, if the SUM of all the divisors (numbers that passed the test) is equal to the input number, then it is PERFECT!
i used the ternary operator (...?...:...) for //D but you can have it in a simple if-else. :)

So now you have a code fragment to test if a number is PERFECT!
i hope u know what to do with it...
best of luck! :3

seriously,
6 and 28 aren't the only perfect numbers
ahaha!

I am well aware the 6 and 28 and not the only perfect numbers. I'm not sure how you remotely fount that amusing.

If you read the the first post I can make it run with a loop but the exercise is with methods and functions, two classes one project.

it was an example. End of story.

dennisme- did my decomposition of the problem give you any ideas? That's a good sort of approach for this sort of problem. If yuo can figure out how to solve the sub-problem, then you're in business

so this is the code updated:


import java.util.Scanner;

public class perfFinder {
public void doPerfNum() {
// find all the perfect numbers 1-upper range

int j=1;
boolean ans;
Scanner inp = new Scanner( System.in );

System.out.print("Enter an upper range: ");
int upperRange = inp.nextInt();

while(j<=upperRange) {
ans = isPerf(j);
if(ans)
System.out.println("Yes, it is perf "+j);
j++;
}//end while

}//end main

public boolean isPerf(int n){
if( n==6 || n==28 )
return true;
else
return false;
}// end isPe

}//end class perfNumEx

-----------------

driver

------------------------

import java.util.*;
public class perfNumEx {

public static void main(String[] args) {
// create an object to find perfect numbers
perfFinder findThem = new perfFinder();
//call the method to "get" the process started
findThem.doPerfNum();
}//end main
}//end class perfNumEx

You said that I have to write a program that shows the proper divisors of a given integer. But the exercise does not require the factors only the perfect numbers. So this should work?

What is a perfect number? There's a mathematical definition, which serves as a specification of the problem you're trying to solve. What is that definition?

its a number like 6 where its factors 3, 2, 1 add up to 6. 28 is the next one and than 496 in the 0 - 1000 range.

Right, good. So if you had a method that gave you the factors of a number, you could just add them up and see if the sum equals the number, no?

ya that should work

So go back and look at my first response - that's how I'd suggest you attack the problem.

Now, the method for getting the factors, that's an interesting problem as well. I haven't really given that a lot of thought, but you might twist the sieve method of finding the primes to suit your purpose. Just a thought - there might be a better way.

"That just leaves you to write "properDivisors(int n)" which returns an array of int. "

we have not learned arrays yet. is there any other ways to do it?

Easiest with arrays - that's how you make up a collection of numbers to return.

Can you get hip to arraylists? You'd have to use autoboxing to turn the integers into Integers, but that's okay.

Without access to some sort of collection, my solution becomes more difficult.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.