i'm taking a computer programming class, and i was given a chalange to create a program in "C++" that would tell you if the number you entered was a Perfect number, being that the sum of all the factors of a number minus itself would = the original number.

aka. 6 is perfect because 1+2+3+6 - 6 = 6... the factors of six are 1,2,3 & 6

8 is not because 1+2+4+8-8=7, so it is deficent... see where i'm going?

anyways i need a program to calculate the factors, and then compare them to the original number, i allready have part of it, i have a program that will calculate some of the factors, but not all of them,

#include <iostream>
using namespace std;
int FactCount(int nLoopCount);
 int main()
{
 int input;
 cout << "Enter Number to be factored:\n";
        cin  >> input;
  FactCount(input);
return 0;
}                                     
 int FactCount(int nLoopCount){
 int nFirst;     
 if(nLoopCount > 1000000){
    nLoopCount = 0;
 cout << "Number to be factored must be less than 1000000:\n";
 }
 
     for (int j=2; j <= nLoopCount; j++){   
  for (int i=2; i <=nLoopCount; i++){
      nFirst = j*i;                 
  if(nFirst > 1000000)
   break;
 
  if(nFirst == nLoopCount){
 
 
  cout << j <<" * "<< i <<"  =  "<< nFirst << "  sum of factors =   "<< j+i <<
  "  difference of factors =  " << j-i << " , " << i-j << "\n";
 
 if(j < i)
 system("pause");
 break;
 return 0;
 
 }
 }
    }
 }

i would be greatful for any help provided.

A nudge for finding factors:

#include <stdio.h>

void factor(int value)
{
   int i;
   printf("factor(%d): ", value);
   for ( i = 1; i <= value; ++i )
   {
      if ( value % i == 0 )
      {
         printf("%d ", i);
      }
   }
   putchar('\n');
}

int main(void)
{
   factor(6);
   factor(28);
   factor(121);
   factor(5);
   return 0;
}

/* my output
factor(6): 1 2 3 6 
factor(28): 1 2 4 7 14 28 
factor(121): 1 11 121 
factor(5): 1 5 
*/

woah, thats awesome, thanks alot, do you know of a way to take all of the factors and add them up in a variable? thats kinda what i need to happen, but thankyou thankyou thankyou for this code, its helps me alot. :cheesy:

Yes, but why not take a stab at the two or three lines of code necessary to do so. Like say an initial sum of zero. And instead of printing a value, you add it to the sum. And perhaps skip the last iteration of the loop when i == value, and instead compare the sum to the value?

You could find all the mersenne primes then subtract 1 ;) or osmething like that.
Also, perfect numbers can only end in 6 or 28, so your search can be greatly expediated by eliminating all other numbers.

You could find all the mersenne primes then subtract 1 ;) or osmething like that.
Also, perfect numbers can only end in 6 or 28, so your search can be greatly expediated by eliminating all other numbers.

Great obfuscation that adds little to this thread. :rolleyes:

allright, so i'm not very good at the whole thing, but assuming taht the code i'm using to calculate all the factors is

int factor;
   printf("factor(%d): ", value);
   for ( factor = 1; factor <= value; ++factor )
   {
      if ( value % factor == 0 )
      {
         printf("%d ", factor);
      }
   }
   putchar('\n');

the line, ("factor(%d): ", value); is used where %d is the number being factored...

( factor = 1; factor <= value; ++factor ) is setting the first factor to 1, and adding numbers to the factor list, while

