NAME

Math::NumSeq::FractionDigits -- the digits of a fraction p/q

SYNOPSIS

use Math::NumSeq::FractionDigits;
my $seq = Math::NumSeq::FractionDigits->new (fraction => '2/11');
my ($i, $value) = $seq->next;

DESCRIPTION

The sequence of digits which are a given fraction. For example 1/7 in decimal, being 0.14285714...

1, 4, 2, 8, 5, 7, 1, 4, etc

After any integer part, the fraction digits are a repeating sequence. If the fraction is num/den and is in least terms (num and den have no common factor) then the period is either den-1 or some divisor of den-1.

A particular a repeating sequence a,b,c,d,a,b,c,d,etc can be cooked up with fraction abcd/9999, the denominator being as many 9s as digits to repeat. For a base other than decimal the "9" is radix-1.

FUNCTIONS

See "FUNCTIONS" in Math::NumSeq for behaviour common to all sequence classes.

$seq = Math::NumSeq::FractionDigits->new (fraction => $f)
$seq = Math::NumSeq::FractionDigits->new (fraction => $f, radix => $r)

Create and return a new sequence object giving the digits of $f. $f is a string "num/den", or a decimal "xx.yy",

2/29
29.125
1.5/3.25

The default sequence values are decimal digits, or the radix parameter can select another base. (But the fraction parameter is still decimal.)

If the numerator or denominator of the fraction is bigger than fits Perl integer calculations then Math::BigInt is used automatically.

Random Access

$value = $seq->ith($i)

Return the $i'th digit of the fraction.

FORMULAS

Next

For a given num/den, with num < den, the next digit below the radix point is formed by

num *= radix               # now 0 <= num/den < radix
quot,rem = num divide den
digit = quot               # 0 <= digit < radix
new num = rem

Ith

For an arbitrary digit i, the repeated num*=radix can be applied by a modular powering

rpower = radix^i mod den
num = num * rpower mod den

i here acts as a count of how many digits to skip. For example if i=0 then rpower=1 and doesn't change the numerator at all. With that big skip the digit is then the same as for "next" above,

num *= radix             # now 0 <= num/den < radix
digit = floor(num/den)   # 0 <= digit < radix

The usual modular powering techniques can be applied to calculate radix^i mod den. Math::BigInt has a bmodpow which is used in the code if the inputs are big.

SEE ALSO

Math::NumSeq, Math::NumSeq::SqrtDigits

HOME PAGE

http://user42.tuxfamily.org/math-numseq/index.html

LICENSE

Copyright 2010, 2011, 2012, 2013, 2014, 2016, 2019, 2020 Kevin Ryde

Math-NumSeq is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

Math-NumSeq is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Math-NumSeq. If not, see <http://www.gnu.org/licenses/>.