-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpi.js
144 lines (118 loc) · 3.46 KB
/
pi.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
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
// PI Calculator Web JS
// Calculating Pi number without limitation until 10k digits or more in your browser powered by JS without any third party library!
// https://github.com/BaseMax/PiCalculatorWebJS
const base = Math.pow(10, 11)
const cell_size = Math.floor(Math.log(base) / Math.LN10)
const digits = (count, array, first_value) => {
for(let i = 1; i < count; i++)
array[i] = null
array[0] = first_value
}
const is_empty = (array) => {
for(i = 0; i < array.length; i++)
if(array[i])
return false
return true
}
const add = (n, array1, array2) => {
let carry = 0
for(let i = n - 1; i >= 0; i--) {
array1[i] += array2[i] + carry
if(array1[i] < base)
carry = 0
else {
carry = 1
array1[i] = array1[i] - base
}
}
}
const sub = (n, array1, array2) => {
for(let i = n - 1; i >= 0; i--) {
array1[i] -= array2[i]
if(array1[i] < 0) {
if(i > 0) {
array1[i] += base
array1[i - 1]--
}
}
}
}
const mul = (n, array1, number) => {
let carry = 0
for(let i = n - 1; i >= 0; i--) {
product = (array1[i]) * number
product += carry
if(product >= base) {
carry = Math.floor(product / base)
product -= (carry * base)
}
else
carry = 0
array1[i] = product
}
}
const div = (n, array1, number, array2) => {
carry = 0
for(let i = 0; i < n; i++) {
const value = array1[i] + (carry * base)
const temp = Math.floor(value / number)
carry = value - temp * number
array2[i] = temp
}
}
const arctan = (angle, n, array) => {
const angles = []
const adivK = []
const angle_square = angle * angle
let k = 3
let sign = 0
digits(n, array, 0)
digits(n, angles, 1)
div(n, angles, angle, angles)
add(n, array, angles)
while(!is_empty(angles)) {
div(n, angles, angle_square, angles)
div(n, angles, k, adivK)
if(sign)
add(n, array, adivK)
else
sub(n, array, adivK)
k += 2
sign = 1 - sign
}
}
const calculate = (digit_number) => {
digit_number = +digit_number + 5
const time_start = new Date()
const angle = [5, 239, 0]
const coeff = [4, -1, 0]
const len = Math.ceil(1 + digit_number / cell_size)
const pi_digits = []
const arctans = []
digits(len, pi_digits, 0)
for(var i = 0; coeff[i] != 0; i++) {
arctan(angle[i], len, arctans)
mul(len, arctans, Math.abs(coeff[i]))
if(coeff[i] > 0)
add(len, pi_digits, arctans)
else
sub(len, pi_digits, arctans)
}
mul(len, pi_digits, 4)
let pi = ""
for(i = 0; i < pi_digits.length; i++) {
if(pi_digits[i].length < cell_size && i != 0)
while(pi_digits[i].length < cell_size)
pi_digits[i] = "0" + pi_digits[i]
pi += pi_digits[i]
}
const time_end = new Date()
const time_taken = (time_end.getTime() - time_start.getTime()) / 1000
// console.warn(pi.startsWith("31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865"))
// console.log("PI (" + digit_number + ") = " + pi + "\n" + "It took: " + time_taken + " seconds\n")
return {
digits: digit_number,
pi: pi,
time: time_taken // in seconds
};
}