Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved logic to check if a number is perfect #1338

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Perfect Numbers/Check Perfect.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
#include <stdio.h>

int isPerfect(int num){
int count=0;
for(int i=1;i<num;i++){
if(num%i==0){
count+=i;
if(num==1)return 0;
int sum=1;
int lim=sqrt(num);

for(int n=2;n<=lim;n++){
if(num%n==0){sum+=n+(num/n);}
}
}
if(num==count)
return 1;
else

if(sum==num)return 1;
return 0;
}

Expand Down
19 changes: 10 additions & 9 deletions Perfect Numbers/Perfect Numbers.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
//Printing n Perfect Numbers
#include <stdio.h>
#include <math.h>

int isPerfect(int num){
int count=0;
for(int i=1;i<num;i++){
if(num%i==0){
count+=i;
if(num==1)return 0;
int sum=1;
int lim=sqrt(num);

for(int n=2;n<=lim;n++){
if(num%n==0){sum+=n+(num/n);}
}
}
if(num==count)
return 1;
else

if(sum==num)return 1;
return 0;
}

Expand All @@ -20,7 +21,7 @@ int main(){
scanf("%d",&num);
for(int i=0;i<num;){
if(isPerfect(temp)==1){
printf("%d",temp);
printf("%d\n",temp);
i++;
temp++;
}
Expand Down
20 changes: 11 additions & 9 deletions Perfect Numbers/Perfect Numbers.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#include <iostream>
#include <cmath>

using namespace std;

int isPerfect(int num){
int count=0;
for(int i=1;i<num;i++){
if(num%i==0){
count+=i;

if(num==1)return 0;
int sum=1;
int lim=sqrt(num);

for(int n=2;n<=lim;n++){
if(num%n==0){sum+=n+(num/n);}
}
}
if(num==count)
return 1;
else

if(sum==num)return 1;
return 0;
}

Expand All @@ -29,4 +31,4 @@ int main(){
temp++;
}
return 0;
}
}