Created
April 18, 2020 23:27
-
-
Save lilpolymath/3041b4323474ac75df8703a0b1561875 to your computer and use it in GitHub Desktop.
Perfect Number
This file contains hidden or 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
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) | |
Fixed!
#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;
}
}
}
10000
6
28
496
8128
real 0m2.832s
user 0m0.297s
sys 0m0.018s
See difference lol.
I know right.
Any other thing? Before Michael roasts me alive.
So we just had to start looping from the second variable lol.
Lmaoooooooooo. Dazzal, you & michael. Let me be your students. Later brov
It has an issue with 0 because of the long long int type.
Oh
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also, from the code above lol, we're not appending the number to an array or smtn