Wednesday, May 19, 2010

More work on Prime numbers and starting Dice

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.

Sunday, May 16, 2010

Starting this a bit late

A few days ago I decided that I wanted to try to learn Python.  Knowing that the xkcd guy likes Python I figured I'd scout out his forum for any suggestions on how to start.  One thing I came across was the idea to start a weblog (I hate the word blog)  about my experience learning Python.  Of course this suggestion was to someone who had no experience in any programming languages which sounds like it would make a really good read.  I figured that I would try it out anyways even though I do have some experience in other programming languages.  

Anyway, I searched Google for recommendations for a compiler and where to start.  I found that I needed to download the Python language which is mostly unfamiliar to me. That felt like this was going to turn into the whole ordeal with trying to learn Java which was kind of a nightmare for me at the time.  Then I downloaded Eclipse which appears to be an editor made for multiple types of languages but for Python you need an ad-don which was called PyDev.  Getting PyDev installed on Eclipse was a little confusing but the website had a very good walk-through.

The Hello World program was delightfully easy with just print("Hello World!") as the program.  I looked through tutorials on the basics.  Lots of stuff is rather different than I am used to.  Lately I've been trying to make a simple program that can find all prime numbers out to some point. One million seems like a good number.  For loops were confusing the hell out of me.  This range stuff is different than I'm used to and I don't know if I can get the counter to increment more than one number at a time (I'll want to skip evens for finding primes).  I am also encountering problems with the math.  I need to divide two numbers and I am guessing that the result is rounding to a whole number.  I'll use the print function to see whats going on in the code but for now my time has almost run out for tonight.


Python: http://www.python.org/download/
Eclipse: http://www.eclipse.org/downloads/
PyDev: http://pydev.org/download.html (read the tutorial on updates)

Documentation: http://docs.python.org/tutorial/ (Looks to be more detailed)
NonProgrammers Intro Guide: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers (I haven't used this but sounds useful for anyone new to programming)
Programmers Intro Guide: http://en.wikibooks.org/wiki/Python_Programming (I've been using this for quick tips. The exercises have been very helpful)


For those of you who are interested about my programming history, I've been programming on and off since I was maybe thirteen or fourteen.  When I first started I was really messing around with QBasic on an old 386 that no one in my family cared about.  I didn't know what programming was at the time but I read through the programming code and could make a bit of sense with it.  I alter the code and learned from it, then using that making my own programs.  I ended my experimentations with that after a made a short semi-graphical RPG with the sprites I had to write myself.  The game had a bug that frustrated me too much since I couldn't fix it. I later found my mistake. The whole thing was written with goto statements :(.

Later my friend and I starting making a few websites here and there.  I ended up becoming friends with a girl who had a weblog and I had a lot of fun reading through it. At the time I didn't know what a weblog was but I wanted one myself. I rented some web-hosting services from some company that supported PHP and MySQL and learned them to program my own weblog.  When I that I told the girl and she told me that she just got her weblog from livejournal.  Blast, all that work for nothing and it costs me 60 dollars a month.  Later in highschool I took a pascal class and had fun with that.

Later in college I took some classes for C/C++, Java (didn't learn a thing. Fucking online classes), Flash programming, and finally ended with a micro-controller programming class which used C and was a blast.  I've always wanted to do some game programming/development and hope that someday I can get skilled enough to do that, until then I'm just trying to learn what I can.  Since the micro-controller programming course I've really enjoyed looking into more micro-controllers and electronics, perhaps I'll start working more for that as well.