Is it?

Dec 16, 2015 14:36

You can modify existing routines to do different jobs. The last program found all the prime factors in a number. This one tells you if a number is a prime.

Function IsPrime(n)
'REM Tests if a number (n) is a prime number.
'If it is, returns the Boolean value, True; otherwise, False.
Dim d as integer, i as integer, v as Boolean
v=True
'Test if multiple of 2
d=n-2*int(n/2)
if d=0 then
    goto IsPrime100
end if
'Test if multiple of 3
d=n-3*int(n/3)
if d=0 then
    goto IsPrime100
end if
'Test if multiple of 6i-1 and 6i+1 to sqr(n)+1
for i=6 to int(sqr(n))+1 step 6
    d=n-(i-1)*int(n/(i-1))
    if d=0 then
        goto IsPrime100
    end if
    d=n-(i+1)*int(n/(i+1))
    if d=0 then
        goto IsPrime100
    end if
next i  
IsPrime=v
Exit Function
IsPrime100: v=False
IsPrime=v
End Function

I write code like this t use as tools for other projects. There are a few "Is" functions in LibreOffice Basic but there are many times that I want to test a value for some characteristic or another (such as "Is it prime") and these macro languages are quite lacking in this respect, and LibreOffice and OpenOffice lacks even in comparison to Microsoft Office, so I have to plug the holes.

The main difference between this and the other program is that this one doesn't collect prime factors and all it returns is "True" or "False".

One of the aggravating things about these macro language is that the "True" string that a routine might reurn and the TRUE Boolean value that a spreadsheet function recognizes are not the same thing, so you still have to process the Boolean values returned by homegrown programs.

libreoffice basic, modfying programs, boolean values

Previous post Next post
Up