{
      if ( value % factor == 0 )
      {
         printf("%d ", factor);

is cheking all the numbers produced for remainders, and only allowing whole numbers...

am i close??

btw, CStalion, is isint about me knowing what the perfect number is, its about a program that uses factoring to figure them out.

the line, ("factor(%d): ", value); is used where %d is the number being factored...

Yes. (Although I was reading this wrong the first time, better wording could perhaps be like this:

The line printf("factor(%d): ", value); is used to display the value being factored.

( factor = 1; factor <= value; ++factor ) is setting the first factor to 1, and adding numbers to the factor list, while

Hm. Stop omitting key things like printf or for . This is a loop that exhausts all possible values from 1 to value.

{
      if ( value % factor == 0 )
      {
         printf("%d ", factor);

If the value is evenly divisible by factor, print the factor.

Great obfuscation that adds little to this thread. :rolleyes:

Perhaps to those unfamiliar with number theory. (Forgive me, I forget that not everyone loves math like I do.)
The mersenne prime statement was a joke, but the second part may actually be useful. Instead of brute forcing the program through all the integers, one could could create an if statement to skip through all numbers not ending in 6 or 28 (it is proven that all perfect numbers must end in 6 or 28, http://www-maths.swan.ac.uk/pgrads/bb/project/node6.html).
For example, 236 or 228 may be candidates for "perfection" because the first ends in '6' and the other in '28'. Skipping over all numbers not ending in 6 or 28 would save time and processing power, since the program would not have to run the check sequence for all integers, only a select few.

btw, CStalion, is isint about me knowing what the perfect number is, its about a program that uses factoring to figure them out.

Gotcha. Sorry for the misunderstanding.

236 or 228 may be candidates for "perfection" because the first ends in '6' and the other in '28'. Skipping over all numbers not ending in 6 or 28 would save time and processing power, since the program would not have to run the check sequence for all integers, only a select few.

its true that it would work, however saving proscessing power isint my goal, and it seems to be easyer to do the broadest and simplest search possible.;)

Dave, would it be possible to change

{
[LEFT]if ( value % factor == 0 )
{printf("%d ", factor);

to something that, insted of printing the number added it to a variable... like

{
      if ( value % factor == 0 )
      {
         finalfactor == finalfactor + ("%d ", factor);

[/LEFT]

Perhaps to those unfamiliar with number theory. (Forgive me, I forget that not everyone loves math like I do.)
The mersenne prime statement was a joke, but the second part may actually be useful. Instead of brute forcing the program through all the integers, one could could create an if statement to skip through all numbers not ending in 6 or 28 (it is proven that all perfect numbers must end in 6 or 28, http://www-maths.swan.ac.uk/pgrads/bb/project/node6.html).
For example, 236 or 228 may be candidates for "perfection" because the first ends in '6' and the other in '28'. Skipping over all numbers not ending in 6 or 28 would save time and processing power, since the program would not have to run the check sequence for all integers, only a select few.

Right. Does vissor3 seem to you to be a beginner just trying to understand looping and the % operator, or someone more advanced who will easily be able to pick off numbers ending in '6' or '28'?

Dave, would it be possible to change

{
if ( value % factor == 0 )
{printf("%d ", factor);

to something that, insted of printing the number added it to a variable... like

{
      if ( value % factor == 0 )
      {
         finalfactor == finalfactor + ("%d ", factor);

Yes. That's where I'm trying to steer you. You seem to find magic in things in parentheses. This is not a value: ("%d ", factor) (pedants, control thyselves); this is: factor . So that's what you want to add.

You're struggling quite a bit, but you're not that far off from this.

int i, sum = 0;
   for ( i = 1; i < value; ++i )
   {
      if ( value % i == 0 )
      {
         sum += i;
      }
   }

Right. Does vissor3 seem to you to be a beginner just trying to understand looping and the % operator, or someone more advanced who will easily be able to pick off numbers ending in '6' or '28'?

I'm not sure what his skill level is, I was just suggesting an alternative to brute force computing if he was interested in such alternatives.
About picking off numbers ending in 6 or 28, the 'if' statement following would work

int n;
 
if( ((n%10)==6)||((n%100)==28) )
{
     //code for testing "perfection"
}
else
{
     //continue loop, n++, or whatever
}

Anyways, since he has nicely said that he is not interested in this alternative I will not take the subject further in this thread.

hey, CStalion, i really do apreciate the time youv'e spent trying to help, and i have no doubt that your way works, its just that with the limited understanding i have with C++, picking specific numbers to check for is still a little beyond me, i know how it would work, but not how to implement it;) But thanks again CStalion...

dave, i got the final code

#include <iostream.h>
#include <cstdlib>
#include <stdio.h>
void factor(int value)
{
   int factor;
   printf("factor(%d): ", value);
   for ( factor = 1; factor <= value; ++factor )
   {
      if ( value % factor == 0 )
      {
         printf("%d ", factor);
      }
   }
   putchar('\n');
}
int main(void)
{
   int number;
   int totalfactor = 0;
   cout << "Enter Number: ";
   cin >> number;
 
   int i, sum = 0;
   for ( i = 1; i < number; ++i )
   {
      if ( number % i == 0 )
      {
         sum += i;
      }
   }
 
   factor(number);
   //system("pause");
   //system("cls");
   if (sum == number)
   {cout << "The Number: " << number << " Is Perfect.\n\n";}
   else if (sum > number)
   {cout << "The Number: " << number << " Is Abundant.\n\n";}
      else if (sum < number)
   {cout << "The Number: " << number << " Is Defficent.\n\n";}
   system("pause");
      return 0;
}

OUTPUT IN CMD:

Enter Number: 6
Factor<6>: 1 2 3 6
The Number: 6 Is Perfect
 
Press any key to continue...
 
--------------------------------
 
Enter Number: 28
Factor<6>: 1 2 4 7 14 28
The Number: 28 Is Perfect
 
Press any key to continue...
 
--------------------------------
 
Enter Number: 12
Factor<6>: 1 2 3 4 6 12
The Number: 12 Is Abundant
 
Press any key to continue...

And so on, Thank you so much for the help, it means a lot, and hey, i might me back when the next chalange comes around:cheesy:

Cya.

hey, CStalion, i really do apreciate the time youv'e spent trying to help, and i have no doubt that your way works, its just that with the limited understanding i have with C++, picking specific numbers to check for is still a little beyond me, i know how it would work, but not how to implement it;) But thanks again CStalion...

dave, i got the final code

#include <iostream.h>
#include <cstdlib>
#include <stdio.h>
void factor(int value)
{
   int factor;
   printf("factor(%d): ", value);
   for ( factor = 1; factor <= value; ++factor )
   {
      if ( value % factor == 0 )
      {
         printf("%d ", factor);
      }
   }
   putchar('\n');
}
int main(void)
{
   int number;
   int totalfactor = 0;
   cout << "Enter Number: ";
   cin >> number;
 
   int i, sum = 0;
   for ( i = 1; i < number; ++i )
   {
      if ( number % i == 0 )
      {
         sum += i;
      }
   }
 
   factor(number);
   //system("pause");
   //system("cls");
   if (sum == number)
   {cout << "The Number: " << number << " Is Perfect.\n\n";}
   else if (sum > number)
   {cout << "The Number: " << number << " Is Abundant.\n\n";}
      else if (sum < number)
   {cout << "The Number: " << number << " Is Defficent.\n\n";}
   system("pause");
      return 0;
}

OUTPUT IN CMD:

Enter Number: 6
Factor<6>: 1 2 3 6
The Number: 6 Is Perfect
 
Press any key to continue...
 
--------------------------------
 
Enter Number: 28
Factor<6>: 1 2 4 7 14 28
The Number: 28 Is Perfect
 
Press any key to continue...
 
--------------------------------
 
Enter Number: 12
Factor<6>: 1 2 3 4 6 12
The Number: 12 Is Abundant
 
Press any key to continue...

And so on, Thank you so much for the help, it means a lot, and hey, i might me back when the next chalange comes around:cheesy:

Cya.

A few things, you shouldn't really need to be mixing c++ syntax with c syntax. You should replace:

<iostream.h>

with

<iostream>
using namespace std;

System pause isn't the best way to pause your program.

ThanQ.

The best way is not to.

"Needing" to pause is a symptom of the environment in which the executable is run.

But if you must... Use a portable input routine such as cin.get or getchar .

But then the message "Press any key to continue..." won't appear...
so I'd just have to make an extra line of code like cout<<"Press any key to continue..."; cin.get();

But then the message "Press any key to continue..." won't appear...
so I'd just have to make an extra line of code like cout<<"Press any key to continue..."; cin.get();

If you are using Visual Studio, pressing Ctrl+F5 will give you that message in your system locale, without you having to do anything. Pressing just F5 will make the console dissapear without a word. If you run from the console you won't need that message. I dont know about other compilers.

This question has already been answered. Start a new discussion instead.