[Solved] Finding if a number is prime using Python [duplicate]


Your problem is that you never eliminate the last number in numbers. If range_number is 21, then len(numbers) is 20 and len(numbers)-1 is 19. So this line here:

for n in range(i + numbers[i], len(numbers)-1, numbers[i]):

Never actually removes the number 20 from the list. You could have seen this if you’d printed out the list. So currently your solution gives the correct answer if range_number is one more than a prime number, but is off by range_number-1 when range_number is one more than a composite.

To fix that problem, simply change the line to be:

for n in range(i + numbers[i], len(numbers), numbers[i]):

1

solved Finding if a number is prime using Python [duplicate]