Edit: I originally posted the RANGE function going to 1000000. Python does not include the last number (e.g., from 1 to 5 goes up to 4). The functions have been updated to use 1000001.
The other day, Astrid said she could count to 1 million by tens. She started off and got to 100, but then skipped up to 200. When she got to 1,000, she started going by thousands. Then she needed help. I told her she wasn't counting by tens any more, and that it would take a really long time to get to 1 million counting by tens.
Today, Astrid and I sat down and wrote a program in Python to count to 1 million by tens. Here was the first version of the program:
for i in range(0, 1000001, 10):
print(str(i))
This program sets up a variable named 'i' and executes a loop for every value between 0 (first argument to RANGE()) and 1000001 (second argument, which is not included, so we have to go past our target) by 10 (third argument). Then it prints the number after converting it to characters (STR() function), like the text you're reading now.
Python was so fast that the numbers just rushed by in a blur on the screen. It didn't give us a good sense of the size of this task. For our next attempt, we decided to write it to a file. Here's the new program:
with open('numbers.txt', 'w') as f:
for i in range(0, 1000001, 10):
f.write(str(i) + '\n')
This program sets up a file first, named numbers.txt, with write ('w') access, and names it 'f'. Then it repeats the same process as in the first program, but instead of printing the number, it writes it to the file (f.write() function / method). So that the file is readable, we have to append (+) a line ending ('\n', what is inserted when you hit ENTER on a keyboard).
Below is a sample of the output.
# Output:0
10
20
30
40
50
60
70
80
90
...
999900
999910
999920
999930
999940
999950
999960
999970
999980
999990
1000000
Finally, we wanted to get an estimate of how long this would take to read. At first, we just opened the file and scrolled through it. However, it was so big that the scroll bar on the right of the text editor program barely moved! So then we thought maybe we'd see how many pages it would be if we printed it.
That's where the computer started to have trouble! It said it was working on the print preview for a while. I opened the task manager and showed Astrid the "workers" - CPUs - on the computer (4), and how the chart showed a lot of work was going on when the line was drawn toward the top. It appeared like the CPUs were "getting tired" and telling another CPU to help out, one at a time. Kind of strange!
We also looked at the computer's "work table" - RAM - and how if the line was toward the top, it was taking up a lot of room to do the work. Working on the print preview did not take up any space on the work table, though, so it was flat at the bottom.
After waiting for a bit, we decided that this seemed like too much work to ask the computer, so we cancelled the print preview. I guessed that the number of pages would be like a thick book. At that point, Astrid was losing interest, so we concluded our little counting / programming play.
As I'm writing this post, I ran the print preview on a more powerful computer and got an estimate of the number of pages. 1,563!!!
P.S. I was running Python 3 on Ubuntu 20.04 GNU/Linux with the GNOME desktop.
Comment(s):
Post a Comment