# Just a thought, why algorithms matter

Just for kicks I started working through Project Euler. I was on problem 3.

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

OK, so my first idea is to do a Sieve or Eratosthenes and work off of that. I code it all out and run it. It works for small numbers. When I try that 12 digit number, I find out that JS can’t build arrays that large.

OK, plan B, brute force. I code it out and it works for small numbers (like 3 and 4 digit). So I plug in mega-number. My laptop CPU fan goes into overdrive and it sounds like the whole thing is about to take off. After about 6 hours of this, I shut it down - time for bed. Who knows how long it would have taken to finish.

This morning I try plan C, a more elegant solution I thought up last night while drifting off to sleep. It takes less than half a second. It was probably less than that.

Algorithms matter. Understanding big-O notation matters. Can you imagine if I had to do that for work and I couldn’t come up with the good solutions?

This is why we learn and practice algorithms. They make us better coders. I will probably never need to do this exact algorithm, but all the algorithms I’ve done help me to solve them when I come up. It’s exercise for the brain.

3 Likes

Something I’ve noticed, and I don’t if you’ve experienced the same. But doing algorithm exercises has helped me get into the habit of looking beyond the exercise itself. Like looking at the bigger picture, the possibilities not specifically tested for. Which in turn is helping with my other programming endeavors by making me think critically about the “what ifs”. Okay my program can do xyz like I what it to…but what if the user does this, or that, is my program equipped for those scenarios? I can’t expect everyone who uses something I made to do what I would do. Like in your case, handling massive numbers. It was something you needed to account for.

1 Like

Yeah. I think that curiosity is an important part of the coder personality. A lot of coding challenges have built in “gotchas” and it forces you to think about “What could go wrong?” They teach you to read closely, to what is stated but also to what is not stated. Maybe in the real world, I wouldn’t just assume massive numbers, but it reinforces that I need to at least ask what the input range is expected to be.

As annoying as I find a lot of the algorithm sites with their sneaky edge cases, it really does force you to start thinking that way.

If you’re building a database with an API for a client, you can’t just build it, you have to find out: how many users are expected, how much traffic, how large are the records, are they expected to change in the future, if there are numbers or strings how big are they expected to get, etc. These are important questions.

I think sleeping matters too, you wouldn’t get that solution without the attempt to sleep

Yeah, that can be part of it too. Sleep specifically, or just more generally, taking a step back and thinking about it or even thinking about something else.