« A Simple Macro: Fibonacci Numbers | Main | Cell Counting 1: COUNTIF »
September 07, 2004
Macros: Prime numbers
Here is another few simple macros - written in OpenOffice Basic. First we have a primality test ...
Function IsPrime(Val As Integer) As Boolean
Dim I As Integer
IsPrime = FALSE
For I = 2 To Val - 1
If Val MOD I = 0 Then
Exit Function
End If
Next I
IsPrime = TRUE
End Function
The second macro finds the next highest prime.
Function NextHighestPrime(Val As Integer) As Integer
Dim I As Integer, PrimeFound As Boolean
I = Val
NextHighestPrime = Val
PrimeFound = FALSE
Do While PrimeFound = FALSE
I = I + 1
If IsPrime(I) = TRUE Then
PrimeFound = TRUE
NextHighestPrime = I
End If
Loop
End Function

Posted by Dave at September 7, 2004 05:28 AM
Comments
Consider Newton's improvment
For I = 2 To sqrt(Val)
this will be faster
Posted by: misan at March 2, 2005 01:29 PM
Soryy everbody,
I've just started to use openoffice and macros.
But Ican't save my macros. I can save my cal. files but macros aren't saved and they disappear.
How to save them.
Thanks
Posted by: omer ozkan at March 16, 2005 02:30 PM