Skip to content

Instantly share code, notes, and snippets.

@lilpolymath
Created April 18, 2020 23:27
Show Gist options
  • Save lilpolymath/3041b4323474ac75df8703a0b1561875 to your computer and use it in GitHub Desktop.
Save lilpolymath/3041b4323474ac75df8703a0b1561875 to your computer and use it in GitHub Desktop.
Perfect Number
def solve(number):
sum = 0
middle = int(number/2) + 1
for i in range(1,middle):
if number%i == 0:
sum += i
if sum == number:
return sum
if __name__ == "__main__":
rangeTo = int(input())
perfect = []
for i in range(rangeTo + 1):
number = solve(i)
if number:
perfect.append(number)
print(perfect)
@lilpolymath
Copy link
Author

#include <iostream>
using namespace std;

bool perfect(long long int number)
{
    long divisors = 1;
    for (long long int i = 2; i <= number / 2; i++)
    {
        if (number % i == 0)
        {
            divisors = divisors + i;
        }
    }

    if (number == divisors)
    {
        return true;
    }
    return false;
}

int main()
{
    int number;
    cin >> number;
    for (int num = 2; num < number; num++)
    {
        if (perfect(num))
        {
            cout << num << endl;
        }
    }
}

@lilpolymath
Copy link
Author

10000
6
28
496
8128

real 0m2.832s
user 0m0.297s
sys 0m0.018s

@Youngestdev
Copy link

See difference lol.

@lilpolymath
Copy link
Author

I know right.

@lilpolymath
Copy link
Author

Any other thing? Before Michael roasts me alive.

@Youngestdev
Copy link

So we just had to start looping from the second variable lol.

@Youngestdev
Copy link

Lmaoooooooooo. Dazzal, you & michael. Let me be your students. Later brov

@lilpolymath
Copy link
Author

It has an issue with 0 because of the long long int type.

@Youngestdev
Copy link

Oh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment