Blog
Zoom
Destructosoft
Projects
Zap War
Links

Blog
Subscribe: Add to Google Add to My Yahoo! Subscribe in NewsGator Online Add to My AOL


Mon, 19 May 2008
It Took Me A Week...

...but I finally finished my prime number factorization program.

First, a little background. I bought a used 80486 computer in 1998, when Pentium 3s were new. My previous experience had been with TRS-80 Basic and Applesoft BASIC, but I had some knowledge of IBM PASCAL and QBASIC off of school and (later) work computers.

But 1998 was six years since I had access to a work computer, and I didn't know about Visual BASIC or anything like that. However, I was shrewd enough to have made a copy of QBASIC just in case I ever got a 486. And now I did.

Unfortunately, the new printer I bought for the old computer didn't work with it. Apparently Windows 3.1 had corrupted drivers. I ended up usually running QBASIC from a modified AUTOEXEC.BAT (starting program on boot) or from a disk (booting to disk was possible in those days).

So it wasn't until YEARS later that I made a program to print out prime numbers in a very small font (Lucinda Console size 4) and in columns.

And because of the difficulty of the learning curve for Visual Basic .NET, I was forced to backtrack and use QBASIC.
----

Now for the current project.

Listing all the prime numbers in columns is easier than it looks, but listing all positive integers followed by a list of their prime factors is much, much harder, because the columns have to line up and the string array cannot exceed 32k, so the whole page has to be calculated twice-- once for the top half and once for the bottom half.

But it's done. Finally.

I also had to deal with making sure it stopped after 20 pages, keeping it confined to 209 columns and 166 rows, allowing a print to screen feature, listing exponents on a seperate line instead of just going 3x3x3x3x3, using a middot instead of "x" for multiplication, and many more things.
----

I notice this sort of thing just isn't marketable. If I look around on the internet, someone's already got free programs to test primality or show factors of numbers. Some of these are Java or Flash or even web-based PHP. Nobody these days uses QBASIC or wants to put up with its limitations (low memory, no standalone windows or compilation, causes other programs to slow down).

But ironically, it is still the most functional thing for me to use. Once I worked out the bugs, I ran it and it saved a 20-page document that I could print out later with Notepad.

If I wanted to print it out any other way, I'd have to take a long, long time learning the programming language, then almost as long figuring out how it opens and writes to files, then have to figure out how to make it align columns.

Anyway, here is the original code:
'FACTOR2 (c) 2008 Zoom
'Puts all primes in columns to fit a 209x166 page
'using Lucinda Console font size 4.
'Set to collating 20 pages at a time
'Program does not print the pages by itself

DEFSTR A-E
DEFLNG F-Q
DEFINT R-Z
DIM d(83)'print data (half page)
DIM w(166)'width of each line (even if not printed)

SCREEN 0
WIDTH 80, 50
CLS
COLOR 7
PRINT FRE(-1)
PRINT "Print to file [y/n]"
DO
a = INKEY$
LOOP UNTIL UCASE$(a) = "Y" OR UCASE$(a) = "N"
z2 = (UCASE$(a) = "Y")'yes(-1)=print to file
INPUT "Enter the number to begin the next page :"; p
IF z2 THEN
x0 = 209'cols
y0 = 166'rows
z3 = 20'pages
b = CHR$(183)
OPEN "factors.txt" FOR OUTPUT AS 1
ELSE 'instead, do page limits for screen
x0 = 79
y0 = 50
z3 = 1
b = "ú"
END IF
z4 = 0'page#
p2 = p
r = 0'0=top of page, 83= middle of page

nxpg:
p = p2
z4 = z4 + 1
IF z4 > z3 THEN GOTO zz
r = -83

