A game awards a "lucky score" badge with this rule: replace the score by the sum of the squares of its digits, and repeat. If the number ever becomes 1, the score is lucky. If it never does, it goes round the same numbers forever. For example 19 gives 1 + 81 = 82, then 64 + 4 = 68, then 36 + 64 = 100, then 1.
Write isLuckyScore(n) that returns true if n is lucky and false otherwise. Since a number that is not lucky loops forever, you need a way to notice the loop.
Input: n = 19 Output: true
19 -> 82 -> 68 -> 100 -> 1.
Input: n = 2 Output: false
2 -> 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4, and 4 was already seen, so it loops forever.
1 <= n <= 2^31 - 1Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #238 Digit Square Streak. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.