' Puzzlet #063
' The equation x^2 = 2y^4 -1 can be satisfied
' for the values x = 1 and y = 1. Find an
' alternative solution with the lowest values
' for x and y except 1. It's possible to solve
' this diophantine equation by using math-
' ematical methods, but not many folk know
' how. This code is an alternative method.
' By Dave Ellis.
def flag, x, y, LHS, RHS: int
openconsole
print "Searching ...": print
x = 2: y = 2
flag = 0
do
LHS = x^2
RHS = 2*y^4 - 1
select 1
case (LHS > RHS)
'need to make y bigger
y = y + 1
case (LHS < RHS)
'need to make x bigger, reset y
x = x + 1
y = 2
default
'both sides equal - solution!
flag = -1
endselect
until flag
' pretty print the solution
print "x = ", x,
print ": y = ", y
print: print "And",
print str$(x), "^2 = 2*",
print ltrim$(str$(y)), "^4 - 1"
print: print "So x^2 = 2*y^4 - 1"
print: print "Press a key to exit... ",
do: until inkey$ <> ""
closeconsole
end
PROGRAM NOTES
There's not point in beginning any search with values x = 1 and y = 1, since this has already been identified as a trivial solution. So we begin by initialising x and y to value 2. Variable flag is used as a success indicator, and is initialised to 0 (FALSE).
For convenience, the left-hand side of the equation is represented by variable LHS, and the right-hand side by RHS. Within the main DO loop, each of these variables is given a respective value by evaluating the current values of x and y and saving them into LHS and RHS.
Now we can compare these values. There are only three possiblities:
1. LHS > RHS. In these circumstances, we need to make y larger so that RHS is bigger, and then try again.
2. RHS > LHS. In these circumstances, we need to make x larger so that LHS is bigger, reset y to its minimum value of 2, and then try again.
3. RHS = LHS. This is the solution we're looking for! When this happens, flag is set to -1 (TRUE).
When the end of the DO loop is reached, flag is examined. If it's not TRUE, the loop is executed again with whatever values of x and y we have revised. If flag is TRUE, we have a valid solution, and it's printed to the screen.
When you run the code, it generates the screen shown inset. With such a large value for x, it's unlikely you could have solved the equation by trial and error.
Searching ...
x = 239 : y = 13
And 239^2 = 2*13^4 - 1
So x^2 = 2*y^4 - 1
Press any key ...
MAIN MENU
DOWNLOAD CODE
ARCHIVES
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.