- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to generate prime numbers using Python?
A prime number is the one that is not divisible by any other number except 1 and itself.
In Python % modulo operator is available to test if a number is divisible by other.
Assuming we have to find prime numbers between 1 to 100, each number (let us say x) in the range needs to be successively checked for divisibility by 2 to x-1. This is achieved by employing two nested loops.
for x in range(1,101): for y in range(2,x): if x%y==0:break else: print (x,sep=' ', end=' ')
Above code generates prime numbers between 1-100
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
- Related Articles
- How to generate prime twins using Python?
- How to generate prime numbers using lambda expression in Java?
- How to generate pyramid of numbers using Python?
- How to Print all Prime Numbers in an Interval using Python?
- How to generate armstrong numbers in Python?
- C++ Program to Generate Prime Numbers Between a Given Range Using the Sieve of Sundaram
- How does Python generate random numbers?
- How to generate XML using Python?
- How to use Python Numpy to generate Random Numbers?
- How to generate non-repeating random numbers in Python?
- Generate Secure Random Numbers for Managing Secrets using Python
- How to generate JSON output using Python?
- How to generate statistical graphs using Python?
- C++ Program to Implement Wheel Sieve to Generate Prime Numbers Between Given Range