Mathfun.py 2.1 review
DownloadMathfun.py project s a rather small Python library that contains some of the more obscure (others would say 'less useful' 8
|
|
Mathfun.py project s a rather small Python library that contains some of the more obscure (others would say 'less useful' 8^) mathematical formula/functions that I have always found interesting/amusing.
These include primality tests, fibonacci sequences and turning characters into numbers, among other things. The next paragraph includes a little story about my strange habits as a teenager. The paragraph after that contains some ramblings about primes and formulas.
The paragraph after that hasn't been decided on yet, but you can be assured that it will be more stream-of- consciousness rambling about something mathematic by yours truly.
One of the reasons that this library exists is that, as a junior programmer in high school, I spent an insane amount of time writing BASIC programs on our IIgs that would calculate the first 100 primes or some other such nonsense. I would write it one way, get out a stop watch, run it, record the time it took, rewrite it, rerun it, retime it and calculate the difference as percentage decrease in time. Repeat the previous until you get BASIC that looked like an explosion in a type factory. All in some sort of quest to... well, I don't really know what I was shooting for. I just enjoyed it. Calculating if a number was prime, calculating the prime factorization, calculating the first N prime numbers... Yeah, I was a strange one.
But, in retrospect, all that solitary math kinda helped. While I reinvented many a prime wheel, I did it on my own, which was something of a personal fulfillment once I had learned more of a history of mathematics. And I never forgot most of those formulas that I worked out on that Apple. For instance, mathfun.isprime(number) utilizes most of what I learned back in high school. It iterates through the odd numbers, up to the integer value of the square root of the number in question. I still remember the epiphany that I had that I only needed to calculate through the square root of the number. 8^)
There's a minor paradox with the calculation of a prime. The quickest way to generate primes would be to only attempt modular arithmetic with prime numbers. However, to do it that way means that you have to generate a list of prime numbers first, which involves a primality test on every odd number up to the square root of the number in question. This would be sloooooow for the primality test for any single number. (For instance, isprime(10000) would involved roughly 50 full primality tests to generate the list needed to be a maximally efficient test on it.) So, we accept that even if we're not going to get any results doing (N mod 9), it's much quicker to eat those wasted cycles than to determine if 9 is prime first.
However, if you're doing nothing more than simply cranking out prime after prime, it becomes much more efficient to only do the primality test with a list of primes, since (if you start from 2) you've already determined the ealier ones before. (There's probably a break-even time first (machine dependent) before "determine all primes with primes" becomes more efficient than "crank through the odd numbers".) It's easy to see however, that for very large numbers it's much better to only work with primes.
There's also a minor question of the speed of the data structures used to store the prime list versus the very fast next odd number test. However, I'll leave that one up to the True Computer Scientists.
Requirements:
Python
Mathfun.py 2.1 search tags