-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Closed
Labels
enhancementNew feature or requestNew feature or request
Description
Detailed description
An algorithm which determines whether a number can be composed of its factors (excluding itself) or not.
Context
An interesting number theory problem that would be beneficial to the algorithm library.
Possible implementation
bool isPerfect(int num) {
int sum = 0;
std::vector factors;
/* We get the factors of the number first by continuously divide the number by any natural number less than it should the remainder be 0*/
for (int i = 1; i <= num; i++)
if (num % i == 0)
factors.push_back(i);
// We don't include the number itself in the addition of its factors
for (unsigned int i = 0; i < factors.size() - 1; i++)
sum += factors.at(i);
if (num == sum)
return true;
return false;
}
Additional information
No response
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request