Perfect and Prime numbers

This is a discussion on Perfect and Prime numbers within the C++ Programming forums, part of the General Programming Boards category; I have an assignment that requires one to enter an integer, and then have the program say if it's either ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    5

    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;
    }
    Last edited by taggrath; 10-20-2009 at 09:52 PM.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    Code:
    What's wrong here?:
     
    if (!prime) {
        cout << "The number is prime.";
    }
    There's probably more, but that's one thing.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    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. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    37
    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;
    			}
    
    
    	  }
    
    
    	}
    Last edited by punkywow; 10-22-2009 at 02:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. prime number finder.
    By Aalmaron in forum C++ Programming
    Replies: 2
    Last Post: 03-05-2004, 03:56 PM
  2. Small program, PRIME/ PERFECT SQUAREs
    By thynksheraze in forum C++ Programming
    Replies: 6
    Last Post: 01-15-2003, 08:54 AM
  3. Replies: 3
    Last Post: 01-14-2003, 09:34 PM
  4. Perfect Number Problem
    By TrazPFloyd in forum C++ Programming
    Replies: 21
    Last Post: 10-20-2002, 11:09 AM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 04:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21