Нахождение n-го простого числа с использованием Python
Когда я запускаю этот код, даже для простого подсчета до 10-го простого числа (вместо 1000) я получаю перекос / измученный вывод - все «не простые» заголовки для моей переменной is_composite, мой test_num дает мне простые и составные числа, и мой prime_count выключен
Некоторые ответы, которые разработчики разделили, используют функции и математический импорт - это то, что мы еще не рассмотрели. Я не пытаюсь получить наиболее эффективный ответ; Я просто пытаюсь написать работающий код на Python, чтобы понять основы циклов.
# test a prime by diving number by previous sequence of number(s) (% == 0). Do this by
# counting up from 1 all the way to 1000.
test_num = 2 #these are the numbers that are being tested for primality
is_composite = 'not prime' # will be counted by prime_count
prime_count = 0 #count the number of primes
while (prime_count<10): #counts number primes and make sures that loop stops after the 1000th prime (here: I am just running it to the tenth for quick testing)
test_num = test_num + 1 # starts with two, tested for primality and counted if so
x = test_num - 1 #denominator for prime equation
while (x>2):
if test_num%(x) == 0:
is_composite = 'not prime'
else:
prime_count = prime_count + 1
x = x - 1
print is_composite
print test_num
print prime_count