Apprenticelogic & control flow
The Echo-Riddle of Corvenne Sinkhole
At the bottom of Corvenne Sinkhole lives a spirit that answers every number shouted down to it with a number of its own. The Lanternwright Expedition has worked out its rule. The spirit takes the number it heard, squares each of its decimal digits, and echoes back the sum of those squares. The expedition then shouts that echo down again, and so on. Sooner or later the spirit says a number that has already been spoken in the chain. The expedition's scribes want the whole chain recorded up to that moment.
Write echoChain(seed).
seedis a positive integer (at least 1): the first number shouted into the sinkhole.- The next value after a number
vis the sum of the squares of the base-10 digits ofv. For example, 130 becomes 1 + 9 + 0 = 10. - Return a list of integers. It starts with
seed, followed by each successive value in order. - Stop just before the first value that already appears in the list, so every value in the returned list is distinct.
- The repeated value may be any earlier entry, not necessarily
seed. It is not included a second time.
examples
- case 1
seed of 7[7, 49, 97, 130, 10, 1] (the next echo of 1 is 1 again, already spoken) - case 2
seed of 2[2, 4, 16, 37, 58, 89, 145, 42, 20] (20 echoes back 4, which was already spoken) - case 3
seed of 1[1]
- reward
- 210 XP
- signature
echoChain
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
loading the editor…
- seed seven reaches one
- seed one repeats itself at once
- seed two loops back to four not two
- seed four closes on itself
- seed ten has a zero digit
- seed nineteen
- seed three long chain
- large seed shrinks into the loop