Last active
March 25, 2024 05:44
-
-
Save Drakvlaa/6eb6dbca7ad15eab78abcfe815e7a746 to your computer and use it in GitHub Desktop.
Perfect number calculator in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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