Dcoder icon

Learning Prime numbers with Loop/Recursion @ Dcoder

By Lochard | As a newbie programmer | 20 Jul 2023


Problem: Raja Beta is bad at maths, his teacher always complaints about him.Help him in his prime numbers homework.

Input: Two space separated positive integer m and n

Output: Print all prime numbers p such that m <= p <= n, one number per line

Constraints: 1 <= m <= n <= 1000000000

Sample Input: 1 10

Sample Output: 

2

3

5

7

m, n = map(int, input().split())
if m < 3:
  print(2)
for i in range(max(m, 3), n+1, 2):
  for j in range(3, int(i**.5)+1):
    if i % j == 0:
      break
  else:
    print(i)

How do you rate this article?

3


Lochard
Lochard

20240228 Arrived in the UK for about 2 week. All my fears persist. Some are even getting worse. https://github.com/locharp/asylum_diary/


As a newbie programmer
As a newbie programmer

Sharing entry level codes. My snippets on GitHub https://github.com/locharp/code-snippets

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.