LeetCode Çözümlerİ – 202. Happy Number – [Google Mülakat Sorusu]

LeetCode içerisinde bulunan “Happy Number”‘ sorusunun açıklaması ve çözümü. Bu soruda bir sayının rakamlarının karelerini toplayıp ve bu işlemi sonsuz döngü olmadığı sürece devam ettirip 1 sayısına ulaşılabiliyor mu olduğu sormakta. LeetCode’a göre Google iş görüşmelerinde sorulmuş sorulardan biriymiş.

🔥 LeetCode 202. Happy Number: https://leetcode.com/problems/happy-number/

➡️ Problem açıklaması:

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, and 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 numbers.

Return True if n is a happy number, and False if not.

Example:

Input: 19

Output: true

Explanation:

1^2 + 9^2 = 82

8^2 + 2^2 = 68

6^2 + 8^2 = 100

1^2 + 0^2 + 0^2 = 1

➡️ Wikipedia açıklaması: https://en.wikipedia.org/wiki/Happy_number

In number theory, a happy number is a number which eventually reaches 1 when replaced by the sum of the square of each digit. For instance, 13 is a happy number because 1^{2}+3^{2}=10 and 1^{2}+0^{2}=1. On the other hand, 4 is not a happy number because the sequence starting with 4^{2}=16 and 1^{2}+6^{2}=37 eventually reaches 2^{2}+0^{2}=4, the number that started the sequence, and so the process continues in an infinite cycle without ever reaching 1. A number which is not happy is called sad or unhappy.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.