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?
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?
Hmm...lemme see:
You're a programmer - right?
You write programs - right?
So how about just "writing" one instead of "looking" for one...
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
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.
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?Well, thats what you asked for, but it doesn't do very muchCode:#define TRUE 1 #define FALSE 0 int MyFunc(int i) { return (TRUE); /* or FALSE, if you want */ }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Well start with what you are trying to do.
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.
nice code hammer i just love what it does i'm pretty sure he means something more like this:
well it at least takes in a numberCode:#define true 1 #define false 0 int myfunc (number) { if(scanf("%d",&number)==1) return true; else return false; }
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
My best code is written with the delete key.
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?
ThanksCode:#include <stdio.h> void main() { int i, integer[10000], array[10000], total=0; float temp[10000], orig, result[10000]; char answer; while (answer !='n') { 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"); scanf("%c", &answer); } }
OK....
>void main()
Nope, not good. Use int main(void)
>char answer;
>while (answer !='n')
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.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]