Simple Fun #23: Square Digits Sequence
Description:
Task
Consider a sequence of numbers a0, a1, ..., an, in which an element is equal to the sum of squared digits of the previous element. The sequence ends once an element that has already been in the sequence appears again.
Given the first element a0
, find the length of the sequence.
Example
For a0 = 16, the output should be 9
Here's how elements of the sequence are constructed:
a0 = 16
a1 = 12 + 62 = 37
a2 = 32 + 72 = 58
a3 = 52 + 82 = 89
a4 = 82 + 92 = 145
a5 = 12 + 42 + 52 = 42
a6 = 42 + 22 = 20
a7 = 22 + 02 = 4
a8 = 42 = 16, which has already occurred before (a0)
Thus, there are 9 elements in the sequence.
For a0 = 103, the output should be 4
The sequence goes as follows: 103 -> 10 -> 1 -> 1, 4 elements altogether.
Input/Output
[input]
integera0
First element of a sequence, positive integer.
Constraints: 1 ≤ a0 ≤ 650.
[output]
an integer
Similar Kata:
Stats:
Created | Jan 24, 2017 |
Published | Jan 24, 2017 |
Warriors Trained | 3302 |
Total Skips | 74 |
Total Code Submissions | 5990 |
Total Times Completed | 1905 |
JavaScript Completions | 591 |
Python Completions | 872 |
C# Completions | 168 |
Ruby Completions | 82 |
Haskell Completions | 38 |
F# Completions | 28 |
TypeScript Completions | 105 |
Rust Completions | 134 |
Scala Completions | 9 |
Total Stars | 60 |
% of votes with a positive feedback rating | 95% of 402 |
Total "Very Satisfied" Votes | 365 |
Total "Somewhat Satisfied" Votes | 35 |
Total "Not Satisfied" Votes | 2 |
Total Rank Assessments | 10 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 7 kyu |