Note that there are some explanatory texts on larger screens.

plurals
  1. POpython - time to execute a loop increases suddenly
    text
    copied!<p>I have some small piece of software that calculates the number of factors of each triangle number to see what is the first one of them has more than X number of factors (yes, it's a projecteuler problem, <a href="http://projecteuler.net/index.php?section=problems&amp;id=12" rel="nofollow">number 12</a>,, although i didn't solve it yet)... as am trying making X some random values to see what the code does and in how much time, I noticed something strange (to me at least): until X=47 the execution time increases in obviously normal way, but when X = 48 it increases more than normal, and function calls are much greater than the rate, it (explodes) if I would say that.. why does it do that??</p> <p>the code:</p> <pre><code>def fac(n): c=0 for i in range (1,n+1): if n%i==0: c=c+1 return c n=1 while True: summ=0 for i in range (1,n+1): summ=summ+i if fac(summ)&gt;X: break n=n+1 print summ </code></pre> <p>and when profiling:</p> <pre><code>when X=45 : 314 function calls in 0.027 CPU seconds when X=46 : 314 function calls in 0.026 CPU seconds when X=47 : 314 function calls in 0.026 CPU seconds when X=48 : 674 function calls in 0.233 CPU seconds when X=49 : 674 function calls in 0.237 CPU seconds </code></pre> <p>I assume that if I continued I would meet other points that system calls increases and time increases suddenly, and previously there were points like that but time was so small so it did't matter so much.. Why function calls suddenly increases?? Isn't it supposed just to call the function one more time for the new value??</p> <p>P.S. am using cProfile as a profiler, and <code>X</code> in the code here is just for demonstration, I write the value directly in the code... thank you in advance...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload