Problem 3 Message #29 Posted by Crawl on 15 Sept 2006, 2:56 p.m., in response to message #1 by Valentin Albillo
Problem 3 seems to be the easiest.
The principle here is just to add a term if the sum is less than the desired value, subtract if it's greater, and go to the next term (whichever the case may be) if they're equal.
You put the desired value on the stack, and select "okay" from the choose boxes as long as you want more terms. Selecting "cancel" will end the program, with the desired value on level 2, and the approximation using the sum, for comparison, on level 1.
%%HP: T(3)A(R)F(.);
\<< 0. 0. 0. \> V P N S
\<< 1.
DO DROP V S >
IF
THEN P 4. * 1. + DUP INV S + 'S' STO P 1. + 'P' STO
ELSE V S <
IF
THEN N 4. * 3. + NEG DUP INV S + 'S' STO N 1. + 'N' STO
ELSE V S ==
IF
THEN P N \<=
IF
THEN P 4. * 1. + DUP INV S + 'S' STO P 1. + 'P' STO
ELSE N 4. * 3. + NEG DUP INV S + 'S' STO N 1. + 'N' STO
END
END
END
END \>STR DUP "{{" SWAP "}}" + + OBJ\> 1. CHOOSE
UNTIL NOT
END V S
\>>
\>>
The first few terms in the sequences for various desired values:
pi/4
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23
pi/5
1, 3, 7, 5, 11, 15, 9, 19, 13, 23, 27, 17
e/4
1, 3, 5, 7, 11, 15, 13, 19, 23, 17, 27, 31
Sin(1)
1, 3, 5, 7, 9, 13, 11, 17, 15, 21, 19, 25
Cos(1)
1, 3, 7, 5, 11, 15, 19, 9, 23, 27, 31, 13
1
1, 3, 5, 9, 13, 7, 17, 21, 11, 25, 29, 15, 33
0
1, 3, 7, 11, 15, 19, 23, 27, 31, 35, 39,
43, 47, 51, 55, 59, 63, 67, 71, 75, 5
11/3+1/5
1, 3, 5, 7, 9, 13, 11, 17, 15, 21, 25, 19
.71
1, 3, 5, 7, 11, 9, 15, 13, 19, 17, 23, 27, 21
.41
1, 3, 7, 11, 15, 5, 19, 23, 27, 31, 9, 35
Edited: 15 Sept 2006, 2:58 p.m.
