Sunday, January 29, 2012

Python - Generating primes

I always make a generating primes program when I'm learning a new language, as if it were my "hello world". I'm using python 3.2

import math

def cribe(size):
    nums = [0]*size
    for i in range(2,int(math.sqrt(size))):
        if(nums[i] == 1):
            continue
        for j in range(2,size):
            if i * j > size - 1:
                break
            if nums[i*j] == 0:
                nums[i*j] = 1
    for i in range(2,size):
        if(nums[i] ==0 ):
            print(i)        
limit = input('Up to what number? ')
cribe(int(limit))

My IDE is by the moment aptana-studio, haven't found anything better.

http://aptana.com/





No comments:

Post a Comment