0

So I'm trying to find the 10,001 prime number. Yes, its the euler #7 problem. The code i wrote appears to give me all the prime numbers from 3 to 10,001 but my answer is still incorrect. I know there are other questions about this that have been answered but stealing someone else code does not help me learn. So I'm looking for insight into where i went wrong with this. First I seperated out all the odd numbers and added them to a list. I noticed that there were squares of some of the prime numbers in the list so I check the list against the squares of every number from 2 to 10,0001. That should have left me with nothing but prime numbers but I am still getting the wrong answer. Any ideas would be great thank you

prime = [i for i in range(2, 10002) if i % 2 != 0]

for i in range(2, 10002):
    if i * i in prime:
        prime.remove(i * i)

print(prime[-1])
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • 4
    15 is not a square number. – Ignacio Vazquez-Abrams Nov 28 '17 at 02:34
  • 1
    Are you trying to find the 10001st prime number, or the largest prime less than 10001? They're different issues. – Daniel H Nov 28 '17 at 02:46
  • im trying to fint the 10001st prime number. – fallenangel226 Nov 28 '17 at 02:57
  • Have you had a look at it after generating ´prime´? Because it is not, what you think it is. Also: The fifth prime number is >5. – Mr. T Nov 28 '17 at 03:05
  • The first line takes all odd numbers. The for loop removes squares of an odd number. Not everything that's left are prime. – alvits Nov 28 '17 at 03:06
  • @Piinthesky I'm not sure i understand what you mean. after initially generating prime it should be a list of all odd numbers which is what is looks like. Am I wrong? What do you mean by the 5th prime is > 5? the 5th prime is 11 but since i excluded 2 in my code its 13. Can you please elaborate? – fallenangel226 Nov 28 '17 at 03:12
  • 1) You won't find the 10001th prime number with `range(2, 10002)`. 2) Your list is not a prime number list. 3) You can't exclude `2` from your list - `13` is the sixth prime number. – Mr. T Nov 28 '17 at 03:19
  • 1
    @Piinthesky - wow ok that just clicked. so your telling me that because my range is set to 10002 the list doesnt go high enough to reach the 10001st prime, correct? – fallenangel226 Nov 28 '17 at 03:38

1 Answers1

2

Have you tried the case of 7*11=77? You're only looking in perfect squares when you should search multiples of all the known primes. In keeping with the theme of Project Euler, I'm not gonna give you the answer, but I_ can_ point you to the Sieve of Eratosthenes.

well actually... Since it's such an early problem, I'll dangle a spoiler.

Make a list of primes, initialize it to [2,3,5]. Starting from n=8 and increasing by six each time, check n-1 and n+1 for primality by testing modulos for each known primes from 2 to the square root of your number.

Jakob Lovern
  • 1,301
  • 7
  • 24