VBA help - Count Prime Numbers

pdhil87

New Member
Joined
Aug 27, 2018
Messages
8
I have created a function PRIME(n) with Boolean output to check if a number is prime or not
I am now looking to create a function that counts the prime numbers between two numbers n1 and n2
Help much appreciated!
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Welcome to the MrExcel board!

Try
Code:
Function CountPrimes(n1 As Long, n2 As Long) As Long
  Dim i As Long
  
  For i = n1 To n2
    CountPrimes = CountPrimes - Prime(i)
  Next i
End Function
 
Upvote 0
The following function (no other functions are required) will return the count for the number of primes between any two positive numbers less than 20000000 (the function returns -1 if you submit invalid values)...
Code:
[table="width: 500"]
[tr]
	[td]Function PrimeCountBetween(FromNumber As Long, ToNumber As Long) As Long
  Dim X As Long, NthPrime As Long, Test As Long, Sum As Long, Results As Variant
  Const MaxPrime As Long = 20000003
  Static Initialized As Boolean, PrimesFlag() As Byte
  If FromNumber > 0 And FromNumber < ToNumber And ToNumber < 20000001 Then
    If Not Initialized Then
      Initialized = True
      PrimesFlag = ChrW(1) & ChrW(257) & String(10000000, ChrW(256))
      Test = 3
      Do While Test <= MaxPrime
        For X = 2 * Test To MaxPrime Step Test
          PrimesFlag(X) = 0
        Next
        Test = Test + 1 - (Test > 2)
      Loop
    End If
    For X = FromNumber To ToNumber
      If PrimesFlag(X) = 1 Then PrimeCountBetween = PrimeCountBetween + 1
    Next
  Else
    PrimeCountBetween = -1
  End If
End Function[/td]
[/tr]
[/table]
Note: The first time the function is used in a session of Excel, there will be a slight delay of a few seconds, after that, the count is returned in about a second or less (at least that is the timing on my computer).
 
Last edited:
Upvote 0
Hi Rick, can you explain me your code ? I can not understand how you find prime numbers
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top