TI-84 Plus CE Python: Diophantine Books
Problem and Circles Inscribed in Squares
Introduction
The
two programs presented today are Python versions from the Idea Book
(Ahl – see Source), where the original programs were posted in
BASIC designed for the Texas Instruments computers in the 1980s. The
Python versions were programmed with the TI-84 Plus CE Python (and
should work on the TI-83 Premium Python Edition). Unfortunately,
the Circle and Squares program may not work on the TI-82 Advanced
Python as that calculator does not have any graphics modules.
Diophantine
Books Problem
A
book store studies the sales of three popular books: A, B, and C,
each with prices PA, PB, and PC. We have the total books sold, the
sales amount, and the prices of the books. How many of each books
were sold?
The
program solve the Diophantine system of equations:
A +
B + C = N
PA *
A + PB * B + PC * C = S
N =
number of books sold
S =
total sales
A,
B, and C are positive integers
The
original BASIC program covered a specific case, where in this version
has the user enter the price of up to three books, total sales, and
number of books sold. The code in Python also counts the number of
solutions.
Code:
books.py
#
Math Calculations
from
math import *
#
Browns Books, pg 43
#
TI HOME IDEA BOOK, 1983
#
Translated to Python
print(“Browns
Books”,”\na+b+c=n”,”\npa*a+pb*b+pc*c=s”)
print(“books:
a, b, c”,”\nbook prices: pa, pb, pc”)
n=int(input(“#
books sold? “))
s=eval(input(“total
sales? “))
pa=eval(input(“price
of book a? “))
pb=eval(input(“price
of book b? “))
pc=eval(input(“price
of book c? “))
print(“Prices:
“,”\n”,pa,”\t\t”,pb,”\t\t”,pc)
print(“—————–“)
#
prices
i=0
for
a in range(1,n+1):
for b in range(1,n+1):
for c in range(1,n+1):
if a+b+c==n and
pa*a+pb*b+pc*c==s:
print(a,”\t\t”,b,”\t\t”,c)
i+=1
print(“\n”,i,”
solutions”)
Circles
In Squares
The
program cirsqu.py calculates four areas:
The
area of a square with a side of length of 2 * r.
The
area of the inscribed circle with radius r.
The
trapped area, which is the area of the square not taken up by the
circle.
A
corner of the area of the square not taken up by the circle, which is
1 / 4 of the trapped area.
The
original program only printed the trapped area and the corner area.
In addition, this Python code uses TI-specific modules, ti_system and
ti_draw.
The
ti_system module is used to temporarily stop execution with the
disp_wait() command. Execution continues with by pressing the [
clear ] key.
The
ti_draw module is used to draw the square, the circle, and the axis.
Both the square and circle are centered at (0,0).
The
code uses a scaling routine based on the radius of the circle, so
that squares look like squares and circles look like circles. The
TI-84 Plus CE Python graphics screen is 320 x 220 pixels. Setting up
the window (xmin, xmax, ymin, and ymax) using the ratio will
accomplish this task. Here are sampling of viewing windows to try:
xmin
= -8, xmax = 8, ymin = -5.25, ymax = 5.25
xmin
= -16, xmax = 16, ymin = -10.5, ymax = 10.5
xmin
= -32, xmax = 32, ymin = -21, ymax = 21
About
the fill_rect and draw_rect commands. The syntax is (x, y, width,
height). The coordinates x and y are supposed to be the upper left
hand corner, however the manual is incorrect in practice. The
correct corner to use is the lower left hand corner.
The OS that was used is 5.8.1.0012 (same number for Python).
Code:
cirsqu.py
from
math import *
from
ti_system import *
from
ti_draw import *
print(“Math
Module Activated”)
print(“Circle
inside a Square”)
print(“circle
of radius r”)
print(“square
of side r”)
r=eval(input(“r?
“))
ac=pi*r**2
asq=(2*r)**2
ad=asq-ac
#
ti system
disp_clr()
print(“Radius:
“,r)
print(“Areas:
\nsquare:”,asq)
print(“circle:
“,ac)
print(“trapped:
“,ad)
print(“1/4
trapped: “,ad/4)
print(“\nPress
[clear] to contiue.”)
#
ti module ti_system
disp_wait()
#
use ti module ti_draw
clear()
#
text factor
f=1
#
window set up
if
r<=5.25:
x=8
y=5.25
else:
y=5.25
while y<r:
y*=2
f*=2
x=y*8/5.25
set_window(-x,x,-y,y)
#
draw axis
set_color(235,235,235)
set_pen(“thin”,”dotted”)
draw_line(-x,0,x,0)
draw_line(0,-y,0,y)
#
draw shapes
set_color(25,125,255)
set_pen(“thin”,”solid”)
#
fill_rect anchors at the LOWER-left hand corner (manual error)
fill_rect(-r,-r,2*r,2*r)
set_color(230,130,0)
fill_circle(0,0,r)
set_color(0,0,0)
draw_text(-x,y-2*f,str(y))
draw_text(x-4*f,-y,str(x))
show_draw()
Source
Ahl,
David H. The Texas Instruments Home Computer Idea Book. Creative
Computing Press. Morris Plains, New Jersey. 1983. pg. 34-35,
105-106 ISBN 0-916688-51-8
Enjoy!
Eddie
All
original content copyright, © 2011-2024. Edward Shore.
Unauthorized use and/or unauthorized distribution for commercial
purposes without express and written permission from the author is
strictly prohibited. This blog entry may be distributed for
noncommercial purposes, provided that full credit is given to the
author.