College Code

The Value of pi

We all learned to write code to calculate the value of pi, a nice way to burn some cycles. But the lesson here was raw processing power or even efficient code is nothing compared to a good algorithm. The choice of equation will make the task a magnitude harder or easier.
So the story was, as an exercise, we were asked by our calculus professor to write a program that will calculate the value of pi. Whoever had the most digits at the end of the week wins.
Didn't have a C++ complier at the time on my machine, so wrote this qbasic code shown here which survived to this day via found backups.
The actual code I used for the homework was indeed in C++ (sadly not available now) and the clever part (I thought at the time, but in fact NOT) was that I used gcc running on the University's mail server instead of my home machine, which is the approach of more hardware cycle vs. better algorithm. Lesson learned.

Those who watched TOS will remember how Spock keep the ship's misbehaving computer busy by asking it to calculate the value of pi to the last digit (which by the way, has been demostrated to be rather pointless as the application of such a high precision of pi is very limited)



'Written by Feipang Ko February 13, 1998 All Rights Reserved (c)
'Graphics added on February 14, 1998 since I have no Valentine
'This is an attempt to use calculus and qbasic to approximate the
'value of PI
'
'

SCREEN 12
DO
PRINT
PRINT " Computing value of Pi..."
PRINT "--------------------------------------------------------------"
PRINT

LOCATE 10, 10
INPUT "what is the value of n"; n#
'INPUT "what is the value of a"; a#
'INPUT "what is the value of b"; b#
a# = 0
b# = 1

delta# = (b# - a#) / n# 'establish delta x
LOCATE 14, 13
PRINT "delta x = "; delta#

'-------------------Axis genetation section----------------------------------
'drawing the axis
'life begins at 400, 300 (backward for y, remember to 480-everything)
LINE (380, 300)-(620, 300) 'the x axis
LINE (400, 320)-(400, 80) 'the y axis

'----draws marks of delta Xs on the x axis
LINE (600, 305)-(600, 300) 'the very last mark
'FOR mark = a# TO b# STEP delta#
' xmark = (mark * 200) + 400 'at every delta Xs
' LINE (xmark, 305)-(xmark, 300)
'NEXT mark

'draws mark on y axis
LINE (395, 100)-(400, 100)
'---------------------------------------------------------------------------

x# = 0
AREA# = 0 'initialize the stuffs

FOR xpoint = 400 TO 600 STEP 1 'draw up the curve first
ypoint = 300 - (SQR(1 - ((xpoint - 400) / 200) ^ 2) * 200)
PSET (xpoint, ypoint), 15
NEXT xpoint

