Last active
December 15, 2015 02:19
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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