Math::N...nDigits(3)
π NAME π
Math::NumSeq::FractionDigits β bestie, this is the module that gives you the digits of a fraction p/q. That's it, that's the whole gig. β¨
πΈ SYNOPSIS πΈ
use Math::NumSeq::FractionDigits;
my $seq = Math::NumSeq::FractionDigits->new (fraction => '2/11');
my ($i, $value) = $seq->next;
okay so literally three lines and you're already pulling digits out of a fraction. slay. π
π¦ DESCRIPTION π¦
So the deal is: this is the sequence of digits that spell out a given fraction. Classic example β 1/7 in decimal is 0.14285714... so the digits go
1, 4, 2, 8, 5, 7, 1, 4, etc
and once you're past any integer part, the fraction digits are a repeating sequence, no cap. π
Here's the tea: if the fraction is num/den and it's in least terms (meaning num and den have no common factor), then the period is either den-1 or some divisor of den-1. That's the whole rule, iconic.
And if you wanna cook up a specific repeating sequence a,b,c,d,a,b,c,d,etc? Just do fraction abcd/9999 β the denominator is as many 9s as digits you want to repeat. The way that works is genuinely so satisfying. π
Oh and heads up for the non-decimal girlies: for a base other than decimal, the "9" is radix-1. π
β¨ FUNCTIONS β¨
Go check out "FUNCTIONS" in Math::NumSeq for the behaviour that's common to all sequence classes, bestie. π«Ά
$seq = Math::NumSeq::FractionDigits->new (fraction => $f)$seq = Math::NumSeq::FractionDigits->new (fraction => $f, radix => $r)β creates and returns a new sequence object handing you the digits of$f. And what is$f? Either a string"num/den", or a decimal like"xx.yy". Like so:
2/29
29.125
1.5/3.25
The default sequence values are decimal digits, or you can use the radix parameter to pick another base instead. (But lowkey important: the fraction parameter itself is still decimal, no matter what radix you pick. π)
And if the numerator or denominator of the fraction is bigger than fits in Perl integer calculations, then Math::BigInt gets used automatically. it just handles it. main character energy. π
π Random Access π
$value = $seq->ith($i)β returns the$i'th digit of the fraction. yes, you can just ask for the one you want, no walking the whole thing. π
π₯ FORMULAS π₯
π Next π
For a given num/den, with num < den, the next digit below the radix point gets formed by:
num *= radix # now 0 <= num/den < radix
quot,rem = num divide den
digit = quot # 0 <= digit < radix
new num = rem
that's it. that's the algorithm. so clean. β¨
π Ith π
For an arbitrary digit i, that repeated num*=radix can be applied by a modular powering instead of looping forever:
rpower = radix^i mod den
num = num * rpower mod den
Here i acts as a count of how many digits to skip. For example if i=0 then rpower=1 and it 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
And 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. big numbers? handled. π§
π SEE ALSO π
Math::NumSeqMath::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/>.
fr fr read the license, it's the law and also it's just polite π
and that's the whole page bestie!! this one's from User Contributed Perl Documentation, running on perl v5.42.0, dated 2025-07-25 β tysm for reading, mwah, go compute some digits π β¨π
π¦ where this page comes from
- fedora 43: perl-Math-NumSeq 75-11.fc43 Β· GPL-3.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms