"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?"
Time to make a for loop that checks if a number is a prime or not.With this code, I seem to be able to calculate prime numbers in a correct way. The problem is that it is incredibly slow.
I'm going to rearrange is order of the for-loops to increase the speed of calculation. This didn't make it faster... Now I'm going to try to see if I can limit the modulus checks even more. I've read something somewhere about prime numbers are not dividable by 2 (except the prime number 2) or by any prime numbers that are smaller than the square of the number the is being checked. Wow, I've made a huge miss. I though 10001 was one hundred thousand and one instead of ten thousand and one. Well well, here is my code for finding the 10001th prime:
I'd like to have a feature in PyCharm that shows the time it took to run an algorithm.
/Ludvig
Time to make a for loop that checks if a number is a prime or not.With this code, I seem to be able to calculate prime numbers in a correct way. The problem is that it is incredibly slow.
t = 0listOfPrimes = [2] print("length", len(listOfPrimes)) for numerator in range(3, 200000, 2): for x in range(2, numerator): if numerator % x != 0: for y in range(0, len(listOfPrimes)): if numerator % listOfPrimes[y] == 0: t += 1 if t == 0: listOfPrimes.append(numerator) break else: t = 0 print(len(listOfPrimes)) if len(listOfPrimes) == 10001: break print("prime no 10001: ", listOfPrimes[10000])
I'm going to rearrange is order of the for-loops to increase the speed of calculation. This didn't make it faster... Now I'm going to try to see if I can limit the modulus checks even more. I've read something somewhere about prime numbers are not dividable by 2 (except the prime number 2) or by any prime numbers that are smaller than the square of the number the is being checked. Wow, I've made a huge miss. I though 10001 was one hundred thousand and one instead of ten thousand and one. Well well, here is my code for finding the 10001th prime:
t = 0listOfPrimes = [2] for numerator in range(3, 200000, 2): if numerator % 2 != 0: for x in range(0, len(listOfPrimes)): if numerator % listOfPrimes[x] == 0: t += 1 if t == 0: listOfPrimes.append(numerator) else: t = 0 print(len(listOfPrimes), numerator) if len(listOfPrimes) == 10001: break print("10001th prime: ", listOfPrimes[10000])
I'd like to have a feature in PyCharm that shows the time it took to run an algorithm.
/Ludvig