Skip to content

Add pronic number implementation #1023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

itsAkshayDubey
Copy link
Contributor

Open in Gitpod know more

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new JavaScript files are placed inside an existing directory.
  • All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.
    Example:UserProfile.js is allowed but userprofile.js,Userprofile.js,user-Profile.js,userProfile.js are not
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Copy link
Member

@raklaptudirm raklaptudirm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Format your code using standard.js. Read the CONTRIBUTING.md before contributing.

@raklaptudirm raklaptudirm added changes required This pull request needs changes code style issue Failing style checks algorithm Adds or improves an algorithm labels May 25, 2022
@raklaptudirm raklaptudirm added tests Adds or fixes tests; issue that points out bugs in the tests and removed code style issue Failing style checks labels May 25, 2022
Reduced time complexity from O(sqrt(n)) to O(1)
@appgurueu
Copy link
Collaborator

The current implementation is very odd: (1) you don't need to check whether numbers are odd if your equation works (2) your equation is unnecessarily convoluted.

@itsAkshayDubey
Copy link
Contributor Author

The current implementation is very odd: (1) you don't need to check whether numbers are odd if your equation works (2) your equation is unnecessarily convoluted.

Refactored it, have a look.

@appgurueu
Copy link
Collaborator

appgurueu commented May 25, 2022

Why use Math.round if you can use Math.floor/Math.ceil? I also failed to consider edge cases properly: 0 is pronic but 1 is not. The following should work:

if (number === 0) return true
const sqrt = Math.sqrt(number)
return sqrt % 1 !== 0 && Math.ceil(sqrt) * Math.floor(sqrt) === number

Please extend the tests to check all example numbers listed on https://oeis.org/A002378 - it's easy to mess up some edge cases. Also check that all other numbers in the range aren't false positives.

@itsAkshayDubey
Copy link
Contributor Author

Why use Math.round if you can use Math.floor/Math.ceil? I also failed to consider edge cases properly: 0 is pronic but 1 is not. The following should work:

if (number === 0) return true
const sqrt = Math.sqrt(number)
return sqrt % 1 !== 0 && Math.ceil(sqrt) * Math.floor(sqrt) === number

Please extend the tests to check all example numbers listed on https://oeis.org/A002378 - it's easy to mess up some edge cases. Also check that all other numbers in the range aren't false positives.

Added, have a look.

@itsAkshayDubey itsAkshayDubey requested a review from appgurueu May 25, 2022 11:55
@raklaptudirm raklaptudirm removed the changes required This pull request needs changes label May 25, 2022
@raklaptudirm raklaptudirm merged commit dc67506 into TheAlgorithms:master May 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
algorithm Adds or improves an algorithm tests Adds or fixes tests; issue that points out bugs in the tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants