+ 1
Help with c++ code
I want to create a function that takes an int N as formal parameter and it prints all the first N perfect numbers, but when n is bigger than 4 it has no output, still if it is 1 2 3 or 4 it works perfectly https://sololearn.com/compiler-playground/cVEwdMcOTYyQ/?ref=app
7 Antworten
+ 4
Hi P <Code> ,
This is a very interesting problem to solve in Programming.
Your code is perfect it's just that after first 4 perfect numbers, the next one takes time
the 5th one is 33,550,336
33 Million 😅
It just takes too much compute time and Sololearn runtime environments are time limited to 30 secs I guess.
Therefore you can't get more than n=4 here.
Even for our computers n>4 will take a LOT of time.
btw, There's some active research going on around this problem.
Hope it helps.
+ 3
Maybe you need a faster algorithm?
I found this picture on wikipedia
https://wikimedia.org/api/rest_v1/media/math/render/svg/71f246aec6d77787b81d111f7d36ebeba1ad93a1
And it is the link:
https://en.wikipedia.org/wiki/Perfect_number#Even_perfect_numbers
+ 3
Wong Hei Ming
yeah, Sololearn times out at about 5 seconds.
P <Code>
you don't even have to use long long int. Unsigned int is big enough for the 5th number. The calculation takes longer than 5 sec at a much lower value of n (approximately 507000 in my test). your algorithm cannot reach the 5th value under 5 sec
https://sololearn.com/compiler-playground/cGmCtqfO1kT7/?ref=app
+ 2
While viewing other user's code, I come across this Python code with time.sleep() function.
After a trial and error, it seems the runtime in playground is 5 seconds.
https://sololearn.com/compiler-playground/c5B5uV1926Iy/?ref=app
+ 1
you can't do long calculations in Sololearn. it times out after a while. I don't think it even goes up to 30 seconds.
P <Code> you should mark Morpheus as best. His answer is more detailed. And yes, It's one of those series where the calculation time blows up beyond the 4th number.
I saw a video about it in the Youtube channel Numberphile.
+ 1
here is a nice link. But it's a Python solution
https://stackoverflow.com/questions/39864603/python-perfect-number-calculation
+ 1
here is the python version copied from the link I posted above.
https://sololearn.com/compiler-playground/cgkD6mfJm2gp/?ref=app