How to Generate All Prime Numbers Between Two Given Numbers in Excel?
Last Updated :
01 Dec, 2022
A number greater than 1 with no positive divisors besides 1 and the number itself is referred to as a prime number. In this article, we will be discussing two approaches for generating all prime numbers between two given numbers in excel. In the first approach, we are creating formulas for generating prime numbers between two numbers, and in the second approach, we are creating a new user-defined function that is simple and easy in comparison to the first approach.
Approach 1: Using Formulas
The Define Name function and formulas can be used to list or generate every prime number between two certain values.
Step 1: Follow, the below steps for creating the first range name
- Click on the Formulas tab
- Click on Name Manager
- Once we click on Name Manager, a new pop-up will be opened. Now, Click New Button.
Step 2: In the New Name popup window, Enter “range” as the range name in the “Name” text field. Now, Enter this formula:
=ROW(INDIRECT(Sheet1!$B$1&”:”&Sheet1!$B$2))
Sheet1 is the name of the worksheet we are now using, and B1 and B2 are the start and finish numbers we selected in the Refers to the text box.
Press the “OK” button and close the pop window.
Step 3: Now, Again click New Button for creating another range name. Enter “prime” as a name in Name Field in the New Name pop window. Enter this below formula in refers to the field.
=SMALL(IF(MMULT(–(IF(range>TRANSPOSE(range-Sheet1!$B$1+2),MOD(range,(range>TRANSPOSE(range-Sheet1!$B$1+2))*TRANSPOSE(range-Sheet1!$B$1+2)))=0), range-Sheet1!$B$1+2)=0,range),ROW(INDIRECT(“1:”&Sheet1!$B$2)))
range variable is created in step 2.
Click the “OK” button and close the pop window.
Step 4: Now, Write any two numbers or ranges (B1 and B2 Column) in which range we want to print or list prime numbers as start and end numbers. We need to select any column in which we want to list or print prime numbers and we need to enter this formula in the formula bar:
=IFERROR(prime,””)
prime is a function that is created in step 3. Once we entered the formula in the formula bar and then press CTRL + SHIFT + ENTER keys together, all prime numbers will be listed in the selected column between the mentioned range.
Approach 2: Using User-Defined Functions
The first method is a little bit difficult, so, in this approach, we will apply a user-defined function for generating a prime number between two numbers.
Step 1: Click the ALT + F11 keys together for opening the Microsoft Visual Basic for Applications window.
Step 2: First, Click on Insert Button and then click the Module button.
Step 3: Once we clicked on the Module button, a new dialog box will open. Just copy and paste this below code to open the dialog box.
Function PRIME(Start_val, End_val As Long)
‘Updateby Extendoffice 20160613
Dim val As String
For i = Start_val To End_val
For j = 2 To i – 1
If i Mod j = 0 Then GoTo 20:
Next j
val = val & i & “,”
20:
Next i
PRIME = val
End Function
Screenshot:
Step 4: Press CTRL + S to save this file and the dialog box will be closed, go back to our worksheet. Now, Select any cell and enter this below formula with two numbers as parameters such as =prime(10, 50) where 10 is starting number and 50 is the ending number. Press ENTER Key and prime numbers will be displayed to our selected cell.

Similar Reads
How to Check if the Number is Prime Number in Excel?
A prime number is a number that is greater than 1 and has no positive divisor other than 1 and the number itself. Approach: To check whether the number is prime or not we have to divide the number with all the numbers between 2 and the square root of that number. And if it gives remainder 0 in any o
2 min read
How to Determine if a Number is Prime in Ruby?
In this article, we will discuss how to Determine if a Number is Prime and contains a specific value in ruby. We can determine if a Number is Prime through different methods. Let us study them in detail Table of Content Determine if a Number is Prime using Prime Library ApproachDetermine if a Number
3 min read
JavaScript Program to Display Prime Numbers Between Two Intervals Using Functions
Prime numbers are natural numbers greater than 1 that are only divisible by 1 and themselves. In this article, we will explore how to create a JavaScript program to display all prime numbers between two given intervals using functions. Approach 1: Using Basic Loop and FunctionThe most straightforwar
2 min read
Find Sum of numbers in given Range when numbers are modified as given
Find the sum of all the numbers between the range l and r. Here each number is represented by the sum of its distinct prime factors. Examples: Input: l = 1, r = 6Output: 17Explanation: For 1, sum of prime factors = 0For 2, Sum of Prime factors = 2For 3, Sum of Prime factors = 3For 4, Sum of Prime fa
7 min read
How to Find Prime Number in Golang?
Prime numbers are a fascinating concept in the field of mathematics and computer science. They are numbers that have only two distinct positive divisors: 1 and the number itself. This article will guide you through the process of finding prime numbers in Golang, a statically typed, compiled language
6 min read
JavaScript Program to Check Prime Number By Creating a Function
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In this article, we will explore how to check if a given number is a prime number in JavaScript by creating a function. Table of Content Check Prime Number using for LoopCheck Prime Number using
2 min read
What are the methods to find prime numbers?
Answer: To find prime numbers, you can use methods like the Sieve of Eratosthenes, trial division, prime factorization, or online tools for quick identification.Here's a more detailed explanation: Sieve of Eratosthenes:Start with a list of numbers up to a certain limit.Mark 2 as prime and eliminate
2 min read
Count of numbers between L and R that are coprime with P
Find the count of numbers between L and R and are coprime with P. Note: Two numbers X and Y are coprime with each other if their GCD is 1. Examples: Input: L = 1, R = 10, P = 16Output: 5Explanation: The numbers coprime with P are 1, 3, 5, 7, 9 Input: L = 10, R = 20, P = 15Output: 6 Approach: The pro
5 min read
How to Teach Prime Numbers to Kids
Prime Numbers are those numbers that have only two factors: 1 and itself. A prime number is completely divisible by 1 and itself leaving zero remainder. Real-life applications of prime numbers are spread in various fields such as cryptography, computer science, and number theory. Prime numbers help
7 min read
How to Print Prime Numbers in MS SQL Server?
In this article, we are going to print Prime numbers using MS SQL. Here we will be using 2 while loops statement for printing prime numbers. Steps 1: First we will DECLARE a variable I with initial value 2. Query: DECLARE @I INT=2Step 2: Then we will DECLARE a variable PRIME with an initial value of
3 min read