Skip to content

Perfect Number Algorithm #1904

@fredward36

Description

@fredward36

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

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions