Happy Numbers

At the start of the year, people often ask simple questions. Is this year going to be lucky? Will it turn out well?
20 Downloads
Aktualisiert 5. Jan 2026

Lizenz anzeigen

Anmerkung des Herausgebers: This file was selected as MATLAB Central Pick of the Week

The Same Rule, Different Fates
At the start of the year, people often ask simple questions. Is this year going to be lucky? Will it turn out well? That’s how I stumbled (again) on a small numerical curiosity called happy numbers.
The rules are almost trivial.
  1. Take a number.
  2. Split it into digits.
  3. Square them.
  4. Add them up.
  5. Repeat
That’s it. No clever tricks. No advanced maths. Just iteration. And yet - something interesting happens. Some numbers quietly settle down. They converge to 1 and stop changing. These numbers are called happy.
Others never do.
They fall into a loop and keep cycling forever.
Same rule, different fates.

Zitieren als

Martin Foltin (2026). Happy Numbers (https://de.mathworks.com/matlabcentral/fileexchange/182984-happy-numbers), MATLAB Central File Exchange. Abgerufen.

Kompatibilität der MATLAB-Version
Erstellt mit R2025b
Kompatibel mit allen Versionen
Plattform-Kompatibilität
Windows macOS Linux
Version Veröffentlicht Versionshinweise
1.0.2

Re-upload due to a technical issue. No functional changes.

1.0.1

Y-axis limits set to [0, 200] to make the repeating cycle more visible.

1.0.0