# Thread: looking for a function:

1. ## looking for a function:

Anyone know where i might find a Perfect number function I could add to my program that basically will take an integer an return true or false?

2. Hmm...lemme see:

You're a programmer - right?

You write programs - right?

3. ## well...

I wouldn't call myself a programmer. I more of a piece together type. I never claimed to be a programmer. just looking for a function to put into another program i am editing. I'm still in the early stages of reading code and getting things to work.

4. ## Re: looking for a function:

Originally posted by Unregistered
Anyone know where i might find a Perfect number function I could add to my program that basically will take an integer an return true or false?
Code:
```#define TRUE 1
#define FALSE 0

int MyFunc(int i)
{
return (TRUE);  /* or FALSE, if you want */
}```
Well, thats what you asked for, but it doesn't do very much

Write out, in plain english, how to determine if a number if perfect or not.

Then try and abstract that into pseudocode (google that if you need to know what it is)

Once you have made a legitimate attempt to do that, and come up against a wall, post what you have and your quandry.

If you are just looking for an algorithm, instead of the knowledge of how to make that algorithm, then you are on the wrong board. Try sourceforge.

6. nice code hammer i just love what it does i'm pretty sure he means something more like this:

Code:
```#define true   1
#define false  0
int myfunc (number)
{
if(scanf("%d",&number)==1)
return true;
else
return false;
}```
well it at least takes in a number

7. A number is perfect if it is equal to the sum of its divisors, like so:

6 is perfect: 6 = 1 + 2 + 3.
28 is perfect: 28 = 1 + 2 + 4 + 7 + 14.

How do you find a perfect number? Simply find all of its divisors and add them together, if the two numbers match then it's perfect. This sounds like homework and is simple when you understand the problem, so I'll leave the coding part up to you.

-Prelude

8. I don't mean to bump this unnecessarily (sorry to offend anyone) but I have just been bitten by this problem and got an answer!

What I was wondering is is this good code?

Code:
```#include <stdio.h>

void main()
{
int i, integer[10000], array[10000], total=0;
float temp[10000], orig, result[10000];

{
printf("\nEnter a number:\n");
scanf("%f", &orig);

total=0;

for(i=0;i<10000;i++)
{
temp[i]=0;
array[i]=0;
integer[i]=0;
result[i]=0;
}

for(i=1;i<orig;i++)
{
temp[i]=orig/i;
integer[i]=orig/i;
}

for(i=1;i<orig;i++)
{
result[i]=temp[i]-integer[i];
}

for(i=1;i<orig;i++)
{
if(result[i]>0)
array[i]=0;
else
array[i]=1;

}

for(i=1;i<orig;i++)
{
if(array[i] !=0)
total=total+i;

}

if(total==orig)
printf("\nThe number %.0f is PERFECT!!\n", orig);
else
printf("\nThe number %.0f is not perfect, sorry!\n", orig);

flushall();
printf("\nAgain? (y/n)\n");
}
}```
Thanks

9. OK....

>void main()
Nope, not good. Use int main(void)

You defined answer, then tested it's value. How did you know it was or wasn't already n? I don't believe this type of variable gets initialised to a default value, you'll get whatever is in that memory location when the program starts.

>scanf("%f", &orig);
Not good. scanf() is pretty poor at reading user data, and if you are going to use it, you must check it's return code to see what it actually did for you.

>for(i=1;i<orig;i++)
i is an int, orig is a float. Not sure of the consequences without going through what your code is really doing, but normally you wouldn't compare different variable types in a loop like this if you could help it.

>missing return(0);
main() should return something to the OS. Add a return statement at the end.

That's a few pointers for you. I've only looked at the syntax though, not the maths involved (I can't do maths at @ 2am!) Maybe someone else will advise you on that front.