Skip to content

Instantly share code, notes, and snippets.

@1st
Last active December 15, 2015 02:19
Show Gist options
  • Save 1st/5186218 to your computer and use it in GitHub Desktop.
Save 1st/5186218 to your computer and use it in GitHub Desktop.
Solution for task: http://projecteuler.net/problem=7 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?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def get_prime_number(index):
'''
Return prime number by given index.
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
Args:
- index: number, starts from 1 and up
Example output:
>>> get_prime_number(10001)
104743
'''
if index < 1:
raise Exception('Invalid parameter. Please set index to 2 or above')
prime_numbers = []
num = 2 # initial prime number
def check_number(number):
'''Chek is given number is prime'''
for x in xrange(2, number):
if number % x == 0:
return False
return True
while len(prime_numbers) < index:
if check_number(num):
prime_numbers.append(num)
num += 1
return prime_numbers[index-1]
try:
index = raw_input("Enter index number: ")
print get_prime_number(int(index))
except ValueError:
print "Enter correct number (1 and above)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment