Title: fix: include number/2 divisor in FriendlyNumbers sumDivisors by shaked-shlomo · Pull Request #1904 · TheAlgorithms/JavaScript · GitHub
Open Graph Title: fix: include number/2 divisor in FriendlyNumbers sumDivisors by shaked-shlomo · Pull Request #1904 · TheAlgorithms/JavaScript
X Title: fix: include number/2 divisor in FriendlyNumbers sumDivisors by shaked-shlomo · Pull Request #1904 · TheAlgorithms/JavaScript
Description: Bug Maths/FriendlyNumbers.js's sumDivisors uses an exclusive upper bound: for (let i = 0; i < number / 2; i++) { 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: sumDivisors(6) returns 9 instead of 12 sumDivisors(28) returns 42 instead of 56 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: for (let i = 1; i <= number / 2; i++) { (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.
Open Graph Description: Bug Maths/FriendlyNumbers.js's sumDivisors uses an exclusive upper bound: for (let i = 0; i < number / 2; i++) { This excludes i === number / 2, which is a divisor of every even number, so t...
X Description: Bug Maths/FriendlyNumbers.js's sumDivisors uses an exclusive upper bound: for (let i = 0; i < number / 2; i++) { This excludes i === number / 2, which is a divisor of every even numb...
Opengraph URL: https://github.com/TheAlgorithms/JavaScript/pull/1904
X: @github
Domain: Github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:832f3a3a-7b6b-f130-f23d-e2ce6c70d77b |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | D9DA:1531FD:41ED77:59C6FC:6A62D1C8 |
| html-safe-nonce | aafef6acaa6414945a6882817f9dd6c91153b0c9b8a6224d5d4fb2ee169e376d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOURBOjE1MzFGRDo0MUVENzc6NTlDNkZDOjZBNjJEMUM4IiwidmlzaXRvcl9pZCI6IjUyNDYyMjUwMjEwOTkyMzM3MzYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3e97663abc953723a14fbe409c647125acce1f517bb2585e9d57b66e4b911910 |
| hovercard-subject-tag | pull_request:3784459366 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/TheAlgorithms/JavaScript/pull/1904/files |
| twitter:image | https://avatars.githubusercontent.com/u/167336994?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/167336994?s=400&v=4 |
| og:image:alt | Bug Maths/FriendlyNumbers.js's sumDivisors uses an exclusive upper bound: for (let i = 0; i < number / 2; i++) { This excludes i === number / 2, which is a divisor of every even number, so t... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | df33b1b61ee7b9a0af988199bfc3503c9c1acafb1f1d40e1f140ea7c84f890dd |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/TheAlgorithms/JavaScript git https://github.com/TheAlgorithms/JavaScript.git |
| octolytics-dimension-user_id | 20487725 |
| octolytics-dimension-user_login | TheAlgorithms |
| octolytics-dimension-repository_id | 97086543 |
| octolytics-dimension-repository_nwo | TheAlgorithms/JavaScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 97086543 |
| octolytics-dimension-repository_network_root_nwo | TheAlgorithms/JavaScript |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d41cd1bdb290013455c0ac430fa755621733f5eb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width