Home > ProjectEuler.Net, Python > Project Euler – Problem 7

Project Euler – Problem 7


Homepagehttp://projecteuler.net/problem=7

Problem:

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

How I solve:
– Pretty much simple for prime search


import math

def isPrime(n):
	if n == 2 or n == 3:
		return True
	if n < 2 or n % 2 == 0:
		return False
	if n < 9:
		return True
	if n % 3 == 0:
		return False

	r = int(math.sqrt(n))
	f = 5

	while f <= r:
		if n % f == 0 or n % (f + 2) == 0:
			return False
		else:
			f += 6

	return True

def listPrime(n):
	primes = []
	i = 2
	while len(primes) < n:
		if isPrime(i) == True:
			primes.append(i)
		i += 1

	return primes

# get result
# 104729
print listPrime(10001)[-1]

Cheers,
Pete Houston

Advertisements
Categories: ProjectEuler.Net, Python Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: