Sum of digits problem

Sherlock Holmes

Gold Member
Jul 1, 2024
2,002
577
163
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.
 
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.

Looking at this it is a root number search right? You sum the second equation (as in 1+1 =11 also equals 2),

I haven't used python in a long time but I could probably find an old code of mine that solved challenges where this was addressed (and I found the solution). I'm sure it would leverage the "sum" function.
 
Last edited:
Looking at this it is a root number search right? You sum the second equation (as in 1+1 =11),
Its just repeated addition from left to right, but replacing a multi-digit result with the sum of those digits before adding the "next" digit in the sequence.
I haven't used python in a long time but I could probably find an old code of mine that solved challenges where this was addressed (and I found the solution). I'm sure it would leverage the "sum" function.
 
Its just repeated addition from left to right, but replacing a multi-digit result with the sum of those digits before adding the "next" digit in the sequence.


Im not sure if that is the Bernouli number or not. Again, it's been awhile since I was interested heavily in this stuff. Pre-covid. I think with this you reduce the numbers though with Bernouli.
 
Last edited:
Im not sure if that is the Bernouli number or not. Again, it's been awhile since I was interested heavily in this stuff. Pre-covid. I think with this you reduce the numbers though with Bernouli.
No, it's nothing fancy at all, just any old set of digits that we can enumerate, these are all examples

94236238
451235634510238634856
337
8462345786518658346519283579680438

What rules can be devised to add them as explained.
 
In reality one's code has to be tested, you might get handed a file that contains a trillion digits for example but you don't know, you'll know when there are no more digits but as to how many digits that's unknown.
 
No, it's nothing fancy at all, just any old set of digits that we can enumerate, these are all examples

94236238
451235634510238634856
337
8462345786518658346519283579680438

What rules can be devised to add them as explained.

Well, it appears that you are trying to reduce the number down to single digits.

So in longhand logical form it might be "if number > 10, split number into its separate digits, sum digits and repeat process until you have a single digit."

Once you arrive at a single digit the process ends. The larger the number the more steps in the loop it is required to obtain a single digit sum.
 
I was hoping to see some interesting answers from our high IQ members scruffy and abu afak those with superior problem solving skills, so looking forward to seeing some impressive solutions.
 
That's not actually an algorithm, what score do you think a Prof would give you for that answer...
I don't get it. You want me to do your homework for you? This is an easy problem, and I usually get paid for writing code. What language do you want it in? C? JavaScript? Assembler? Lisp?
 
while(string not consumed)
{
get_digit()
while(digits != 1)
add_digits()
}

did I miss anything?
 
I don't get it. You want me to do your homework for you? This is an easy problem, and I usually get paid for writing code. What language do you want it in? C? JavaScript? Assembler? Lisp?
Here's my solution, so far as I know (I did this 7 years ago) this is the most compact representation of a solution that exists, can you better it?

This function will run as-is, it's complete code:

1724360563113.png
 

Attachments

  • 1724360538178.png
    1724360538178.png
    4.9 KB · Views: 0
while(string not consumed)
{
get_digit()
while(digits != 1)
add_digits()
}

did I miss anything?
Yes, the implementations of "get_digit" and "add_digits" and "string not consumed" and the variable "digits" how about something that we can run? see if you can get rid of the loop and the variable too, that would be a good start.

Basically, are you actually cleverer than me?
 
Last edited:

Forum List

Back
Top