Alright, well I've mostly finished a quick run through of a prime number finding program. I am pretty sure it should work well but there can be some improvement on its speed. I'm doing this just to get an understanding and feeling for the language. Basically the program will keep a list of prime numbers it has found and divide those numbers into the next number it has to check for being a prime.
Thanks to Aiiane's comment I've gotten more information on what range does and it makes me feel much better about for loops in python. Before I thought that they were weaker compared to other languages because you can't set how the for loop counts. Having the program skip evens it sped the program up about 10% and if I work in a part where it doesn't check a number with primes that are more than half its value then I should really get some good speed out of the program.
While messing with the program I noticed that if I checked the first 50,000 numbers for primes then over 10% of those numbers would be primes. At 100,000 less than 10% would be primes. I took the time to find when there would be exactly 10% prime numbers which was 64470. I find that kind of interesting. Here is the code I made:
primes = [2,3] #sets a list with two initial values
for i in range(5,64700,2): #loop from 5 to 64700 with every second number
isPrime = 1 #inits the number to check as prime.
for j in primes: #check i against all found primes
if ( (float(i) / j) % 1) == 0: #if previous prime goes evenly into number to check then
isPrime = 0 #mark as non prime
break #job done, break from inner loop
if isPrime == 1: #if number was found to be prime
primes.append(i) #insert the number in the prime list
for i in primes: #loops through all primes and prints them.
print(i)
print("There are ", len(primes), " prime numbers between 1 and 64700")
Thanks again to Aiiane for helping me get the code formatted more properly. I'll probably figure out how to make it looking better soon since theres too little room for all the text.
---
Anyways, my girlfriend and I have been playing a whole lot of monopoly (Check out monopoly city. The newish spin on it makes it kind of fun) and we were talking about the chances of getting which numbers. Theres a pretty basic way to solve that with maths but we figured it could be the next project for myself. I started working on a dice rolling doodad. I love the random module but I still need to play with it.
Out of 120,000 rolls I am getting an 18% chance of rolling a 7 which seems a bit high and a 1.5% chance of getting 2 which seems low so I think I screwed up somewhere. Also when recording when I get doubles I am getting 9% which seems quite off. I think that I need to get 16.6% for rolling a seven or doubles, and a 2.76% for rolling two. Also when playing monopoly it seems like after rolling doubles there is a higher chance of rolling doubles again. I need to actually start recording this on paper. I'll post the code on this one when I can get a little more done on it. Damn, I wanted to keep these posts short and simple.

Not sure if Blogger supports it, but the PRE html tag will allow preformatted text (and thus whitespace isn't collapsed, so indents work properly). Blogger might even have code formatting for CODE tags... I don't know for sure.
ReplyDeleteThis might be relevant, I suppose: http://stackoverflow.com/questions/679189/formatting-code-snippets-for-blogging-on-blogger
ReplyDeleteAlso, you can make your prime finder run *much* faster with a simple optimization: only check divisors up to sqrt(i) before breaking out of the inner loop. This makes your code run at least an order of magnitude quicker. :)
ReplyDeleteTo do this, add "import math" at the start, and then inside the inner loop, this:
if j > math.sqrt(i):
break
Worst case scenario, you can post the code somewhere else and link to it - sites like tinypaste.com, pastebin.ca, et cetera all work well for that.
ReplyDelete