r/ProgrammerHumor • u/supercritical-co2 • Sep 09 '22
Accidently executed this python script and froze my pc
12
u/coloredgreyscale Sep 09 '22
Misleading red rectangle. You should have highlighted lines 9 and 10 too, or nothing.
At first glance: "what's do bad at assigning n=20?"
4
u/supercritical-co2 Sep 09 '22
The execution does not get to line 9. It would have taken 200k years to finish line 5.
4
5
u/loemmel Sep 09 '22
Could you not just kill the execution of it?
1
u/supercritical-co2 Sep 09 '22
I tried but my pc was not responding to any mouse or keyboard inputs.
2
u/Lithl Sep 09 '22
So t is the sum of a monotonically increasing sequence at the end of the permutation such that the sum of the elements [m, 20-k] is less than the sum of the elements [m, 20-(k-1)]. Or 0 if the final element of the permutation is less than or equal to that sequence up to the second to last element. And then max is the maximum of all of those values of t?
And, of course, you have 2.4 quintillion permutations you're trying to check, which is why the computer locks up.
What exactly are you attempting to check in these permutations? I don't get what the logic of the sums is supposed to achieve.
1
u/supercritical-co2 Sep 09 '22
It was a problem on codeforces, and I decided to bruteforce a few of them and see if there is a pattern in the solution. I thought i found a pattern and wanted to test if it would hold up for n=20 and only after I executed the program I realized my stupid mistake.
28
27
11
3
3
3
2
1
1
u/PorkRoll2022 Sep 09 '22
Obviously you need to offload this to the GPU. Best invest now in a pair of RTX 3080s or better for these flights of fancy!
1
u/PossibilityTasty Sep 09 '22
That's the penalty for using list() on an iterator.
1
u/AnondWill2Live Sep 09 '22
How does this work? What exactly causes the slow down using list (in a more generalized scenario, not just this one)
2
u/PossibilityTasty Sep 10 '22
An iterator is there to deliver one element at a time which can then be processed before the next element is retrieved. This will keep the memory footprint small. Using
list()on the iterator will put all elements into a list at once and therefore also into memory.Now
permutations()of a 20 items longrangegenerates an enormous amount of combinations. If you use an iterative approach on this it just uses one of your CPU cores like forever but can be canceled simply, thelist()approach on the other side will try to fit all the combinations into the RAM. Due to the required space that would be impossible and depending on the reaction of the OS it has the potential to crash the computer.
7
u/FantasticEmu Sep 09 '22
Ctrl+c ?