Math::NumSeq::HappySteps -- number of sum of squares of digits iterations to reach a repeat


 use Math::NumSeq::HappySteps;
 my $seq = Math::NumSeq::HappySteps->new (radix => 10);
 my ($i, $value) = $seq->next;


This is the number of iterations of the HappyNumbers style "sum of squares of digits" is required to reach a repeat of a value seen before, and therefore to establish whether a number is a happy number or not.

    1, 9, 13, 8, 12, 17, 6, 13, 12, 2,
    starting i=1

For example i=10 is value 2 because 10->1->1 is 2 iterations to get to a repeat (a repeat of 1). At i=1 itself the value is 1 since 1 iteration reaches 1 again which is itself the repeat. That count 1 at i=1 is the minimum.


An optional radix parameter selects a base other than decimal. In binary radix=>2 the digits are all either 0 or 1 so "sum of squares of digits" is the same as a plain "sum of digits".

In some bases there's longer cycles than others which a non-happy number might fall into. For example base 20 has a cycle

    10 -> 100 -> 25 -> 26 -> ... -> 61 -> 10
    total 26 elements

When a non-happy falls into such a cycle its HappySteps count here is at least 26 (or whatever amount) to reach a repeat.


See "FUNCTIONS" in Math::NumSeq for behaviour common to all sequence classes.

$seq = Math::NumSeq::HappySteps->new ()
$seq = Math::NumSeq::HappySteps->new (radix => $r)

Create and return a new sequence object.

Random Access

$value = $seq->ith($i)

Return the number of iterations starting from $i required to reach a repeat.


Math::NumSeq, Math::NumSeq::HappyNumbers, Math::NumSeq::DigitSum



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