From 483cd3629d42acbf3909cf534f5a9224f292cbeb Mon Sep 17 00:00:00 2001 From: James Raikes Date: Thu, 14 Jul 2016 15:45:14 +0100 Subject: [PATCH] Started Problem 10 Made improvements (incomplete) to Problem 7 --- problem10.py | 0 problem7.py | 27 +++++++++++++++++---------- 2 files changed, 17 insertions(+), 10 deletions(-) create mode 100644 problem10.py diff --git a/problem10.py b/problem10.py new file mode 100644 index 0000000..e69de29 diff --git a/problem7.py b/problem7.py index 8295db8..f3559cc 100644 --- a/problem7.py +++ b/problem7.py @@ -5,19 +5,22 @@ # efficiently check for primes. # Divide by 2, 3, NOT 4, 5, NOT 6, 7 etc -def is_prime(n): - '''v2, only test odd numbers up to root(n) inclusive''' - divisors = [2] - divisors.extend(list(range(3, int(n ** 0.5) + 1, 2))) - for i in divisors: - if not n % i: - return False +def is_prime(test_num): + '''Test a number for primality. + + v3, test divisible by 2 or 3 then 6k+/-1''' + if test_num == 1: return False + if test_num in (2, 3): return True + if not (test_num % 2 and test_num % 3): + return False return True -def main(): - '''v1, tests all numbers for primality''' - prime_no = 10001 +def prime_generator(n): + '''Generate the nth prime number + + v1, tests all numbers for primality''' + prime_no = n prime_count = 0 test_number = 0 while prime_count < prime_no: @@ -26,6 +29,10 @@ def main(): prime_count += 1 print(test_number) +def main(): + prime_generator(1) + prime_generator(10001) + if __name__ == "__main__": main()