Need help in understanding a vba function

Ria_Ko

New Member
Joined
Mar 18, 2020
Messages
43
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hello, hope everyone is doing well.
I had to create a function called "prime", which is used to return True if the number is a prime number followed by a function called "countprime" that counts the number of prime numbers between the integers n1 and n2 (inclusive).
I am new to VBA and i am not being able to understand the "countprime" function logically. If anybody could explain it to me it would be of great help. Thankyou.

Function prime(n As Integer) As Boolean
Dim i As Integer
prime = True
If n = 1 Then
prime = False
ElseIf n > 2 Then
For i = 2 To n - 1
If n Mod i = 0 Then
prime = False
Exit Function
End If
Next i
End If
End Function

Function countprime(n1 As Integer, n2 As Integer) As Integer
Dim i As Integer
For i = n1 To n2
countprime = countprime - prime(i)
Next i
End Function
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
The prime function returns true or false, in VBA true equals -1 & false is 0, so -prime(i) is effectively either -0 or - -1 (ie +1)
HTH
 
Upvote 0
A few lines to help you visualise the numbers
Debug.Print writes to the Immediate Window
(In VBA editor) Display Immediate Window with {CTRL} g

I am not sure that 0 is a Prime Number but your function says it is!

This is the result of running Test with values 0 and 10

OLDcount 0
PLUS 1 0 True
NEWcount 1

OLDcount 1
PLUS 0 1 False
NEWcount 1

OLDcount 1
PLUS 1 2 True
NEWcount 2

OLDcount 2
PLUS 1 3 True
NEWcount 3

OLDcount 3
PLUS 0 4 False
NEWcount 3

OLDcount 3
PLUS 1 5 True
NEWcount 4

OLDcount 4
PLUS 0 6 False
NEWcount 4

OLDcount 4
PLUS 1 7 True
NEWcount 5

OLDcount 5
PLUS 0 8 False
NEWcount 5

OLDcount 5
PLUS 0 9 False
NEWcount 5

OLDcount 5
PLUS 0 10 False
NEWcount 5

Result 5


Rich (BB code):
Sub Test()
    Debug.Print "RESULT", countprime(0, 10)
End Sub

Rich (BB code):
Function prime(n As Integer) As Boolean
    Dim i As Integer
    prime = True
    If n = 1 Then
        prime = False
    ElseIf n > 2 Then
        For i = 2 To n - 1
            If n Mod i = 0 Then
                prime = False
                Exit Function
            End If
        Next i
    End If
End Function
Function countprime(n1 As Integer, n2 As Integer) As Integer
    Dim i As Integer, B As Long
    For i = n1 To n2
        If prime(i) Then B = 1 Else B = 0
Debug.Print "OLDcount", countprime
Debug.Print "PLUS", B, i, prime(i)
        countprime = countprime - prime(i)
Debug.Print "NEWcount", countprime
Debug.Print
    Next i
End Function
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,378
Members
448,955
Latest member
BatCoder

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