forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PerfectNumber.dart
52 lines (43 loc) · 956 Bytes
/
PerfectNumber.dart
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
51
/*
Perfect number is a number which is equal to the sum of its divisors except the number itself
For example consider 6
Divisors of 6 are 1, 2, 3
Sum of divisors = 1 + 2 + 3 = 6.
*/
import 'dart:io';
// Check if number is perfect or no
bool isPerfect(int number) {
int sum = 0;
for (int index = 1; index < number; index++) {
// if number is a factor add it to sum
if (number % index == 0) {
sum += index;
}
}
if (sum == number) {
return true;
}
return false;
}
// Main Function, with driver code
void main() {
print("Enter a number:");
int input = int.parse(stdin.readLineSync()!);
// Call isPerfect function on input
if (isPerfect(input)) {
print("$input is a perfect number");
} else {
print("$input is not a perfect number");
}
}
/**
Time Complexity: O(n)
Space Complexity: O(1)
Sample input/output:
Enter a number:
6
6 is a perfect number
Enter a number:
12
12 is not a perfect number
*/