Count the occurrence of each digit in e
fn count_digits_in_e(n: int) {
autofree let l: int* = malloc(n * sizeof(int));
for i in 0..n { l[i] = 1; }
let dc: [int; 10];
dc[2] = 1; // to count the non-fractional digit
for col in 1..(2 * n) {
let a = n + 1;
let c = 0;
for i in 0..n {
c += l[i] * 10;
l[i] = c % a;
c /= a--;
}
dc[c]++;
}
for d in 0..=9 { println ": "; }
let t = 0;
for d in dc { t += d; }
println "Total digits: ";
}
fn main() {
count_digits_in_e(2000);
println "";
count_digits_in_e(3000);
}
Output:
0: 396
1: 395
2: 390
3: 403
4: 387
5: 390
6: 422
7: 408
8: 397
9: 412
Total digits: 4000
0: 602
1: 592
2: 604
3: 625
4: 559
5: 586
6: 644
7: 600
8: 597
9: 591
Total digits: 6000
Attribution: This is a community solution for the Rosetta Code task Count the occurrence of each digit in e in Zen C.
This article uses material from the Rosetta Code article Count the occurrence of each digit in e, which is released under the GNU Free Documentation License 1.3. A list of the original authors can be found in the page history.