forked from esovetkin/bc-extensions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextensions.bc
406 lines (343 loc) · 6.46 KB
/
extensions.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/********************************
bc program: extensions.bc
author: Steffen Brinkmann
e-mail: [email protected]
comments: - published under the GPL
- contains functions of trigonometry, exponential functions,
functions of number theory and some mathematical constants
so far.
*********************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/***********************************************
--I-- constants
1.- pi : defined as 4*atan(1)
2.- e : defined as e(1)
--II-- trigonometry:
1.- sin(x) : returns sine of x
2.- cos(x) : returns cosine of x
3.- atan(x) : returns arc tangent of x
4.- tan(x) : returns tangent of x
5.- asin(x) : returns arc sine of x
6.- acos(x) : returns arc cosine of x
7.- cot(x) : returns cotangent of x
8.- acot(x) : returns arc cotangent of x
9.- sec(x) : returns secans of x
10.- cosec(x),csc(x) : returns cosecans of x
11.- asec(x) : returns arc secans of x
12.- acosec(x),ascs(x) : returns arc cosecans of x
13.- sinh(x) : returns hyperbolical sine of x
14.- cosh(x) : returns hyperbolical cosine of x
15.- tanh(x) : returns hyperbolical tangent of x
16.- coth(x) : returns hyperbolical cotangent of x
17.- asinh(x) : returns arc hyperbolical sine of x
18.- acosh(x) : returns arc hyperbolical cosine of x
19.- atanh(x) : returns arc hyperbolical tangent of x
20.- acoth(x) : returns arc hyperbolical cotangent of x
21.- sech(x) : returns secans hyperbolicus of x
22.- cosech(x),csch(x) : returns cosecans hyperbolicus of x
23.- asech(x) : returns arc secans hyperbolicus of x
24.- acosech(x),acsch(x) : returns arc cosecans hyperbolicus of x
--II-- exponential functions:
1.- ln(x) : returns natural logarithm of x
2.- log(x) : returns logarithm (base 10) of x
3.- lb(x),ld(x) : returns logarithm (base 2) of x
4.- pow(x,y) : returns x to the power of y
--III-- number theory:
1.- abs(n) : returns absolute value of n
2.- mod(a,b) : returns a modulo b
3.- factorize(n),fac(n) : prints primefactors of n,
returns number of primefactors
returns 0 if n is a prime number
returns -1 if n is +-1 or 0
CAUTION: 13-digit number may need 30 s
4.- factorial(n),f(n) : returns n factorial
5.- gcd(a,b) : returns the greatest common divisor of a and b
6.- lcm(a,b) : returns the least common multiple of a and b
7.- bessel(n,x) : returns the Bessel function order n of x
8.- cont_frac(x,n) : returns first n integers from continued fraction of x
************************************************/
pi=4*a(1)
e=e(1)
define sin(x)
{
return (s(x))
}
define cos(x)
{
return (c(x))
}
define atan(x)
{
return (a(x))
}
define tan(x)
{
return (s(x)/c(x))
}
define asin(x)
{
if(x==1) return(pi/2)
if(x==-1) return(-pi/2)
return(a(x/sqrt(1-(x^2))))
}
define acos(x)
{
if(x==1) return(0)
if(x==-1) return(pi)
return(pi/2-a(x/sqrt(1-(x^2))))
}
define cot(x)
{
return(c(x)/s(x))
}
define acot(x)
{
return(pi/2-a(x))
}
define sec(x)
{
return(1/c(x))
}
define cosec(x)
{
return(1/s(x))
}
define csc(x)
{
return(1/s(x))
}
define asec(x)
{
return(acos(1/x))
}
define acosec(x)
{
return(asin(1/x))
}
define acsc(x)
{
return(asin(1/x))
}
define sinh(x)
{
return((e(x)-e(-x))/2)
}
define cosh(x)
{
return((e(x)+e(-x))/2)
}
define tanh(x)
{
return((e(x)-e(-x))/e(x)+e(-x))
}
define coth(x)
{
return((e(x)+e(-x))/e(x)-e(-x))
}
define asinh(x)
{
return(l(x + sqrt(x^2 + 1)))
}
define acosh(x)
{
return(l(x + sqrt(x^2 - 1)))
}
define atanh(x)
{
return((l(1 + x) - l(1 - x))/2)
}
define acoth(x)
{
return(atanh(1/x))
}
define sech(x)
{
return(1/cosh(x))
}
define cosech(x)
{
return(1/sinh(x))
}
define csch(x)
{
return(1/sinh(x))
}
define asech(x)
{
return(acosh(1/x))
}
define acosech(x)
{
return(asinh(1/x))
}
define acsch(x)
{
return(asinh(1/x))
}
/************************/
define ln(x)
{
return(l(x))
}
define log(x)
{
return(l(x)/l(10))
}
define lb(x)
{
return(l(x)/l(2))
}
define ld(x)
{
return(lb(x))
}
define pow(x,y)
{
return(e(y*l(x)))
}
/************************/
define abs(n){
if(n>=0) return(n)
return(-n)
}
define mod(a,b){
auto c,tmp_scale
tmp_scale=scale(sqrt(2))
scale=0
c=a%b
scale=tmp_scale
if(a>=0) return(c)
if(c==0) return(0)
return(c+b)
}
define fac(n)
{
auto tmp,i,factors
if(abs(n)<=1)
{
print abs(n),"\nnumber of factors: "
return(0)
}
if(abs(n)==2)
{
print 2,"\nnumber of factors: "
return(1)
}
tmp=n
while(mod(tmp,2)==0)
{
print 2," "
tmp/=2
factors+=1
}
if(prime[0]==2) /*primenumbers.bc is loaded*/
{
i=0
while((prime[i]*prime[i])<=(n+1))
{
if(mod(tmp,prime[i])==0)
{
print prime[i]," "
tmp/=prime[i]
factors+=1
}else{
i+=1
if(i>65535)
{
break
}
}
}
}
if(i>65535)
{
i=prime[65535]
}else
{
i=3
}
while((i*i)<=(n+1))
{
if(mod(tmp,i)==0)
{
print i," "
tmp/=i
factors+=1
}else{
i+=2
}
}
if(tmp!=1)
{
factors+=1
print tmp," " /*BUG: prints zeros after factor*/
}
print "\n"
print "number of factors: "
return(factors)
}
define factorize(n)
{
return (fac(n))
}
define f(n)
{
if (n <= 1) return (1);
return (f(n-1) * n);
}
define factorial(n)
{
return(f(n))
}
define gcd(m,n){
auto a,b,c,tmp_scale
a=abs(m) /* a=r[0] */
if(n==0) return(a)
b=abs(n) /* b=r[1] */
/*tmp_scale=scale(sqrt(2))*/
/*c=a%b /* c=r[2]=r[0] mod(r[1]) */
c=mod(a,b)
while(c>0){
a=b
b=c
/*(c=a%b /* c=r[j]=r[j-2] mod(r[j-1]) */
c=mod(a,b)
}
/*scale=tmp_scale*/
return(b)
}
define lcm(a,b){
auto g
g=gcd(a,b)
if(g==0) return(0)
return(abs(a*b)/g)
}
define bessel(n,x){
return(j(n,x))
}
define cont_frac(x,n) {
auto y;
i=1
while (i < n) {
os=scale; scale=0;
y = x/1;
print y; print " ";
scale=os;
x = 1/(x-y);
i += 1;
}
}