Skip to content

Commit

Permalink
feat: update aliquot_sum (#309)
Browse files Browse the repository at this point in the history
optimize code and add test two
  • Loading branch information
suiwater committed Jun 24, 2024
1 parent ef3292b commit 0aa7cca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
11 changes: 4 additions & 7 deletions packages/math/src/aliquot_sum.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
/// # Returns
/// * `felt252` - The aliquot sum of the input number.
pub fn aliquot_sum(number: u128) -> u128 {
if number == 0 {
return 0;
}
if number == 1 {
if number == 0 || number == 1 {
return 0;
}

let limit = (number / 2) + 1;
let mut index = 1;
let mut res = 0;
while (index != limit) {
let mut index = 2;
let mut res = 1;
while (index < limit) {
if number % index == 0 {
res = res + index;
}
Expand Down
7 changes: 7 additions & 0 deletions packages/math/src/tests/aliquot_sum_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ fn zero_test() {
fn one_test() {
assert_eq!(aliquot_sum(1), 0, "invalid result");
}

#[test]
#[available_gas(200000)]
fn two_test() {
assert_eq!(aliquot_sum(2), 1, "invalid result");
}

#[test]
#[available_gas(200000)]
fn one_digit_number_test() {
Expand Down

0 comments on commit 0aa7cca

Please sign in to comment.