Skip to content

Commit a904c24

Browse files
committed
add pybNano pi demo
1 parent 7fcc143 commit a904c24

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

boards/pybNano/pi/upi.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
3+
pybNano board
4+
pi calculate demo
5+
6+
'''
7+
8+
from utime import ticks_ms, ticks_diff
9+
10+
def pi(places=1000):
11+
# 3 + 3*(1/24) + 3*(1/24)*(9/80) + 3*(1/24)*(9/80)*(25/168)
12+
# The numerators 1, 9, 25, ... are given by (2x + 1) ^ 2
13+
# The denominators 24, 80, 168 are given by (16x^2 -24x + 8)
14+
extra = 8
15+
one = 10 ** (places+extra)
16+
t, c, n, na, d, da = 3*one, 3*one, 1, 0, 0, 24
17+
18+
while t > 1:
19+
n, na, d, da = n+na, na+8, d+da, da+32
20+
t = t * n // d
21+
c += t
22+
return c // (10 ** extra)
23+
24+
def pi_t(n=1000):
25+
t1 = ticks_ms()
26+
r = pi(n)
27+
t2 = ticks_ms()
28+
print('Calculation time:', ticks_diff(t2, t1), 'ms')
29+
print('Result:', r)
30+
31+
pi_t(1000)

0 commit comments

Comments
 (0)