Skip to content

Instantly share code, notes, and snippets.

@Drakvlaa
Last active March 25, 2024 05:44
Show Gist options
  • Save Drakvlaa/6eb6dbca7ad15eab78abcfe815e7a746 to your computer and use it in GitHub Desktop.
Save Drakvlaa/6eb6dbca7ad15eab78abcfe815e7a746 to your computer and use it in GitHub Desktop.
Perfect number calculator in C++
#include <iostream>
#include <math.h>
#define u64 unsigned long long
bool is_perfect(u64 num){
u64 sum = 0;
for (u64 d = 1; d < num; d += 1) {
if (num % d == 0) sum += d;
if (sum > num) return false;
}
if (sum==num) return true;
return false;
}
int main(void) {
u64 to_check = 2;
u64 i = 1;
while (true){
if (is_perfect(to_check)) {
std::cout << i << ". " << to_check << " is a perfect number" << std::endl;
to_check += pow(10, i++);
}
to_check += 2;
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment