Skip to content

Instantly share code, notes, and snippets.

@ialexpovad
Created March 14, 2024 09:17
Show Gist options
  • Save ialexpovad/cd419c472a014dfb8ce7cf88f85537a2 to your computer and use it in GitHub Desktop.
Save ialexpovad/cd419c472a014dfb8ce7cf88f85537a2 to your computer and use it in GitHub Desktop.
Methods for solving the diophantine type equation: "X^2=NY^2 - 1".
from sympy import sqrt, Rational
def solve_equation_brute_force(n):
'''
Brute Force Method:
This method involves iterating through possible values of xx and yy until a solution is found.
'''
solutions = []
for y in range(1, 10000): # Adjust the range as needed
x_squared = n * y * y - 1
if x_squared < 0:
break
x = int(x_squared ** 0.5)
if x * x == x_squared:
solutions.append((x, y))
return solutions
# Example usage:
# n = 17
# solutions = solve_equation_brute_force(n)
# print("Solutions for x^2 = {}y^2 - 1:".format(n))
# for solution in solutions:
# print(solution)
def solve_equation_binary_search(n):
'''
Binary Search
Another approach is to use binary search over the range of possible values for yy and checking if x2=ny2−1x2=ny2−1 holds true. Here's how to implement it:
'''
x, y = 0, 1
while True:
x += 1
while x * x > n * y * y - 1:
y += 1
if x * x == n * y * y - 1:
return x, y
# Example usage:
# n = 17
# x, y = solve_equation_binary_search(n)
# print(f"x = {x}, y = {y}")
from math import isqrt
def solve_equation_continued_fractions(n, limit):
'''
Continued Fractions
This method utilizes continued fractions to find solutions efficiently.
'''
solutions = []
for k in range(1, limit):
m_squared = k**2 * n - 1
if isqrt(m_squared)**2 == m_squared:
x = isqrt(m_squared)
y = k
solutions.append((x, y))
return solutions
# Example usage:
# n = 2
# limit = 100
# solutions = solve_equation_continued_fractions(n, limit)
# print("Solutions for x^2 = {}y^2 - 1 where x, y < {}: {}".format(n, limit, solutions))
def solve_equation_iterative(n, limit):
'''
Iterative Method
'''
solutions = []
for y in range(1, limit):
x_squared = n * y * y - 1
if x_squared > 0 and x_squared ** 0.5 % 1 == 0:
x = int(x_squared ** 0.5)
solutions.append((x, y))
return solutions
# Example usage:
# n = 2 # Example value of n
# limit = 1000 # Limit for y values
# solutions = solve_equation_iterative(n, limit)
# if solutions:
# for x, y in solutions:
# print(f"Solution: x = {x}, y = {y}")
# else:
# print("No solutions found within the limit.")
import math
def pell_lucas_method(n, num_solutions):
solutions = []
m, d = 0, 1
a = int(math.sqrt(n))
a0 = a
h, k, h_prev, k_prev = a, 1, 1, 0
while len(solutions) < num_solutions:
m = d*a - m
d = (n - m*m) // d
a = (a0 + m) // d
h, h_prev = a*h + h_prev, h
k, k_prev = a*k + k_prev, k
x, y = h, k
if x**2 == n * y**2 - 1:
solutions.append((x, y))
return solutions
# Example usage
# n = 2
# num_solutions = 1
# print("\nSolutions using Pell-Lucas Method:")
# print(pell_lucas_method(n, num_solutions))
def solve_equation_binary_search(n, num_pairs):
solutions = []
x, y = 0, 1
while len(solutions) < num_pairs:
x += 1
low, high = 0, x
while low <= high:
mid = (low + high) // 2
if x**5 == n * mid**2 - 1:
solutions.append((x, mid))
break
elif x**5 < n * mid**2 - 1:
high = mid - 1
else:
low = mid + 1
return solutions
# Example usage:
n = 2
num_pairs = 1
pairs = solve_equation_binary_search(n, num_pairs)
for i, (x, y) in enumerate(pairs, start=1):
print(f"Pair {i}: x = {x}, y = {y}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment