## Archive for the ‘**python**’ Category

## Python and primes

Today I spent a while reaqcuainting myself with Python and IDLE and trying the first few project Euler challenges. I decided it would be worthwhile to have a list of the first few prime numbers, that would make my job easier for several of the problems. (I don’t care if that’s cheating, I still had to make the list of primes…) So I wrote a program to find primes. It’s tremendously inelegant and probably terrible inefficient, but I’m only going for the primes under 10,000 or so. It doesn’t take long to get them. Then I decided to write something that would find the prime factors of an input. The idea is that if the number to factorise is bigger than the biggest prime in the list, it will run the “find primes” program, until it has big enough primes. Anyway, I haven’t got there yet because it’s late and my brain won’t work properly any more. But I do have a number that will give you prime factors, as long as you give it small enough inputs. To try it out, I ask it for the prime factors of the first number to come into my head: 24. [2,3]. Awesome. Now, try it on something bigger. 2400? [2,3,5] hm. Try something else 24001. Too big. 2401 [7] Whaat? What are the odds of my pick 7^4 to test a prime factorisation program. Anyway, eventually I found a number random enough to satisfy me that the thing was working…

The thing is, the whole thing kind of pulls iteslf up by its bootstraps. So if, somehow, a number that isn’t prime crept into the list or a prime was missed out, the whole thing would be invalid. Because it only checks for divisibility of numbers in the list of primes… I should write some sort of consistency check program to make sure all the numbers in the list are not divisible by any of the smaller numbers in the list.

## Computer Programming and Second Order Logic are sexy

I’ve updated that spreadsheet of important people’s dates I made a while ago. I still haven’t found a good “timeline” making program. I was thinking instead of writing a program that would print all the people who were alive at the same time as your input. Or everyone alive and their age in a given year. For example:

user> Newton

Program> Descartes, Oldenburg, Boyle, Spinoza, Newton, Leibniz, Berkeley, Euler, Hume, Kant

It wouldn’t be too hard. The tough part, as far as I’m concerned would be extracting the info from a spreadsheet into an object python can read. That would be handled by a separate “extract” program. But I’m certain it is possible. When I feel the need to waste a lot of time I shall look into it. For now I am busy drafting an essay about Boolos’ two different interpretations of second order logic and why he was wrong to abandon his first position (the one from “On Second Order Logic”).

God, I’m such a crazy fun loving guy.

## I have a new-found respect for programmers

Respect might be a bit of a strong word for it…

Basically, I spent far too long today trying to get a Python program to check whether a number was divisible by any number on a list of numbers and then to use this to add to a list of primes. I could do it, but it was very inelegant and I couldn’t find a neat way to get the program to skip to the next number if it found a divisor. My program now works, but it is pretty clunky and not very neat. I am sure there is a much better way to do it.

Still, I am getting the hang of writing Python, which was the point of the exercise. And now onwards to bigger and better things… Having the list print to a file, cutting out unnecessary computational steps (ie, stopping when it finds no divisors up to half the size of the number in question…) and so on.

Or I could go back to my original project which was to create an array of numbers and their prime factors.

Well, when I first thought of learning Python I wanted to make a program that computed digits of pi. But that seemed to involve too much maths and not really that much programming proper. I might do it one day, still…

## Geek level up!

So I’ve decided to learn Python. Why? because I can. And because I want to make a program that can calculate digits of pi and then analyse the relative frequency of various digits. Possibly in various counting bases (binary hex etc) I’ve got as far a writing a program that asks for a number of sides and a length and computes the circumference of the shape, and tells you what shape it is (up to dodecagon).

I feel I am increasing my maths geek and computer nerd levels at the same time. Score!

In other news, I am going on holiday tomorrow, which sort of scuppers my plans of learning Python. But I will be back beginning of August and I will get cracking then.

Here is the code for my program, if anyone is terribly desperate for crap Python code…