Codecov Report✅ All modified and coverable lines are covered by tests. @@ Coverage Diff @@
## master #1904 +/- ##
==========================================
+ Coverage 85.91% 86.11% +0.19%
==========================================
Files 379 379
Lines 19778 19778
Branches 3016 3024 +8
==========================================
+ Hits 16993 17031 +38
+ Misses 2785 2747 -38
☔ View full report in Codecov by Sentry.
|
Sorry, something went wrong.
Bug
Maths/FriendlyNumbers.js's sumDivisors uses an exclusive upper bound:
This excludes i === number / 2, which is a divisor of every even number, so the sum of divisors (and therefore the abundancy index σ(n)/n) is wrong for even inputs:
Because the error scales both numbers similarly it sometimes cancels out (the classic FriendlyNumbers(6, 28) still returns true by luck), but it produces wrong classifications in general — e.g. FriendlyNumbers(15, 20) returns true even though σ(15)/15 = 1.6 ≠ σ(20)/20 = 2.1.
Fix
Loop from 1 to number / 2 inclusive:
(Starting at 1 also drops the harmless number / 0 = Infinity check the old i = 0 start relied on.)
After the fix: sumDivisors(6) = 12, sumDivisors(28) = 56, FriendlyNumbers(6, 28) = true, FriendlyNumbers(30, 140) = true, FriendlyNumbers(15, 20) = false.
The module had no test file, so I added Maths/test/FriendlyNumbers.test.js covering friendly pairs, a non-friendly pair, and invalid input.