Sum of digits problem

No you didn't you left most of the code undefined!

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've been writing computer code longer than you've been alive. I don't need to prove anything.
 
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.

Read your post.

You said "code or pseudo code".

You didn't say anything about web sites, lambda calculus or anything else.

I work with Python and TensorFlow on a daily basis. That's using a digital computer to program an analog computer. As you know interpreters are horribly inefficient (that's why WASM exists), but they help with interactive programming.

TensorFlow is like a GPU in software, except it's more flexible. It's used to explore artificial neural networks. Except the matrices are fixed So you can't add neurons very easily.

So I built a list based simulator, that maps lists to matrices. This way, I can study network development. It's slow but it works. I can run 30 minutes of hippocampus overnight, and visualize all the new neurons and synapses.

I'm going to post it on Git for open source use. Some people will find it useful.
 
I've been writing computer code longer than you've been alive. I don't need to prove anything.
Yes you do, you need to at least prove that you know how long I've been alive, that's a necessary condition, if you do not then that statement is unprovable so why assert with such confidence?

I already apologized, so the actual challenge now (if you're willing to man up) is to solve that HackerRank problem, that's where my own solution arose years ago.

I want to see how good you are, I want to see what kind of actual solution you come up with because I suspect much of your posting here is bluster.

Look at that test case (your code will be subjected to seven challenging cases)

1724423552820.png


The task is to compute the "super digit" for that long numeric string (740...153) repeated 100,000 times, that's a real problem, not for a novice, for a person who's been writing code for "longer than" I've been alive, this is run of the mill, not trivial, might even take a few days all told but certainly within your much publicized skill set.

We'll then compare/contrast solutions, who knows you might actually impress me, teach me something new.

So pick one of the available languages and man up, have you not even used a functional language before? is that the problem?

Here's what happens when your code passes all tests:

1724423877855.png
 
Last edited:
Read your post.

You said "code or pseudo code".

You didn't say anything about web sites, lambda calculus or anything else.
Yes, I apologized, it was - with hindsight - much better to post the challenge itself, that levels the playing field and avoids subjectivity.
I work with Python and TensorFlow on a daily basis. That's using a digital computer to program an analog computer. As you know interpreters are horribly inefficient (that's why WASM exists), but they help with interactive programming.

TensorFlow is like a GPU in software, except it's more flexible. It's used to explore artificial neural networks. Except the matrices are fixed So you can't add neurons very easily.

So I built a list based simulator, that maps lists to matrices. This way, I can study network development. It's slow but it works. I can run 30 minutes of hippocampus overnight, and visualize all the new neurons and synapses.

I'm going to post it on Git for open source use. Some people will find it useful.
 
Just a complete novice here ... but my first step would be to search the public use forums for the code already written ... this sounds awful familiar like there might be a command in PHP for this already ... not that I know anything about PHP commands mine you ... just a novice spitting out my opinion ...
 
Just a complete novice here ... but my first step would be to search the public use forums for the code already written ... this sounds awful familiar like there might be a command in PHP for this already ... not that I know anything about PHP commands mine you ... just a novice spitting out my opinion ...
That's a very good idea in the case of a typical work problem. HackerRank though is about seeing if one can solve it themselves. It's a good way to push the limits of our thinking, they ways we look at problems, the things we assume and so on.

If this were a problem that just came up in some kind of project then yes, searching existing codebases is a sensible step, especially as the code we find might well have been well tested.

I suspect that this particular problem has been defined in an usual way to make it unlikely to exist in any easy to find public way, that's why they define the string to be processed as a function of a number and a repetition factor I think.

There are lots of problems on HackerRank that I've started and gotten stumped, totally stumped.

Those problems are for functional languages, very very different to imperative languages like Python, C, C++ and so on, Functional languages are very very different they:

1. Have no loops
2. No variables
3. Execution order is a minor detail

Some of the strengths of such languages is that one writes less code, more terse, one writes it more quickly and bugs are much less of a problem.
 
Last edited:
Here's a random example of it running locally

1724444096780.webp



