Day 10 - 'Far from the madding crowd' and caught up in Python loops
Wendy Ware
Sustainably unlocking value across mining value chains through full rock understanding
Hello, esteemed friends, colleagues and connections,
I am tracking a bit behind schedule. There is a lot happening in the work space and it has been difficult to find the time to practice challenges and keep up with the blog. I have been maintaining my streak in Sololearn and am nearly at the end of my first module - one get's certificates in Sololearn, so I will be sure to post it when done.
To say the last few days have been a whirlwind would be putting it lightly. In my pursuit of understanding the mechanics and logic of programming, I've found myself ensnared, time and again, by the intriguing and sometimes maddening concept of loops.
Ah, loops! What a joy they can be! Imagine: the simple, repetitive beauty of executing the same set of instructions, over and over, until a certain condition is met. Sounds simple, right? But, as many of you might know, it's not always so straightforward.
While I pride myself on resilience and the determination to overcome challenges, I confess: there've been moments of frustration, head-scratching, and even some good old-fashioned keyboard thumping. But every stumble and misstep has taught me something new.
Take, for example, my recent adventure into the world of prime numbers. Prime numbers, as I'm sure many of you remember from math classes past, are numbers greater than 1 that aren't divisible by any other number except 1 and themselves. Numbers like 2, 3, 5, 7, and so on. Simple enough, right? Well, when you're attempting to craft a program that accurately determines whether a number is prime, things can get... complicated.
领英推荐
Above is the code I wrote to check for prime numbers. It looks neat and nifty, doesn't it? But let me assure you, behind those few lines is a journey filled with iterations, errors, corrections, and yes, many loops!
You see, while it might be tempting to check for divisibility with every single number up to n, that would be highly inefficient. The magic lies in understanding that if n is divisible by a number greater than its square root, it would definitely be divisible by a number smaller than its square root. Therefore, we only need to check up to the square root of n.
Furthermore, by excluding even numbers (except 2 - which is not considered prime) from our checks, we can effectively halve the number of operations, making the program run more efficiently. Efficiency in code, as in life, often comes down to the tiny tweaks and the application of a deeper understanding.
In conclusion, my looping journey through coding, especially with loops, has been as much about personal growth as it has been about skill acquisition. I'm learning, slowly but surely, to embrace each hurdle, understand the underlying logic, and adapt my approach accordingly.
I might be doing "loop de loops" for now, but with perseverance and a bit of humor, I'm determined to come out on top. To everyone else out there learning something new or grappling with a complex challenge: Keep at it! Remember, it's not about how many times we stumble but how many times we rise.
Until next time, keep coding and stay curious!