Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Happy number
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Programming example== The examples below implement the perfect digital invariant function for <math>p = 2</math> and a default base <math>b = 10</math> described in the definition of happy given at the top of this article, repeatedly; after each time, they check for both halt conditions: reaching 1, and [[Cycle detection|repeating a number]]. A simple test in [[Python (programming language)|Python]] to check if a number is happy: <syntaxhighlight lang="python"> def pdi_function(number, base: int = 10): """Perfect digital invariant function.""" total = 0 while number > 0: total += pow(number % base, 2) number = number // base return total def is_happy(number: int) -> bool: """Determine if the specified number is a happy number.""" seen_numbers = set() while number > 1 and number not in seen_numbers: seen_numbers.add(number) number = pdi_function(number) return number == 1 </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)