How to print prime numbers in Python

Learn to Code Today!

Learn 10x faster: coding, no-code and data skills. Join millions of users mastering new tech skills and accelerating their career with Enki.
Get started

Prime numbers are natural numbers greater than 1 that are only divisible by 1 and themselves. They cannot be formed by multiplying two smaller natural numbers. This property makes primes fundamental in mathematics. Unlike composite numbers, which have more than two factors, each prime number has only two factors source.

At Enki, we provide a diverse learning platform where learners can explore more about prime numbers and other mathematical concepts using interactive coding exercises.

What is a Prime Number

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For instance, the numbers 2, 3, 5, 7, 11, and 13 are all prime numbers. It's worth noting that 2 is unique as the only even prime number. Composite numbers, on the other hand, are natural numbers greater than 1 that have more than two factors. For example, the number 6 is composite because it can be divided evenly by 1, 2, 3, and 6.

The fundamental theorem of arithmetic states that every integer greater than 1 is either a prime or can be uniquely factored into primes. This theorem highlights the building-block nature of prime numbers. Primes are extensively used in fields like cryptography, where they ensure secure digital communications. Prime factorization, or breaking down a composite number into a product of prime numbers, stands as a critical concept in these applications.

Using a Double Nested Loop to Print All the Prime Numbers

To find prime numbers in a certain range, one can use a simple double nested loop. The outer loop iterates over each number in the range, while the inner loop checks if the number can be divided by any number other than 1 and itself without leaving a remainder.

We're going to walk through a step-by-step process:

  • First, set the range for the numbers you want to test for primality.
  • For each number in this range, assume it is a prime until proven otherwise.
  • Check the divisibility of the number by every integer between 2 and the square root of the number. If it divides evenly by any of these, it's not prime.
  • When no divisors are found, the number is a prime number, and you can print it.

In this code, print_primes_using_loops takes two parameters: start and end, defining the range within which we search for prime numbers. The if num > 1 check ensures that only numbers greater than 1 are considered. We then assume every number is prime (setting is_prime to True) until shown otherwise. The internal loop verifies divisibility, and if a factor is found, it sets is_prime to False.

Finding Prime Numbers with the Sieve of Eratosthenes

The Sieve of Eratosthenes is an efficient algorithm for finding all prime numbers up to a certain integer. This method eliminates composite numbers by iteratively marking the multiples of each prime starting from 2.

  1. Create a list (or array) of consecutive integers from 2 up to your target number n.
  2. Start with the first number in the list (p=2).
  3. Mark all multiples of p as non-prime.
  4. Find the next number in the list that is not marked and set it as the new p.
  5. Repeat steps 3 and 4 until p^2 is greater than n.
  6. The numbers that remain unmarked in the list are all prime numbers.

In this implementation, the sieve_of_eratosthenes function creates an array, prime, where each index represents whether the number is potentially prime. Multiples of each prime are marked as False, leaving only the prime numbers marked as True. The numbers that are still marked True are printed as they are prime.

The Sieve of Eratosthenes is highly efficient for larger ranges compared to the double nested loop approach. Its time complexity is O(n log log n), making it optimal for tasks requiring the generation of prime numbers up to a large size.

Wrapping Up

Prime numbers are pivotal in mathematics, serving as fundamental elements in number theory and encryption technologies. At Enki, we encourage you to explore more tutorials and enhance your coding skills with our interactive platform. Here, you can embark on a journey to master Python and other exciting technologies.

About Enki

  • Fully personalized online up-skilling
  • Unlimited AI coaching
  • Designed by Silicon Valley experts

More articles

Meet your AI-enabled coach

Professional athletes have a coach for every aspect of their performance. Why can’t you for your work? Enki’s AI-powered coaching on-demand - combined with state of the art, structured learning content - makes this a reality.
1
1:1 AI Coaching
How do I remove duplicate emails?
Convert the list to a set and back to a list. Sets automatically remove duplicates.
2
Personalized Exercises
3
Interactive practice

Unlock full access to all skills on Enki with a 7-day free trial

Get started