Math::N...tzSteps(3)
π Math::N...tzSteps(3) π
okay bestie, so this is the Math::NumSeq::CollatzSteps page, from the User Contributed Perl Documentation collection. it's giving math, it's giving chaos theory, it's giving "why is this so addictive". buckle up. π
β¨ NAME β¨
Math::NumSeq::CollatzSteps β steps in the 3n+1 problem π
πΈ SYNOPSIS πΈ
use Math::NumSeq::CollatzSteps;
my $seq = Math::NumSeq::CollatzSteps->new;
my ($i, $value) = $seq->next;
literally three lines and you're in. periodt. π
π¦ DESCRIPTION π¦
so this sequence counts how many steps it takes to reach 1 using the Collatz 3n+1 problem. iconic behavior honestly.
0, 1, 7, 2, 5, 8, 16, 3, 19, 6, 14, 9, 9, 17, 17, 4, 12, 20, ...
starting i=1
the Collatz problem iterates like this:
n -> / 3n+1 if n odd
\ n/2 if n even
for example, i=6 takes value=8 β that's 8 whole steps β to reach 1, like:
6 -> 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
and the tea is: it's conjectured that any starting n will always eventually reduce to 1, so the number of steps is finite. no cap, nobody has proven it, we're all just vibing on faith here. π«Ά
β οΈ there is no limit in the code on how many steps are counted, bestie. andMath::BigIntgets pulled in if the3n+1steps go past the usual scalar integer limit. so like, it won't break, it'll just get fancy. π
π₯ Up Steps π₯
option step_type => "up" counts only the 3n+1 up steps.
step_type => "up"
0, 0, 2, 0, 1, 2, 5, 0, 6, 1, 4, 2, 2, 5, 5, 0, 3, 6, 6, 1, 1, ...
you can also think of this as steps iterating
n -> (3n+1)/2^k for maximum k
π§ Down Steps π§
option step_type => "down" counts only the n/2 down steps.
step_type => "down"
0, 1, 5, 2, 4, 6, 11, 3, 13, 5, 10, 7, 7, 12, 12, 4, 9, 14, ...
and the total up+down is what gives you the default step_type=both. math is mathing. β¨
π Odd Numbers π
option on_values => "odd" counts steps on the odd numbers 2*i+1.
on_values => "odd"
0, 7, 5, 16, 19, 14, 9, 17, 12, 20, 7, 15, 23, 111, 18, 106, ...
starting i=0 for odd number 1
π Even Numbers π
option on_values => "even" counts steps on the even number 2*i,
on_values => "even"
1, 2, 8, 3, 6, 9, 17, 4, 20, 7, 15, 10, 10, 18, 18, 5, 13, 21, ...
starting i=0 for even number 2
since 2*i is even, the first step is down n/2 to give i, and after that it's the same as the plain count. which means the steps for "even" is simply 1 more than for plain "all". that's it, that's the whole trick. π
π FUNCTIONS π
peep "FUNCTIONS" in Math::NumSeq for the behaviour that's common to all sequence classes, bestie. this is just the local flavor.
$seq = Math::NumSeq::CollatzSteps->new ()$seq = Math::NumSeq::CollatzSteps->new (step_type => $str, on_values => $str)β create and return a new sequence object. the optionalstep_typeparameter (a string) can be:
"up" upward steps 3n+1
"down" downward steps n/2
"both" both up and down (the default)
the optional on_values parameter (a string) can be:
"all" all integers i
"odd" odd integers 2*i+1
"even" even integers 2*i
β Random Access β
$value = $seq->ith($i)β return the number of steps to take$idown to 1.$bool = $seq->pred($value)β return true if$valueoccurs as a step count. this is simply$value >= 0. yeah, that easy. π
πΈ SEE ALSO πΈ
Math::NumSeqMath::NumSeq::JugglerStepsMath::NumSeq::ReverseAddStepsMath::NumSeq::HappySteps
π 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, you ate it up. π
this was perl v5.42.0, dated 2025-07-25, and it lives at Math::N...tzSteps(3). go count some steps, bestie, ily π«Άβ¨
π¦ 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