In python How do I print prime numbers from 1 to 1000, in group of 10's? like 2,3,5,7,11,13,17,19,23,29, then another 10 prime number ?
Answers
Answered by
1
Here comes the solution using Python -
def isPrime(n):
c=0
for i in range(1,n+1):
if n%i==0:
c+=1
return c==2
counter=0
for i in range(1,1001):
if isPrime(i):
print(i,end=' ')
counter+=1
if counter>9:
counter=0
print()
- The isPrime() function checks whether a number is prime or not.
- Then, we have initialised counter = 0
- Here the loop iterates in the range 1 to 1000. If number is prime, the number is displayed and the value of counter is incremented.
- When counter becomes greater than 9, we are confirmed that prime numbers are displayed in group of 10's. Now, a new line is added so as to display the next 10 prime numbers in another line and the counter is initialised to 0. This goes on and hence the problem is solved.
- See the attachment.
Attachments:
![](https://hi-static.z-dn.net/files/d9d/9b796a2e06d72da131079349798a09a0.jpg)
![](https://hi-static.z-dn.net/files/dc7/076c1f2aca59179c50c64841d96efc86.jpg)
Similar questions