04. The Click

Today I would like to write about the ‘click’. Not a mouse click or a remote control click, but the click that inevitably happens inside a programmers head when he/she starts viewing problems in a new, different way, due to practice and perseverance. A better way, or, as many like to say, ‘the programming way’. Here is a short story about my first ‘click’.

1001st prime number

During my second year as a computer science student I attended a very interesting class called Programming methods and abstractions. Today I teach practical exercises for that course, but that is a different story altogether.

During one of the first lectures, which were essentially the introduction to real programming (I dare say ‘real’, although we had Introduction to programming during our first year which was very trivial), the teacher asked us to think about an algorithm that would calculate the 1001st prime number. ‘That’s it for me’, I said to myself, ‘time to go home and admit this isn’t for me. This is for smarter people.’ But, when I overcame indulging in self-pity and defeatist attitude, I sat down and started thinking, opening a new C project on my laptop.

‘What are prime numbers, anyway?’, I googled because math was never my strong suite. Okay… Now, how do we calculate it? That part I didn’t want to google. I wanted to see for myself will I be able to break it. I had ups and downs, I wrote unneeded code, bad code, erased it, tried again… It took me three days of playing around and thinking about it to solve it. I was so very proud when I came up with this little piece of code (today I would probably write it differently, but this is the original):

```#include <stdio.h>
#include <math.h>

int main() {

int i, j, ok, c = 5;

for (i=12; ;++i) {
ok = 1;
for (j = 2; j <= sqrt(i); ++j) {
if (i % j == 0) {
ok = 0;
break;
}
}
if (ok) {
++c;
if (c == 1001) break;
}
}
printf("The 1001th prime number is: %d\n", i);
return 0;
}

```

I couldn’t wait for the next class to tell the teacher I’ve done it.  ‘Respect’ was his comment when I told him I did it and that’s pretty much the best praise one could get from that guy. ‘Maybe I do have a chance in this field’, a glimmer of hope appeared shyly.

The most important thing I learned was not how to calculate the 1001st prime number, but rather how to approach problems. It was the ‘click’ for me and it is hard to say more than that or explain it properly. It is an alchemical process, to be poetic. You change without realising.

After that, I took another task and tried to solve it. It didn’t take me three days anymore: it took two. The next one took one day etc. until they took a few minutes to solve. That was probably the first time in my life I believed in the power of practice. You should, too.