6

As some of you might know,this is Brahmagupta's equation . How to find solution for this ? I mean integral solution? How to solve it using programming ?

I tried something like $x^2=1+92y^2$

$x=\sqrt{1+92y^2}$

Use brute force approach to check for every y ? Is there any better answer ?

Bart Michels
  • 24,846
  • 5
  • 45
  • 108
vaidy_mit
  • 611
  • 1
  • 9
  • 21
  • I am a newbie to stack exchange !! Please let me know why you downvoted,so that i can improve in my future posts !! – vaidy_mit Jun 17 '13 at 09:59
  • Actually there is a verse that "The person who solves this within one year is a mathematician " !! Minimal answer given by mathematician brahmagupta was (1151,120) .http://en.wikipedia.org/wiki/Brahmagupta's_problem – vaidy_mit Jun 17 '13 at 10:03
  • And i would like to know how to approach this better in programming !! – vaidy_mit Jun 17 '13 at 10:04
  • 1
    You should look at http://en.wikipedia.org/wiki/Pell%27s_equation – Quimey Jun 17 '13 at 10:11
  • Bramhagupta was very great mathematician because he finded an equation for finding real mathematician. But according to me, a person who finds the solution for the equation x^2-99y^2=1 In 10 seconds was a real mathematician –  Nov 11 '14 at 15:21
  • Um... I don't see anywhere that x and y are supposed to be integers. x = ... **is** the solution that provides all uncountably many infinite real solutions. – fleablood Feb 18 '16 at 07:01
  • $(X-1)(x+1)=4*23y^2$ x-1 and x+1 can't have any factors greater than 2 in common. Both x-1 and x+1 are even but one divides by 4 and the other doesnt. – fleablood Feb 18 '16 at 07:15

4 Answers4

8

I think the most common systematic way to solve this type of problem is using continued fractions. I'll reiterate @Quimey's suggestion to refer to Wikipedia for Pell's Equation and specifically the section "Fundamental solution via continued fractions" and the Lenstra paper cited there.

In this case as a periodic continued fraction $$ \sqrt{92} = [9;1,1,2,4,2,1,1,18,1,1,2\ldots] = 9+\cfrac{1}{1+\cfrac{1}{1+\cfrac{1}{2+\cfrac{1}{4+\cfrac{1}{2+\cdots}}}}} $$ and after 7 terms we get the approximation $\sqrt{92}\simeq 1151/120$ which gives the fundamental solution.

As for implementing an algorithm for finding the continued fraction for square roots, you should be able to find resources online with a search, but can start by taking a look at this question.

Zander
  • 10,788
  • 1
  • 24
  • 53
2

As for the programming part, I thought I'd put up some simple brute force Python just as an example.

# Start at (1, 1) because (1, 0) is a trivial solution
x, y = 1, 1
z = x**2 - 92*(y**2)

while z != 1:
    if z > 1:
        y += 1
    else:
        x += 1

    z = x**2 - 92*(y**2)

print x, y

This outputs the first solution

1151 120

Of course, as a brute force solution this code won't get you very far if, for example, you replaced 92 with larger numbers (or even if you replaced it by 61, for that matter).

Sp3000
  • 1,544
  • 9
  • 16
  • Actually this was asked in a google interview :) Hope this solution helps everyone :) And programming makes it simpler !! :) – vaidy_mit Jun 17 '13 at 14:22
  • Bonus points if you manage to program in the continued fraction algorithm in that case then :P (if you get it right you can solve for large numbers really quickly, just for interest) – Sp3000 Jun 17 '13 at 15:16
  • This reminds me of some of Dijkstra's programs in "A Discipline of Programming" and the online site. – marty cohen Jan 22 '15 at 06:49
1

The key is a (kind of) miraculous identity: $(x_1^2 - Ny_1^2)(x_2^2 - Ny_2^2)=(x_1x_2 + Ny_1y_2) - N(x_1y_2 + x_2y_1)^2$.

This means that if $x_1^2 - Ny_1^2 = k_1$ and $x_2 - Ny_2^2 = k_2$, then $(x_1x_2 + Ny_1y_2) - N(x_1y_2 + x_2y_1)^2 = k_1k_2$.

We can rewrite this in the following way: if $(x_1,y_1,k_1)$ and $(x_2,y_2,k_2)$ satisfy $x^2-92y^2 = k$, then $(x_1x_2 + Ny_1y_2,x_1y_2 + x_2y_1, k_1k_2)$ is also a solution. This operation is called "composing".

Now it is obvious that $(10,1,8)$ is one such triple satisfying $x^2 - 92y^2 = k$. We can compose $(10,1,8)$ with $(10,1,8)$ to get the triple $(192, 20, 64)$ and divide $x$ and $y$ by $8$ (so that $k$ is reduced by a factor of $64$), so that we have $(24, \frac{5}{2}, 1)$. Composing this with itself again, we have the solution $(1151, 120, 1)$.

-1
def pr_sol(maxval,N,ee,ff):
    count=0
    aa,bb=0,1
    gg=N*ff
    while count<maxval:
        if bb**2 - 1 ==N*aa**2:
            print(bb,'^2-1=',N,'*',aa,'^2')
        else:
            print(bb,'^2+1=',N,'*',aa,'^2')
        anew=ee*aa+ff*bb
        bnew=gg*aa+ee*bb
        aa=anew
        bb=bnew
        count+=1
  return 

pr_sol(20,92,1151,120) #print first 20 solutions when N=92

""" the smallest solution is 1151,120 where 1151 ^2-1= 92 * 120 ^2 (from wikipedia). (I'm not sure if it's the same as wikipedia recursion, but in this case the following recursion applies:

a_{t+1}=1151*a_{t} +120*b_{t}
b_{t+1}=120*92*a_{t} +1151*b_{t}
(in general you can generate such a recursion, which gives 
values for x^2-1=Ny^2  AND x^2+1=Ny^2 
(but x^2 ==91 mod 92 has no solutions so
there are no sol to x^2+1=Ny^2 ))

The first few solutions are:
1 ^2-1= 92 * 0 ^2  (a,b)=( 0 , 1 ) 
1151 ^2-1= 92 * 120 ^2 (a,b)=( 120 , 1151 )
2649601 ^2-1= 92 * 276240 ^2 (a,b)=( 276240 , 2649601 ) 
6099380351 ^2-1= 92 * 635904360 ^2 
14040770918401 ^2-1= 92 * 1463851560480 ^2 
32321848554778751 ^2-1= 92 * 3369785656320600 ^2
I've just been doing some investigation into these equations
and found this recursion. (which has some advantage. For 
example N=313 has very large first solution to x^2-1=Ny^2 but 
you can generate the numbers from the much smaller solution:
126862368^2 +1 =313 *  7170685^2
and the recursion: 
a_{t+1}= 126862368 a_t+7170685 b_t
b_{t+1}=313*7170685a_t+126862368  b_t
so 
pr_sol(20,313,126862368,7170685) 
prints first 20 solutions for N=313 
(note they alternate between x^2+1=313y^2 and x^2-1=313y^2)

"""