Prime factors

Dec 13, 2015 12:08

Here's a routine to finnd the prime factors of a number:

Function PrimeFact(n)
'REM Returns the prime factors of n. Also gives a message
'of how many factors there are in case you need to reselect
'the output range.
Dim OutMat(1,int(sqr(n)+1))
Dim d as double, c as integer, i as integer
c=1
'Test if multiple of 2
PF50:    d=n-2*int(n/2)
if d=0 then
    OutMat(1,c)=2
    c=c+1
    n=n/2
    goto PF50
end if
'Test if multiple of 3
PF100: d=n-3*Int(n/3)
if d=0 then
    OutMat(1,c)=3
    c=c+1
    n=n/3
    goto PF100
end if
'Test for divisors 6i-1 and 6i+1 up to sqr(n)
'Prime numbers are of the form 6i-1 or 6i+1
For i=6 to sqr(n)+1 step 6
PF150:    d=n-(i-1)*int(n/(i-1))
    if d=0 then
        OutMat(1,c)=i-1
        c=c+1
        n=n/(i-1)
        goto PF150
    end if  
PF200: d=n-(i+1)*int(n/(i+1))
    if d=0 then
        OutMat(1,c)=i+1
        c=c+1
        n=n/(i+1)
        goto PF200
    end if
next i
if n>1 then
        OutMat(1,c)=n
        c=c+1
end if
msgbox str(c-1)&" factors were found"
PrimeFact=OutMat
End Function

Many programmers will tell you that it's "inelegant" to use GoTo statments. It's sort of a moot point anymore. Back when your computer had 256K memory, you had to be very careful about how big your programs were, and speed was also an issue. With today's computers, speed isn't that much of an issue and memory is measured in millions and billions of bytes, So, frankly, I just want programs to work; therefore, I'm a rather inelegant programmer. I'm sure any of these routines can be improved, but I've been making OpenOffice do plenty that regulars on the forum were telling people could not or should not be done with a spreadsheet.......oooooh, I feel pretty good about my programming.

There are two big reasons I use GoTo statements. First, I don't have to strain my brain to figure out all the "elegant" looping structures. Second, following the tenant of programmers from the earliest history of programming, I don't "reinvent the wheel." If there's a program out there that can do the job, I have no problem with rewriting it to work on my spreadsheet. Much of the early Fortran and Basic programs were repleat with Goto statements.

The structure of Goto statements in OpenOffice is Goto linelabel and, at the place you want the programming to go to, you start the line with the line label. You don't have to follow the line label with anything (in some older languages, you did and the default was the Continue statement). Best I can tell, OpenOffice doesn't even have a Continue statement.

Note that in OpenOffice Basic, you can name your line labels anything but, on the line that they label, you have to end them with a colon.

With most languages today, there are a lot of statements to jump in and out of loops, got straight to the next cycle of a loop, and so forth. There isn't a lot of that in OpenOffice Basic, so the Goto statement comes in handy.

msgbox str(c-1)&" factors were found"

If you want your program to talk to you, there is the message box. It will display just about any result you want in a dialog box that you can then close so the program can get on with it's business. MSGBOX doesn't equal anything. You just type it and a space and follow it with what ever you want to display. I included it in this program so it could tell me how many prime factors it found; thereby saving  me the trouble of counting them. I can be abominably lazy sometimes.

Note that you can string strings together using the & operator. The STR function takes a number and turns it into a text string. A similar function, FORMAT, lets you change the way the resulting string appears. With STR, if the number is positive, it throws a blank onto the front of the string, whether you ant it or not. With FORMAT, you have to include instructions about how you want the result to look (and that can be involved - especially in tarcking down the right code), but you can be flexible. I didn't care about the leading blank here so I took the easy way. If the program finds seven factors, the message would be "7 factors were found."

I have several personal conventions. OutMat is one of them. Many of my routines gather results that will be displayed on the spreadsheet as a matrix. It's easy if you just load them all into a matrix called "OutMat" and make the last line in the program (name of routine)=OutMat. Arrays (AKA "matrices") in OpenOffice Basic can contain any kind of variable and can be mixed.I will often output arrays that have values and strings to tell what those values are.

Here, I dimension OutMat to be a row of numbers.

Dim OutMat(1,int(sqr(n)+1))

The first number in the parentheses is the number of rows, the second is the number of columns. I used int(sqr(n)+1) (which means the integer portion of one more than the square root of n) because there will be no more than that many prome factors in n.

display, openoffice basic, goto, outmat.output, prime factors

Previous post Next post
Up