Skip to content

reduced pythagorean-triplet's time complexity from cubic to quadratic #1324

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

eakray
Copy link
Contributor

@eakray eakray commented Sep 7, 2021

In the previous implementation the triplets function had a time complexity of O(n^3). This commit reduces it to O(n^2). I believe the current best known optimal solution for this type of problem is O(n^2(log log n)/log ^2n) but in order to achieve that result we would need to make changes to this solution which would negatively impact readability/understandability. The final test still takes a long time (~16s) but I don't think that can be significantly improved for an input of that size.

@@ -58,7 +58,7 @@ describe('Triplet', () => {
]);
});

test.skip('triplets for large number', () => {
xtest('triplets for large number', () => {
Copy link
Member

Choose a reason for hiding this comment

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

If it still takes ~ 16 s, we can't "unskip" this, because the web UI has a maximum of 10 s of run time. Apart from that, all these changes are great!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would it be possible to run the test on a smaller input?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

an input of 25000 takes ~9.5s and returns [ [ 5000, 9375, 10625 ] ]. Or do you mean i should mark the test as skipped but keep the code changes?

Copy link
Member

Choose a reason for hiding this comment

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

That last thing! So people can run it likely but it won't run in the web.

@junedev junedev linked an issue Sep 8, 2021 that may be closed by this pull request
Copy link
Member

@SleeplessByte SleeplessByte left a comment

Choose a reason for hiding this comment

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

Looking great. I'll have the workflow format your code by writing:

/format

@SleeplessByte
Copy link
Member

/format

@github-actions
Copy link
Contributor

github-actions bot commented Sep 8, 2021

The "Format code" action has started running.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 8, 2021

The "Format code" action has finished running.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 8, 2021

For security reasons, /format does not trigger CI builds when the PR has been submitted from a fork. If checks were not passing due to code format, trigger a build to make the required checks pass, through one of the following ways:

  • Push an empty commit to this branch: git commit --allow-empty -m "Trigger builds".
  • Close and reopen the PR.
  • Push a regular commit to this branch.

@SleeplessByte SleeplessByte reopened this Sep 8, 2021
@SleeplessByte SleeplessByte merged commit e1f69d6 into exercism:main Sep 8, 2021
@eakray eakray deleted the pythagorean-triplet-optimise-implementation branch September 8, 2021 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pythagorean-triplet: optimise implementation
2 participants