Variable
it is used to keep track
of the "written down" numbers. The variable count holds the number of times the
digit 1 has been used in the "written down" numbers.
The main program is the very short DO loop. It increments it, then adds the number of 1's in it's new value to the totaliser count. The loop is re-executed
until the number of 1's in count
is equal to the latest value of it.
Function CountOnes() does all
the hard work. It uses variable i
to iterate through a string representation of it, held in c$. This iteration continues to the
end of c$, denoted by
variable Lc$ itself).
Every time a "1" character is encountered in c$, it's totalised in variable nrOnes. When all characters have
been examined, the value in nrOnes
is returned to the calling routine.
(derived form the
length of
When the program is run, it
generates the inset screen. The answer is a much larger value
than might be guessed at.
|
|
Counting ...
Writing down all the integers from
1 to 199981 inclusive involves
writing the digit '1' 199981 times.
Press any key to exit ...
|