# Thread: Perfect and Prime numbers

1. ## Perfect and Prime numbers

I have an assignment that requires one to enter an integer, and then have the program say if it's either a prime number, or a perfect number, and it must show all the factors of every number that isn't prime.

So far, I have written this, but it always says the number is prime and not perfect(even when it is perfect), and then just tells me the factors.

It's also supposed to loop back to the beginning and ask for the user to re-input another number, but doesn't achieve that as well.

I've been trying to get this to work for hours.

Any help would be much much much appreciated. Thanks .
Code:
```
#include <iostream>

using namespace std;

int main ()
{
int n,i,sum=0;
bool notprime,perfect,isvalid;
char s='s';
cout << "Please enter a positive integer between 1-1000 to see if it is a prime"<< endl
<< "or perfect number, and what its factors are"<<
"Enter the s key to terminate the program";

cin>>n;

while (n!=s)
{

bool notprime=false;
for(i=2;i<n;i++)
{
if(n%i==0)
bool notprime=true;
break;

}
if(!notprime)
{
cout << "the number entered is prime";
}

else
bool perfect=false;
for(i=2;i<=n;i++)
{
if(n%i!=0)
continue;
sum+=n/i;
if(sum==n)
{	bool perfect=true;
break;
}
}

if(perfect)
{
cout<< "the number entered is perfect, and its factors are"<<endl;

for(i=1;i<=n;i++)
{
if(n%i==0)
cout << n/i <<endl;

}

}

else
{
cout << "The number entered is neither perfect nor prime, but its factors are";

for(i=1;i<=n;i++)
{
if(n%i==0)
cout << n/i <<endl;
}

}

cout << "Enter another number between 1-1000 to find if its prime or perfect";
cin>>n;
}
return 0;
}```

2. Code:
```What's wrong here?:

if (!prime) {
cout << "The number is prime.";
}```
There's probably more, but that's one thing.

3. You have all the right parts, yet the solution is unnecessarily complex.

A more concise solution. It only does one number, you should be able to make it repeat until the user types in an acceptable number.

Code:
```	int number;
cin >> number;

vector <int> factors;
int factorSum = 0;
for (int i = 1; i < number; ++i) {
if (number % i == 0) {
factors.push_back (i);
factorSum += i;
}
}

// Prime. Number has only one factor.
if (factors.size () < 2) {
cout << "Number is prime.\n";
}

// Perfect. The sum of the factors equal the number.
if (number == factorSum) {
cout << "Number is perfect.\n";
} else if (factors.size () > 1) {
// Neither.
cout << "Number is neither prime, nor perfect.\n";
}

cout << "Factors:\n";
for (int i = 0; i < factors.size (); ++i) {
cout << factors[i] << endl;;
}```

4. to dedect primes very fast code it can find all primes of 1000000 in about 5mins or lower with just one cpu core, may it can help u ...

Code:
```int c=0;
int zr=30; // this is the range for the  primenumber  search
for(int i=zr;i>0;i--)
{
c=0; // reset counter for the new number
for(int z=1;z<=zr;z++){
if(i%z==0){ // lets look if i % z = 0
c++;

}

if(c>2){ //if c is bigger than 2 we dont need to look at rest of the number it cant be a prime
break;
}

if(c<=2&&z==i){
//if the number can only be devided 2 times  and we are at the end of the number range  we found a
//prime number  as u remember the prime can only be devided by 1 and itself
printf(" (%d) ",i); // this stdc output slows down the whole search, if u wanna do a speedtest skip it!
break;
}

}

}```