euler-0.1.0.0

Safe HaskellSafe
LanguageHaskell2010

Euler.Util.Collatz

Description

Collatz sequences are introduced in Euler problem 14.

Synopsis

Documentation

collatz :: Natural -> Natural Source #

One step of the Collatz sequence, defined for the set of positive integers:

  • n → n/2 (n is even)
  • n → 3n + 1 (n is odd)

The example from problem 14:

>>> takeWhile (/= 1) $ iterate collatz 13
[13,40,20,10,5,16,8,4,2]

collatzLengths :: [Natural] -> Lengths Source #

collatzLengths xs evaluates to a Lengths mapping which contains the collatz length for at least every element of xs (and possibly more).

The example from problem 14:

>>> collatzLengths [13] Map.! 13
10

type Lengths = Map Natural Natural Source #

A mapping from n to the length of the Collatz sequence starting from n.