-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibmath.kde
342 lines (308 loc) · 6.75 KB
/
libmath.kde
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
`Package libmath
-------------------------------
This is a library for different mathematical and statistical tools for Kode.
Constants:
e : Eular's Number e=2.7182818284590452354 with available max precision value.
pi : The number π = 3.14159265358979323846 with available max precision value.
`;
fun isNumber(m){try{m.asReal();return True;}except{return False;}}
fun isNatural(m)
{
if(isNumber(m))
{
var c=String(m);
var l=[];
for(var r=0;r<10;r=r+1)l.append(String(r));
var k=0;
for(var i=0;i<len(c);i=i+1){for(var j=0;j<10;j=j+1){if(c[i]==l[j])k=k+1;}}
if(k<len(c) or k==0)return False;
else return True;
}
else return False;
}
fun gcd(m,n)
{
`Function gcd(m,n)
-----------------------------------
parameter
---------
m,n : two integers.
returns
-------
The greatest common divisor of m and n.\n`;
if(m==n and m!=0) return m;
else if(m==0 and n!=0) return n;
else if(n==0 and m!=0) return m;
else if(n>m) return gcd(n,m);
else if(m==0 and n==0)return NaN;
else return gcd(m%n,n);
}
fun lcm(m,n)
{
`Function lcm(m,n)
-----------------------------------
parameter
---------
m,n : two integers.
returns
-------
The least common multiple of m and n.\n`;
if(m==0 or n==0)return NaN;
else
{
var x=m*n;
return x/gcd(m,n);
}
}
fun factorial(x)
{
`Function factorial(x)
-----------------------------------
parameter
---------
x : a Nonnegative integer.
returns
-------
The factorial of x.\n`;
if(isNatural(x))
{
if(x==0)return 1;
else
{
var fac = 1;
for(var i=x;i>0;i=i-1)fac=fac*i;
return fac;
}
}
else raise Error("Argument must be a nonnegative integer.").skip(1);
}
fun abs(x) {
`Function abs(x)
-----------------------------------
This is a Sample Documentation for the funtion abs().
parameter
---------
x : the number whose absolute value is to be calculated
returns
-------
The absolute value of x i.e., |x|.\n`;
if(isNumber(x))
{
if(x>=0) return x;
else return -x;
}
else raise Error("Argument must be a number.").skip(1);
}
fun comb(n, k){
if(!isNatural(n)) raise Error("Argument n must be a nonnegative integer").skip(1);
if(!isNatural(k)) raise Error("Argument k must be a nonnegative integer").skip(1);
if(k<=n) return factorial(n) / (factorial(k) * factorial(n-k));
else return 0;
}
fun perm(n, k)
{
if(!isNatural(n)) raise Error("Argument n must be a nonnegative integer").skip(1);
if(!isNatural(k)) raise Error("Argument n must be a nonnegative integer").skip(1);
if(k<=n)return comb(n,k)*factorial(k);
else return 0;
}
fun isPrime(m)
{
`Function isPrime(n)
-----------------------------------
parameter
---------
n : the number to check prime or not.
returns
-------
The boolean value True or False.
\n`;
if(isNatural(m))
{
var t=0;
var b=Number(m);
var s=b**0.5;
if(b==1 or b==0)t=1;
else if(b==2 or b==3)t=0;
else if(b%6==1 or b%6==5){for(var x=5;x<=s;x=x+2){if(b%x==0){t=1;break;}}}
else t=1;
if(t==1)return False;
else return True;
}
else raise Error("Argument must be a nonnegative integer").skip(1);
}
fun ceil(x)
{
if(!isNumber(x)) return Error("Argument x must be a number");
else{if(x%1!=0) return ((x+1)\1).asInt();
else return x.asInt();}
}
fun floor(x)
{
`Function floor(x)
-----------------------------------
parameter
---------
x : a integer.
returns
-------
The floor value of x.
\n`;
if(!isNumber(x)) return Error("Argument x must be a number");
else return (x\1).asInt();
}
fun fib(n)
{
`Function fib(n)
-----------------------------------
parameter
---------
n : index of the Fibonacci number.
returns
-------
The nth Fibonacci number.
\n `;
var a=1,b=1,c;
if(isNatural(n) and Number(n)>0)
{
for(var i=1;i<Number(n);i=i+1)
{
c=a+b;
a=b;
b=c;
}
return a;
}
else raise Error("Argument must be a positive integer").skip(1);
}
fun modulo(a,b,c)
{
var x=1;
while(b>0)
{
if(b%2==1)
{
x=(x*a)%c;
}
a=(a*a)%c;
b=b/2;
}
return x%c;
}
fun fermat(p)
{
var n=Number(p);
if(isNatural(n) and n>0)
{
var a=2;
if(n<=200000000)return isPrime(n);
else if(n%5==0 or n%3==0)return False;
else if(modulo(a,n-1,n)!=1)return False;
else return True;
}
else raise Error("Argument must be a positive integer").skip(1);
}
fun sum(varargin)
{
`Function sum(varargin)
-----------------------------------
parameter
---------
Comma seperated Numbers like sum(1,2,3) or only a List of Numbers like sum([1,2,3]).
returns
-------
The sum of the numbers or the sum of the numbers of the List.
`;
if(len(varargin)==1)
{
if(isinstance(varargin[0],List))
{
var s=0;
for(var i=0;i<len(varargin[0]);i=i+1)
{
if(isNumber(varargin[0][i]))s=s+varargin[0][i];
else{s="Error";break;}
}
if(s=="Error")raise Error("Invalid Input").skip(1);
else return s;
}
else if(isinstance(varargin[0],Number)) return varargin[0];
else raise Error("Invalid Input").skip(1);
}
else
{
var s=0;
for(var i=0;i<len(varargin);i=i+1)
{
if(isNumber(varargin[i]))s=s+varargin[i];
else{s="Error";break;}
}
if(s=="Error")raise Error("Invalid Input").skip(1);
else return s;
}
}
fun prod(varargin)
{
`Function prod(varargin)
-----------------------------------
parameter
---------
Comma seperated Numbers like prod(1,2,3) or only a List of Numbers like prod([1,2,3]).
returns
-------
The product of the numbers or the product of the numbers of the List.
\n `;
if(len(varargin)==1)
{
if(isinstance(varargin[0],List))
{
var s=1;
for(var i=0;i<len(varargin[0]);i=i+1)
{
if(isNumber(varargin[0][i]))s=s*varargin[0][i];
else{s="Error";break;}
}
if(s=="Error")raise Error("Invalid Input").skip(1);
else return s;
}
else if(isinstance(varargin[0],Number)) return varargin[0];
else raise Error("Invalid Input").skip(1);
}
else
{
var s=1;
for(var i=0;i<len(varargin);i=i+1)
{
if(isNumber(varargin[i]))s=s*varargin[i];
else{s="Error";break;}
}
if(s=="Error")raise Error("Invalid Input").skip(1);
else return s;
}
}
fun calfac(n)
{
if(isNatural(n)){
if(n== 1 or n== 0)return 1;
var handle_odd = False;
var upto_number = n;
if(n%2==1)
{
upto_number = upto_number-1;
handle_odd = True;
}
var next_sum = upto_number;
var next_multi = upto_number;
var factoria = 1;
while(next_sum >= 2)
{
factoria =factoria*next_multi;
next_sum=next_sum-2;
next_multi= next_multi+next_sum;
}
if (handle_odd)factoria= factoria*n;
return factoria;}
else raise Error("Argument must be a nonnegative integer").skip(1);
}
var e=2.7182818284590452354;
var pi=3.14159265358979323846;