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;
DESCRIPTION
The number of steps it takes to reach 1 by the Collatz "3n+1" problem,
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
n -> / 3n+1 if n odd
\ n/2 if n even
For example i=6 takes value=8 many steps to reach 1,
6 -> 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
It's conjectured that any starting n will always eventually reduce to 1 and so the number of steps is finite. There's no limit in the code on how many steps counted. Math::BigInt
is used if 3n+1 steps go past the usual scalar integer limit.
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, ...
This can also be thought of 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, ...
The total up+down gives the default "step_type=both".
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 thereafter the same as the plain count. This means the steps for "even" is simply 1 more than for plain "all".
FUNCTIONS
See "FUNCTIONS" in Math::NumSeq for behaviour common to all sequence classes.
$seq = Math::NumSeq::CollatzSteps->new ()
$seq = Math::NumSeq::CollatzSteps->new (step_type => $str, on_values => $str)
-
Create and return a new sequence object. The optional
step_type
parameter (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
$i
down to 1. $bool = $seq->pred($value)
-
Return true if
$value
occurs as a step count. This is simply$value >= 0
.
SEE ALSO
Math::NumSeq, Math::NumSeq::JugglerSteps, Math::NumSeq::ReverseAddSteps Math::NumSeq::HappySteps
HOME PAGE
http://user42.tuxfamily.org/math-numseq/index.html
LICENSE
Copyright 2011, 2012, 2013, 2014, 2016, 2019 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/>.