nxhf:
r = r + 83
IF r > 83 THEN GOTO nxpg
IF r = 83 THEN p2 = p
z5 = 0'next row to put data in
FOR t = 0 TO 165
w(t) = 0
IF t < 84 THEN d(t) = ""
NEXT t
IF p = 1 THEN
FOR t = 0 TO 2
w(t) = 8
NEXT t
z5 = 3
p2 = 4
IF r = 0 THEN
d(0) = "1=unit  "
d(1) = "2=prime "
d(2) = "3=prime "
END IF
END IF

calc:
e0 = ""'e0,e1=2-line data for number
e1 = ""
v = 0'flag- (-1)yes use 2 lines (0)no use 1 line
v0 = 0'flag - yes= factor already shown, put "ú"
before next factor
z0 = LEN(STR$(p2)) - 1
e1 = LTRIM$(RTRIM$(STR$(p2))) + "="
e0 = SPACE$(LEN(e1))
p0 = p2
s = 0

c0:
IF p0 MOD 2 = 0 THEN p0 = p0 / 2: s = s + 1: GOTO c0
IF s > 0 THEN
e0 = e0 + " "
e1 = e1 + "2"
IF s > 1 THEN
e2 = LTRIM$(RTRIM$(STR$(s)))
e0 = e0 + e2
e1 = e1 + SPACE$(LEN(e2))
v = -1
END IF
v0 = -1
END IF
FOR q = 3 TO INT(SQR(p0) + 1) STEP 2
s = 0

c1:
IF p0 MOD q = 0 THEN p0 = p0 / q: s = s + 1: GOTO c1
IF s > 0 THEN
IF v0 THEN
e0 = e0 + " "
e1 = e1 + b
END IF
e2 = LTRIM$(RTRIM$(STR$(q)))
e0 = e0 + SPACE$(LEN(e2))
e1 = e1 + e2
IF s > 1 THEN
e2 = LTRIM$(RTRIM$(STR$(s)))
e0 = e0 + e2
e1 = e1 + SPACE$(LEN(e2))
v = -1
END IF
v0 = -1
END IF
NEXT q
IF v0 = 0 THEN
e0 = e0 + "     "
e1 = e1 + "prime"
ELSE
IF p0 > 1 THEN
e2 = LTRIM$(RTRIM$(STR$(p0)))
e0 = e0 + SPACE$(LEN(e2) + 1)
e1 = e1 + b + e2
END IF
END IF
IF z5 - v > y0 - 1 THEN z5 = 0: GOSUB align
IF w(z5) + LEN(e0) > x0 OR (v AND w(z5 + 1) +
LEN(e1) > x0) THEN GOTO pri
IF v = 0 THEN e0 = e1

c2:
w(z5) = w(z5) + LEN(e0)
IF z2 THEN
IF (r = 0 AND z5 < 83) OR (r = 83 AND z5 > 82) THEN
z8 = z5 - r
d(z8) = d(z8) + e0
END IF
ELSE
d(z5) = d(z5) + e0
END IF
z5 = z5 + 1
IF v THEN e0 = e1: v = 0: GOTO c2
IF z5 > y0 - 1 THEN z5 = 0: GOSUB align

c9:
p2 = p2 + 1
GOTO calc

zz:
IF z2 THEN
CLOSE
END
END IF
z4 = 0
GOTO nxhf

align: 'column finished, align what we have so far
u = 0
FOR t = 0 TO 165
IF w(t) > u THEN u = w(t)
NEXT t
u = u + 1
FOR t = 0 TO 165
IF w(t) < u THEN w(t) = u
NEXT t
FOR t = 0 TO 82
IF LEN(d(t)) < u THEN d(t) = d(t) + SPACE$(u -
LEN(d(t)))
NEXT t
RETURN

pri: 'get ready to print
IF z2 THEN GOTO p0
CLS
FOR t = 0 TO 49
LOCATE t + 1, 1
PRINT d(t);
NEXT t
WHILE INKEY$ = ""
WEND
GOTO nxpg

p0:
FOR t = 0 TO 82
PRINT #1, d(t)
NEXT t
GOTO nxhf

'Do not exceed 2.1 billion
Posted 23:10

No comments


Post a Comment: