Lintcode Python Simple class topic 488. Happy number

Source: Internet
Author: User
Tags lintcode

Title Description:

Write an algorithm to determine if a number is a "happy number".

A number is the definition of happiness: For a positive integer, each time the number is replaced with the sum of the squares of the number in each of his positions, and then the process repeats until the number becomes 1, or the infinite loop but does not change to 1. If it can become 1, then this number is the number of happiness.

Have you ever encountered this problem in a real interview? Yes Sample Example

19 is a happy number.

1^2 + 9^2 = 828^2 + 2^2 = 686^2 + 8^2 = 1001^2 + 0^2 + 0^2 = 1
labelMath Hash Table

Topic Analysis:

Converts the number n bitwise into a list, and then loops the sum of the squares of the elements, only to the result n==1 or n ==4;

Recommended Baidu happy number of circulation structure.

Non-happy numbers are always entered in the following repeating series:
4→16→37→58→89→145→42→20→4
For example non-happy number 8, 14:
8→64→52→29→85→89→145→42→20→4

14→17→50→25→29→85→89→145→42→20→4

For example Happy number 7:
7→49→97→130→10→1

In the decimal, the number of pleasures within 100 (Oeis in the series A00770): 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100.

Source:

Class solution:    # @param {int} n an integer    # @return {Boolean} True if the is a happy number or false    def is Happy (self, N):        # Write Your code here        if n was none:return False while                n! = 1 and N! = 4:            nums = List (str (n))            n = 0            for i in Nums:                n + = int (i) **2        # Loop End, return result        if n = = 1:return True        if n = = 4:return False

Lintcode Python Simple class topic 488. Happy number

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.