-
Notifications
You must be signed in to change notification settings - Fork 0
Miscellaneous Code Examples
danieltan1517 edited this page Nov 5, 2025
·
27 revisions
A perfect number is an integer that is equal to the sum of its positive proper divisors, that is, divisors excluding the number itself. For instance, 6 has proper divisors 1, 2, and 3, and 1 + 2 + 3 = 6, so 6 is a perfect number. The next perfect number is 28, because 1 + 2 + 4 + 7 + 14 = 28.
We can create a function to determine if a number is perfect or not. Add up all the factors of the number, and if the sum of all factors is equal to the number, then return true.
#import "Basic";
is_perfect :: (n: int) -> bool {
if n <= 1 return false;
sum := 1;
for i: 2..n/2 + 1 {
if n % i == 0 {
sum += i;
}
}
return sum == n;
}
main :: () {
for i: 1..10000 {
if is_perfect(i) {
print("% is a perfect number.\n", i);
}
}
}