DO
'LEFT HAND SUM
LOCATE 15, 11
PRINT "X is now at "; x#;
bar# = SQR(1 - x# ^ 2) * delta#
curveY = 300 - (bar# / delta#) * 200
curveX = (x# * 200) + 400
COLOR 9
LINE (curveX, curveY)-(curveX + (delta# * 200), curveY)
LINE -(curveX + (delta# * 200), 300)
COLOR 15
AREA# = AREA# + bar#
LOCATE 16, 11
PRINT " AREA NOW-->"; AREA#
x# = x# + delta#
LOOP WHILE x# < (b# - delta# / 2) 'A QUICK AND VERY DIRTY WAY
'TO SOLVE THE ROUNDING PROBLEM
'LIKE, 0.9999999 IS STILL < 1
LOCATE 20, 20
PRINT "area-->"; AREA# * 4 '2 * pi is full area, area# is 1/2
'BEEP
INPUT "again? <0/1>"; again
PRINT " "
CLS
LOOP WHILE (again <> 0) 'repeat loop



Gravity Simulation

Yes, BASIC. I am that old and unshamed now, that young and simple then, but when I saw the Satellite Orbit Simulator, the geek in me had to have it. When I can't find it to buy, I had to write it. A C++ complier wasn't free back then on a Wintel PC, which is what I have besides the Tandy Color Computer III, so QBasic was what I have to use.


SCREEN 12
PSET (320, 240), 15
'LINE (315, 235)-(325, 235), 15 'drawing the square sun
'LINE (325, 235)-(325, 245), 15
'LINE (325, 245)-(315, 245), 15
'LINE (315, 245)-(315, 235), 15
y = 95
x = 305 'initial satellite position
lastvectorx = x + 5
lastvectory = y - 0
counter = 0

DO
PSET (x, y), 15 'paint start position

orbitradius = ((x - 320) ^ 2 + (y - 240) ^ 2) ^ .5
IF orbitradius = 0 THEN orbitradius = 1
'-------gravity---------
gravitylength = 6400 / orbitradius ^ 2
IF INKEY$ = "s" THEN
gravitylength = gravitylength / 2
ELSE IF INKEY$ = "x" THEN gravitylength = gravitylength * 3
ELSE
gravitylength = gravitylength
END IF
gravitypointy = y - (((y - 240) * gravitylength) / orbitradius)
gravitypointx = x - (((x - 320) * gravitylength) / orbitradius)
'PSET (gravitypointx, gravitypointy), 15
'LINE (x, y)-(gravitypointx, gravitypointy), 15
'--------vector---------
'PSET (lastvectorx, lastvectory), 11
vectorx = lastvectorx + (gravitypointx - x)
vectory = lastvectory + (gravitypointy - y)
IF INKEY$ = "a" THEN
vectorx = vectorx * 1.001
vectory = vectory * 1.001
ELSEIF INKEY$ = "z" THEN
vectory = vectory / 2
vectorx = vectorx / 2
ELSE
vectory = vectory
vectorx = vectorx
END IF
PSET (vectorx, vectory), 13
LINE (x, y)-(vectorx, vectory), 15
vectorlength = ((vectorx - x) ^ 2 + (vectory - y) ^ 2) ^ .5
'--------trace----------
COLOR 9
LOCATE 1, 1
PRINT "x", x
LINE (200, 22)-(640, 22), 8
LINE (200, 22)-(200 + x, 22), 9

LOCATE 2, 1
PRINT "y", y
LINE (200, 38)-(640, 38), 8
LINE (200, 38)-(200 + y, 38), 9

LOCATE 3, 1
PRINT "orbitradius", orbitradius
LINE (200, 7)-(640, 7), 8
LINE (200, 7)-(200 + orbitradius, 7), 9

LOCATE 4, 1
PRINT "gravityX", gravitypointx
LINE (200, 54)-(640, 54), 8
LINE (200, 54)-(200 + gravitypointx, 54), 9

LOCATE 5, 1
PRINT "gravityY", gravitypointy
LINE (200, 70)-(640, 70), 8
LINE (200, 70)-(200 + gravitypointy, 70), 9

LOCATE 6, 1
PRINT "grav strength", gravitylength
LINE (200, 86)-(640, 86), 8
LINE (200, 86)-(200 + gravitylength * 50, 86), 9

LOCATE 7, 1
PRINT "vectorX", vectorx
LINE (200, 118)-(640, 118), 8
LINE (200, 118)-(200 + vectorx, 118), 9

LOCATE 8, 1
PRINT "vectorY", vectory
LINE (200, 134)-(640, 134), 8
LINE (200, 134)-(200 + vectory, 134), 9

LOCATE 9, 1
PRINT "vector length", vectorlength
LINE (200, 102)-(640, 102), 8
LINE (200, 102)-(200 + vectorlength * 15, 102), 9

LOCATE 28, 1
PRINT "cycles left"; 1000 - counter
LINE (140, 440)-(640, 440), 10
LINE (640, 440)-(640 - counter / 2, 440), 8
'------next loop-------
LINE (x, y)-(vectorx, vectory), 12
lastvectorx = vectorx * 2 - x
lastvectory = vectory * 2 - y
x = vectorx
y = vectory
'IF x > 640 THEN END
counter = counter + 1
IF counter = 1000 THEN CLS
IF counter > 1010 THEN counter = 0
IF INKEY$ = "q" THEN END
LOOP

'0 0 Black 0(c) Off
'1 1 Blue Underlined(d)
'2 2 Green 1(c) On(d)
'3 3 Cyan 1(c) On(d)
'4 4 Red 1(c) On(d)
'5 5 Magenta 1(c) On(d)
'6 6 Brown 1(c) On(d)
'7 7 White 1(c) On(d)
'8 8 Gray 0(c) Off
'9 9 Light Blue High-intensity
'10 10 Light green 2(c) High-intensity
'11 11 Light cyan 2(c) High-intensity
'12 12 Light red 2(c) High-intensity
'13 13 Light magenta 2(c) High-intensity
'14 14 Yellow 2(c) High-intensity
'15 15 High-intensity 0(c) Off
' white
Content last updated:
July 15, 2015 23:34

Unless noted otherwise, graphics and text and designs are all Copyright (c) P A N G Industries.
Trademarks are the properties of their respective owners.
Please
Contact us if you claim ownership of content and wish to have them removed.

Post your comments here.
071515 21:44:52
011216 10:03:27
011216 10:09:34
011516 13:34:51 Testing
011516 13:42:20
011516 13:42:28 Testing
021816 23:46:59
021816 23:54:20
030716 20:43:47
030716 20:43:48
032116 13:53:05
032116 14:00:51
041916 18:35:38
041916 18:38:39
051016 23:17:41
051016 23:24:44
061116 13:02:03
061116 13:07:45
061416 19:00:34
061416 19:07:43
070816 17:58:13
070816 18:04:06
091316 17:05:33
091416 18:32:36
091416 18:39:13
101316 1:53:47
101316 2:00:26
110916 23:14:00
110916 23:20:18
121916 23:57:38
122016 0:02:52
010517 3:10:09
010517 3:17:39
031517 17:19:44
040817 1:34:05
051617 2:37:28
051617 2:44:40
062617 15:33:04
071117 16:59:17
071117 18:53:46
071117 19:00:48
071117 20:39:09
071117 20:46:16
071317 23:01:00
071317 23:01:04
072817 14:47:42
081217 3:12:34
082617 15:30:05
091017 3:07:31
091217 20:23:29
091217 20:29:10
092417 20:30:14
092417 20:31:11
092417 20:34:17
092917 17:38:16
100617 21:13:21
100617 21:17:58
100917 18:33:07
100917 18:34:12
100917 18:35:56
101617 11:20:00
102617 13:22:59
102617 13:24:59
102617 13:29:23
103117 12:51:40
110317 21:49:30
111017 17:11:05
111017 17:21:25
111017 17:21:29
111617 14:50:53
112617 13:26:10
112617 13:30:09
112617 13:30:17
112617 13:32:18
120117 17:08:03
120617 21:37:11
120617 21:43:07
121217 4:01:54
121217 4:04:12
121217 4:09:50
121717 1:56:05
122617 23:52:25
122617 23:53:37
122617 23:55:30
010118 1:35:08
010918 23:58:05
011018 0:08:49
011018 23:09:30
011018 23:10:14
011018 23:18:42
011518 23:50:02
012618 4:33:04
012618 4:39:25
012618 4:40:25
013118 2:15:43
021018 4:12:18
021018 4:18:14
021118 1:08:34
021118 1:13:59
021118 1:26:55
021118 7:17:15
021118 7:18:47
021118 7:22:47
021618 12:16:59
030218 17:21:41
030218 17:24:53
030218 17:25:26
030818 4:53:33
031318 9:41:11
031318 9:46:43
050418 21:25:54
050418 21:35:22
050718 13:25:05
050718 13:36:53
050718 13:59:59
050718 14:05:54
050718 14:19:47
050718 14:20:02
050718 14:22:24
051618 9:10:28
051618 9:22:23
051618 9:49:54
051618 10:02:39
051618 10:07:59
051618 10:13:10
051618 10:16:26
061318 0:40:38
061318 0:53:42
061318 1:05:37
061318 10:32:50
061318 10:46:29
061318 10:57:27
061318 11:09:38
061318 11:21:02
061318 11:32:43
061318 13:25:48
061318 13:37:25
061318 13:59:28
061318 14:09:17
061318 14:17:13
061318 14:19:59
061318 14:20:08
061318 16:10:43
061318 16:24:17
061318 16:49:13
061318 16:56:06
061318 17:08:49
061318 17:21:49
061318 17:30:02
061318 17:45:19
061318 18:02:37
061318 18:05:12
070718 13:01:03
070718 13:14:02
070718 13:35:40
070718 13:47:31
070718 13:51:45
070718 13:56:39
070718 14:00:12
080318 1:02:50
080318 1:14:21
080318 2:43:39
080318 3:25:29
080318 3:48:04
080318 3:59:53
080318 4:22:45
080318 4:39:40
080318 4:42:11
080618 10:38:15
080618 10:52:32
080618 11:06:08
080618 11:19:00
080618 12:01:35
080618 12:01:44
080618 12:21:14
080618 12:26:14
080618 12:39:22
080618 13:01:07
080618 13:20:02
080618 13:22:32