Math::N...Numbers(3)

πŸ“¦
version 75 Β· 2 versions
, in fedora 43

πŸ’– NAME πŸ’–

Math::NumSeq::HappyNumbers β€” bestie, it's the happy numbers, aka the ones that reach 1 under repeated sum of squares of digits πŸŽ€

This is a page from the User Contributed Perl Documentation, and it's genuinely one of the cutest corners of CPAN, no cap.


✨ SYNOPSIS ✨

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

That's it, that's the whole vibe. Three lines and you're iterating happiness πŸ’…


🌸 DESCRIPTION 🌸

So this sequence is the happy numbers β€” the numbers that eventually reach 1 when you keep taking the sum of the squares of the digits. Over and over. Forever. Until you land on 1. πŸ¦‹

1, 7, 10, 13, 19, 23, ...
starting i=1

Okay let's take 23 because it's the example and it's lowkey iconic. Sum of squares of its digits β€” that's 2 and 3 β€” is 2*2+3*3=13. Then do the same thing again: 1*1+3*3=10. Then again: 1*1+0*0=1. And boom, you've reached 1. Happy number. Periodt. πŸ”₯

In decimal it can be shown that for a non-zero starting point this procedure always reaches either 1 or the cycle:

4,16,37,58,89,145,42,20

The values that reach 1 are called happy numbers β€” and yes, the ones that fall into that cycle are the sad ones, though the docs are too polite to say it 😭

There's an optional radix parameter if you want a base other than decimal. But ngl, base 2 and base 4 are not very interesting, since for them every number except 0 is happy. Everyone's happy, nothing's at stake, the drama is gone πŸ™ˆ


🧚 FUNCTIONS 🧚

See "FUNCTIONS" in Math::NumSeq for behaviour common to all sequence classes. That's the shared bestie code.

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

Create and return a new sequence object. Slay. That's your main character entry point ✨

  • $bool = $seq->pred($value)

Return true if $value is a happy number, meaning repeated sum of squares of its digits reaches 1. A vibe check that returns a boolean, honestly πŸ’…


🌈 SEE ALSO 🌈

  • Math::NumSeq
  • Math::NumSeq::HappySteps
  • Math::NumSeq::DigitSum

πŸ‘‘ 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/>.

Fr fr, read the license before you ship it β€” the warranty disclaimer is not a drill 🫢

That's the whole page bestie, tysm for reading to the end πŸ’– This was perl v5.42.0, dated 2025-07-25, and you just got the Math::N...Numbers(3) page in full. Go forth and compute happy numbers βœ¨πŸŽ€πŸŒΈ

πŸ“¦ where this page comes from