Pell's equation: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|REXX}}: added the REXX computer programming language for this task.)
m (→‎{{header|REXX}}: made display of numbers more robust, minimized output width, changed whitespace.)
Line 211: Line 211:
=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program to solve Pell's equation for the smallest solution of positive integers. */
<lang rexx>/*REXX program to solve Pell's equation for the smallest solution of positive integers. */
numeric digits 2000 /*ensure enought decimal digits for ans*/
numeric digits 2200 /*ensure enought decimal digits for ans*/
parse arg $ /*obtain optinal arguments from the CL.*/
parse arg $ /*obtain optinal arguments from the CL.*/
if $='' | $="," then $= 61 109 181 277 /*Not specified? Then use the defaults*/
if $='' | $="," then $= 61 109 181 277 /*Not specified? Then use the defaults*/
d=25 /*used for aligning the output numbers.*/
d=22 /*used for aligning the output numbers.*/
do j=1 for words($); #= word($, j) /*process all the numbers in the list. */
do j=1 for words($); #= word($, j) /*process all the numbers in the list. */
parse value pells(#) with x y /*extract the two values of X and Y.*/
parse value pells(#) with x y /*extract the two values of X and Y.*/
say 'x^2 -'right(#, 4) "* y^2 = 1 when x = " right(x,d) ' and y =' right(y,d)
say 'x^2 -'right(#,max(4,length(#))) "* y^2 == 1 when x="right(x, max(d,length(x))),
' and y='right(y, max(d,length(y)))
end /*j*/
end /*j*/
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
floor: procedure; parse arg x; _=x%1; return _ - (x < 0) * (x \= _)
floor: procedure; parse arg x; _= x % 1; return _ - (x < 0) * (x \= _)
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
iSqrt: procedure; parse arg x; r=0; q=1; do while q<=x; q=q*4; end
iSqrt: procedure; parse arg x; r= 0; q= 1; do while q<=x; q= q * 4; end
do while q>1; q=q%4; _=x-r-q; r=r%2; if _>=0 then do;x=_;r=r+q; end; end
do while q>1; q= q%4; _= x-r-q; r= r%2; if _>=0 then do; x= _; r= r+q; end; end
return r /*R: is the integer square root of X. */
return r /*R: is the integer square root of X. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
pells: procedure; parse arg n; x= iSqrt(n) /*obtain arg; obtain integer sqrt of N*/
pells: procedure; parse arg n; x= iSqrt(n); y=x /*obtain arg; obtain integer sqrt of N*/
y= x
r= x + x
r= x + x
parse value 1 0 with e1 e2 1 f2 f1 /*assign values for: E1, E2, F1, F2. */
parse value 1 0 with e1 e2 1 f2 f1 /*assign values for: E1, E2, F1, F2. */
z= 1
z= 1
do until (e2 + x * f2)**2 - n * f2 * f2 == 1
do until (e2 + x * f2)**2 - n * f2 * f2 == 1
y= r * z - y
y= r * z - y
z= floor( (n - y*y) / z)
z= floor( (n - y*y) / z)
r= floor( (x + y ) / z)
r= floor( (x + y ) / z)
parse value e2 r*e2+e1 with e1 e2
parse value e2 r * e2 + e1 with e1 e2
parse value f2 r*f2+f1 with f1 f2
parse value f2 r * f2 + f1 with f1 f2
end /*until*/
end /*until*/
return e2 + x * f2 f2</lang>
return e2 + x * f2 f2</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
x^2 - 61 * y^2 = 1 when x = 1766319049 and y = 226153980
x^2 - 61 * y^2 == 1 when x= 1766319049 and y= 226153980
x^2 - 109 * y^2 = 1 when x = 158070671986249 and y = 15140424455100
x^2 - 109 * y^2 == 1 when x= 158070671986249 and y= 15140424455100
x^2 - 181 * y^2 = 1 when x = 2469645423824185801 and y = 183567298683461940
x^2 - 181 * y^2 == 1 when x= 2469645423824185801 and y= 183567298683461940
x^2 - 277 * y^2 = 1 when x = 159150073798980475849 and y = 9562401173878027020
x^2 - 277 * y^2 == 1 when x= 159150073798980475849 and y= 9562401173878027020
</pre>
</pre>



Revision as of 14:16, 4 February 2019

Pell's equation is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Pell's equation is a Diophantine equation of the form

x2 - ny2 = 1

with integer solutions for x and y, where n is a given nonsquare positive integer.

Task requirements
  •   find the smallest solution in positive integers to Pell's equation for n = {61, 109, 181, 277}.
See also

C#

Translation of: Sidef

<lang csharp>using System; using System.Numerics;

static class Program {

   static void Fun(ref BigInteger a, ref BigInteger b, int c)
   {
       BigInteger t = a; a = b; b = b * c + t;
   }
   static void SolvePell(int n, ref BigInteger a, ref BigInteger b)
   {
       int x = (int)Math.Sqrt(n), y = x, z = 1, r = x << 1;
       BigInteger e1 = 1, e2 = 0, f1 = 0, f2 = 1;
       while (true)
       {
           y = r * z - y; z = (n - y * y) / z; r = (x + y) / z;
           Fun(ref e1, ref e2, r); Fun(ref f1, ref f2, r); a = f2; b = e2; Fun(ref b, ref a, x);
           if (a * a - n * b * b == 1) return;
       }
   }
   static void Main()
   {
       BigInteger x, y; foreach (int n in new[] { 61, 109, 181, 277 })
       {
           SolvePell(n, ref x, ref y);
           Console.WriteLine("x^2 - {0,3} * y^2 = 1 for x = {1,27:n0} and y = {2,25:n0}", n, x, y);
       }
   }

}</lang>

Output:
x^2 -  61 * y^2 = 1 for x =               1,766,319,049 and y =               226,153,980
x^2 - 109 * y^2 = 1 for x =         158,070,671,986,249 and y =        15,140,424,455,100
x^2 - 181 * y^2 = 1 for x =   2,469,645,423,824,185,801 and y =   183,567,298,683,461,940
x^2 - 277 * y^2 = 1 for x = 159,150,073,798,980,475,849 and y = 9,562,401,173,878,027,020

Go

Translation of: Sidef

<lang go>package main

import (

   "fmt"
   "math/big"

)

var big1 = new(big.Int).SetUint64(1)

func solvePell(nn uint64) (*big.Int, *big.Int) {

   n := new(big.Int).SetUint64(nn)
   x := new(big.Int).Set(n)
   x.Sqrt(x)
   y := new(big.Int).Set(x)
   z := new(big.Int).SetUint64(1)
   r := new(big.Int).Lsh(x, 1)
   e1 := new(big.Int).SetUint64(1)
   e2 := new(big.Int)
   f1 := new(big.Int)
   f2 := new(big.Int).SetUint64(1)
   t := new(big.Int)
   u := new(big.Int)
   a := new(big.Int)
   b := new(big.Int)
   for {
       t.Mul(r, z)
       y.Sub(t, y)
       t.Mul(y, y)
       t.Sub(n, t)
       z.Quo(t, z)
       t.Add(x, y)
       r.Quo(t, z)
       u.Set(e1)
       e1.Set(e2)
       t.Mul(r, e2)
       e2.Add(t, u)
       u.Set(f1)
       f1.Set(f2)
       t.Mul(r, f2)
       f2.Add(t, u)
       t.Mul(x, f2)
       a.Add(e2, t)
       b.Set(f2)
       t.Mul(a, a)
       u.Mul(n, b)
       u.Mul(u, b)
       t.Sub(t, u)
       if t.Cmp(big1) == 0 {
           return a, b
       }
   }

}

func main() {

   ns := []uint64{61, 109, 181, 277}
   for _, n := range ns {
       x, y := solvePell(n)
       fmt.Printf("x^2 - %3d*y^2 = 1 for x = %-21s and y = %s\n", n, x, y)
   }

}</lang>

Output:
x^2 -  61*y^2 = 1 for x = 1766319049            and y = 226153980
x^2 - 109*y^2 = 1 for x = 158070671986249       and y = 15140424455100
x^2 - 181*y^2 = 1 for x = 2469645423824185801   and y = 183567298683461940
x^2 - 277*y^2 = 1 for x = 159150073798980475849 and y = 9562401173878027020

Perl

<lang perl>sub solve_pell {

   my ($n) = @_;
   use bigint try => 'GMP';
   my $x = int(sqrt($n));
   my $y = $x;
   my $z = 1;
   my $r = 2 * $x;
   my ($e1, $e2) = (1, 0);
   my ($f1, $f2) = (0, 1);
   for (; ;) {
       $y = $r * $z - $y;
       $z = int(($n - $y * $y) / $z);
       $r = int(($x + $y) / $z);
       ($e1, $e2) = ($e2, $r * $e2 + $e1);
       ($f1, $f2) = ($f2, $r * $f2 + $f1);
       my $A = $e2 + $x * $f2;
       my $B = $f2;
       if ($A**2 - $n * $B**2 == 1) {
           return ($A, $B);
       }
   }

}

foreach my $n (61, 109, 181, 277) {

   my ($x, $y) = solve_pell($n);
   printf("x^2 - %3d*y^2 = 1 for x = %-21s and y = %s\n", $n, $x, $y);

}</lang>

Output:
x^2 -  61*y^2 = 1 for x = 1766319049            and y = 226153980
x^2 - 109*y^2 = 1 for x = 158070671986249       and y = 15140424455100
x^2 - 181*y^2 = 1 for x = 2469645423824185801   and y = 183567298683461940
x^2 - 277*y^2 = 1 for x = 159150073798980475849 and y = 9562401173878027020

Perl 6

Works with: Rakudo version 2018.12
Translation of: Perl

<lang perl6>sub pell (Int $n) {

   my $y = my $x = Int(sqrt $n);
   my $z = 1;
   my $r = 2 * $x;
   my ($e1, $e2) = (1, 0);
   my ($f1, $f2) = (0, 1);
   loop {
       $y = $r * $z - $y;
       $z = Int(($n - $y²) / $z);
       $r = Int(($x + $y)  / $z);
       ($e1, $e2) = ($e2, $r * $e2 + $e1);
       ($f1, $f2) = ($f2, $r * $f2 + $f1);
       my $A = $e2 + $x * $f2;
       my $B = $f2;
       if ($A² - $n * $B² == 1) {
           return ($A, $B);
       }
   }

}

for 61, 109, 181, 277 -> $n {

   my ($x, $y) = pell($n);
   printf("x² - %3dy² = 1 for x = %-21s and y = %s\n", $n, $x, $y);

}</lang>

Output:
x² -  61y² = 1 for x = 1766319049            and y = 226153980
x² - 109y² = 1 for x = 158070671986249       and y = 15140424455100
x² - 181y² = 1 for x = 2469645423824185801   and y = 183567298683461940
x² - 277y² = 1 for x = 159150073798980475849 and y = 9562401173878027020

REXX

<lang rexx>/*REXX program to solve Pell's equation for the smallest solution of positive integers. */ numeric digits 2200 /*ensure enought decimal digits for ans*/ parse arg $ /*obtain optinal arguments from the CL.*/ if $= | $="," then $= 61 109 181 277 /*Not specified? Then use the defaults*/ d=22 /*used for aligning the output numbers.*/

    do j=1  for words($);    #= word($, j)      /*process all the numbers in the list. */
    parse value   pells(#)   with   x  y        /*extract the two values of  X  and  Y.*/
    say 'x^2 -'right(#,max(4,length(#))) "* y^2 == 1  when x="right(x, max(d,length(x))),
                                                     ' and y='right(y, max(d,length(y)))
    end   /*j*/

exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ floor: procedure; parse arg x; _= x % 1; return _ - (x < 0) * (x \= _) /*──────────────────────────────────────────────────────────────────────────────────────*/ iSqrt: procedure; parse arg x; r= 0; q= 1; do while q<=x; q= q * 4; end

        do  while q>1; q= q%4; _= x-r-q; r= r%2; if _>=0  then do; x= _; r= r+q; end; end
      return r                                  /*R:  is the integer square root of X. */

/*──────────────────────────────────────────────────────────────────────────────────────*/ pells: procedure; parse arg n; x= iSqrt(n); y=x /*obtain arg; obtain integer sqrt of N*/

      r= x + x
      parse value  1 0  with  e1 e2  1  f2 f1   /*assign values for:  E1, E2,  F1, F2. */
      z= 1
                             do  until (e2 + x * f2)**2   -   n * f2 * f2  ==  1
                             y= r * z   -   y
                             z= floor( (n - y*y) / z)
                             r= floor( (x + y  ) / z)
                             parse value  e2   r * e2  +  e1     with    e1  e2
                             parse value  f2   r * f2  +  f1     with    f1  f2
                             end   /*until*/
      return e2   + x * f2      f2</lang>
output   when using the default inputs:
x^2 -  61 * y^2 == 1  when x=            1766319049  and y=             226153980
x^2 - 109 * y^2 == 1  when x=       158070671986249  and y=        15140424455100
x^2 - 181 * y^2 == 1  when x=   2469645423824185801  and y=    183567298683461940
x^2 - 277 * y^2 == 1  when x= 159150073798980475849  and y=   9562401173878027020

Sidef

<lang ruby>func solve_pell(n) {

   var x = n.isqrt
   var y = x
   var z = 1
   var r = 2*x
   var (e1, e2) = (1, 0)
   var (f1, f2) = (0, 1)
   loop {
       y = (r*z - y)
       z = floor((n - y*y) / z)
       r = floor((x + y) / z)
       (e1, e2) = (e2, r*e2 + e1)
       (f1, f2) = (f2, r*f2 + f1)
       var A = (e2 + x*f2)
       var B = f2
       if (A**2 - n*B**2 == 1) {
           return (A, B)
       }
   }

}

for n in [61, 109, 181, 277] {

   var (x, y) = solve_pell(n)
   printf("x^2 - %3d*y^2 = 1 for x = %-21s and y = %s\n", n, x, y)

}</lang>

Output:
x^2 -  61*y^2 = 1 for x = 1766319049            and y = 226153980
x^2 - 109*y^2 = 1 for x = 158070671986249       and y = 15140424455100
x^2 - 181*y^2 = 1 for x = 2469645423824185801   and y = 183567298683461940
x^2 - 277*y^2 = 1 for x = 159150073798980475849 and y = 9562401173878027020

Visual Basic .NET

Translation of: Sidef

<lang vbnet>Imports System.Numerics

Module Module1

   Sub Fun(ByRef a As BigInteger, ByRef b As BigInteger, c As Integer)
       Dim t As BigInteger = a : a = b : b = b * c + t
   End Sub
   Sub SolvePell(n As Integer, ByRef a As BigInteger, ByRef b As BigInteger)
       Dim x As Integer = Math.Sqrt(n), y As Integer = x, z As Integer = 1, r As Integer = x << 1,
           e1 As BigInteger = 1, e2 As BigInteger = 0, f1 As BigInteger = 0, f2 As BigInteger = 1
       While True
           y = r * z - y : z = (n - y * y) / z : r = (x + y) / z
           Fun(e1, e2, r) : Fun(f1, f2, r) : a = f2 : b = e2 : Fun(b, a, x)
           If a * a - n * b * b = 1 Then Exit Sub
       End While
   End Sub
   Sub Main()
       Dim x As BigInteger, y As BigInteger
       For Each n As Integer In {61, 109, 181, 277}
           SolvePell(n, x, y)
           Console.WriteLine("x^2 - {0,3} * y^2 = 1 for x = {1,27:n0} and y = {2,25:n0}", n, x, y)
       Next
   End Sub

End Module</lang>

Output:
x^2 -  61 * y^2 = 1 for x =               1,766,319,049 and y =               226,153,980
x^2 - 109 * y^2 = 1 for x =         158,070,671,986,249 and y =        15,140,424,455,100
x^2 - 181 * y^2 = 1 for x =   2,469,645,423,824,185,801 and y =   183,567,298,683,461,940
x^2 - 277 * y^2 = 1 for x = 159,150,073,798,980,475,849 and y = 9,562,401,173,878,027,020