Smorgasbord for Computer Junkies

Image

 

COMPUTE THE FIRST 100 TRIANGULAR NUMBERS
(CHAPTER 1)

10  REM Compute Triangular Numbers

20  FOR I = 1 TO 100

30          T = (1/2) *I*(I + 1)

40          PRINT I, T

50  NEXT I

60  END

 

 

 

COMPUTE UMBUGIO’S “END-OF-THE-WORLD” NUMBERS
(CHAPTER 2)

10 REM Compute Umbugio’s “End-of-the-World” Numbers

20 FOR N = 1 TO 10

30          A = 1492¬N – 1770¬N – 1863¬N + 2141¬N

40          PRINT N, A

50 NEXT N

60 END

Note: Can you write a program to check the divisibility of Umbugio’s numbers? Calculating the powers, as in this short program, will quickly cause numerical overflows. Can you design a program to avoid this problem?

COMPUTE POLYGONAL NUMBERS, TRIANGULAR TO HEXAGONAL
(
CHAPTER 3)

10 REM Compute Polygonal Numbers, Triangular to Hexagonal

20 FOR R = 3 TO 6

30          FOR N = 1 TO 6

40                 P = (N/2) * ((N – 1)*R – 2*(N – 2)

50                 IF R = 3 THEN PRINT “Triangular:” P

60                 IF R = 4 THEN PRINT “Square:” P

70                 IF R = 5 THEN PRINT “Pentagonal :” P

80                 IF R = 6 THEN PRINT “.Hexagonal:” P

90        NEXT N

100 NEXT R

110 END

Note: Joseph F. Pycior from San Jose, California, writes, “I converted the polygonal number program to Fortran, and found it ironic that I had to use floating point numbers to produce the polygonal numbers, because integers were inadequate. Pythagoras wouldn’t have approved. I computed as high as 11-sided polygons and was amazed the numbers didn’t become large as fast as I expected.”

COMPUTE VON FOERSTER DOOMSDAY CURVE (CHAPTER 4)

10 REM Compute Doomsday Date

20 FOR T = 0 TO 2000 STEP 10 0

25 REM FOR T = 19 80 TO 2000

30           N = (1.79E11) / ((2026.87 – T) ¬.99

40           PRINT T, N

50 NEXT T

60 END

COMPUTE UNITED STATES POPULATION VIA PEARL-REED
(CHAPTER 4)

10 REM Compute United States Population via Pearl-Reed

20 FOR T = 1790 TO 1910

30          X = 210 / (1 + 51.5*EXP(–0.0313* (T – 1790)))

40          PRINT T, X

50 NEXT T

60 END

GENERATE PYTHAGOREAN TRIANGLES (CHAPTER 5)

10 REM Generate Pythagorean Triangles

15 PRINT “Pythagorean Triples (side lengths):”

20 FOR U = 1 TO 6

30          FOR V = U + 1 TO 7

40                  X = 2*U*V

50                  Y = ABS(U¬2 – V¬2)

60                  Z = V¬2 + U¬2

70                  PRINT X,Y,Z

80          NEXT V

90   NEXT U

100 END

COMPUTE PRAYING TRIANGLES (CHAPTER 5)

10 REM Compute Lengths for Triangles that Pray

15 REM Print First Four Examples

17 REM High precision needed for larger examples

20 D= (SQR(2) +1)*(SQR(2) +1)

30 S = 1

40 FOR I = 1 TO 4

50          S = S*D

60          S = INT(S)

70          A = SQR((S*S)/2)

80          A = INT(A)

90          B = A + 1

100        C = SQR(A*A + B*B)

110        PRINT “PYTHAGOREAN TRIANGLE (A,B,C):”,A,B,C

120 NEXT I

130 END

 

FIND ALL ST. AUGUSTINE NUMBERS (CHAPTER 6)

5 REM Search for St. Augustine Numbers

10 REM (Cubical Narcissistic Numbers)

15 REM Search all 3-digit integers

20 FOR I = 100 TO 999

30          A = INT(I/100)

40          B = INT(I/10) – 10*A

50          C = I – 100*A – 10*B

55          IF I <> A**3 + B**3 + C**3 THEN 90

60          PRINT “Narcissistic Number”; I

70          PRINT “Equals”;A**3; “+”;B**3;“+”

80          PRINT

90     NEXT I

100   END

FIND PERFECT NUMBERS (CHAPTER 7)

10 REM Compute Perfect Numbers

15 FORN = 2 TO 100

20          S = 0

30          FOR D = 1 TO N/2

40               IF INT(N/D) <> N/D THEN 60

50               S = S + D

60          NEXT D

70          IF S <> N THEN 90

80          PRINT N; “is a perfect number”

90 NEXT N

100 END

 

FIND AMICABLE NUMBERS (CHAPTER 7)

100 REM Find Amicable Numbers

110 FOR L = 1 TO 8000

120          S = 0

130          FOR D = 1 TO L/2

140                   IF L/D <> INT(L/D) THEN 160

150                   S = S + D

160          NEXT D

170          IF S <= L THEN 260

180          B = S

190          T = 0

200          FOR F = 1 TO B/2

210                   IF B/F <> INT(B/F) THEN 230

220                   T = T + F

230          NEXT F

240          IF T <> L THEN 260

250          PRINT L; “and”;B; “are amicable numbers.”

260 NEXT L

270 END

SOLVE TURK AND CHRISTIAN PROBLEM (CHAPTER 8)

10 REM Solve Turk and Christian Problem

20 REM A “1” in the array means the person has died.

30 REM Each row of output indicates the cumulative deaths.

40 DIM A (30)

50 J = 0

60 FOR I = 1 TO 15

70         J = J + 13

80         IF J > 30 THEN J = J – 30

90         A(J) = 1

100        FOR K = 1 TO 30

110                 PRINT A(K);

120        NEXT K

130        PRINT

140 NEXT I

150 END

COMPUTER DOOMSDAY ORBITS OF ASTEROIDS (CHAPTER 10)

/* Compute Doomsday Orbits of Asteroids */

/* Simulation: Is the GPPA necessary? */

#include <math.h>

#include <stdio.h>

main ()

{

int i;

float a, ex, cy, theta, x, y;

/* Draw Earth Orbit at Center */

for (theta=0; theta <= 6.3; theta=theta+.1) {

x= .5*cos(theta);

y= .5*sin(theta);

printf(“%f %f\n”,x,y);

           }

    /* Position Earth on its Orbital Drawing */

   x = .5*cos (0);

   y = .5*sin (0);

   printf(“%f %f\n”,x,y);

    /* Draw Random Elliptical Asteroid Orbits Near Earth */

    for (i = 0; i <= 10; i + +)

    /* Generate random number to control eccentricity of orbit */

    a= (float)rand()/32767.;

    /* Draw Asteroid Orbit */

      for (theta = 0; theta <= 6.3; theta = theta + .1) {

x = cos(theta);

y = a*sin(theta);

printf (“%f %f\n”,x,y);

   }

   /* Randomly Position Asteroids on their Elliptical Orbits */

   theta = 6.28*(float)rand( )/32767.;

   x = cos(theta);

   y = a*sin(theta);

   printf (“%f %f\n”,x,y);

 

FIND PRIME FACTORS OF URANTIA NUMBERS (CHAPTER 12)

10 REM Find Prime Factors of Urantia Numbers

20 REM Urantia Number to be factored:

30 A = 611121

35 REM Also try A = 5342482337666

40 IF ABS (A) <= 1 THEN 210

50 N = INT (ABS (A))

60 REM Find the prime factors and print

70 B = 0

80 FOR I = 2 TO N/2

90            IF N/I > INT(N/I) THEN 170

100          B = B + 1

110          IF B > 1 THEN 130

120          PRINT “Prime Factors of”;N; “are:”

130          PRINT I

140          N = N/I

150          IF N = 1 THEN 210

160          I = I – 1

170 NEXT I

180 IF N <> INT (A) THEN 120

190 PRINT N; “is a prime number.’

210 END

 

FRACTAL GENERATOR (CHAPTER 13)

100 REM Jul ia set program

110 CR = 0           ’ Real part of C

120 CI = 1            ’ Imaginary part of C

130 X1 = –1.5         ’Boundaries of plot

140 X2 = 1.5

150 Yl = –1.5

160 Y2 = 1.5

170 SCREEN 12     ’Assume VGA color graphics mode

180 W% = 640       ’ Screen width

190 H% = 480        ’ Screen height

200 KMAX% = 64 ’Bailout condition

210 FOR 1% = : 0 TO W% – 1

220          FOR J% = 0 TO H% – 1

230                  K% = 0

240                  C% = KMAX%

250                  X = X1 + 1% * (X2 – X1) / W%

260                  Y = Y2 – J% * (Y2 – Yl) / H%

270                  XS = X * X

280                  YS = Y * Y

290                  WHILE K% < KMAX%

300                         Y = Y + Y

310                         Y = X * Y + CI

320                         X = XS – YS + CR

330                         XS = X * X

340                         YS = Y * Y

350                         K% = K% + 1

360                         IF XS + YS > 4 THEN C% = K% : K% = KMAX%

370                  WEND

380                  PSET (1%, J%), C% MOD 16

390          NEXT J%

400 NEXT 1%

410 END

 

I personally enjoy programming a computer using the language C these days, but I know most hobbyists use BASIC. Therefore, I’ve included a BASIC program that should allow you to compute and display Julia sets. Computer-literate readers should be able to further modify it for their own use. The program has been tested with GW-BASIC, QuickBASIC, VisualBASIC, and PowerBASIC on the IBM PC platform and with QuickBASIC on the Macintosh. Users may wish to adjust the screen mode and size in lines 170-190 for the various platforms. The program produces a Julia dendrite in about 12 seconds using PowerBASIC 3.0 on a 486-DX2/66 machine. The values for CR and CI may be adjusted to produce different Julia sets. Professor J. Clint Sprott, well-known for his work in strange attractors, wrote the preceding code.

 

COMPUTE QUIPU PATTERN (CHAPTER 14)

/* Simulate Quipu Structure */

#include <math.h>

#include <stdio.h>

main()

{

int i;

float a, cx, cy, theta, x, y;

 

/* Draw Main Cord of Quipu * /

   for (theta=3.14; theta <= 7.28; theta = theta + .1) {

x = cos(theta);

y = sin(theta) + 1;

printf (“%f %f\n”,x,y);

}

 

/* Position Pendant Cords */

for (theta = 3 .14; theta <= 6.28; theta = theta + .1) {

x = cos(theta);

y = sin (theta) + 1;

printf(“%f %fn”,x,y);

x = 2*cos(theta);

y = 2*sin(theta) + 1;

printf (“%f %f\n”,x,y);

 

/* subsidiary cords */

x =1.5*cos(theta);

y = 1.5*sin(theta) + 1;

printf (“%f %f\n”,x,y);

x = 2.2*cos(theta + .05)

y = 2.2*sin(theta + .05) +1;

printf (“%f %f\n”,X,Y);

}

}

 

COMPUTE FIBONACCI NUMBERS (CHAPTER 15)

10 REM Compute Fibonacci Numbers

20 DIM F(40)

30 F(l) = 1

40 F(2) = 1

50 FOR N = 1 TO 38

60          F(N + 2) = F(N + 1) + F(N)

70 NEXT N

80 FOR N = 1 TO 40

90          PRINT F (N)

100 NEXT N

110 END

 

COMPUTE FIBONACCI SPIRALS (CHAPTER 15)

10 REM Compute Fibonacci Spirals

20 G = (1 + SQR(5))/2

30 T = 2*3.1415926/G

35 REM Experiment with different values of K

40 K = 2

50 R2 = K*G/2

55 REM 2000 Circles

60 FOR I = 1 TO 2000

70          R = K * SQR(I)

80          A = T*I

85          REM C1 and C2 are circles’ centers

90          CI= R*SIN(A)

100          C2 = R*COS(A)

150          REM DRAW A CIRCLE, T2 in degrees

200          FOR T2 = 1 TO 370 STEP 10

300             X = R2*COS(T2) + CI

400             Y = R2*SIN(T2) + C2

500             PRINT X, Y

600          NEXT T2

700 NEXT I

900 END

 

COMPUTE NUMERICAL GARGOYLES (CHAPTER 17)

C Program Code

 

/* Create numerical gargoyles. Magnify “creatures” as needed. */

#include <stdio.h>

#include <math.h>

main ()

{

float r; /* random number, 0-1 */

short c[513] [513]; / * Arrays for holding 1 and 0 values * /

short ch[513] [513];

int size, time_steps, steps, i,j,k,sum;

size = 80; /* Use larger sizes for nicer images */

/* Controls how many steps gargoyle is to evolve */

time_steps = 20;

/* Initially seed space with random 0’ s and 1’ s */

for(i = 0; i <= size; i + +)

  for(j = 0; j <= size; j + +) {

r= (float) rand() /32767.;

if (r >= .5) c[i] [j] =0; if (r <= .5) c[i] [j] =1;

}

/* perform simulation based on twisted maj ority rules

to form gargoyle objects in 2-D */

for(steps =1; steps < time_steps; steps++) {

for (i = 1; i<size; i + +) {

for(j = 1; j < size; j++) {

/* compute sum of neighbor cells */

 sum = c[i + 1] [j + 1] +

           c[i – 1] [j – 1] +

           c[i – 1] [j + 1] +

           c[i + 1] [j – 1] +

           c[i + 1] [j] + c[i – 1] [j] +

           c[i] [j + 1] + c[i] [j – 1] +

           c[i] [j];

if (sum == 9) ch[i] [j] = 1;

if (sum == 8) ch[i] [j] = 1;

if (sum == 7) ch[i] [j] = 1;

if (sum == 6) ch[i] [j] = 1;

/* Notice “twist” in rules which destabilizes

  creature boundaries. */

if (sum == 5) ch[i] [j] =0;

if (sum == 4) ch[i] [j] =1;

if (sum == 3) ch[i] [j] =0;

if (sum == 2) ch[i] [j] =0;

if (sum == 1) ch[i] [j] =0;

if (sum == 0) ch[i] [j] =0;

}

}

for(i = 0; i <= size; i + +) for(j = 0; j <= size; j + +) c[i] [j]

= ch[i] [j];

}

/* If you like, you can make a movie of all frames */

/* to show gargoyle evolving .          */

printf (“A plot of last frame of simulation\n”);

for(i = 0; i <= size; i + +) {

for(j = 0; j <= size; j+ +) {

     /* Crude attempt to draw creatures using characters */

     /* Better to use hi-res graphics package    */

     if (c[i] [j] == 1) printf (“*”); else printf (“ ”);

}

printf (“\n”);

}

}

BASIC Program Code

10 REM Create numerical gargoyles

15 REM Magnify “creatures” as needed

20 REM R is a random number, 0-1

30 REM C and C1 – Arrays for holding 1 and 0 values

35 DIM C(81,81)

40 DIM H(81, 81)

50 REM Use larger size, S, for nice images

60 S = 80

70 REM T Controls how many steps gargoyle is to evolve

80 T = 20

90 REM Initially seed space with random 0’ s and 1’ s

100 FOR I = 1 TO S

120          FOR J = 1 TO S

130              R = RND

140              IF R >= .5 THEN C(I, J) = 0

150              IF R <= .5 THEN C(I, J) = 1

160          NEXT J

170 NEXT I

175 REM Perform simulation based on twisted majority rules

180 REM to form gargoyle objects in 2-D

190 FOR K = 1 TO T

200          FOR I = 2 TO S – 1

210         FOR J = 2 TO S – 1

220               REM compute sum of neighbor cells

230              A = C(I + 1,J+1) +C(I-1,J-1) +C(I-1,J + 1)

240              A = A + C(I + 1,J-1) + C(I + 1,J) + C(I-1,J)

C(I, J+l)

250              A = A+C(I, J-l) + C(I, J)

260               IF A = 9 THEN H(I, J) = 1

270               IF A = 8 THEN H(I, J) = 1

280               IF A = 7 THEN H(I, J) = 1

290               IF A = 6 THEN H(I, J) = 1

295               REM Notice “twist” in rules which destabilizes

297               REM blob boundaries .

300               IF A = 5 THEN H (I, J) = 0

310               IF A = 4 THEN H(I, J) = 1

320               IF A = 3 THEN H (I, J) = 0

330               IF A = 2 THEN H (I, J) = 0

340               IF A = 1 THEN H (I, J) = 0

350               IF A = 0 THEN H (I, J) = 0

360            NEXT J

370        NEXT I

380        REM Swap values in arrays

390        FOR I = 1 TO S

400               FOR J = 1 TO S

410                 C(I, J) = H(I, J)

415          NEXT J

417          NEXT I

420 NEXT K

430 REM If you like, you can make a movie of all frames

440 REM to show gargoyles evolving.

450 PRINT “To plot the gargoyle, place a dot wherever the”

460 PRINT “C array has a value of 1 in it as you scan”

470 PRINT “I and J from 0 to S.”

480 END

 

SIMULATE SIZE DISTRIBUTION OF MOON CRATERS (CHAPTER
21)

/* Simulate Size Distribution of Moon Craters */

#include <math.h>

#include <stdio.h>

 

main()

{

int i;

float r, cx, cy, theta, x, y;

for(i = 0; i < 80; i + +) {

/* Randomly determine center of each crater */

cx = (float)rand ()/32767.;

cy = (float)rand()/32767.;

/* Distribution of sizes is l/r**2 via

the Czarnecki transformation */

r = (float)rand()/32767.;

r = 0.01*tan(r*3.1415/2.);

/* Draw Crater */

for (theta = 0; theta <= 6.3; theta = theta + .1) {

x = r*cos (theta) + cx;

y = r*sin(theta) + cy;

/* Data points trace out circular craters */

printf(“%f %f\n”,x,y);

}

  }

}