Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Test whether an integer is a happy number
ResourceFunction["HappyNumberQ"][n] tests whether repeatedly summing up the square of the digits of n reaches 1. | |
ResourceFunction["HappyNumberQ"][n,b] considers the integer digits of n in base b. | |
ResourceFunction["HappyNumberQ"][n,b,p] raises the digits to the power p. |
Test whether 19 is a happy number:
In[1]:= |
Out[1]= |
Test whether 1579 is a happy number based on its digits in base 7:
In[2]:= |
Out[2]= |
Cube the numbers rather than squaring them:
In[3]:= |
Out[3]= |
In base 2, all numbers are happy:
In[4]:= |
Out[4]= |
The negative sign in a negative number is ignored:
In[5]:= |
Out[5]= |
Checking for higher powers can take a lot more time:
In[6]:= |
Out[6]= |
Find numbers that are happy in many bases:
In[7]:= |
Out[11]= |
Find happy numbers that are also prime:
In[12]:= |
Out[12]= |
Wolfram Language 11.3 (March 2018) or above
This work is licensed under a Creative Commons Attribution 4.0 International License