A Prime Number is a number that can only be divided by itself and 1 without remainders (e.g., 2, 3, 5, 7, 11).
This article covers a Python programs that prints alternate prime numbers up to a given number n. We have discussed the various methods, each with an example and detailed explanation.
To solve this problem you need to know how to use the below concepts.
- for loop, range() function and while loop
- if-else condition
- Math module
Table of contents
1. Using a List to Store All Prime Numbers
This method in Python checks if the number is prime, stores it in a list, and then prints alternate elements from that list.
Also, See:
- Python Programs to Check if Number is Prime Number
- Python Programs to Find Prime Numbers within a Range
Code Example
Output:
All prime numbers from 1 to 20: [2, 3, 5, 7, 11, 13, 17, 19] Alternate prime numbers from 1 to 20: 2 5 11 17
Explanation
1. is_prime(num) function:
- This function checks if a number is prime or not.
- To check if a number is prime, we need to see if it has any divisors other than 1 and itself.
- Instead of checking all numbers up to
num, we only check up to its square root (√num). - If a number is not prime, it must have at least one factor smaller than or equal to its square root.
- The expression
num ** 0.5is used to calculate the square root ofnumin Python (√n =n^0.5).
For example:
Checkingis_prime(15)
Is15less than2? → No.
Find square root of15→√15 ≈ 3.8, so we check2, 3.
Check if15is divisible by2→15 % 2 != 0(not divisible).
Check if15is divisible by3→15 % 3 == 0→ so15is not a prime number
2. Use for loop to check all numbers from 2 to n
- If
is_prime(num)isTrue, add it to theprimeslist. - Now use the range() function to print numbers.
range(0, len(primes), 2)is used to print alternate prime numbers fromprimeslist using steps through the list in increments of 2 (i.e., it skips one prime number after printing one).
2. Using a Counter for Alternate Primes
This method in Python avoids storing all primes in a list and directly prints alternate primes by using a counter to track every second prime number.
Code Example
Explanation
n = 20, meaning we want to find and print prime numbers up to 20.- We introduce a
countervariable that increments every time a prime number is found. - Only when the
counteris even the prime number is printed (ifcount % 2 == 0). This ensures that every second prime number is skipped (i.e., we print alternate primes). - The
counteris incremented by 1 after each prime. - The
is_prime()function remains the same as the previous method for checking if the number is prime.
3. Using Generators for Efficient Prime Generation
This method in Python uses a generator to yield prime numbers and prints alternate primes using the generator.
A generator in Python is a special type of iterator used to produce a sequence of values lazily, one at a time, as needed. Unlike regular functions that return a single value and exit, generators use the yield keyword to pause and resume execution, allowing them to generate values on demand without storing the entire sequence in memory. This makes them memory-efficient for handling large datasets or infinite sequences.
Code Example
Explanation
prime_generator(n) function:
- It takes an integer n as input.
- A for loop iterates through each number from 2 to n.
- For each number, it calls the
is_prime(num)function to check if it is prime. - If the number is prime, the yield statement produces the prime number without exiting the function. This makes the function a generator, which allows us to iterate over the prime numbers one at a time.
- For printing the alternate prime number, we are maintaining the
countervariable, the logic is the same as discussed previously.

Leave a Reply