Math::N...:ReRound(3)
β¨ Math::N...:ReRound(3) β¨
okay bestie, pull up a chair πͺ because this is the Math::NumSeq::ReRound page from the User Contributed Perl Documentation, and it's literally about a sequence made by repeatedly rounding up π
no cap.
πΈ NAME πΈ
Math::NumSeq::ReRound β sequence from repeated rounding up π
π¦ SYNOPSIS π¦
use Math::NumSeq::ReRound;
my $seq = Math::NumSeq::ReRound->new;
my ($i, $value) = $seq->next;
π DESCRIPTION π
So here's the vibe: this is the sequence of values you get by repeatedly rounding up to a multiple of i-1, i-2, ..., 2, 1. Like, round up, then round up again, then again, all the way down π«Ά
1, 2, 4, 6, 10, 12, 18, 22, 30, 34, 42, 48, 58, 60, 78, ...
starting i=1
ngl the example makes it click. Take i=5: you start at 5, round up to a multiple of 4 and boom you get 8, then round up to a multiple of 3 and you get 9, then round up to a multiple of 2 and you get 10, and finally rounding up to a multiple of 1 is literally no change so value=10 at i=5. iconic. π
Here's the little secret though: when you round up, if the value is already a suitable multiple, it just stays put. That always happens on the final round up to a multiple of 1, but it can happen in the middle too, fr fr. Example: i=4, start at 4, round up to a multiple of 3 to get 6, then 6 rounding up to a multiple of 2 is 6 β unchanged, because it's already a multiple of 2. π (in a good way)
For i>=3 the last step is always a round up to a multiple of 2, so every value is even. They're also always increasing, and they end up approximately
value ~= i^2 / pi
though ngl there's values both bigger and smaller than this approximation. it's giving "roughly" not "exactly" π
π Extra Multiples π
The "extra_multiples" option can round up by extra multiples at each step. For example:
# extra_multiples => 2
1, 4, 10, 18, 30, 42, 58, 78, 102, 118, 150, 174, ...
Let's walk it, bestie. At i=5 you start at 5, round up to a multiple of 4 which is 8, and then two extra multiples of 4 to give 16. Then round up to a multiple of 3 is 18 and two extra multiples of 3 to give 24. Then round up to a multiple of 2 is 24 already and two extra multiples of 2 gives 28. Then round up to a multiple of 1 is 28 already and two extra multiples of 1 gives finally value=30 at i=5. slay π
When "extra_multiples" is 0 the final round up to a multiple of 1 can be ignored, but with extra multiples there's a fixed extra amount to add there. noted π
π Sieve π
plot twist: this sequence can also be constructed as a sieve. Start with the integers,
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,...
Delete every 2nd, starting counting from the 2nd. So at 2 you keep that 2, drop value 3, keep value 4, drop value 5, etc β which leaves the even integers.
1,2,4,6,8,10,12,14,16,18,20,22,24,26,28...
Then delete every 3rd starting counting from the 3rd. So starting at value 4 you keep 4,6, drop 8, keep 10,12, drop 14, etc.
1,2,4,6,10,12,16,18,22,24,28,...
Then delete every 4th starting counting from the 4th. So starting at 6 you keep 6,10,12, drop 16, keep 18,22,24, drop 28, etc.
1,2,4,6,10,12,18,22,24,...
And so on deleting every increasing multiples. At the "delete every k-th" stage the first 2*k-1 values are unchanged, so the procedure can stop when the stage is past the desired number of values. cute little shortcut β¨
This sieve process makes it clear the values always increase β which is, lowkey, not quite obvious from the repeated rounding-up. main character insight π§
π¦ Flavius Josephus Sieve π¦
When "extra_multiples => 1" the sequence is the sieve of Flavius Josephus,
# extra_multiples => 1
1, 3, 7, 13, 19, 27, 39, 49, 63, 79, 91, 109, 133, 147, 181, ...
This sieve again begins with the integers
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,...
Drop every 2nd number,
1,3,5,7,9,11,13,15,17,19,...
Drop every 3rd from the remaining, so
1,3,7,9,13,15,19,...
Drop every 4th from the remaining, so
1,3,7,13,15,19,...
And so on, dropping an every increasing multiple. Unlike the sieve for the default case above, the start point for the drop counting is the start of the remaining values. β οΈ different vibe, same energy.
This case can also be calculated by working downwards from the square i^2
value = i^2
for m = i-1 down to 1
value = next smaller multiple of m < value
The next smaller multiple is strictly less than value, so if value is already a multiple of m then it changes to value-m, the next lower multiple. The last step is m=1. In that case value is always a multiple of 1 and the next lower multiple always means value-1. periodt. π
π FUNCTIONS π
See "FUNCTIONS" in Math::NumSeq for behaviour common to all sequence classes.
$seq = Math::NumSeq::ReRound->new ()$seq = Math::NumSeq::ReRound->new (extra_multiples => $integer)β Create and return a new sequence object. π«Ά
β Random Access β
$value = $seq->ith($i)β Return$irounded up to multiples ofi-1,...,2.$bool = $seq->pred($value)β Return true if$valueis a ReRound value.$i = $seq->value_to_i_estimate($value)β Return an estimate of theicorresponding to$value. See"Value to i Estimate"below. π
π₯ FORMULAS π₯
π Predicate π
The rounding procedure can be reversed to test for a ReRound value.
for i=2,3,4,etc
value -= extra_multiples*i
if value < 0 then not a ReRound
remainder = value mod i
if remainder==i-1 then not a ReRound
value -= remainder # round down to multiple of i
stop when value <= i
is a ReRound if value==i, and i is its index
Example time! To test 28: it's a multiple of 2, so ok for the final rounding step. Its predecessor in the rounding steps was a multiple of 3, so round down to a multiple of 3 which is 27. The predecessor of 27 was a multiple of 4 so round down to 24. But at that point there's a contradiction, because if 24 was the value then it's already a multiple of 3 and so wouldn't have gone up to 27. π
This case where a round-down gives a multiple of both i and i-1 is identified by the remainder = value % i == i-1. If value is already a multiple of i-1 then subtracting an i-1 would leave it still so. π
πΈ Value to i Estimate πΈ
For the default sequence as noted above the values grow roughly as
value ~= i^2 / pi
so can be estimated as
i ~= sqrt(value) * sqrt(pi)
There's no need for high accuracy in pi. The current code uses an approximation sqrt(pi)~=296/167 for faster calculation if the value is a "Math::BigInt" or "Math::BigRat".
i ~= sqrt(value) * 296/167
"extra_multiples => m" adds a fixed amount i*m at each step, for a total
value_EM = m + 2*m + 3*m + 4*m + ... + i*m
= m * i*(i+1)/2
As m becomes large this part of the value dominates, so
value =~ m * i*(i+1)/2 for large m
i =~ sqrt(value*2/m)
As m increases the factor sqrt(pi) progressively morphs towards sqrt(2/m). For m even it might be sqrt(pi)/2, 3/8*sqrt(pi), 5/16*sqrt(pi), 35/128*sqrt(pi), etc, and for m odd it might be 2*sqrt(1/pi), 4/3*sqrt(1/pi), 16/15*sqrt(1/pi), 32/35*sqrt(1/pi), 256/315*sqrt(1/pi), etc. What's the pattern? π€ (genuinely asking, bestie, the manpage asks too)
The current code uses the "large m" formula for any m>0, which is no more than roughly a factor 1.25 too big. ngl, close enough π
π SEE ALSO π
Math::NumSeq, Math::NumSeq::ReReplace
π HOME PAGE π
<http://user42.tuxfamily.org/math-numseq/index.html>
π LICENSE π
Copyright 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/>.
okay that's the whole page, bestie π this was generated from the perl v5.42.0 manual set, dated 2025-07-25 β that's the version and date, so you know exactly which era of lore this is. stay rounding up, queen π«Άβ¨
π¦ 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