-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
routines.bc
219 lines (197 loc) · 5.22 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
### 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
}
# Print the sequence of positive integers that results from
# iteratively applying the function prescribed by the Collatz Conjecture.
define void collatz(n) {
auto s
s = scale
scale = 0
collatz_(n)
scale = s
}
# Recursive helper function to collatz()
define collatz_(n) {
if(n==1)
return 1
print n, " → ";
if (n%2) {
return collatz_(3*n+1);
} else {
return collatz_(n/2);
}
}
# Print every way that n can be written as a sum
# of consecutive positive integers.
define void sumofpositiveintegers(n) { sumofpowers(n,1) }
# Print every way that m can be written as a sum
# of nth powers of consecutive positive integers.
define void sumofpowers(m,n) {
auto s, i, j, cap, powers[]
print "n = ", n, "\n"
cap = pow(m,1/n)+1
s = scale
scale = 0
for (i=1; i<cap; i++) {
powers[i] = i^n
}
i = j = sum = 1;
while (i<cap) {
if (sum >= m) {
if (sum == m) {
print m, " = ", i, "ⁿ + … + ", j, "ⁿ\n"
}
sum -= powers[i];
i+=1;
} else {
j+=1;
sum += powers[j];
}
}
print " ✓\n"
scale = s
}