Skip to content

Project Euler problem148 #8662

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

Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5c8d0da
Added solution for problem 148 in Project Euler
saitejamanchi Apr 13, 2023
7a58574
Added solution for problem 148 in Project Euler
saitejamanchi Apr 13, 2023
449f3cc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 13, 2023
c0c3e3a
Added solution to problem 148 in Project Euler
saitejamanchi Apr 14, 2023
a5b0ca6
Added solution to problem 148 in Project Euler
saitejamanchi Apr 14, 2023
922bc8a
Added Solution for Problem 148 in Project Euler
saitejamanchi Apr 15, 2023
ba60c23
Added Solution for Problem 148 in Project Euler
saitejamanchi Apr 15, 2023
04f30b3
Added Solution for Problem 148 in Project Euler
saitejamanchi Apr 15, 2023
8c1f2c6
Added Solution for Problem 148 in Project Euler
saitejamanchi Apr 15, 2023
41c6d41
Added Solution for Problem 148 in Project Euler
saitejamanchi Apr 15, 2023
9d98bb1
Added Solution for Problem 148 of Project Euler
saitejamanchi Apr 16, 2023
e4e89ca
Added solution for problem 148 in Project Euler
saitejamanchi Apr 24, 2023
237b6c9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 24, 2023
4270291
Added an optimal solution for Problem 148 in project Euler
saitejamanchi Apr 30, 2023
ba29c52
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 30, 2023
249a507
Added an optimal solution for Problem 148 in project Euler
saitejamanchi Apr 30, 2023
1a22299
Added an optimal solution for Problem 148 in project Euler
saitejamanchi Apr 30, 2023
31b1831
Added an optimal solution for Problem 148 in project Euler
saitejamanchi Apr 30, 2023
cf0c3b1
Added doctests for get_num_binomials function in Problem 148 in proje…
saitejamanchi Apr 30, 2023
9f529c5
Merge branch 'TheAlgorithms:master' into project-euler-problem148
saitejamanchi Apr 30, 2023
3854de8
Reformatted code
saitejamanchi Apr 30, 2023
3c13d94
Merge branch 'project-euler-problem148' of https://github.com/saiteja…
saitejamanchi Apr 30, 2023
0b706b9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 30, 2023
bc06932
Merge branch 'TheAlgorithms:master' into project-euler-problem148
saitejamanchi May 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
77 changes: 77 additions & 0 deletions project_euler/problem_148/sol1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
Project Euler Problem 148 : https://projecteuler.net/problem=148
Author: Sai Teja Manchi
Problem Statement:
We can easily verify that none of the entries in the
first seven rows of Pascal's triangle are divisible by 7:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
However, if we check the first one hundred rows, we will find that
only 2361 of the 5050 entries are not divisible by 7.
Find the number of entries which are not divisible by 7
in the first one billion (109) rows of Pascal's triangle.

Solution:
We iteratively generate each row in the pascal triangle one-by-one.
Since Pascal's triangle is vertically-symmetric,
We only need to generate half of the values.
We then count the values which are not divisible by 7.
We only store the remainders(when divided by 7) in the list to reduce memory usage.

Note: In the original problem, we need to calculate for 10^9 rows
but we took 10^4 rows here by default.
"""


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

@rohan472000 rohan472000 Apr 24, 2023

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.

"""
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.

To evaluate the solution, use solution()
>>> solution(3)
6
>>> solution(10)
40
>>> solution(100)
2361
"""

# Initializing pascal row and count
pascal_row = [1, 2]
count = 6

# To keep track of length of the pascal row
l = 2

for i in range(3, pascal_row_count):
j = 1

# Generating the next pascal row
while j < l:
pascal_row[j - 1] = (pascal_row[j - 1] + pascal_row[j]) % 7
if pascal_row[j - 1] != 0:
count += 2
j += 1

# Adding the middle element for even rows
if i % 2 == 0:
pascal_row[-1] = pascal_row[-1] * 2
l += 1
if pascal_row[-1] % 7 != 0:
count += 1
# Deleting the last element for odd rows since 1 is added at beginning
else:
del pascal_row[-1]
pascal_row.insert(0, 1)

# Adding 2 to the count for the Additional 1's in the new pascal row
count += 2

return count


if __name__ == "__main__":
print(f"{solution()}")