Shawn's blog Shawn's blog
About Me
  • Category
  • Tag
  • Archive
GitHub (opens new window)

Shawn Jin

I am not a creator of knowledge, I am just a porter of knowledge.
About Me
  • Category
  • Tag
  • Archive
GitHub (opens new window)
  • data-structure

  • searching

    • Leetcode: Happy Number
      • What is a Happy Number?
      • Programming
      • REFERENCES
  • sorting

  • Algorithm
  • searching
Shawn Jin
2019-02-18

Leetcode: Happy Number

# Happy Number Checker

# What is a Happy Number?

In number theory, a b-happy number is a natural number in a given number base b that eventually reaches 1 when iterated over the perfect digital invariant function for p=2. Those numbers that do not end in 1 are b-unhappy numbers (or b-sad numbers).

For instance, the number 19 is a happy number because

12+92=821^2 + 9^2 = 8212+92=82

82+22=688^2 + 2^2 = 6882+22=68 62+82=1006^2 + 8^2 = 10062+82=100 12+02+02=11^2 + 0^2 + 0^2 = 112+02+02=1

# However, 20 is not happy number

22+02=42^2 + 0^2 = 422+02=4 42=164^2 = 1642=16 12+62=371^2 + 6^2 = 3712+62=37 ... and it won't stop

# Programming

This is tricky because if a number is not happy number, the checker would be running forever and won't stop. In this case, I just think the number be happy number if we compute over 1000 times and the answer is still not equal to zero.

Due to we want to get the square of each bit, so we could use operator % to handle this.

# REFERENCES

wikipedia (opens new window)

#LeetCode
Updated: 2021/09/05, 15:42:35
Basic Data Structure
Basic Sorting Algorithm

← Basic Data Structure Basic Sorting Algorithm→

最近更新
01
Python import files from different directories
12-31
02
Classmethod in Python
09-15
03
Single/Double Star (/*) Parameters in Python
09-15
更多文章>
Theme by Vdoing | Copyright © 2019-2021 Shawn Jin | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式