-
Notifications
You must be signed in to change notification settings - Fork 166
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
Implement math.factorial(n)
to calculate very large factorials with speed
#2558
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e52a1b3
Implement `math.factorial(n)` to calculate very large factorials with…
kmr-srbh 743c322
Merge branch 'lcompilers:main' into modules
kmr-srbh e7857d0
Use `int` as return type
kmr-srbh 6445496
Merge branch 'main' into modules
kmr-srbh 1478355
Revert setting return type to `int`
kmr-srbh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do understand the approach, here the final result is printed out using concatenation.
I have not looked into the pre-requisites but even after a conversion from string to int, how will you return an integer here? The highest supported annotation we have is
i64
, which has a maximum value of2^63 - 1
.This is not large enough to store anything over
20!
, which means this will still encounter an overflow and return a garbage value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tried something similar in another project, let me know if there are any other approaches. Thanks :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@faze-geek I understand your point. Internally in LPython, the method used for handling values larger than 2 ^ 63 - 1 is handled by the BigInt module we have. The part of the module which does the handling for large numbers is broken and returns a pointer to the value instead. I had worked on a related issue #1356 and proposed implementing the
vector<char>
method for handling large values. Only then will we be able to handle values larger than 2 ^ 63 - 1.I had forgotten to mention this prerequisite. Thanks for reminding me. I am adding it above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes there are! As I had mentioned above, the industry standard for scientific computing applications is to use the PrimeSwing algorithm. The Python implementation is available online, but I do not want to type something I do not understand. 😄
The Python
math.factorial(n)
uses this algorithm.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Will surely explore this once the
BigInt
module gets working.