!Chapter 1. Program ITERLIN.TRU CLEAR !Clears the screen !Iterate the function f(x) = ax + b n times PRINT "Iterate the function f(x) = ax + b" INPUT PROMPT "What is a? ": a INPUT PROMPT "What is b? ": b INPUT PROMPT "What is the initial value of x? ": x INPUT PROMPT "How many iterations? ": n PRINT "The initial value chosen was x = "; x FOR I = 1 to n LET y = a * x + b PRINT using "i = ### y = ##.#########":i,y LET x = y NEXT i END ======================= !Chapter 2. Program DIFFER.TRU ! Program: DIFFER.TRU ! Program revision from book's version: 6/23/97 ! This program finds differences of distinct non-zero squares ! mod m and counts repetitions ! sqlst(v) counts # times v occurs as square mod m ! difflst(v) counts # times v occurs as diff. of squares ! sq(i) is the ith distinct non-zero square mod m DIM sqlst(0 to 1000) DIM difflst(0 to 1000) DIM sq(1000) DO CLEAR !initialize the counters to 0 FOR i=0 to 1000 LET sqlst(i)=0 LET difflst(i)=0 IF i>0 then LET sq(i)=0 NEXT i PRINT "What is the modulus m:"; INPUT m FOR i = 1 TO m - 1 LET value = MOD(i * i,m) LET sqlst(value) = sqlst(value) + 1 NEXT i ! Listing and counting distinct non-zero squares mod m PRINT "The non-zero squares are" ! numsq counts distinct non-zero squares mod m LET numsq = 0 !initialize numsq FOR v = 1 TO m - 1 IF sqlst(v) > 0 THEN PRINT v; LET numsq = numsq + 1 LET sq(numsq) = v !sq(n)=v makes nth square = v END IF NEXT v PRINT PRINT "Number of distinct squares is "; numsq PRINT "Press a key to continue"; GET KEY akey ! Creating and counting all possible differences FOR i = 1 TO numsq FOR j = 1 TO numsq LET value = MOD(sq(i) - sq(j) + m ,m) !Need value >=0 LET difflst(value) = difflst(value) + 1 NEXT j NEXT i ! Listing distinct differences and counting repetitions ! numdiff counts distinct non-zero differences of squares mod m LET numdiff = 0 PRINT "Distinct non-zero differences and number of repetitions of each:" FOR v = 1 TO m IF difflst(v) > 0 THEN LET numdiff = numdiff + 1 PRINT v, difflst(v) IF MOD(numdiff,18)=0 then PRINT "Press a key to continue"; GET KEY:akey !freeze action at each screenfull PRINT END IF END IF NEXT v PRINT "Number of distinct differences:", numdiff PRINT "Again (y/n): "; GET KEY: AGAIN ! Define the variable 'again' PRINT chr$(again) LOOP UNTIL (AGAIN <> 121) ! 121 is ascii for y END ======================= ! Chapter 2. Program: SQUARES.TRU ! Computes the distinct squares mod m for 1 0 THEN PRINT v; NEXT v END ======================= !Chapter 3: Program: Euclid1.tru CLEAR INPUT prompt "a = ": a INPUT prompt "b = ": b IF a>b then LET u=a LET v=b ELSE LET u=b LET v=a END IF PRINT u,v ! v <= u LET k=0 LET r=1 DO while(r>0) LET q=int(u/v) LET r=u-v*q LET oldr=v LET u=v LET v=r PRINT oldr, r LET k=k+1 LOOP PRINT PRINT "GCD(";a;",";b;") = ";oldr PRINT "Number of steps was ";k END ======================= ! Chapter 3. Program: Euclid2.tru CLEAR RANDOMIZE LET N=10000 LET a=int(1+rnd*N) LET b=int(1+rnd*N) IF a>b then LET u=a LET v=b ELSE LET u=b LET v=a END IF PRINT u,v LET k=0 LET r=1 DO while(r>0) LET q=int(u/v) LET r=u-v*q LET oldr=v LET u=v LET v=r PRINT oldr, r LET k=k+1 LOOP PRINT PRINT "GCD(";a;",";b;") = ";oldr PRINT "Number of steps was ";k END ======================= ! Chapter 3. Program: Euclid3.tru CLEAR RANDOMIZE DIM count(20) LET N=10000 FOR j=1 to 1000 LET a=int(1+rnd*N) LET b=int(1+rnd*N) IF a>b then LET u=a LET v=b ELSE LET u=b LET v=a END IF LET k=0 LET r=1 DO while(r>0) LET q=int(u/v) LET r=u-v*q LET oldr=v LET u=v LET v=r LET k=k+1 LOOP IF(k<21) then LET count(k)=count(k)+1 NEXT j PRINT "Steps","Occurrences" FOR j=1 to 20 PRINT j,count(j) NEXT j END ======================= ! Chapter 3. Program: Euclid4.tru CLEAR RANDOMIZE DIM count(20) LET N=10000 FOR j=1 to 1000 LET a=int(1+rnd*N) LET b=int(1+rnd*N) IF a>b then LET u=a LET v=b ELSE LET u=b LET v=a END IF LET k=0 LET r=1 DO while(r>0) LET q=int(u/v) LET r=u-v*q LET oldr=v LET u=v LET v=r LET k=k+1 LOOP IF(oldr<21) then LET count(oldr) = count(oldr)+1 NEXT j PRINT "GCD","Occurrences" FOR j=1 to 20 PRINT j,count(j) NEXT j END ======================= ! Chapter 3. Program: Euclid5.tru CLEAR INPUT prompt "a = ": a INPUT prompt "b = ": b IF b>a then LET c=b LET b=a LET a=c END IF LET a1=a LET b1=b LET xold=1 LET yold=0 LET x=0 LET y=1 LET r=1 DO while(r>0) LET q=int(a/b) LET c=a-q*b IF(c=0) then PRINT "GCD =";b;" =";a1;"*";x;"+";b1;"*";y STOP END IF LET xnew=xold-q*x LET ynew=yold-q*y LET xold=x LET x=xnew LET yold=y LET y=ynew LET a=b LET b=c LOOP END ======================= ! Chapter 4. Program is countmd4.tru DEF prime(n) ! Returns 1 if n prime, 0 otherwise IF n < 2 THEN LET p = 0 ELSE LET p =1 LET a = 2 LET s = INT(SQR(n)) DO UNTIL a > s IF n - INT(n/a)*a = 0 then LET p=0 EXIT DO END IF LET a = a + 1 LOOP END IF LET prime = p END DEF CLEAR ! The program steps begin here PRINT "Compute pi1(n) and pi3(n). What is n"; INPUT n LET pi1 = 0 LET b = 1 DO until 4*b + 1 > n IF prime(4*b+1) = 1 then LET pi1 = pi1 + 1 LET b = b+1 LOOP LET pi3 = 0 LET c = 0 DO until 4*c + 3 > n IF prime(4*c + 3) = 1 then LET pi3 = pi3 + 1 LET c = c + 1 LOOP PRINT "pi1 ="; pi1 PRINT "pi3 ="; pi3 END ======================= ! Chapter 4. Program is countpr1.tru DEF prime(n) ! This function returns 1 if n prime, IF n<2 then ! and 0 otherwise LET p=0 ELSE LET p =1 LET a = 2 LET s = INT(SQR(n)) DO UNTIL a > s IF n - INT(n/a)*a=0 then LET p=0 EXIT DO END IF LET a = a + 1 LOOP LET prime = p END IF END DEF CLEAR ! The program steps begin here PRINT "Count primes less than or equal to n. What is n"; INPUT n LET m = 2 LET pin =0 DO UNTIL m > n IF prime(m) = 1 then LET pin = pin + 1 LET m = m + 1 LOOP PRINT "There are";pin;"primes less than or equal to";n END ======================= ! Chapter 4. Program is eulerchk.tru CLEAR LET q = 41 ! Change this number to change the value of q PRINT "List numbers from 0 to k for which m^2 + m +";q;"is prime." INPUT prompt "What is k? ": k DEF prime(n) !returns 1 if n prime, 0 otherwise IF n<2 then LET p = 0 ELSE LET p =1 LET a = 2 LET s = INT(SQR(n)) DO UNTIL a > s IF MOD(n,a) = 0 then LET p=0 EXIT DO END IF LET a = a + 1 LOOP END IF LET prime = p END DEF FOR m = 0 to k LET n = m^2 + m + q IF prime(n) = 1 then PRINT m; LET count = count+1 END IF NEXT m PRINT PRINT "There are ";count; " numbers in this list." END ======================= ! Chapter 4. Program is interval.tru PRINT "Compute pi(k,m) = number of primes in interval [k,m]." INPUT prompt "What is k? ": k INPUT prompt "What is m? ": m DEF prime(n) ! Returns 1 if n prime, 0 otherwise IF n<2 then LET p=0 ELSE LET p =1 LET a = 2 LET s = INT(SQR(n)) DO UNTIL a > s IF n - INT(n/a)*a = 0 THEN LET p=0 EXIT DO END IF LET a = a + 1 LOOP LET prime = p END IF END DEF LET b = k LET count = 0 DO UNTIL b > m IF prime(b) = 1 then LET count = count + 1 LET b = b+1 LOOP PRINT "There are";count;"primes in the interval" END ======================= ! Chapter 4. Program is listprim.tru CLEAR PRINT "List primes less than or equal to n. What is n "; INPUT n DEF prime(n) ! Returns 1 if n prime, 0 otherwise IF n<2 then LET p = 0 ELSE LET p =1 LET a = 2 LET s = INT(SQR(n)) DO until a > s IF n-INT(n/a)*a = 0 then LET p=0 EXIT DO END IF LET a = a + 1 LOOP END IF LET prime = p END DEF LET m = 1 DO UNTIL m > n IF prime(m) = 1 then PRINT m; LET m = m + 1 LOOP END ======================= ! Chapter 4. Program is mersenne.tru CLEAR PRINT "Compute whether 2^p - 1 is prime" INPUT prompt "What is p? ":p DEF prime(n) ! Returns 1 if n prime, 0 otherwise IF n < 2 then LET p = 0 ELSE LET p =1 LET a = 2 LET s = INT(SQR(n)) DO UNTIL a > s IF n - INT(n/a)*a = 0 THEN LET p=0 EXIT DO END IF LET a = a+1 LOOP END IF LET prime = p END DEF LET n = 2^p -1 IF prime(n) = 1 THEN PRINT n;"is a prime." ELSE PRINT n;"is not prime. " PRINT "It is ";a;" times ";n/a END IF END ======================= ! Chapter 4. Program is testprim.tru PRINT "The function prints 1 if n is prime, 0 if not. What is n"; INPUT n DEF prime(n) ! Returns 1 if n prime, 0 otherwise IF n<2 then LET p=0 ELSE LET p =1 LET a = 2 LET s = INT(SQR(n)) DO until a > s IF n - INT(n/a)*a = 0 THEN LET p=0 EXIT DO END IF LET a = a + 1 LOOP LET prime = p END IF END DEF LET answer = prime(n) PRINT "The function returns";answer IF answer = 0 then PRINT n;"is not a prime. It is";a;"x";n/a ELSE PRINT n;"is a prime" END IF END ======================= ! Chapter 6. Program is ran1resp.tru DIM A(3, 3, 3), B(3, 3, 3), ROW$(3) RANDOMIZE LET TrueYes = 1 LET TrueNo = 2 LET Real = 1 LET Decoy = 2 LET Yes = 1 LET No = 2 LET Total = 3 LET ROW$(1) = "REAL " LET ROW$(2) = "DECOY " LET ROW$(3) = "TOTAL " LET C1$ = " T R U E Y E S T R U E N O W H O L E G R O U P" LET C2$ = " YES | NO | TOTAL YES | NO | TOTAL YES | NO | TOTAL" LET C3$ = " ------+------+------ ------+------+------ ------+------+------" !*****GET STARTING INFORMATION***** CALL get_start_info(GrpSize,YesInGrp,PrRealQ,PrYesDecoy,NRep,PrntFreq) ! !*****MAIN LOOP. EACH TIME THROUGH IS ONE REPETITION OF THE SURVEY***** FOR Survey = 1 TO NRep CLEAR MAT A = zer(3,3,3) ! First survey the True Yes respondents FOR Respondent = 1 TO YesInGrp ! * Toss the dime to decide which question: IF RND < PrRealQ THEN ! * Dime lands Heads -- Answer Real question Yes: LET A(TrueYes, Real, Yes) = A(TrueYes, Real, Yes) + 1 ELSE ! * Dime lands Tails -- Answer Decoy question by tossing penny IF RND < PrYesDecoy THEN ! * Penny lands Heads -- Yes to Decoy question: LET A(TrueYes, Decoy, Yes) = A(TrueYes, Decoy, Yes) + 1 ELSE ! * Penny lands Tails -- No to Decoy question: LET A(TrueYes, Decoy, No) = A(TrueYes, Decoy, No) + 1 END IF END IF NEXT Respondent ! Now survey the True No respondents FOR Respondent = YesInGrp + 1 TO GrpSize ! * Toss the dime to decide which question: IF RND < PrRealQ THEN ! * Dime lands Heads -- Answer Real question No: LET A(TrueNo, Real, No) = A(TrueNo, Real, No) + 1 ELSE ! * Dime lands Tails -- Answer Decoy question by tossing penny IF RND < PrYesDecoy THEN ! * Penny lands Heads -- Yes to Decoy question: LET A(TrueNo, Decoy, Yes) = A(TrueNo, Decoy, Yes) + 1 ELSE ! * Penny lands Tails -- No to Decoy question: LET A(TrueNo, Decoy, No) = A(TrueNo, Decoy, No) + 1 END IF END IF NEXT Respondent ! Get marginal totals for component tables FOR I = 1 TO 2 FOR J = 1 TO 2 LET A(Total, I, J) = A(TrueYes, I, J) + A(TrueNo, I, J) NEXT J NEXT I FOR I = 1 TO 3 FOR J = 1 TO 2 LET A(I, J, Total) = A(I, J, 1) + A(I, J, 2) LET A(I, Total, J) = A(I, 1, J) + A(I, 2, J) NEXT J LET A(I, Total, Total) = A(I, Total, 1) + A(I, Total, 2) NEXT I ! Update cumulative totals FOR I = 1 TO 3 FOR J = 1 TO 3 FOR K = 1 TO 3 LET B(I, J, K) = B(I, J, K) + A(I, J, K) NEXT K NEXT J NEXT I !*****Check to see whether to print***** IF ABS(Survey - PrntFreq * INT(Survey / PrntFreq)) < .1 THEN PRINT PRINT USING "REPETITION NUMBER #####": Survey; PRINT " of ";NRep PRINT C1$ PRINT C2$ FOR J = 1 TO 3 PRINT C3$ PRINT ROW$(J); FOR I = 1 TO 3 FOR K = 1 TO 2 PRINT USING " #### |": A(I, J, K); NEXT K PRINT USING " #### ": A(I, J, 3); PRINT " "; NEXT I PRINT NEXT J PRINT PRINT USING "AVERAGES BASED ON ##### REPETITIONS": Survey PRINT C1$ PRINT C2$ FOR J = 1 TO 3 PRINT C3$ PRINT ROW$(J); FOR I = 1 TO 3 FOR K = 1 TO 2 PRINT USING "####.#|": B(I, J, K) / Survey; NEXT K PRINT USING " ####.#": B(I, J, K) / Survey; PRINT " "; NEXT I PRINT NEXT J PRINT "Press any key to continue"; GET KEY :zz END IF NEXT Survey ASK CURSOR nr,nc SET CURSOR nr ,1 PRINT "==================================================================" PRINT "Initial conditions were: " PRINT "Group Size: ";GrpSize; " Of these, ";YesInGrp;" were YES" PRINT "Pr(H) for Dime: ";PrRealQ;" and Pr(H) for Penny: ";PrYesDecoy; PRINT " Press 'Q' to Quit"; DO until (Ucase$(chr$(dummy)) = "Q") GET KEY: dummy LOOP END !End of the program SUB get_start_info(GrpSize,YesInGrp,PrRealQ,PrYesDecoy,NRep,PrntFreq) !*****GET STARTING INFORMATION***** CLEAR PRINT PRINT PRINT "RANDOMIZED RESPONSE SIMULATION" PRINT PRINT "First set up the group you will survey:" INPUT prompt " What is the size of the group? ": GrpSize INPUT prompt " How many of these are (true) Yes? ": YesInGrp PRINT PRINT "Now set up the survey:" INPUT prompt " What is Pr(Real Question), i.e. Pr(H) for the dime? ": PrRealQ INPUT prompt " What is Pr(Yes|Decoy), i.e., Pr(H) for the penny? ": PrYesDecoy PRINT PRINT "Now set up your simulation:" INPUT prompt " How many times do you want to repeat the survey? ": NRep PRINT " How often do you want to see the results" INPUT prompt " (1=every time, 2=every other time, etc.)? ": PrntFreq END SUB ======================= ! Chapter 6. Program is ran2resp.tru ! User-defined functions DEF Estimate (NYes, GrpSize, PrRealQ, PrYesDecoy) LET Estimate = NYes / GrpSize END DEF DEF OpChar (Estimate, True) LET OpChar = Estimate END DEF DEF Toss (PrHead) IF (rnd 121) ! end when you don't press "y" END ======================= !Chapter 10. Program: ball.tru RANDOMIZE PRINT "n is the dimension" INPUT prompt "What is n? ":n PRINT "m is the number of points to be chosen in the unit cube" INPUT prompt "What is m? ":m LET s = 0 FOR i = 1 TO m Let sum = 0 FOR j = 1 TO n LET x = rnd LET sum = sum + (x - 1/2) * (x - 1/2) NEXT j IF sum <= 1/4 then LET s = s+1 NEXT i PRINT "The volume of the ball is approximately "; s/m END ======================= !Chapter 10 ! Program: Integral.tru DIM Fval(0 to 2000) ! Enter any function and any interval [a,b] here DEF f(x) = cos(x) LET a = 0 LET b = 1.570796 !approx pi/2 CLEAR PRINT INPUT prompt "What is m (a positive integer)? ":m LET n = 2 * m LET h = (b - a)/n ! Computing the function at endpoints of subintervals (array) LET x = a FOR i = 0 TO n LET Fval(i) = f(x) LET x = x + h NEXT i ! Computing the master sum for left, right, and trap sums LET Sum = 0 FOR i = 0 to m LET Sum = Sum + Fval(2 * i) NEXT i LET Leftsum = [Sum - Fval(n)] * (2 * h) LET Rightsum = [Sum - Fval(0)] * (2 * h) LET Trapsum = [Leftsum + Rightsum]/2 ! Computing the midpoint approximation LET x = a LET MidTotal = 0 FOR i = 1 to m LET MidTotal = MidTotal + Fval(2*i-1) NEXT i LET Midsum = MidTotal * (2 * h) ! Computing the Simpson's rule approximation LET E = 0 IF (m > 1) THEN FOR i = 1 to (m - 1) LET E = E + Fval(2 * i) NEXT i END IF LET U = 0 FOR i = 1 to m LET U = U + Fval(2 * i - 1) NEXT i LET Simpsum = ( Fval(0) + 4 * U + 2 * E + Fval(n) ) * h/3 ! Display values PRINT PRINT "The left endpoint sum gives: "; Leftsum PRINT "The right endpoint sum gives: "; Rightsum PRINT "The midpoint sum gives: "; Midsum PRINT "The trapezoidal rule gives: "; Trapsum PRINT "Simpson's rule gives: "; Simpsum END ======================= ! Chapter 10. Program: Integr-2.tru DIM Fval(0 to 2000) ! Enter any function and any interval [a,b] here DEF f(x) = cos(x) LET a = 0 LET b = 1.570796 !approx pi/2 ! Enter actual value of the integral here LET Actual = 1 ! Lines copied from integral.tru CLEAR PRINT INPUT prompt "What is m (a positive integer)? ":m LET n = 2 * m LET h = (b - a)/n ! Computing the function at endpoints of subintervals (array) LET x = a FOR i = 0 TO n LET Fval(i) = f(x) LET x = x + h NEXT i ! Computing the master sum for left, right, and trap sums LET Sum = 0 FOR i = 0 to m LET Sum = Sum + Fval(2 * i) NEXT i LET Leftsum = [Sum - Fval(n)] * (2 * h) LET Rightsum = [Sum - Fval(0)] * (2 * h) LET Trapsum = [Leftsum + Rightsum]/2 ! Computing the midpoint approximation LET x = a LET MidTotal = 0 FOR i = 1 to m LET MidTotal = MidTotal + Fval(2*i-1) NEXT i LET Midsum = MidTotal * (2 * h) ! Computing the Simpson's rule approximation LET E = 0 IF (m > 1) THEN FOR i = 1 to (m - 1) LET E = E + Fval(2 * i) NEXT i END IF LET U = 0 FOR i = 1 to m LET U = U + Fval(2 * i - 1) NEXT i LET Simpsum = ( Fval(0) + 4 * U + 2 * E + Fval(n) ) * h/3 ! Display values PRINT PRINT "The left endpoint sum gives: "; Leftsum PRINT "The right endpoint sum gives: "; Rightsum PRINT "The midpoint sum gives: "; Midsum PRINT "The trapezoidal rule gives: "; Trapsum PRINT "Simpson's rule gives: "; Simpsum ! Display values PRINT PRINT "The error in the left endpoint sum is: "; Leftsum - Actual PRINT "The error in the right endpoint sum is: "; Rightsum - Actual PRINT "The error in the midpoint sum is: "; Midsum - Actual PRINT "The error in the trapezoidal rule is: "; Trapsum - Actual PRINT "The error in Simpson's rule is : "; Simpsum - Actual END ======================= ! Chapter 10. Program: Monte-1.tru ! Enter a function with 0 <= f(x) <= H on the interval [a,b] DEF f(x) = cos(x) LET a = 0 LET b = 1.570796 ! approx pi/2 LET H = 1 RANDOMIZE ! User resets the seed CLEAR INPUT prompt "What is m? ":m LET s = 0 FOR i = 1 TO m LET x = a + (b - a) * rnd ! Choose x randomly in [a,b] LET y = H * rnd ! Choose y randomly in [0,H] IF (f(x) - y) >= 0 THEN LET s = s + 1 END IF NEXT i LET estimate = H * (b - a) * s / m PRINT PRINT "estimate = "; PRINT estimate END ======================= ! Chapter 10. Program Monte-2.tru ! Enter any function and an interval [a,b] DEF f(x) = cos(x) LET a = 0 LET b = 1.570796 !approx pi/2 RANDOMIZE !lets the user reset the seed CLEAR INPUT prompt "What is m? ":m ! Loop here to sum values of f(x) LET sum = 0 FOR i = 1 TO m LET x = a + (b - a) * rnd !choose x randomly in [a,b] LET sum = sum + f(x) NEXT i LET estimate = (b - a) * sum / m PRINT PRINT "estimate = "; estimate END ======================= ! Chapter 11. Program: Euler.tru !Variable L holds the natural logarithm !Initialize: n to 0, SUM to 0, L to 0, DIFF to 0 LET n=0 LET SUM = 0 LET L = 0 LET DIFF = 0 CLEAR !Clears the screen PRINT "A reasonable answer here is 20." INPUT prompt "How many rows do you want to see at a time? ": ROWS IF ROWS < 1 then STOP !Assures value is at least 1 CALL heading PRINT DO LET n = n+1 PRINT using "#####":n; LET SUM = SUM + 1/n ! the SUM is accumulating, ! adding 1/n in the loop PRINT using " ##.############":SUM; PRINT " "; LET L=log(n+1) PRINT using " ##.############":L; PRINT " "; LET DIFF=SUM-L PRINT using " ##.############":DIFF IF mod(n, ROWS) = 0 then IF ROWS > 22 then CALL heading PRINT "Press 'q' to exit the program, ENTER to continue"; GET KEY:quitter !to pause for an input IF quitter = 113 then !checks for "q" pressed STOP !gets you out of the loop ELSE CLEAR ! Clears the screen IF ROWS <= 22 then CALL heading END IF ! This ends the IF A$ = "q" then... END IF ! This ends the IF MOD(N,ROWS) = 0 then LOOP ! This ends the "do" loop SUB heading PRINT "=================================="; PRINT "==================================" PRINT " n SUM(n) "; PRINT "LN(n+1) DIFF=SUM-LN" PRINT "=================================="; PRINT "==================================" END SUB END ======================= ! Chapter 13. Program: Solver.tru CLEAR ! Iterate the function f(x) = cos(x) n times DEF f(x) = cos(x) INPUT PROMPT "What is the initial value of x? ": x INPUT PROMPT "How many iterations? ": n PRINT "The initial value chosen was x = "; x FOR I = 1 to n LET y = f(x) Print i, y LET x = y NEXT i END ======================= !Chapter 14. Program: diagram.tru !The Feigenbaum Diagram for f(x) = Ax(1 - x) !The vertical axis shows the values of the iterates !The horizontal axis shows values of A between 0 and 4 SET WINDOW -.2, 4.5, -.2, 1.2 DEF fnf(x) = A*x*(1 - x) !This part draws axes PLOT LINES: 0, -.1; 0, 1.2 ! y-axis PLOT LINES: -.1,0; 4.5,0 ! x-axis !The outside loop (indexed by i) varies A !The inside loop (indexed by j) computes 100 iterates of f(x) = Ax(1-x) FOR i = 1 to 100 LET A = 4*i/100 LET x = .1 FOR j = 1 TO 100 LET x = fnf(x) !Only the last 50 iterates are plotted !The plot is a tiny line, not a dot, to be more visible IF j > 49 THEN plot lines: A, x; A + .001, x NEXT j NEXT i END ======================= !Chapter 14. Program: itergraf.tru !Slightly changed from the textbook version !Graphical iteration of f(x) = Ax(1 - x) SET WINDOW -.1, 1.1, -.1, 1.1 PRINT "Choose the parameter A, 0 <= A <= 4. What is A "; INPUT A DEF fnf(x) = A*x*(1 - x) PRINT "Choose the initial value 0 <= x0 <= 1. What is x0"; INPUT x0 PRINT "Choose the number of iterations: "; INPUT n !This part draws axes and the line y = x. PLOT LINES: -.1,-.1; 1,1 ! y = x PLOT LINES: -.1,0; 1,0 ! x-axis PLOT LINES: 0,-.1; 0,1 ! y-axis !This part draws the graph of y = f(x). LET old_x = -.1 LET delta = .01 LET x = old_x + delta FOR j = 1 TO 110 PLOT LINES: old_x, FNF(old_x) ; x, FNF(x) LET old_x = x LET x = x + delta NEXT j !this part draws the cobwebs LET x = x0 FOR i = 1 TO n LET y = FNF(x) PLOT LINES: x,x ; x,y PLOT LINES: x,y ; y,y LET x = y NEXT i END ======================= !Chapter 14. Program name: iterquad.tru !Program iterates f(x) = Ax(1-x) CLEAR PRINT "Choose A such that 0 <= A <= 4. What is A"; INPUT A DEF FNF (x) = A*x*(1-x) PRINT "Choose the initial value. What is x0"; INPUT x0 PRINT "Choose the number of iterations. What is n"; INPUT n !This part iterates the function PRINT "The iterates are:" LET x = x0 FOR i = 1 TO n LET y = FNF(x) PRINT i,x LET x = y IF (mod(i,20)=0) then PRINT "Press a key to continue" GET KEY:z ! Pauses every 20 lines END IF NEXT i END ======================= !Chapter 14. Program: iterqua2.tru !Look at dependence on initial conditions of f(x) = 4x(1 - x) CLEAR PRINT "Choose initial value of x1, 0 <= x1 <= 1" INPUT x1 PRINT "Choose initial value of x2, 0 <= x2 <= 1" INPUT x2 PRINT "Choose the number of iterations" INPUT n PRINT "The iterates are" FOR i = 1 TO n PRINT i, x1, x2 LET y1 = 4*x1*(1 - x1) LET y2 = 4*x2*(1 - x2) LET x1 = y1 LET x2 = y2 NEXT i END ======================= !Chapter 14. Program: music.tru !The sound of iterating f(x) = 4x(1 - x) PRINT "How many notes would you like?" INPUT n LET x = .1 FOR i = 1 TO n SOUND 40 + 500*x, 3 !frequency (pitch) 40 + 500x for 3 seconds SOUND 32767, 1 !silence for 1 second LET y = 4*x*(1-x) LET x = y NEXT i END ======================= !Chapter 14. Program: random.tru !This program looks at randomness of f(x)=4x(1 - x) PRINT "Choose initial value of x, 0 <= x0 <= 1" INPUT x0 PRINT "Choose the number of iterations" INPUT n LET n1 = 0 LET n2 = 0 LET x = x0 LET i = 0 FOR i = 1 to n IF x < (1/2) THEN LET n1 = n1 + 1 ELSE LET n2 = n2 + 1 END IF LET y = 4*x*(1 - x) LET x = y NEXT i PRINT "The number of values between 0 and 1/2 is "; n1 PRINT "The number of values between 1/2 and 1 is "; n2 END ======================= ! Chapter 14. Program: transit2.tru ! This is a REPLACEMENT PROGRAM for that on p. 240 of the text. ! This program looks at transitivity of f(x)=4x(1-x). ! It iterates the function f(x) until the iteration value falls ! in the chosen interval, or until it has done MAX_IT iterations. LET MAX_IT = 1000 CLEAR PRINT "Choose target interval [k/m , (k+1)/m]." PRINT PRINT "First choose m > 0. What is m"; INPUT m PRINT DO PRINT "Now choose 0 <= k < ";m;". What is k"; INPUT k LOOP WHILE ((k<0) or (k>=m)) DO PRINT PRINT "The target interval is"; PRINT PRINT " = [";k;"/";m;",";k+1;"/";m;"] = [";k/m;",";(k+1)/m;"]" PRINT PRINT "Choose an initial value of x, 0 <= x <= 1," PRINT " NOT in the target interval. What is x"; INPUT x LOOP WHILE ((x<0) or (x>1)) LET i = 0 DO WHILE ((abs(x-(2*k+1)/(2*m)) > 1/(2*m)) and (i < MAX_IT)) LET y = 4*x*(1 - x) LET x = y LET i = i + 1 LOOP IF ((k/m<=x) and (x<=(k+1)/m)) then PRINT PRINT "After i =";i;" iterations, the value x =";x;" is in the chosen" PRINT " target interval: "; ELSE PRINT PRINT "x(";MAX_IT;") = ";x PRINT "After ";MAX_IT;" iterations, x has not appeared "; PRINT "in the chosen target interval:" END IF PRINT " [";k;"/";m;",";k+1;"/";m;"] = [";k/m;",";(k+1)/m;"]." END ======================= ! Chapter 15. Program: Planar1.tru DIM A(2,2) SET WINDOW -10, 10, -10, 10 INPUT x0 INPUT y0 !plot it PLOT xold,yold LET A(1,1) = 1/5 LET A(1,2) = 99/100 LET A(2,1) = 1 LET A(2,2) = 0 LET xold = x0 LET yold = y0 FOR iteration = 1 to 25 ! Map vector and plot LET xnew = A(1,1) * xold + A(1,2) * yold LET ynew = A(2,1) * xold + A(2,2) * yold PRINT xnew/xold, ynew/yold, ynew/xnew PLOT xnew, ynew ! Keep the new vectors for the next iteration LET xold = xnew LET yold = ynew NEXT iteration END ======================= ! Chapter 15. Program: Planar2.tru DIM x0(17), y0(17), xold(17), yold(17), xnew(17), ynew(17), A(2,2) SET WINDOW -10, 10, -10, 10 !17 initial random vectors in plane FOR j = 1 to 17 LET X0(j) = 2 * rnd - 1 ! 0 <= rnd <= 1 LET y0(j) = 2 * rnd - 1 ! -1 <= x0, y0 <= 1 LET xold(j) = x0(j) LET yold(j) = y0(j) NEXT j ! Plot them FOR j = 1 to 17 PLOT x0(j), y0(j) NEXT j LET A(1,1) = 1/5 LET A(1,2) = 99/100 LET A(2,1) = 1 LET A(2,2) = 0 FOR iteration = 1 to 25 !map vectors and plot them FOR j = 1 to 17 LET xnew(j) = A(1,1) * xold(j) + A(1,2) * yold(j) LET ynew(j) = A(2,1) * xold(j) + A(2,2) * yold(j) PLOT xnew(j), ynew(j) !keep the new vectors for the next iteration LET xold(j) = xnew(j) LET yold(j) = ynew(j) NEXT j NEXT iteration END ======================= !Chapter 16. Program: euclid-c.tru PRINT "Enter a = a1 + a2 i" INPUT prompt "a1 = ":a1 INPUT prompt "a2 = ":a2 PRINT "Enter b = b1 + b2 i" INPUT prompt "b1 = ":b1 INPUT prompt "b2 = ":b2 LET k=0 LET v1 = a1 ! v = a LET v2 = a2 DO WHILE (b1 <> 0) OR (b2 <> 0) ! While b <> 0 LET u1 = a1 ! u = a LET u2 = a2 LET v1 = b1 ! v = b LET v2 = b2 LET a1 = b1 ! a = b LET a2 = b2 ! c = u/v = c1 + c2 i is in Q(i) LET c1 = (u1 * v1 + u2 * v2) / (v1 ^ 2 + v2 ^ 2) LET c2 = (u2 * v1 - u1 * v2) / (v1 ^ 2 + v2 ^ 2) ! Replace c by "nearby" complex integer q IF c1 - INT(c1) <= .5 THEN LET c1 = INT(c1) ELSE LET c1 = INT(c1) + 1 END IF IF c2 - INT(c2) <= .5 THEN LET c2 = INT(c2) ELSE LET c2 = INT(c2) + 1 END IF LET b1 = u1 - (v1 * c1 - v2 * c2) ! r = b1 + b2 i = u - vq LET b2 = u2 - (v1 * c2 + v2 * c1) ! r is a complex integer PRINT "q="; c1; "+"; c2; "i"; " r="; b1; "+"; b2; "i" LET k = k + 1 LOOP PRINT "GCD is"; v1; "+"; v2; "i" PRINT "Number of steps was"; k END ======================= !Chapter 16. Program: proportn.tru RANDOMIZE ! m = number of pairs a, b selected randomly ! N = bound on coeffs of a and b PRINT "Integer m is the number of pairs a, b selected randomly." INPUT prompt "What is m? ": m PRINT "N is the bound on the coefficients of a and b." INPUT prompt "What is N? ": N LET s = 0 ! Counts relatively prime pairs FOR j = 1 TO m LET a1=INT(1+N*RND) ! Randomly choose complex integer a LET a2=INT(1+N*RND) LET b1=INT(1+N*RND) ! Randomly choose complex integer b LET b2=INT(1+N*RND) LET v1 = a1 ! v = a LET v2 = a2 DO WHILE (b1 <> 0) OR (b2 <> 0) ! While b <> 0 LET u1 = a1 ! u = a LET u2 = a2 LET v1 = b1 ! v = b LET v2 = b2 LET a1 = b1 ! a = b LET a2 = b2 ! c = u/v = c1 + c2 i is in Q(i) LET c1 = (u1 * v1 + u2 * v2) / (v1 ^ 2 + v2 ^ 2) LET c2 = (u2 * v1 - u1 * v2) / (v1 ^ 2 + v2 ^ 2) ! Replace c by "nearby" complex integer q IF c1 - INT(c1) <= .5 THEN LET c1 = INT(c1) ELSE LET c1 = INT(c1) + 1 END IF IF c2 - INT(c2) <= .5 THEN LET c2 = INT(c2) ELSE LET c2 = INT(c2) + 1 END IF LET b1 = u1 - (v1 * c1 - v2 * c2) ! r = b1 + b2 i = u - vq LET b2 = u2 - (v1 * c2 + v2 * c1) ! r is a complex integer LOOP ! If last nonzero remainder v is 1, -1, i, or -i, ! then a and b are relatively prime IF (v1=1) AND (v2=0) THEN LET s = s + 1 IF (v1=-1) AND (v2=0) THEN LET s = s + 1 IF (v1=0) AND (v2=1) THEN LET s = s + 1 IF (v1=0) AND (v2=-1) THEN LET s = s + 1 NEXT j PRINT "Proportion of relatively prime pairs is"; s/m END