sol1 Algorithm

The sol1 Algorithm, also known as "Squaring the Circle" algorithm, is a mathematical technique that aims to solve the ancient geometrical problem of constructing a square with the same area as a given circle using only compass and straightedge. This algorithm is based on the approximation of the value of Pi (π), which is the ratio of the circumference of a circle to its diameter. The main idea behind the sol1 Algorithm is to find the side length of a square that, when multiplied by itself, gives the same area as that of a circle with a given radius. The sol1 Algorithm begins by drawing a circle with the desired radius, followed by constructing an inscribed square within the circle. The next step involves dividing the circle's circumference into a number of equal segments, which are then used to create a polygon that approximates the circle. The area of this polygon can be easily calculated using basic trigonometry, and as the number of segments increases, the approximation of the circle's area becomes more accurate. Finally, the side length of the square is determined by finding the square root of the approximated circle's area, and a square with this side length is constructed using a compass and straightedge. Although the sol1 Algorithm provides an approximation to the problem of squaring the circle, it has been proven mathematically impossible to achieve an exact solution using only compass and straightedge due to the transcendental nature of the number π.
"""
Starting in the top left corner of a 2×2 grid, and only being able to move to
the right and down, there are exactly 6 routes to the bottom right corner.
How many such routes are there through a 20×20 grid?
"""
from math import factorial


def lattice_paths(n):
    """
    Returns the number of paths possible in a n x n grid starting at top left
    corner going to bottom right corner and being able to move right and down
    only.

bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 50
1.008913445455642e+29
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 25
126410606437752.0
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 23
8233430727600.0
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 15
155117520.0
bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 1
2.0

    >>> lattice_paths(25)
    126410606437752
    >>> lattice_paths(23)
    8233430727600
    >>> lattice_paths(20)
    137846528820
    >>> lattice_paths(15)
    155117520
    >>> lattice_paths(1)
    2

    """
    n = 2 * n  # middle entry of odd rows starting at row 3 is the solution for n = 1,
    # 2, 3,...
    k = n / 2

    return int(factorial(n) / (factorial(k) * factorial(n - k)))


if __name__ == "__main__":
    import sys

    if len(sys.argv) == 1:
        print(lattice_paths(20))
    else:
        try:
            n = int(sys.argv[1])
            print(lattice_paths(n))
        except ValueError:
            print("Invalid entry - please enter a number.")

LANGUAGE:

DARK MODE: