Skip to content

Happy number: detect cycles with a hash set #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2021
Merged

Happy number: detect cycles with a hash set #123

merged 1 commit into from
Apr 4, 2021

Conversation

vbrazo
Copy link
Owner

@vbrazo vbrazo commented Apr 4, 2021

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

A happy number is a number defined by the following process:

Starting with any positive integer, replace the number by the sum of the squares of its digits. Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy. Return true if n is a happy number, and false if not.

# Example 1:
#
# Input: n = 19
# Output: true
# Explanation:
# 12 + 92 = 82
# 82 + 22 = 68
# 62 + 82 = 100
# 12 + 02 + 02 = 1
#
# Example 2:
#
# Input: n = 2
# Output: false

@vbrazo vbrazo changed the title Happy number: Detect Cycles with a HashSet Happy number: detect cycles with a HashSet Apr 4, 2021
@vbrazo vbrazo merged commit 23f899a into main Apr 4, 2021
@vbrazo vbrazo deleted the happy-number branch April 4, 2021 00:21
@vbrazo vbrazo changed the title Happy number: detect cycles with a HashSet Happy number: detect cycles with a hash set Apr 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant