Back to questions

Looping Number Fintech Python Interview Question

Looping Number

Fintech Python Interview Question

Given an integer , check if it is a looping number. A looping number has the following properties:

  1. It is repeatedly replaced by the sum of the squares of its digits.
  2. This process continues until:
    • The number becomes , in which case it is not a looping number.
    • The number starts repeating in a cycle that does not include , making it a looping number.

Return if is a looping number, otherwise return .

Example #1

Input:

Output:

Explanation:

  1. 42=164^2 = 16
  2. 12+62=1+36=371^2 + 6^2 = 1 + 36 = 37
  3. 32+72=9+49=583^2 + 7^2 = 9 + 49 = 58
  4. 52+82=25+64=895^2 + 8^2 = 25 + 64 = 89
  5. 82+92=64+81=1458^2 + 9^2 = 64 + 81 = 145
  6. 12+42+52=1+16+25=421^2 + 4^2 + 5^2 = 1 + 16 + 25 = 42
  7. 42+22=16+4=204^2 + 2^2 = 16 + 4 = 20
  8. 22+02=4+0=42^2 + 0^2 = 4 + 0 = 4

Since the number loops back to , it is a looping number.

Example #2

Input:

Output:

Explanation:

  1. 12+92=1+81=821^2 + 9^2 = 1 + 81 = 82
  2. 82+22=64+4=688^2 + 2^2 = 64 + 4 = 68
  3. 62+82=36+64=1006^2 + 8^2 = 36 + 64 = 100
  4. 12+02+02=1+0+0=11^2 + 0^2 + 0^2 = 1 + 0 + 0 = 1

Since the number reaches , it is not a looping number.

Input

Python

Output