Skip to content

Commit cb6987c

Browse files
authored
Add edigits program for Acton (#323)
1 parent ea1f567 commit cb6987c

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

bench/algorithm/edigits/1.act

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# port of 1.py
2+
import math
3+
4+
ln_tau = math.log(6.283185307179586)
5+
ln_10 = math.log(float(10))
6+
7+
8+
def sum_terms(a: int, b: int) -> (int, int):
9+
if b == a + 1:
10+
return 1, b
11+
mid = (a + b) // 2
12+
p_left, q_left = sum_terms(a, mid)
13+
p_right, q_right = sum_terms(mid, b)
14+
return p_left * q_right + p_right, q_left*q_right
15+
16+
17+
def binary_search(n) -> int:
18+
a = 0
19+
b = 1
20+
while not test_k(n, b):
21+
a = b
22+
b *= 2
23+
while b - a > 1:
24+
m = (a + b) // 2
25+
if test_k(n, m):
26+
b = m
27+
else:
28+
a = m
29+
return b
30+
31+
32+
def test_k(n, k) -> bool:
33+
if k <= 0:
34+
return False
35+
else:
36+
ln_k_factorial = float(k) * (math.log(float(k))-1) + 0.5 * ln_tau
37+
log_10_k_factorial = ln_k_factorial / ln_10
38+
return (int(log_10_k_factorial) >= n+50)
39+
40+
actor main(env):
41+
n = 27 if len(env.argv) < 2 else int(env.argv[1])
42+
k = binary_search(n)
43+
p, q = sum_terms(0, k - 1)
44+
p += q
45+
answer = p * (10 ** (n - 1)) // q
46+
s = str(answer)
47+
for i in range(0, n, 10):
48+
if i+10 <= n:
49+
print(s[i:i+10] + "\t:" + str(i+10))
50+
else:
51+
spaces = ""
52+
for j in range(0, (10-(len(str(s[i:])))), 1):
53+
spaces += " "
54+
print(s[i:] + spaces + "\t:" + str(n))
55+
#print(f'{s[i:]}{" "*(10-n%10)}\t:{n}')
56+
await async env.exit(0)

bench/bench_acton.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ problems:
33
- name: helloworld
44
source:
55
- 1.act
6+
- name: edigits
7+
source:
8+
- 1.act
69
- name: pidigits
710
source:
811
- 1.act

0 commit comments

Comments
 (0)