-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
routines.bc
166 lines (147 loc) · 4.02 KB
/
routines.bc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
### routines.bc
# Display the Pythagorean triple generated by parameters m and n,
# and return the hypotenuse
define pythagtriple(m,n) {
print abs(n*n-m*m), "\n"
print abs(2*m*n), "\n"
return m*m+n*n
}
# Display the Pythagorean quadruple generated by parameters m, n, p, and q,
# and return the hypotenuse
define pythagquadruple(m,n,p,q) {
print abs(n*n+m*m-p*p-q*q), "\n"
print abs(2*(m*q+n*p)), "\n"
print abs(2*(n*q-m*p)), "\n"
return m*m+n*n+p*p+q*q
}
# Iteratively applies Newton's Method n times, printing each result,
# with initial guess x (parameter) and (global) functions f and ff=f'
define void newtoniter(x,n) {
auto i
for (i=0; i<=n; x -= f(x)/ff(x))
print "n=", i++, " | ", x, "\n"
}
# Print the roots and vertex coordinates of ax^2+bx+c
define void quadratic(a,b,c) {
print "For vertex (h,k) and roots r[1] and r[2],\n"
h = -b/(2*a)
k = a*h^2 + b*h + c
print "h = ", h, "\n"
print "k = ", k, "\n"
if (b^2-4*a*c < 0) {
print "The roots are non-real: \n"
scale/=2
print h/1, " ± i", sqrt(k/a), "\n"
scale*=2
} else {
r[1] = h - sqrt(-k/a)
r[2] = h + sqrt(-k/a)
print "r[1] = ", r[1], "\n"
print "r[2] = ", r[2], "\n"
}
}
# Print increasingly better rational approximations
# to the decimal number x obtained by truncating its continued fraction
define void rational(x) {
auto i,a,h,k,m
m = x
a[0] = int(x)
h[0] = a[0]
k[0] = 1
if (m == h[0]/k[0]) { print h[0]/k[0], " is an integer ✓\n"; return }
x=1/(x-a[0])
a[1] = int(x)
h[1] = a[1]*h[0]+1
k[1] = a[1]
if (m == h[1]/k[1]) { print h[1]/k[1], " = ", h[1], "/", k[1], " and is rational ✓\n"; return }
x=1/(x-a[1])
for (i=1; m!=h[i]/k[i]; ++i) {
print h[i]/k[i], " = ", h[i], "/", k[i], "\n"
a[i+1] = int(x)
h[i+1] = a[i+1]*h[i]+h[i-1]
k[i+1] = a[i+1]*k[i]+k[i-1]
x=1/(x-a[i+1])
}
print m/1, " = ", h[i], "/", k[i], " ✓\n"
}
# Display n expressed in bases 2,3,…,36
define void bases(n) {
auto i,base
base = obase
for (i=2; i<=36; i++) {
obase = 10
if (i<10) print " "
print " ", i, " | "
obase = i
print n, "\n"
}
obase = base
}
# Display the prime integer factorization of n
define void factor(n) {
auto i, s, p
s = scale
scale = 0
for (i=1; n!=1; i++) {
p=prime(i)
while (n % p == 0) {
print p, " "
n/=p
}
}
print "✓\n"
scale = s
return
}
# Convert 2-dimensional rectangular coordinates to polar coordinates
define void rect2polar(x,y) {
r = sqrt(x^2+y^2)
theta = atan2(y,x)
print "r = ", r, "\n"
print "theta = ", theta, "\n"
return
}
# Convert 2-dimensional polar coordinates to rectangular coordinates
define void polar2rect(r,theta) {
x = r*c(theta)
y = r*s(theta)
print "x = ", x, "\n"
print "y = ", y, "\n"
return
}
# Convert 3-dimensional rectilinear coordinates to cylindrical coordinates
define void rect2cyl(x,y,zed) {
rect2polar(x,y)
z = zed
print "z = ", z, "\n"
return
}
# Convert 3-dimensional cylindrical coordinates to rectilinear coordinates
define void cyl2rect(r,theta,zed) {
polar2rect(r,theta)
z = zed
print "z = ", z, "\n"
return
}
# Convert 3-dimensional rectilinear coordinates to spherical coordinates
# This follows the "mathematical" conventions for spherical coordinates
define void rect2sphere(x,y,z) {
rho = sqrt(x^2+y^2+z^2)
theta = atan2(y,x)
phi = arccos(z/rho)
print "rho = ", rho, "\n"
print "theta = ", theta, "\n"
print "phi = ", phi, "\n"
return
}
# Convert 3-dimensional spherical coordinates to rectilinear coordinates
# This follows the "mathematical" conventions for spherical coordinates
define void sphere2rect(rho,theta,phi) {
x = rho*s(phi)*c(theta)
y = rho*s(phi)*s(theta)
z = rho*c(phi)
print "x = ", x, "\n"
print "y = ", y, "\n"
print "z = ", z, "\n"
return
}