blob: 690868386c0a10cf0654b3c3ae03954fe32a4fab (
plain) (
blame)
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
|
namespace;
include rt;
// a +1 loop whose bound is len of the fixed array: the check is gone, the sum is not
fn acc(fixed int[?] f) {
int n = len(f), i = 0, s = 0;
for (; i < n; i += 1) f[i] = i + 1;
for (i = 0; i < n; i++) s = s + f[i];
return s;
}
o3 fn acc3(fixed int[?] f) {
int n = len(f), i = 0, s = 0;
for (; i < n; i++) f[i] = i + 1;
for (i = 0; i < n; i++) s += f[i];
return s;
}
o3 fn small(fixed int[?] f) {
int n = len(f), c = 0, i, j;
for (i = 2; i < n; i++) f[i] = 1;
for (i = 2; i * i < n; i++)
if (f[i]) for (j = i * i; j < n; j += i) f[j] = 0;
for (i = 2; i < n; i++) c += f[i];
return c;
}
fn main() {
fixed int[?] f = rt.new(fixed int[?], 10);
fixed int[?] g = rt.new(fixed int[?], 20);
rt.put_n(acc(f)); rt.put_c(32); rt.put_n(acc3(f)); rt.put_c(32); rt.put_n(small(g)); rt.put_c(10);
rt.free(d*f); rt.free(d*g);
return 0;
}
|