Member Avatar for humorousone

This one's a strange question.
But first, a little bit of context, a math problem that a teacher asked:

In a given range, how many 4 digit numbers are there, where the 'thousands' digit is equal to the sum of the other 3 digits?

I wrote a quick (to write, not to run) brute-force algorithm that simply iterates through the all the values in a given range.

From here, I have two problems.

Here's the first algorithm I wrote: (ignore syntax errors please, I'm writing this outside of an IDE)

public class Test
{
    public static void Main()
    {
        int UpperBoundInput = 1234; //Input 4 digit upper bound here.
        string LowerBoundInput = 0000; //Input 4 digit bound here.

        int TrueCount = 0;

        int(int CurrentCount = LowerboundInput; CurrentCount < UpperBoundInput; CurrentCount++)
        {
            string CurrentCountString = CurrentCount.tostring();

            if(CurrentCountString[0] = (CurrentCountString[1] + CurrentCountString[2] + CurrentCountString[3]))
            {
                TrueCount++;
            }
        }

}

This code can be found here.

The problem here is that having 4 digit numbers leads to all sorts of problems with zeroes (0001 == 1), out of range errors.

I tried a different algorithm (for the sake of length, I'll only describe it), which is 4 nested for loops which iterates through the thousands, tens ect ect and does the same comparason.
This algorithm works, but it's incredibly innefficent.
~~~

Can anyone write a nicer looking algorithm to brute force this problem?

I know there's a mathematical algorithm that could be used, posting that would be awesome too.

Just leave the zeros out.
Do you know any 4 digit number, when summing up the rest of his digits is equal to zero, the 1000th digit? In fact 0089 for example is not a number. 89 is.
I would set my lower limit to 1000 and upper limit to 9999 to start with.
And why the conversion to string on line 12 ?
The plus sign on line 14 is not an addition, it is a concatenation.

Member Avatar for humorousone

The plus sign on line 14 is not an addition, it is a concatenation.

Ah, thanks, just noticed that. Thanks.

Do you know any 4 digit number, when summing up the rest of his digits is equal to zero, the 1000th digit?

Could you clarify?
Not exactly sure what you mean.

'Could you clarify?' Yes
6123 is a number and 6 = 1 + 2 + 3 so it is also a number that meets the conditions.
0123 is not a number(123 is) and you never will find 0 = 1 + 2 + 3 to become true. So your lower limit must start at 1000 not at 0000.
To solve your problem you might consider the div and mod operators / and %.
Or continue as you are doing on line 14, but then change the digits out of the string to integer digits first, before doing the addition.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.