Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions exercises/practice/perfect-numbers/.meta/example.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ pub fn classify(number: u64) -> Classification {
let mut i = 2;
let stop = sqrt(number);

while i < stop {
while i <= stop {
if number % i == 0 {
let mut increment = i;
if i * i != number {
increment += number / i;
}
sum += increment;
sum += i + number / i;
}
i += 1;
}

if stop * stop == number {
// When `number` is a square number, we added the same divisor twice inside the loop.
sum -= stop;
}

if sum < number {
Classification::Deficient
} else if sum > number {
Expand All @@ -38,9 +39,10 @@ pub fn classify(number: u64) -> Classification {
fn sqrt(radicand: u64) -> u64 {
let mut candidate = 1;
if radicand > 2 {
while (candidate * candidate) < radicand {
while (candidate * candidate) <= radicand {
candidate += 1;
};
}
return candidate - 1;
}
candidate
}
3 changes: 3 additions & 0 deletions exercises/practice/perfect-numbers/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ description = "Abundant numbers -> Medium abundant number is classified correctl
[ec7792e6-8786-449c-b005-ce6dd89a772b]
description = "Abundant numbers -> Large abundant number is classified correctly"

[05f15b93-849c-45e9-9c7d-1ea131ef7d10]
description = "Abundant numbers -> Perfect square abundant number is classified correctly"

[e610fdc7-2b6e-43c3-a51c-b70fb37413ba]
description = "Deficient numbers -> Smallest prime deficient number is classified correctly"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ fn large_abundant_number() {
assert_eq!(classify(33550335), Classification::Abundant);
}

#[test]
#[ignore]
fn perfect_square_abundant_number_is_classified_correctly() {
assert_eq!(classify(196), Classification::Abundant);
}

// Deficient numbers

#[test]
Expand Down
Loading