Archive for the ‘OpenOffice Basic’ Category

Macros: Prime numbers

Tuesday, September 7th, 2004

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

prime.jpg

A Simple Macro: Fibonacci Numbers

Monday, September 6th, 2004

When learning a new (computer) language - it is important to have a good mastery of the basics. Developing simple functions like this is what is called for here.

Function Fibonacci(Val As Integer) As Long
Dim I As Integer
Fibonacci = 1
For I = 1 To Val
Fibonacci = Fibonacci * I
Next I
End Function

We can now call our newly created function from the spreadsheet - just like the builtin OOo Calc functions.

fibonacci.jpg