Skip to content

Commit

Permalink
divisor: go back to simpler scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
j2kun committed Sep 1, 2020
1 parent e574df2 commit ae535ec
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 36 deletions.
1 change: 0 additions & 1 deletion riemann/counterexample_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ def search(max_range: int, search_start: int = SEARCH_START) -> int:
"Are you sure trying to disprove RH is wise?")

def best_witness(max_range: int, search_start: int = SEARCH_START) -> int:
[divisor_sum(i) for i in range(1, max_range)]
return max(range(search_start, max_range), key=witness_value)
39 changes: 12 additions & 27 deletions riemann/divisor.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
'''Compute the sum of divisors of a number.'''
from functools import lru_cache
import math
from numba import njit


@lru_cache(maxsize=1000000)
@njit
def divisor_sum(n: int) -> int:
'''Compute the sum of divisors of a positive integer.
Algorithm is described https://math.stackexchange.com/a/22744
'''
'''Compute the sum of divisors of a positive integer.'''
if n <= 0:
raise ValueError(
"Non-positive number {} is not supported.".format(n)
)

sign_sequence = [1, 1, -1, -1]
return 0

i = 1
limit = math.sqrt(n)
the_sum = 0
i = 0
while True:
sign = sign_sequence[i % 4]
x = i // 2 + 1
if i % 2 == 1:
x = -x
arg_diff = int((3 * x * x - x) / 2)

if n - arg_diff < 0:
break

if n - arg_diff == 0:
the_sum += sign * n
break

the_sum += sign * divisor_sum(n - arg_diff)
while i <= limit:
if n % i == 0:
the_sum += i
if i != limit:
the_sum += n // i
i += 1

return the_sum
8 changes: 0 additions & 8 deletions test/divisor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,3 @@ def test_sum_of_divisors_of_1():

def test_sum_of_divisors_of_72():
assert 195 == divisor_sum(72)

def test_illegal_input_0():
with pytest.raises(ValueError):
divisor_sum(0)

def test_illegal_input_negative():
with pytest.raises(ValueError):
divisor_sum(-5)

0 comments on commit ae535ec

Please sign in to comment.