-
Notifications
You must be signed in to change notification settings - Fork 0
/
brmbrmfunctions.js
83 lines (78 loc) · 4.18 KB
/
brmbrmfunctions.js
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
e = Fraction("2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320030599218174135966290435729003342952605956307381323286279434907632338298807531952510190115738341879307021540891499348841675092447614606680822648001684774118537423454424371075390777449920695517027618386062613313845830007520449338265602976067371132007093287091274437470472306969772093101416928368190255151086574637721112523897844250569536967707854499699679468644549059879316368892300987931277361782154249992295763514822082698951936680331825288693984964651058209392398294887933203625094431173012381970684161403970198376793206832823764648042953118023287825098194558153017567173613320698112509961818815930416903515988885193458072738667385894228792284998920868058257492796104841984443634632449684875602336248270419786232090021609902353043699418491463140934317381436405462531520961836908887070167683964243781405927145635490613031072085103837505101157477041718986106873969655212671546889570350354")
pi = Fraction("3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989")
function exponent(fraction, exponent, iter = 100) { // partially stolen algorithm
var expt = new Fraction(exponent.toString().replace("-",""))
var fract = new Fraction(fraction.toString().replace("-",""))
if (expt.mod() == 0) return fract.pow(expt)
var a = Fraction('0')
var b = Fraction(fract)
var denom = Fraction(exponent.toString().replace("-","")).toFraction().split("/")[1]
for (var n = 0; n <= iter; n++) {
var c = a.add(b).div('2');
var test = c.pow(denom)
if (test.compare(fract) < 0) {
a = c;
} else {
b = c;
}
}
if (c.round().pow(denom).equals(fract)) c = c.round()
return c.pow(expt.toFraction().split("/")[0])
}
function calce(iter = 21) {
var iter = BigInt(iter)
e2 = Fraction('2')
var factr = BigInt('1')
var factrc = BigInt('2')
var bigone = 1n
var fracone = Fraction('1')
while (factrc < iter) {
factr *= factrc
factrc += bigone
e2 = e2.add(Fraction('1', factr))
}
return e2
}
function calcpi(iter = 20) { // stolen algorithm
var S = Fraction('13591409')
var K = Fraction('6')
var M = Fraction('1')
var L = Fraction('13591409')
var X = Fraction('1')
var iter = BigInt(iter)
var bigone = Fraction('1')
var ladd = Fraction('545140134')
var xmul = Fraction('-262537412640768000')
var bigthree = Fraction('3')
var bigsixteen = Fraction('16')
var bigtwelve = Fraction('12')
var countr = 0n
while (countr < iter) {
M = K.pow(bigthree).sub(bigsixteen.mul(K)).mul(M)
L = L.add(ladd)
X = X.mul(xmul)
S = S.add(M.mul(L).div(X))
K = K.add(bigtwelve)
countr += 1n
}
var toiter = iter > 100n ? iter : 100n
pi2 = Fraction('426880').mul(exponent('10005', '0.5', toiter)).div(S)
return pi2
}
function logarithm(base, numb, iter = 100, places = 0) {
var base = Fraction(base)
var numb = Fraction(numb)
if (numb.compare('1') < 0) numb = Fraction('1').div(numb)
var a = Fraction('0')
var b = numb
for (var n = 0; n <= iter; n++) {
var c = a.add(b).div('2').ceil()
var test = base.pow(c)
if (test.compare(numb) < 0) {
a = c;
} else {
b = c;
}
}
return c
}