forked from keep-starknet-strange/alexandria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perfect_number.cairo
43 lines (39 loc) · 1018 Bytes
/
perfect_number.cairo
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
//! # Perfect Number.
/// Algorithm to determine if a number is a perfect number
/// # Arguments
/// * `num` - The number to be checked.
/// # Returns
/// * `bool` - True if num is a perfect number, false otherwise.
pub fn is_perfect_number(num: u128) -> bool {
if num == 0 {
return false;
}
if num == 1 {
return false;
}
let mut index = 1;
let mut sum = 0;
while (index != num - 1) {
if num % index == 0 {
sum += index;
}
index += 1;
};
num == sum
}
/// Algorithm to determine all the perfect numbers up to a maximum value
/// # Arguments
/// * `max` - The maximum value to check for perfect numbers.
/// # Returns
/// * `Array` - An array of perfect numbers up to the max value.
pub fn perfect_numbers(max: u128) -> Array<u128> {
let mut res = array![];
let mut index = 1;
while (index != max) {
if is_perfect_number(index) {
res.append(index);
}
index += 1;
};
res
}