Finding Perfect Number with C++

  • Context: C/C++ 
  • Thread starter Thread starter Freyster98
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around writing a C++ program to determine if a user-provided number is a perfect number. Participants share code snippets, troubleshoot issues, and suggest improvements related to the implementation of the program, including the use of subroutines and boolean control for output.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares an initial implementation that incorrectly identifies all numbers as perfect due to a mistake in the conditional check for the boolean variable.
  • Another participant points out that the expression if(found=true) does not function as intended, suggesting that it should be if(found) instead.
  • A participant expresses gratitude for the correction and shares a personal experience of overlooking similar mistakes.
  • Further suggestions include placing constants first in comparisons to avoid assignment errors and using boolean checks directly.
  • Alternate code snippets are provided, showcasing different approaches to implementing the perfect number check, including variations in logic and efficiency.
  • One participant presents a simplified version of the perfect number function, highlighting a different coding style and structure.

Areas of Agreement / Disagreement

Participants generally agree on the importance of correct boolean comparisons and share various coding approaches, but no consensus is reached on a single best implementation method. Multiple competing views on how to structure the program remain present.

Contextual Notes

Some code snippets contain potential issues such as incorrect variable names (e.g., count instead of cout) and assumptions about the input range for perfect numbers. The discussion does not resolve these issues.

Who May Find This Useful

Readers interested in C++ programming, particularly those learning about functions, boolean logic, and number theory, may find this discussion beneficial.

Freyster98
Messages
49
Reaction score
0
I am supposed to write a program to test a user-provided number to see if it is perfect. Also, I have to use a subroutine to do the test, and call to that in the main program. Finally, I must use a boolean to control the final output. I understand that it would be SO MUCH easier if I could just write a simple program to do this without the boolean and subroutine, but an assignment is an assignment. Here is what I have so far...it runs, but no matter what number I enter when running the program, it comes back as perfect. Can anyone see what I am doing wrong? Any help would be appreciated.

Code:
#include<iostream>
#include<cmath>
using namespace std;

void Perfect(int number,int&ans,bool&valid)
{
	int sum=0;
	valid=false;
	for(int i=1;i<=number/2;i++)
	{
		if(number%i==0)
			sum+=i;
	}
	if(sum==number)
		valid=true;
		ans=sum;
}

void Perfect(int,int&,bool&);
char ch;

void main()
{
	int num,answer; 
	bool found;
	cout<<"Enter the number you want to test to see if it is perfect: ";
	cin>>num;
	Perfect(num,answer,found);
	if(found=true)
	{
		cout<<num<<" is a perfect number.";
	}
	else
	{
		cout<<num<<" is NOT a perfect number.";
	}
	cin>>ch;
}
 
Technology news on Phys.org
Code:
if(found=true)
doesn't do what you think it does!
 
mgb_phys said:
Code:
if(found=true)
doesn't do what you think it does!

I can't tell you how many times I've gone over this and completely missed that. Thank you for taking the time to help, I really appreciate it.
 
I can't tell you how many times I've gone over this and completely missed that.
Been there - done that - bought the Tshirt!

A good tip is to always put the constant first. eg.
if ( 2=count) or if ( true=found) then the compiler will warn you.

Even better is not to compare with true/false eg.
if (found) or if (!found) is much better.
 
Last edited:
Alternate C++ code:
Code:
#include<iostream>
#include<cmath>
using namespace std;

bool perfect(int n)
{
	int sum=0;
	int lim = (int)sqrt(n);
	for(int i=1; i <= lim; i++)
	{
		if(n%i == 0)
			sum += i + n/i;
	}
	return sum == n;
}

void main()
{
	int num;
	cout << "Enter the number you want to test to see if it is perfect: ";
	cin >> num;
	if(perfect(num))
		cout<<num<<" is a perfect number.";
	else
		cout<<num<<" is NOT a perfect number.";
}

Alternate function perfect (replace in the above):
Code:
bool isPrime(int n) {
	if ((n&1) == 0)
		return n == 2;
	if (n%3 == 0)
		return n == 3;
	if (n < 25)
		return n > 1;
	int lim = (int)sqrt(n);
	for (int i = 5; i <= lim; i += 4)
	{
		if (n%i == 0)
			return false;
		i += 2;
		if (n%i == 0)
			return false;
	}
	return true;
}

bool perfect(int n)
{
	int s = 0;
	while ((n&1) == 0)
	{
		s++;
		n >>= 1;
	}

	if (1 << s != (n+1) >> 1)
		return false;
	return isPrime(n);
}

Alternate function perfect (replace in the above; assumes 32-bit signed ints):
Code:
bool perfect(int n)
{
	if (n > 496)
		return n == 8128 || n == 33550336;
	return n == 6 || n == 28 || n == 496;
}
 
"Guys here is a short one if you appreciate"

#include<iostream>
#include<cmath>
using namespace std;


bool perfect(int number){
int sum=0;
bool flag=false;

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

if(sum==number)
return true;
else
return false;


}


int main(){

int num;
bool q;
count<<"Enter the number you want to test to see if it is perfect: ";
cin>>num;
q=perfect(num);
if(q)
count<<"perfect number.\n";
else
count<<"NOT.\n";
return 0;
}
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 12 ·
Replies
12
Views
2K