Sum of digits problem

Sherlock Holmes

Gold Member
Gold Supporting Member
Joined
Jul 1, 2024
Messages
11,787
Reaction score
4,574
Points
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.
 
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.
 
see also

 
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.webp
 

Attachments

  • 1724360538178.webp
    1724360538178.webp
    7.3 KB · Views: 11
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:
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?
Why should I get rid of the loop?

You never said I had to be efficient, that wasn't one of the criteria.

CPU cycles are cheap. I'm not going to spin my wheels trying to minimize them.

I'll leave that for people with too much time on their hands
 
Why should I get rid of the loop?
Go and read about lambda calculus and then get back to me.
You never said I had to be efficient, that wasn't one of the criteria.

CPU cycles are cheap. I'm not going to spin my wheels trying to minimize them.

I'll leave that for people with too much time on their hands
 
Go and read about lambda calculus and then get back to me.
WTF are you talking about?

You asked me to solve a problem and I solved it.

Now you're moving the goalposts.

Sorry, I don't have time for such nonsense.

You go ahead and be clever, since you're good at it.

I have to make a living and put my kids through college.

Maybe some other time. When I'm old and gray.
 
WTF are you talking about?

You asked me to solve a problem and I solved it.
No you didn't you left most of the code undefined!
Now you're moving the goalposts.

Sorry, I don't have time for such nonsense.
This shouldn't take an expert like you very long.
You go ahead and be clever, since you're good at it.

I have to make a living and put my kids through college.

Maybe some other time. When I'm old and gray.
Lets start again, apologies if not clear, here this is the problem - you can code it and test it in the site and then submit it, when you do that the system will subject your code to a battery of test cases.


i.e.

1724367096690.png


It's functional programming so you need to choose a functional language, the site supports many:

1724367449760.png


Hence loops and variables are frowned upon. Give it your best shot and we can compare solutions - mine will be better.

This isn't nonsense, these are real world problems, separates the men from the boys, here's one of the test cases:

1724367355090.png
 
Last edited:
Back
Top Bottom