-
Perfect number...
Does anyone knows why this isn't working?
I got a problem where I must make a program in C++ which finds the percet
number between 2 and number m(which user enters).
Anyway,I made the program to see if the number you entered is perfect number
but I can't make it so that it calculates which number between 2 and m is the
perfect.
Sorry my english is not so good. :(
Note:The perfect number is number which is equal to the sum of his (dividive
numbers?)
I'm sorry I don't know how to write it in english,but here is example:
28=1+2+4+7+14
Number 28 can be divided by number 2,4,7,14,28.Sum of that dividing is 28.
Here's the number which checks is the entered number(only one
number)perfect:
*********************
Code:
#include<iostream.h>
void main(){
int x,suma=0,number;
cout<<"Please insert number which will be checked if it is a perfect\n";
cout<<"number:\n";
cin>>broj;
for(int i=2;i<=number;i++){
if(broj%i==0){
suma=suma+(number/i);
}
}
if(suma==number)cout<<number;
cin>>x; //so that the program doesn't close
return;
}
********************************
And here is the program that isn't working somehow...:
*******************************
Code:
#include<iostream.h>
void main(){
int m,x,suma=0;
cout<<"Please enter number m:\n";
cin>>m;
for(int i=2;i<=m;i++){
for(int j=2;j<=i;j++){
if(i%j==0)
suma=suma+(i/j);
}
if(suma==i)
cout<<i<<endl;
}
cin>>x; //so that the program doesn't close
return;
}
***********************************
What do you think?
Please tell me,I'm very interested in this problem,and I've been smashing my
head for so long...
-
You must reset suma to 0 each time inside your first loop.
-
Code:
#include <iostream.h>
Should use this one if possible:
Code:
#include <iostream>
using namespace std;
Code:
void main()
{
...
}
The main function should always return an int:
Code:
int main()
{
...
return 0;
}
Code:
int x;
...
cin>>x; //so that the program doesn't close
Usually you would just do this:
Code:
cin.get(); // So that program does not close
That way you don't need an extra junk/garbage variable.
You should make a function out of your code that determines if a number is perfect or not. Have it return a bool (true/false) value. Also, your code does not look correct based on your description of what a perfect number is, here is some revised code:
Code:
bool IsPerfectNumber(int value)
{
int sum = 0;
for( int i = 1; i <= value/2; i++ )
{
if( value % i == 0 )
sum += i;
}
return sum == value;
}
Then you can call that function repeatedly inside of a loop check the numbers from 2 to m.
Code:
int m;
cout<<"Please enter number m:\n";
cin>>m;
for(int i=2;i<=m;i++)
{
if( IsPerfectNumber(i) )
cout << i << endl;
}
-
... and main always returns an int.
*edit* A point to which I was beaten...
And some info which may help you with your algorithm: http://www.answers.com/topic/perfect-number
In particular, the formula using Mersenne primes, the endings in 6 and 8, and no odd perfect numbers below an insanely large threshold.
Cheers
-
do not void main itll give you an error. just to tell you also your mixing C and C++ together.
\n works but its not the best way thats the C way
ex.
Code:
cout << "text\n";
// is the same as this in C++
cout << "text" << endl; //then endl is the same as \n
try reading thesse tutorials explicitly for C++ there great even a little better than this sites *SHH* for C++ that is but these forums are great!
http://cplus.about.com
-
There is one subtle difference between '\n' and endl (which surprisingly few books/tutorials seem to mention): endl inserts a new line, and flushes the buffer (which you could do explicitly). But, since you should flush the buffer, at least at the end of your outputting of stuff, endl is a very good way to do that.
Cheers
-
Code:
bool IsPerfectNumber(int value)
{
int sum = 0;
for( int i = 1; i <= value/2; i++ )
{
if( value % i == 0 )
sum += i;
}
return sum == value;
}
This algorithm could be made more efficient. Instead of looping up to n/2, it could loop up to the square root of n.
(We could get way more efficient than this, but why bother?)
Code:
bool IsPerfectNumber(int value)
{
int sum = 1;
int root = (int) sqrt((double) value);
int sq = root * root;
if (sq == value) {
return false;
}
++ root;
sq += 2 * root + 1;
/* sq now equals (root + 1) * (root + 1) */
/* I'm no expert on floating point errors, so I double check. */
if (sq == value)
return false;
/* Start at 2 because sum starts at 1. */
for( int i = 2; i < root; ++i )
{
if( value % i == 0 ) {
sum += i;
sum += value / i;
}
}
return sum == value;
}
Note that the perfect squares get ruled out right away because otherwise their square root would get double-counted in the sum, and that might be able to cause a false positive. (It might not be able to cause a false positive, but we don't know that.) No perfect square can be a perfect number anyway. This is provable. (The sum of the factors of an even perfect square is always odd; the sum of the factors of an odd perfect square is always even.)
(If you did not know this (and it really isn't healthy to know these things) you could still just make sure that in the case of a perfect square, you don't add the square root twice.)
-
If I may, I spent quite some time trying to do the same thing. The key to finding perfect numbers efficiently is to first find Mersenne primes. Every mersenne primes correlates to exactly one perfect number.
-
Wow!
Thanks everyone,you really helped me.Oh man,how could I forget to set the suma to 0 again!!??
And all this info about the thnigs I put and it could be better,thank you!