How to resolve the algorithm Pell's equation step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pell's equation step by step in the zkl programming language

Table of Contents

Problem Statement

Pell's equation   (also called the Pell–Fermat equation)   is a   Diophantine equation   of the form: with integer solutions for   x   and   y,   where   n   is a given non-square positive integer.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pell's equation step by step in the zkl programming language

Source code in the zkl programming language

var [const] BI=Import("zklBigNum");  // libGMP

fcn solve_pell(n){
   x,y,z,r := BI(n).root(2),  x.copy(),  BI(1),  x*2;
   e1,e2, f1,f2 := BI(1), BI(0),  BI(0), BI(1);
   reg t;	// a,b = c,d is a=c; b=d
   do(30_000){  // throttle this in case of screw up
      y,z,r = (r*z - y),  (n - y*y)/z,  (x + y)/z;
 
      t,e2,e1 = e2,  r*e2 + e1,  t;
      t,f2,f1 = f2,  r*f2 + f1,  t;

      A,B := e2 + x*f2, f2;

      if (A*A - B*B*n == 1) return(A,B);
   }
}

foreach n in (T(61, 109, 181, 277)){
   x,y:=solve_pell(n);
   println("x^2 - %3d*y^2 = 1 for x = %-21d and y = %d".fmt(n,x,y));
}

  

You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Bulls and cows step by step in the Oforth programming language
You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the Mirah programming language
You may also check:How to resolve the algorithm Averages/Median step by step in the D programming language
You may also check:How to resolve the algorithm Guess the number/With feedback (player) step by step in the Red programming language