Skip to content
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

Project Euler problem148 #8662

Conversation

saitejamanchi
Copy link

Describe your change:

Added solution for Problem 148 in Project Euler.

  • 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 Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels Apr 16, 2023
"""


def solution(pascal_row_count: int = 10**4) -> int:
Copy link
Contributor

Choose a reason for hiding this comment

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

It has a hardcoded value of pascal_row_count which is only 10^4. As a result, the function returns the result for the first 10^4 rows of Pascal's triangle instead of the first billion rows, try this:

def solution(pascal_row_count: int = 10**9) -> int:

Copy link
Author

Choose a reason for hiding this comment

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

Hey,

I've tried with 10**9 but one of the tests failed due to timeout as it was taking more than 6 hours to run.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's not uncommon for brute force solutions to take a long time or run into timeout issues when dealing with very large inputs. In this case, the input value of 10**9 is extremely large, and the current implementation of the "solution" function does not have an efficient algorithm to handle such large inputs.

@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Apr 24, 2023
rohan472000

This comment was marked as outdated.



def solution(pascal_row_count: int = 10**9) -> int:
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

use below codes for better efficiency which will solve the timeout issue:-

  def solution(pascal_row_count: int = 10**9) -> int:
      """
      To evaluate the solution, use solution()
      >>> solution(3)
      6
      >>> solution(10)
      40
      >>> solution(100)
      2361
      """
  
      # Helper function to compute the number of odd binomial coefficients in a row
      def odd_binomials(n: int) -> int:
          res = 0
          while n > 0:
              res += n % 2
              n //= 2
          return res
  
      # Compute the sum of odd binomial coefficients modulo 7 for each row up to pascal_row_count
      result = 0
      for k in range(pascal_row_count):
          result += odd_binomials(k) % 7
  
      return result

Copy link
Contributor

Choose a reason for hiding this comment

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

also add docs in odd_binomials, you can put above comment of that function inside as a doc.

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Apr 30, 2023
@rohan472000
Copy link
Contributor

use ruff . then ruff . --fix to fix ruff issues.

@algorithms-keeper algorithms-keeper bot added the require tests Tests [doctest/unittest/pytest] are required label Apr 30, 2023
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

"""


def get_num_binomials(row_num: int) -> int:

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file project_euler/problem_148/sol1.py, please provide doctest for the function get_num_binomials

Copy link
Contributor

Choose a reason for hiding this comment

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

@saitejamanchi add doctests.

Copy link
Author

Choose a reason for hiding this comment

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

@rohan472000 Sure, will add.

Thanks a lot for the help!!

Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

"""


def get_num_binomials(row_num: int) -> int:

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file project_euler/problem_148/sol1.py, please provide doctest for the function get_num_binomials

@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Apr 30, 2023
@algorithms-keeper algorithms-keeper bot removed the require tests Tests [doctest/unittest/pytest] are required label Apr 30, 2023
@saitejamanchi saitejamanchi reopened this Apr 30, 2023
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Apr 30, 2023
@rohan472000
Copy link
Contributor

rohan472000 commented May 1, 2023

kindly fix ruff issue before committing...by ruff . then ruff . --fix

@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label May 3, 2023
@saitejamanchi
Copy link
Author

Hi @dhruvmanila ,

Could you please look into this pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants