-
Notifications
You must be signed in to change notification settings - Fork 59
Improve performance of derangement
/subfactorial
with iterative implementation
#146
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
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #146 +/- ##
==========================================
+ Coverage 96.89% 96.93% +0.03%
==========================================
Files 8 8
Lines 805 815 +10
==========================================
+ Hits 780 790 +10
Misses 25 25 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
With this change I pass from
to
|
Changes
Consequences
|
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.
Need a rebase to master.
If CI looks good, I'll merge this pr.
There seems to be a small regression in |
…plementation Use the recursive formula !n = (n-1) * (!(n-1) + !(n-2)) presented here: https://en.wikipedia.org/wiki/Derangement#Counting_derangements
…rsive formula and inplace computations Use the simpler formula !n = n * !(n-1) + (-1)^n and use inplace operations on `BigInt`s to avoid allocations.
Ok, rebasing resolved the issue. |
This PR changes the implementation of
derangement
, hence alsosubfactorial
,to use the recursive formula
!n = (n-1) * (!(n-1) + !(n-2))
presented here.For values such as
subfactorial(100)
I get a huge speed-up, like ~10x.