- Jul 1, 2024
- 11,787
- 4,574
- 188
Imagine a file or string in some programming language that consists of a non-empty sequence of digits (assume ASCII) it has a finite length but unknown length, do not assume some specific or "max" length.
You are to write the code (or pseudo code if you want) that can consume the sequence from the start digit, by adding a digit and the following digit in such a way that any result of > 1 digit long is again added until you get a single digit result, and then continue adding that result to the next digit
Examples
164 ⇒ 1 + 6 + 4 ⇒ 7 + 4 ⇒ 11 ⇒ 2
9576 ⇒ 9 + 5 + 7 + 6 ⇒ 14 + 7 + 6 ⇒ 5 + 7 + 6 ⇒ 12 + 6 ⇒ 3 + 6 ⇒ 9
So, devise an algorithm, a set of rules that can achieve this.
You are to write the code (or pseudo code if you want) that can consume the sequence from the start digit, by adding a digit and the following digit in such a way that any result of > 1 digit long is again added until you get a single digit result, and then continue adding that result to the next digit
Examples
164 ⇒ 1 + 6 + 4 ⇒ 7 + 4 ⇒ 11 ⇒ 2
9576 ⇒ 9 + 5 + 7 + 6 ⇒ 14 + 7 + 6 ⇒ 5 + 7 + 6 ⇒ 12 + 6 ⇒ 3 + 6 ⇒ 9
So, devise an algorithm, a set of rules that can achieve this.