Zero Sink

Saturday, January 10, 2009

Project Euler and Python

I've been hacking my way through the problems at Project Euler, and for two days I've been frustrated by a segmentation fault bug in my code for problem 57. I'm using the GMP bignum library and the GCC C compiler.

The problem arises when one of my routines takes a pair of mpz_t variables and divides them both by 10 using mpz_tdiv_q(). When I then try to evaluate the resulting quotient (of the smaller variable) using mpz_cmp() (to see if it's == 0) I get a segmentation fault. I've tried using mpz_sgn() and a number of other approaches, but I keep getting that same segfault. I also get a segmentation fault if I try to use mpz_sizeinbase(x,10). It's almost as if, after being divided by 10, the mpz_t variable is no longer initialized, or something.

This morning (an hour ago) I decided to chuck the existing C code out the window and try using Python instead. Bear in mind that I've never written a single program in Python before (other than working through the first few pages of the tutorial).

They aren't kidding when they say Python saves a lot of development time. The code I came up with is only 6 lines long, runs in .049 seconds, and works. The problem's solved! Wow. Now I have to learn Python, I guess.

0 Comments:

Post a Comment

<< Home