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
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