Project Euler is a great website that any CS student interested in math should look at. What it basically does is take a bunch of math related problems that are great for practicing your understanding of certain mathematical concepts and polishing up your programming. This week, I’m going to look at two questions, one which I’ve solved and one which I’m currently (and have been for a while) working on – #__ and #487 for those interested.
(Note: No answers will be given, and the number for the first one is removed because sample code is given.)
We’ll start with the hard one first.
Just looking at the question, it’s not very difficult to write one that goes through the problem through brute force, however there are two problems with this approach.First, it takes an extremely long time for this to run if you just power through it (to be honest, I have no clue if I had an infinite loop – pretty sure I didn’t – or whether this thing would run on for days. I wasn’t booing to wait days). Secondly, one of Project Euler’s requirements is that anything you write should be able to give you the answer in under a minute. I approached this question like this at first. I then tried doing it again with a new approach. The trick with this question is simplifying the summation into a formula that speeds the process. The problem is that (1) I couldn’t think of how to simplify the last summation, and (2) though I’m certain my approach to is quite fast, I’m positive that it can be made more efficient. Also, the current prime sieve I’m using is complete trash for this question. This is one I’m going to enjoy tackling after finals. I can’t wait to get this one, then I can brag to my friends that use project Euler that I figured out one of the more difficult questions 🙂 .
The second question is this.
The cool thing about this one is that you really can go through it through brute force. By no means is it an elegant way of going through it, but with something this simple, it’s very tempting. The first approach I took with this one worked, and this is thecode I came up with:
(I just realized that capitol ‘S’ in the function name. The things you notice when you double check.)
What basically happens here is that I take every number from 0 to , and raise it to , this way I get every number from , instead of from . And every number, I add it to x, make x a string, and return the last ten digits as requested. For me, the thing that took a small amount longer than I expected was getting the last 10 digits. I had two options, return x, and copy+paste the last 10 digits, or making my not so elegant code a bit more elegant, that’s when I thought to just make it a string and splice out the last 10 digits.
Project Euler is really a great thing. I’ve been trying to do 1 question a week, and it’s really good excersize.
If you learn anything from this blog, I think the most important thing is this:
1) CSC165 is really a great course. The things you learn in it are quite helpful in Math and Philosophy, and of course, Computer Science (interestingly enough, I’m taking all three this year). The instructors really care and want you to understand. One of the most important things you learn from this course is how to solve problems, and for me personally, the most important lesson was something Prof. Heap talked about in the first week of school, the methods one should take towards problem solving.
2) Do Project Euler questions. They’re great “brain food” and help you think abstractly.
There are other sites that do similar things, you could also do those.
Thank you all,
NDE.