Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding the total divisors of a number up to 10^12 as fast as possible?
    text
    copied!<p>I need to calculate the total number of divisors of a number N (without caring about what the values of the divisors are) and do so within 40-80 operations for all such numbers N. How can I do it? This is not a homework question. I tried out <a href="http://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm">Pollard's Rho</a> algorithm but even that turned out too slow for my purposes. Here is my code in python. How can I improve its performance, if possible?</p> <pre><code>def is_prime(n): if n &lt; 2: return False ps = [2,3,5,7,11,13,17,19,23,29,31,37,41, 43,47,53,59,61,67,71,73,79,83,89,97] def is_spsp(n, a): d, s = n-1, 0 while d%2 == 0: d /= 2; s += 1 t = pow(int(a),int(d),int(n)) if t == 1: return True while s &gt; 0: if t == n-1: return True t = (t*t) % n s -= 1 return False if n in ps: return True for p in ps: if not is_spsp(n,p): return False return True def gcd(a,b): while b: a, b = b, a%b return abs(a) def rho_factors(n, limit=100): def gcd(a,b): while b: a, b = b, a%b return abs(a) def rho_factor(n, c, limit): f = lambda x: (x*x+c) % n t, h, d = 2, 2, 1 while d == 1: if limit == 0: raise OverflowError('limit exceeded') t = f(t); h = f(f(h)); d = gcd(t-h, n) if d == n: return rho_factor(n, c+1, limit) if is_prime(d): return d return rho_factor(d, c+1, limit) if -1 &lt;= n &lt;= 1: return [n] if n &lt; -1: return [-1] + rho_factors(-n, limit) fs = [] while n % 2 == 0: n = n // 2; fs = fs + [2] if n == 1: return fs while not is_prime(n): f = rho_factor(n, 1, limit) n = int(n / f) fs = fs + [f] return sorted(fs + [n]) def divs(n): if(n==1): return 1 ndiv=1 f=rho_factors(n) l=len(f) #print(f) c=1 for x in range(1,l): #print(f[x]) if(f[x]==f[x-1]): c=c+1 else: ndiv=ndiv*(c+1) c=1 # print ("C",c,"ndiv",ndiv) ndiv=ndiv*(c+1) return ndiv </code></pre>
 

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