-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckPerfect.cc
More file actions
50 lines (43 loc) · 1.23 KB
/
checkPerfect.cc
File metadata and controls
50 lines (43 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*Name : Kale, Bharat Kumar
*Z-ID : Z1787884
*Course : CSCI 689/Section 1
*TA : Anthony Schroeder
*Purpose: This file contains a funtion that takes an unsigned integer as input and returns whether the number is perfect
*/
#include "perfectNums.h"
#include <iostream>
#include <cmath>
using std::cout;
using std::endl;
using std::sqrt;
/*
*Function Name: isPerfect
*Parameters List:
unsigned int num
*Return Type:
bool
True - if num is perfect
False - if num is not perfect
*Description: 1 is always a divisor of any num.
To minimize iterations, loop from 2 to squreroot(num) and get all the divisors in pairs instead of individual numbers.
i.e., if i is a divisor of n then n/i is also another divisor.
Add all the divisors other than the number itself and check if the sum is equal to 'num'.
*/
bool isPerfect(unsigned int num)
{
unsigned int sumOfDivisors=1,checkLimit=0;
if(num==1)
return false;
checkLimit=(int) sqrt(num)+1;
for(unsigned int i=2;i<checkLimit;i++)
{
if(num%i==0)
{
sumOfDivisors+=i+(num/i);
}
}
if(sumOfDivisors==num)
return true;
else
return false;
}