Topic: Five hundred prime number
Hi!
I'm pretty new to python and need some help writing a program that prints the five hundred prime number.
Community of Helpful Python Developers. Our Python forum is filled with tips, tricks and developers waiting to help.
You are not logged in. Please login or register.
Hi!
I'm pretty new to python and need some help writing a program that prints the five hundred prime number.
I'm pretty new to Python too.
I started learning this programming language few days ago.
I have found really interesting the lessons at:
These lessons are free.
In 1 of the problem set of the 6.0.0 course the professor gave the same problem.
I do not think that the solution that here i share is the best solution possible to this problem.
This solution simply covers my current knowledge of the language.
Here is the code.
from math import *
def defineIthPrime(position):
"""Computes the ith prime number"""
def divisible(curr,d):
"""Check if curr is divisible by an element of d"""
for i in d:
if (i > sqrt(curr)):
return False
elif (curr % i == 0):
return True
return False
def calculateIthPrime(position):
"""Computes the 1:ith prime number"""
d = {1:2}
d1 = {}
idx = 2
cnt = 1
curr = 1+2*cnt
while (idx <= position):
if (not divisible(curr,d1.values())):
d1.update({idx:curr})
idx += 1
cnt += 1
curr = 1+2*cnt
d.update(d1)
return d
assert(position > 0)
assert(type(position)==type(1))
d = calculateIthPrime(position)
return d[position]
def problemSet1():
"""Computes and prints the 1000th prime number"""
result = defineIthPrime(1000)
print "The 1000th prime number is ", resultLast edited by 5tarson (February 13, 2012 11:57)