Kevin Ryde > Math-NumSeq-65 > Math::NumSeq::CollatzSteps

Math-NumSeq-65.tar.gz

Dependencies

Annotate this POD

Website

# CPAN RT

 Open 0
View/Report Bugs
Module Version: 65   Source   Latest Release: Math-NumSeq-69

# 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,

```    n -> / 3n+1  if n odd
\ n/2   if n even```

It's conjectured that any starting n will always eventually reduce to 1, 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.

# 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 => 'down')`

Create and return a new sequence object.

The optional `step_type` parameter (a string) selects between

```    "up"      upward steps 3n+1
"down"    downward steps n/2
"both"    both up and down, which is the default```

## 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`.

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