The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Math::Pell - Solution for Pell equations

VERSION

version 0.6

DESCRIPTION

This module solves the Pell equation by finding a minimal solution and making a method available for generating all solutions to the Pell equation:

    x^2 - Dy^2 = 1

Of particular interest are non-trivial solutions to this equation. The numbers x and y are integers.

SYNOPSIS

Finding the first 5 solutions of x^2 - 7y^2 = 1

    use Math::Pell;
    my $p = Math::Pell->new({D=>7});# equation is x^2 - Dy^2 = 1 where x,y are integers
    $p->find_minimal_sol;
    printf "(%d,%d)\n",$p->nth_solution($_)
    for 1..5;


    (8,3)
    (127,48)
    (2024,765)
    (32257,12192)
    (514088,194307)

SAMPLE TEST OUTPUT

    sqrt[13]=  [3,1,1,1,1,6]
    n= 2 p=4 q=1 p/q = 4
    n= 3 p=7 q=2 p/q = 3
    n= 4 p=11 q=3 p/q = 3
    n= 5 p=18 q=5 p/q = 3
    n= 6 p=119 q=33 p/q = 3
    n= 7 p=137 q=38 p/q = 3
    n= 8 p=256 q=71 p/q = 3
    n= 9 p=393 q=109 p/q = 3
    n= 10 p=649 q=180 p/q = 3
    Minimal solution -> (649,180)
    ok 14 - (649,180) is a working solution 1
    ok 15 - (1093435849,303264540) is a working solution 1
    ok 16 - (1419278889601,393637139280) is a working solution 1
    ok 17 - (1842222905266249,510940703520900) is a working solution 1
    ok 18 - (2391203911756701601,663200639532988920) is a working solution 1
    ok 19 - (3103780835237293411849,860833919173116097260) is a working solution 1
    ok 20 - (4028705132934095091878401,1117361763886065161254560) is a working solution 1
    ok 21 - (5229256158767620191964752649,1450334708690193406192321620) is a working solution 1
    ok 22 - (6787570465375238075075157060001,1882533334518107155172472208200) is a working solution 1
    ok 23 - (8810261234800900253827361899128649,2443526817869794397220462733921980) is a working solution 1
    ok 24 - (11435712295201103154229840669911926401,3171695927061658609485005456158521840) is a working solution 1
    ok 25 - (14843545748909797093290079362183781339849,4116858869799215005317139861631027426340) is a working solution 1
    ok 26 - (19266910946372621425987368782273878267197601,5343679641303454015243038055391617440867480) is a working solution 1
    ....






    sqrt[61]=  [7,1,4,3,1,2,2,1,3,4,1,14]
    ...
    Minimal solution -> (1766319049,226153980)
    ok 14 - (1766319049,226153980) is a working solution 1
    ok 15 - (22042834973108102061352541449,2822295814832482312327709940) is a working solution 1
    ok 16 - (77869358613928486808166555366140995201,9970149719303180503641083029374964080) is a working solution 1
    ....

CF2fraction($max_iter,$conv_callback,@cont_fraction)

  • @cont_fraction is the continued fraction form of the square root the non-periodic and periodic parts are provided here.

  • $conv_callback is a subref which is called whenever a new convergent is found and it's passed as argument to the callback

  • $max_iter is the maximum iteration

nth_solution($i)

if a minimal solution a+b\sqrt{D} is found , then all solutions can be found by expanding (a+b\sqrt{D})^i = A+B\sqrt{D} so (A,B) is also a solution.

cfrac(D)

computes the continued fraction representation of a square root D. it can be proved that the continued fraction will be periodic.

find_minimal_sol()

returns a minimal solution for the Pell equation by finding the first convergent of \sqrt{D} for which it's numerator and denominator are solutions to the Pell equation.

BIBLIOGRAPHY

    [1] L. Panaitopol A. Gica - O Introducere in aritmetica si Teoria Numerelor

SEE ALSO

http://en.wikipedia.org/wiki/Pell's_equation

AUTHOR

Stefan Petrea, <stefan.petrea at gmail.com>