Repository navigation
Commit 660701d
committed
fix(maths): add the leftover prime factor in sum_of_divisors
sum_of_divisors only multiplied in the prime factors it found while
trial-dividing, so any prime factor above the trial-division bound was
dropped: 94 of the first 119 inputs returned the wrong answer
(sum_of_divisors(3) -> 1 instead of 4, sum_of_divisors(12) -> 7 instead
of 28). number_of_divisors in the same file already handles this case
with `if n > 1: div *= 2`.
Also compute with // instead of /: the function accumulated a float and
truncated with int(s), so sum_of_divisors(2**62) returned
9223372036854775808 instead of 9223372036854775807 and large inputs were
off by orders of magnitude (3**40 was wrong by ~10^4). math.isqrt
replaces int(math.sqrt(n)) for the same reason.
Verified against an independent factorization of sigma(n) for
n = 1..50000: 0 mismatches.1 parent 6e03a48 commit 660701d
1 file changed
Lines changed: 18 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
70 | 78 | | |
71 | 79 | | |
72 | 80 | | |
| |||
82 | 90 | | |
83 | 91 | | |
84 | 92 | | |
85 | | - | |
| 93 | + | |
86 | 94 | | |
87 | | - | |
88 | | - | |
| 95 | + | |
| 96 | + | |
89 | 97 | | |
90 | 98 | | |
91 | 99 | | |
92 | | - | |
| 100 | + | |
93 | 101 | | |
94 | | - | |
95 | | - | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
96 | 108 | | |
97 | 109 | | |
98 | 110 | | |
| |||
0 commit comments