2 Posts Tagged 'Math' RSS

Math fun

I solved another 17 problems on Project Euler today. I've not solved more than the average Ruby participant on the site. But Rubyists have a pretty low average ranking on that site compared with other languages like Python and C.

Kind of makes sense given the number-crunching nature of most of the problems; Ruby isn't that kind of language. But it's also a bit surprising given that many of the problems there can be solved in one or two lines of code using standard Ruby libraries or a gem or two. In particular Ruby Facets is a great resource any time you find yourself dealing with combinatorics (permutations, combinations etc.).

Also, thanks to the speed of modern computers, given a decent algorithm, the difference between a C solution and a Ruby solution is often a matter of less than one second with C vs. 5 or 10 seconds with Ruby. The speed with which you can bang out working Ruby code likely more than offsets this.

I used to be good at math, back in the day. I was top of the class in all of the (few) calc classes I took. But then I verged into computer science and never looked back. Pure math never really held my interest. Now I find myself pretty lost even on simple math problems, which is sad.

November 05, 2007 @ 12:07 AM PST
Cateogory: Programming

DISASTROUS

If only I had a dime for every time Stroustrup used the phrase "This would be disastrous". It can refer to everything from misuse of pointers, to making your destructors non-virtual in a class that will be a base class for some derived class, to all sorts of other C++ pitfalls. I got my copy of Effective C++: 55 Specific Ways to Improve Your Programs and Designs today, and Meyers seems to use the same phrase a lot too. Maybe that says something about C++.

Maybe it's my twisted sense of humor, but I really get a kick out of that kind of over-the-top hyperbole though. The other thing I get a kick out of is programs that insult their users. You can't beat that for a laugh. Scott Meyers has already insulted the average uninformed C++ programmer a bunch of times in his book, and I'm only on chapter 1. Clearly good times are ahead.

Although I'm theoretically "done" reading Stroustrup, I've resolved to go back through the book and do all the end-of-chapter exercises. I figure that's a good place to start before trying any kind of larger project in C++. He must've included all those problems in there for a good reason. I can already think of something at work I'm going to try to redo in C++ to try to speed it up a bit compared to the Ruby version I have now. (I doubt I could make it any slower than Ruby if I tried.)

Tonight though I was distracted by a really fun site called Project Euler, which is a collection of small mathematical-type programming problems that you can try to solve for fun. I finished 15 of the 159 problems after work tonight. All in Ruby, of course. But I think I did the easy ones. Some are harder than others. But according to the FAQ, all problems on the site are meant to be able to be solved in less than a minute of runtime on a modern computer. Some of the problems are brute-force solvable but some others require some sophisticated algorithms to have any chance of solving before the universe ends. Coming up with an algorithm for a problem that has a nice big-O runtime is by far one of my weakest areas, so this is good practice.

You can register on the site, write a solution, submit it and it'll tell you if you're right. It also collects statistics (if you choose to give them) about who's using what programming language to solve the problems, and what country you're from. And then it ranks how many people use each language, and how successful each languages' users are (going by how many problems they've solved). So in that sense the whole site is a kind of programming language battle to the death. (Did some people really solve these problems in Tcl?)

If that site is any judge, C and C++ (lumped together perhaps inappropriately, according to Stroustrup anyways) is/are the most popular language(s), followed by Python and Java. Ruby made a good showing, which makes me happy. Ruby's immense standard libraries make many of the problems there trivial no-brainers. Either way, this seems to indicate that there are a lot of C programmers out there. Or it may be that the number-crunching mathematical nature of these problems lends itself well to C, which is why so many people picked C to solve it.

July 25, 2007 @ 9:06 PM PDT
Cateogory: Book Reviews