Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Nibbles

is x a perfect number?

This topic is 6109 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I''m trying to write a program that will check to see if the user inputed variable is a perfect number. this is basically what I have so far, but I''m getting weird results:
  
long number;
long i, j;
int d=0;
long divisor[] = new long[1000];

// loop through answers

for (j=1;j<number;j++)
{
	// loop through divisors

	for (i=1;i<number;i++)
	{
		if ((number / i) == j && j < number)
		{
			divisor[d] = j;
			d++;
		}
	}
}
  
All this does, is loop through all possible divisors, check to see if number divided by i will equal j, and if so, make divisor[d] = j. I''m not sure that makes too much sense, but if you think you might be able to help me, please do. Thanks, Scott

Share this post


Link to post
Share on other sites
Advertisement
You'll find some interesting information on Perfect numbers here. Pay special attention to the Euclidean theorem later completed by Euler that says

If 2k - 1 is prime, then 2k-1 (2k - 1) is perfect and every even perfect number has this form.

The existence of odd perfect numbers is still unconfirmed. If you find one, you might end up a celebrity.

Edit: formatting.

Edited by - Oluseyi on October 29, 2001 2:16:50 PM

Share this post


Link to post
Share on other sites
The following code determines the perfection of a number.

    
bool isPerfect (double x) {
double j, t = 1;
for (j = floor(sqrt(x)); j > 1; --j)
if (x == floor(x / j) * j)
t += j + floor(x / j);
if (t == x)
return true;
return false;
}


Note that the loop starts from sqrt(x). This makes the loop run in sqrt(n) time, rather than n time. When a divisor is found we add not only the divisor, but x/divisor. This is because if x is divisible by n, it must be divisible by x/n.

With 6, for example, the loop starts at 2, which 6 is divisible by, so we add 2 and 6/2 - 3 - to the total. The final result is 6. Thus, 6 is perfect.

It's pretty fast. I haven't done any proper timing tests on it, but it can determine the perfection of 8589869056.0 without any noticable pause.

I'd be interested to know why you need to test the perfection of a number.

'Nuff said. I'll enjoy watching you live, demon.

Edit: Fixed a barrel-load of typos.

Edited by - Mayrel on October 29, 2001 3:29:37 PM

Share this post


Link to post
Share on other sites
wow, that looks a lot better than I was doing it, however I''m using Java for this, and i''m not sure what the equivalent of floor is in java, whether it even exists.

I''m writing this for a computer science class. I''m basically just writing some functions of different math formulae/techniques that nobody likes doing by hand.

Thanks,
Scott

Share this post


Link to post
Share on other sites
Java has lots of commonly used math functions, including floor in the Math package. Floor is given by double Math.floor ( double ). See http://java.sun.com/j2se/1.3/docs/api/java/lang/Math.html

Share this post


Link to post
Share on other sites
finally works! thanks, and btw, that is fast!
Now lets see if I can figure out to see if a number is triangular or not

thanks again,
Scott

Share this post


Link to post
Share on other sites
In case anyone was wondering: floor(x) returns the nearest integer that is less than or equal to x. So floor(2.3) and floor(2.7) are both 2. The opposite is ceil(x), which returns the nearest integer that is greater than or equal to x. ceil(2.3) and ceil(2.7) are both 3.

Share this post


Link to post
Share on other sites
I''m using Java for this, and I removed the floor functions, and it still works fine for any numbers I''ve tried.

but thanks, and good to know.
Scott

Share this post


Link to post
Share on other sites
quote:
Original post by wojtos
I'm using Java for this, and I removed the floor functions, and it still works fine for any numbers I've tried.

but thanks, and good to know.
Scott


Wouldn't using the mod function be slightly easier? Perhaps more efficient?

Java:
  
boolean isPerfect(double x)
{
double check = 1;
for (double j = Math.floor(Math.sqrt(x)); j > 1; j--)
{
check += ((x % j) == 0) ? j + (x / j) : 0;
}

return (check == x);
}



Edited by - Spinal Confusion on October 29, 2001 4:56:58 PM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!