Home python Find prime numbers in a range

Find prime numbers in a range

Author

Date

Category

Write a program that searches among the integers in the numeric segment [2422000; 2422080], prime numbers. Print all found prime numbers in ascending order, to the left of each number print its number in order.

my way gives infinite program execution

for x in range (2422000, 2422080):
  a = []
  for n in range (1, x):
    if x% n == 0 and (x == n or n == 1):
      a.append (n)
  print (a)

Answer 1

In theory, something like this

a = []
for x in range (2422000, 2422080):
  d = 2
  while x% d! = 0:
    d + = 1
  if d == x:
    a.append (x)
print (a)

Answer 2

Try this:

import math
def is_prime (i):
  m = min (i, int (math.sqrt (b)))
  l = range (2, m)
  r = map (lambda x: i% x == 0, l)
  return not any (r)
a = 2422000
b = 2422080
ls = range (a, b)
_list = list (filter (is_prime, ls))
print (* [f '{i + 1}: {v}' for i, v in enumerate (_list)], sep = '\ n')

Answer 3

By “number in order” do I mean the number in the list (2422000, 2422080), or the number of the number in the list of prime numbers? If the first option, then the solution is:

for i in range (2422000, 2422081):
  for j in range (2, i // 2):
    if i% j == 0: break
  else: print (i - 2421999, i)

Answer 4

Try it through a list assembly, in the first list put the desired interval in range (2, 1001) …, numbers up to 1000 are set for an example,
here:

print ([x for x in range (2, 1001) if not [n for n in range (2, x) if not x% n]])

or

print ([x for x in range (2, 1001) if all (x% t for t in range ( 2, int (math.sqrt (x)) + 1))])

Answer 5

for i in range (2422000, 2422081):
deli = []
for a in range (2, i + 1):
  if i% a == 0:
    deli.append (a)
    if len (deli) & gt; 1:
      break
if len (deli) == 1:
  print (deli)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions