Recognize Digit-Square Sequences That Reach One

Quick Overview

Recognize happy numbers by exact decimal digit-square transitions and finite-state repetition detection.

Recognize Digit-Square Sequences That Reach One

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Starting from positive n, repeatedly replace the number by the sum of the squares of its decimal digits. A happy number eventually reaches 1; otherwise the process repeats in a cycle not containing 1. Return whether n is happy. ```cpp bool isHappy(int n); ``` ### Examples ```text Input: n = 7 Output: true ``` Its sequence is 7→49→97→130→10→1. ```text Input: n = 4 Output: false ``` ### Constraints - `1 <= n <= 2147483647`.

Overview: Recognize happy numbers by exact decimal digit-square transitions and finite-state repetition detection.

Google logo
Google
Date not reported
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Starting from positive n, repeatedly replace the number by the sum of the squares of its decimal digits. A happy number eventually reaches 1; otherwise the process repeats in a cycle not containing 1. Return whether n is happy.

bool isHappy(int n);

Examples

Input: n = 7
Output: true

Its sequence is 7→49→97→130→10→1.

Input: n = 4
Output: false

Constraints

  • 1 <= n <= 2147483647 .