Code Comments
Programming Forum and web based access to our favorite programming groups.A number of years ago a small university in western Canada tried a publicity stunt to recruit prospective students. It offered a scholarship to the first group of students who could solve a puzzle. The last part of the puzzle instructed the contestants to add up the decimal digits in the numbers from one to one million and send the answer in for a chance at the prize. One way to find this answer is to use some math. I'll skip the math :-). Another way is to use brute force and write a computer program to find the answer. Just for fun, along the lines of the "99 bottles of beer program", see how easy this is in COBOL. [For math history buffs, this is not along the same lines as Gauss adding the numbers from 1 to 100 in grade school.] -- Elliot
Post Follow-up to this messageFor anybody interested in such activities, Personal Computer World magazine published a series of articles (back in the 90s) for idle minds to occupy themselves. I believe they were authored by Mike Mudge and may be available in a collected edition. epc8@juno.com wrote: > A number of years ago a small university in western Canada tried a > publicity stunt to recruit prospective students. It offered a > scholarship to the first group of students who could solve a puzzle. > The last part of the puzzle instructed the contestants to add up the > decimal digits in the numbers from one to one million and send the > answer in for a chance at the prize. > > One way to find this answer is to use some math. I'll skip the math > :-). Another way is to use brute force and write a computer program to > find the answer. > > Just for fun, along the lines of the "99 bottles of beer program", see > how easy this is in COBOL. > > [For math history buffs, this is not along the same lines as Gauss > adding the numbers from 1 to 100 in grade school.] > > -- Elliot
Post Follow-up to this messageOn 18 May 2006 21:15:31 -0700, epc8@juno.com wrote: >A number of years ago a small university in western Canada tried a >publicity stunt to recruit prospective students. It offered a >scholarship to the first group of students who could solve a puzzle. >The last part of the puzzle instructed the contestants to add up the >decimal digits in the numbers from one to one million and send the >answer in for a chance at the prize. So we're all thinking Gauss now. >One way to find this answer is to use some math. I'll skip the math >:-). Another way is to use brute force and write a computer program to >find the answer. Not much force is required when we have a computer and very easy code. >Just for fun, along the lines of the "99 bottles of beer program", see >how easy this is in COBOL. > >[For math history buffs, this is not along the same lines as Gauss >adding the numbers from 1 to 100 in grade school.] How is it different?
Post Follow-up to this messageHoward Brazee wrote: > > On 18 May 2006 21:15:31 -0700, epc8@juno.com wrote: > > So: if "sigma" = n(n + 1)/2 to calculate the sum of 1, 2, ..., n, then the result would be sigma (1000000) + (sigma(1000000) - sigma(100000)) (for 100000 to 1000000) etc. etc.? Or are we calcualting the sum of each column and adding the six totals together? PL
Post Follow-up to this messageHoward Brazee wrote: > On 18 May 2006 21:15:31 -0700, epc8@juno.com wrote: > > > So we're all thinking Gauss now. > > > Not much force is required when we have a computer and very easy code. > > > How is it different? The digits have no positional value. 10 contributes a value of 1 not a value of 10. 99 has a value of 18, for example. -- Elliot
Post Follow-up to this messagePeter Lacey wrote: > Howard Brazee wrote: > > > So: if "sigma" = n(n + 1)/2 to calculate the sum of 1, 2, ..., n, then > the result would be > > sigma (1000000) + (sigma(1000000) - sigma(100000)) (for 100000 to > 1000000) etc. etc.? > > Or are we calcualting the sum of each column and adding the six totals > together? > > PL Yes. Exactly. The digits in this problem have no positional (place) value at all. So 99 contributes 18 to the sum, for example. -- Elliot
Post Follow-up to this messageOn 22 May 2006 10:38:42 -0700, epc8@juno.com wrote: >The digits have no positional value. 10 contributes a value of 1 not a >value of 10. >99 has a value of 18, for example. OK. It's still a trivial CoBOL task. It might be more interesting to have a mathematical short-cut, but that won't make for a better program.
Post Follow-up to this message
Howard Brazee wrote:
> On 22 May 2006 10:38:42 -0700, epc8@juno.com wrote:
>
>
> OK. It's still a trivial CoBOL task. It might be more interesting
> to have a mathematical short-cut, but that won't make for a better
> program.
How might you solve the problem in other languages? One way is to take
each number in binary and convert it to base 10 using quotient and
remainder operations. A second way is to convert each number to a
string, then add up the digits.
Just for fun, here is a version in Microsoft BASIC:
10 T#=0
20 FOR I=1 TO 1000000!
30 S$=STR$(I)
40 FOR J=2 TO LEN(S$)
50 T#=T#+ASC(MID$(S$,J,1))-ASC("0")
60 NEXT J
70 NEXT I
80 PRINT T#
90 SYSTEM
[J starts at 2 to skip the leading space in S$.]
In CoBOL it's a trivial program becasue there is no type conversion
involved at all.
-- Elliot
Post Follow-up to this message
<epc8@juno.com> wrote in message
news:1148320969.215163.4070@i40g2000cwc.googlegroups.com...
>
> Howard Brazee wrote:
>
> How might you solve the problem in other languages? One way is to take
> each number in binary and convert it to base 10 using quotient and
> remainder operations. A second way is to convert each number to a
> string, then add up the digits.
>
> Just for fun, here is a version in Microsoft BASIC:
>
> 10 T#=0
> 20 FOR I=1 TO 1000000!
> 30 S$=STR$(I)
> 40 FOR J=2 TO LEN(S$)
> 50 T#=T#+ASC(MID$(S$,J,1))-ASC("0")
> 60 NEXT J
> 70 NEXT I
> 80 PRINT T#
> 90 SYSTEM
>
> [J starts at 2 to skip the leading space in S$.]
>
> In CoBOL it's a trivial program becasue there is no type conversion
> involved at all.
Sorry for the late reply. It's also trivial in any language which has
the addition, division and modulus operator, and does truncation for integer
division, and has a conditional looping construct.
int sum = 0;
for (int i = 1 to 1000000) {
int temp = i;
while (temp > 0) {
sum += temp % 10;
temp = temp / 10;
}
}
- Oliver
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.