Solving Perfect Number Program in C

  • Thread starter Thread starter Dili
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around writing a C program to determine if a number is a perfect number, with participants sharing code snippets, corrections, and alternative approaches. The scope includes programming logic, debugging, and optimization techniques.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant presents an initial code attempt to check for perfect numbers, noting it does not work as intended.
  • Another participant identifies mistakes in variable initialization and parentheses placement, providing a corrected version of the code.
  • A different approach is suggested that utilizes the Euclid identity for perfect numbers, which is presented as a faster method.
  • Further optimization is proposed by checking against known perfect numbers for a quick solution, particularly for 32-bit machines.
  • A participant expresses gratitude for the corrections and seeks clarification on the difference between return values in the main function.
  • Another participant explains that return values indicate program termination status, with 0 signifying normal termination and non-zero indicating an error.

Areas of Agreement / Disagreement

Participants generally agree on the need for corrections and optimizations in the code, but there are multiple competing approaches to solving the problem, and the discussion remains unresolved regarding the best method.

Contextual Notes

Some code snippets contain unresolved issues related to variable initialization and logical flow. The discussion also reflects varying levels of understanding of C programming concepts among participants.

Who May Find This Useful

Individuals interested in C programming, particularly those learning about algorithms for number theory and debugging techniques.

Dili
Messages
13
Reaction score
0
I need to write a program to check whether a number is perfect
eg: 6=1+2+3 , 28=1+2+4+7+14
i wrote one, but it doesn't work. also I've seen programs written with different preprocessors, we are required to write it on stdio.h and math.h


#include<stdio.h>
#include<math.h>

int main()
{
int a,b,c,d,i;
scanf("%d",&a);
{
for(i=1;i<a;i++)
b=a%i;
if(b==0)
{
c=0;
c=c+i;
}
if(c==a)
printf("perfect\n");
}
return 1;
}

 
Technology news on Phys.org
Few mistakes in variable initialization and placing of parentheses. Here is the modified correct version. Make sure you understand the logic.

#include<stdio.h>
#include<math.h>

int main()
{
int a,b,c,d,i;
scanf("%d",&a);c=0;
for(i=1;i<a;i++)
{
b=a%i;
if(b==0)
{
c=c+i;
} }
if(c==a)
printf("perfect\n");
return 1;
}
 
Heh. Here's a version you can't turn in, but which is far faster; it uses the Euclid identity of perfect numbers.

Code:
#include<stdio.h>
#include<math.h>

int is_prime(n) {
  int i,root;
  if (!(n&1))
    return n == 2;
  if (n%3 == 0)
    return n == 3;
  if (n < 25)
    return n > 1;
  root = (int)sqrt(n);
  for (i = 5; i <= root; i += 6)
    if (n%i == 0 || n%(i+2) == 0)
      return 0;
  return 1;
}

int main() {
  int even_part, odd_part;
  scanf("%d",&odd_part);
  even_part = 1;
  while (!(odd_part&3)) {
    odd_part = odd_part >> 2;
    even_part = even_part << 2;
  }
  if (!(odd_part&1)) {
    odd_part = odd_part >> 1;
    even_part = even_part << 1;
  }
  if (even_part << 1 == odd_part + 1 && is_prime(odd_part))
    printf("perfect\n");
  return 0;
}

Even faster and even cheatier, for 32-bit machines:

Code:
int main() {
  int n;
  scanf("%d",&n);
  if (n == 6 || n == 28 || n == 496 || n == 8128 || n == 33550336)
    printf("perfect\n");
  return 0;
}
 
Last edited:
Thank you very much arunbg. I am not good at C programming.
I do now understand the placing of brackets and c=0.
If no bother can you explain what is the difference between return 1 and return 0
Ive used them but with no real understanding
Thanks again
 
int main() {
int n;
scanf("%d",&n);
if (n == 6 || n == 28 || n == 496 || n == 8128 || n == 33550336 || n == 8589869056)
printf("perfect\n");
return 0;
}
Lol...nice one.
 
Dili said:
If no bother can you explain what is the difference between return 1 and return 0
Ive used them but with no real understanding

Don't worry about it. These are error codes that the program returns, but they won't display or do anything unless they're checked by an outside program. 0 is supposed to mean "terminated normally" and anything other than 0 is supposed to mean "terminated with some kind of error".
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
Replies
12
Views
3K
Replies
7
Views
2K