PerfectNumber

Just wanted to get some help getting this program running

Page 1 of 1

2 Replies - 15981 Views - Last Post: 29 September 2007 - 07:06 PM Rate Topic: -----

#1 dontKnowJava  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 221
  • Joined: 29-September 07

PerfectNumber

Posted 29 September 2007 - 06:02 PM

public class aa
{
public static void main(String args[])
	{
	System.out.println("Perfect numbers from 1 to 650");
	test1();
	}


public static void test1()
	{
	int divisor;
	int number = 1;
	int sum = 0;
	for(divisor=1; divisor<number; divisor++)
		{
		if(number%divisor==0)
			{
			sum = sum + divisor;
			}
		}
	if(sum==number)
		{
		System.out.println(number + " is a perfect number");
		}
	}
}
[edit] added code tags PB


So what i need to do is find all perfect numbers between 1 and 650. A perfect number is a number whose divisors(except the number itself) equal to the original number. For example, divisors of 6 are 1,2, and 3. 1+2+3=6 so 6 is a perfect number. i figured out how to get the divisors of a number and add them together, but for some reason i cant increment to next number. If i set variable "number" in my formula for example to 6 it will print. I've tried putting an outer for and while loops and try to set outer loop counter to = number but it says variable not found. I was wondering is there a way to make a variable accessible to both outer and inner loop? Any help would be great. Thanks.

This post has been edited by PennyBoki: 30 September 2007 - 06:33 AM


Is This A Good Question/Topic? 0
  • +

Replies To: PerfectNumber

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4337
  • View blog
  • Posts: 12,137
  • Joined: 18-April 07

Re: PerfectNumber

Posted 29 September 2007 - 06:40 PM

Hi there,

Good work on the program. I had to modify only a few things. What you were missing was a loop to loop through the numbers. So notice the for statement in the main function to do this. You also needed to pass the number to your function for testing. The last thing I did was removed the divisor variable declaration and just put it right in the for statement. I did this because you don't use it anywhere outside the loop. So keep it as local as possible. It makes it more readable and cleaner looking. Removes a line of code and makes your for statement nice.

Here is the finished product. Test it and you should see it finds your perfect numbers perfectly! ;)

public class aa
{
	public static void main(String args[])
	{
		System.out.println("Perfect numbers from 1 to 650");
		
		// Loop through the numbers between 1 and 650
		for (int i = 1; i <= 650; i++) {
			// Pass the number to our function for testing.
			test1(i);
		}
	}

	// Notice how we pass the number to the function for then testing
	// in the for statement. 

	public static void test1(int number)
	{
		int sum = 0;

		// No need to create the divisor variable outside the for statement
		// divisor can be declared right in the for statement.

		for(int divisor=1; divisor < number; divisor++)
		{
			if ((number % divisor) ==0)
			{
				sum = sum + divisor;
			}
		}

		if(sum==number)
		{
			System.out.println(number + " is a perfect number");
		}
	}
}



I put a few in code comments that show you were I made some of the changes. Keep it up and enjoy the programming!

Enjoy!

At DIC we be coding ninjas! :snap:
Was This Post Helpful? 0
  • +
  • -

#3 dontKnowJava  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 221
  • Joined: 29-September 07

Re: PerfectNumber

Posted 29 September 2007 - 07:06 PM

View PostMartyr2, on 29 Sep, 2007 - 06:40 PM, said:

Hi there,

Good work on the program. I had to modify only a few things. What you were missing was a loop to loop through the numbers. So notice the for statement in the main function to do this. You also needed to pass the number to your function for testing. The last thing I did was removed the divisor variable declaration and just put it right in the for statement. I did this because you don't use it anywhere outside the loop. So keep it as local as possible. It makes it more readable and cleaner looking. Removes a line of code and makes your for statement nice.

Here is the finished product. Test it and you should see it finds your perfect numbers perfectly! ;)

public class aa
{
	public static void main(String args[])
	{
		System.out.println("Perfect numbers from 1 to 650");
		
		// Loop through the numbers between 1 and 650
		for (int i = 1; i <= 650; i++) {
			// Pass the number to our function for testing.
			test1(i);
		}
	}

	// Notice how we pass the number to the function for then testing
	// in the for statement. 

	public static void test1(int number)
	{
		int sum = 0;

		// No need to create the divisor variable outside the for statement
		// divisor can be declared right in the for statement.

		for(int divisor=1; divisor < number; divisor++)
		{
			if ((number % divisor) ==0)
			{
				sum = sum + divisor;
			}
		}

		if(sum==number)
		{
			System.out.println(number + " is a perfect number");
		}
	}
}



I put a few in code comments that show you were I made some of the changes. Keep it up and enjoy the programming!

Enjoy!

At DIC we be coding ninjas! :snap:




Thanks for help!

Thanks for your help!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1