Help with c++ code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

8th May 2024, 1:02 PM
P <Code>
P <Code> - avatar
7 Answers
+ 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.
8th May 2024, 8:51 PM
Morpheus
Morpheus - avatar
9th May 2024, 9:23 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 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
11th May 2024, 8:15 AM
Bob_Li
Bob_Li - avatar
+ 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
11th May 2024, 7:34 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 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.
9th May 2024, 6:24 AM
Bob_Li
Bob_Li - avatar
9th May 2024, 10:03 PM
Bob_Li
Bob_Li - avatar
+ 1
here is the python version copied from the link I posted above. https://sololearn.com/compiler-playground/cgkD6mfJm2gp/?ref=app
11th May 2024, 8:49 AM
Bob_Li
Bob_Li - avatar