Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

more fun with decimal arithmetic
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


Report this thread to moderator Post Follow-up to this message
Old Post
epc8@juno.com
05-19-06 08:55 AM


Re: more fun with decimal arithmetic
For 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


Report this thread to moderator Post Follow-up to this message
Old Post
Alistair
05-20-06 12:55 PM


Re: more fun with decimal arithmetic
On 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?

Report this thread to moderator Post Follow-up to this message
Old Post
Howard Brazee
05-22-06 12:55 PM


Re: more fun with decimal arithmetic
Howard 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

Report this thread to moderator Post Follow-up to this message
Old Post
Peter Lacey
05-22-06 11:55 PM


Re: more fun with decimal arithmetic
Howard 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


Report this thread to moderator Post Follow-up to this message
Old Post
epc8@juno.com
05-22-06 11:55 PM


Re: more fun with decimal arithmetic
Peter 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


Report this thread to moderator Post Follow-up to this message
Old Post
epc8@juno.com
05-22-06 11:55 PM


Re: more fun with decimal arithmetic
On 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.

Report this thread to moderator Post Follow-up to this message
Old Post
Howard Brazee
05-22-06 11:55 PM


Re: more fun with decimal arithmetic
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


Report this thread to moderator Post Follow-up to this message
Old Post
epc8@juno.com
05-22-06 11:55 PM


Re: more fun with decimal arithmetic
<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


Report this thread to moderator Post Follow-up to this message
Old Post
Oliver Wong
06-17-06 12:55 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Cobol archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:12 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.