forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Perfect_Number.js
37 lines (28 loc) · 909 Bytes
/
Perfect_Number.js
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
/*
Perfect number is a positive integer which is equal to the sum of its proper positive divisors.
For example: 6 is the first perfect number
Proper divisors of 6 are 1, 2, 3
Sum of its proper divisors = 1 + 2 + 3 = 6.
Hence 6 is a perfect number.
*/
const prompt = require("prompt-sync")({ sigint: true });
let n = +prompt("Enter a number to check whether it is a perfect number or not: ");
let sum = 0;
for (let i = 1; i < n; i++)
{
if (n % i == 0)
sum += i;
}
if (sum == n)
console.log(`${n} is a perfect number.`);
else
console.log(`${n} is not a perfect number.`);
/*
Time Complexity: O(n)
Space Complexity: O(1)
node JavaScript/math/Perfect_Number.js
sample 1: Enter a number to check whether it is a perfect number or not: 6
Output: 6 is a perfect number.
sample 2: Enter a number to check whether it is a perfect number or not: 7
output: 7 is not a perfect number.
*/