It's computing the sum of the string of digits of 4521356853475648753 repeated 2,535 times, we get the answer 6.

Is this a little too hard for you, scruffy ?
 
Here's a random example of it running locally

View attachment 1000180


It's computing the sum of the string of digits of 4521356853475648753 repeated 2,535 times, we get the answer 6.

Is this a little too hard for you, scruffy ?

I don't have an "any" key on my keyboard ... is that a Windoze thing? ...
 
This problem is interesting because there's actually no need to perform arithmetic at all, this is the reason I posted the problem, it's a great example of Edward de Bono's lateral thinking, by approaching the problem in a fundamentally original way we can sometimes produce solutions that are often very hard to do in a conventional way.
 
Here's a random example of it running locally

View attachment 1000180


It's computing the sum of the string of digits of 4521356853475648753 repeated 2,535 times, we get the answer 6.

Is this a little too hard for you, scruffy ?
Here, apply your functional methods to this problem:

You have 8,000 neurons in a small volume of hippocampus. 1,000 of them are excitatory and feed forward recurrently, 7000 of them are inhibitory and locally lateral.

The rules are: there is one new neuron born every minute, and one dies every minute, at random locations. Forward cells are omni connected, lateral cells connect with at most 400 neighbors. You are to run the simulation for 24 virtual hours (1 virtual day), ensuring that all updates are asynchronous.

The outcome is, you are to graph the STATISTICS of the connection weights for all the newly born cells, and provide at least a dozen examples of stored memory sequences that conform with those statistics.

You can use as many cores as you want, and you can choose from any of the MNIST training sets. If you're using Python you can find them here

from keras.datasets import mnist

but you can use any language you want.
 
Here, apply your functional methods to this problem:

You have 8,000 neurons in a small volume of hippocampus. 1,000 of them are excitatory and feed forward recurrently, 7000 of them are inhibitory and locally lateral.

The rules are: there is one new neuron born every minute, and one dies every minute, at random locations. Forward cells are omni connected, lateral cells connect with at most 400 neighbors. You are to run the simulation for 24 virtual hours (1 virtual day), ensuring that all updates are asynchronous.

The outcome is, you are to graph the STATISTICS of the connection weights for all the newly born cells, and provide at least a dozen examples of stored memory sequences that conform with those statistics.

You can use as many cores as you want, and you can choose from any of the MNIST training sets. If you're using Python you can find them here

from keras.datasets import mnist

but you can use any language you want.
That's not a problem domain I've ever worked in, unlike integers which most people understand. This seems like a deflection, striving to avoid actually addressing the problem I posed in this thread which requires no domain knowledge other than lateral thinking.

I'm sure we could each throw problems at one another that are in problem domains we know well but the other party does not, that proves nothing.
 
Here, apply your functional methods to this problem:
It's a shame you've not encountered functional languages, they are far closer to mathematics than imperative languages and thus well suited to problems in engineering and simulation and large data. The language OCaml for example is used at this investment firm because it is a good fit for their mathematical models:

Jane Street.

It’s no secret that we’re big believers in functional programming and use OCaml, a statically typed functional language, as our primary development platform. Jane Street’s technology group is small by design, which means we need to maximize the productivity of each person we hire; we believe functional programming helps us do that. But it’s not about productivity alone: programming in a rich and expressive language like OCaml is just more fun.

At Jane Street, functional programming isn’t a tool we reserve for some special set of problems. From systems automation to trading systems, from monitoring tools to research code, we write everything that we can in OCaml.

Imperative languages make bugs likely, they almost encourage bugs because they rely on mutable state (variables), by eliminating that one's code becomes automatically parallelizable without issue.

Think of the statement:

X = X + 1;

That's anathema to any mathematician (it's never true) and never arises in a functional language.

Functional languages are to lambda calculus what imperative languages are to Turing machines, there's a proof that lambda calculus and Turing machines are equally able to model computability.

This is why they are very good fit for many problems, and your sounds an ideal candidate. There's lots of evidence that functional code is

1. Easier to reason about
2. Faster to write than imperative.
3. Leads to fewer bugs.
 
Back
Top Bottom