6 kyu

Simple Fun #23: Square Digits Sequence

591 of 1,905myjinxin2015

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] integer a0

    First element of a sequence, positive integer.

    Constraints: 1 ≤ a0 ≤ 650.

  • [output] an integer

Puzzles

Stats:

CreatedJan 24, 2017
PublishedJan 24, 2017
Warriors Trained3302
Total Skips74
Total Code Submissions5990
Total Times Completed1905
JavaScript Completions591
Python Completions872
C# Completions168
Ruby Completions82
Haskell Completions38
F# Completions28
TypeScript Completions105
Rust Completions134
Scala Completions9
Total Stars60
% of votes with a positive feedback rating95% of 402
Total "Very Satisfied" Votes365
Total "Somewhat Satisfied" Votes35
Total "Not Satisfied" Votes2
Total Rank Assessments10
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • suic Avatar
  • smile67 Avatar
  • imjasonmiller Avatar
  • kazk Avatar
  • Blind4Basics Avatar
  • a.merezhanyi Avatar
  • hobovsky Avatar
  • cliffstamp Avatar
  • testfirstcoder Avatar
  • mauro-1 Avatar
  • Just4FunCoder Avatar
  • KayleighWasTaken Avatar
Ad