Skip to content

Commit e824df5

Browse files
docs: auto-sync Rosetta Code examples from zenc repo
1 parent b9fbe19 commit e824df5

12 files changed

Lines changed: 938 additions & 0 deletions

rosetta/Blum_integer.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
+++
2+
title = "Blum integer"
3+
+++
4+
5+
# Blum integer
6+
7+
{{trans|Wren}}
8+
9+
```zc
10+
import "locale.h"
11+
12+
let inc = [4, 2, 4, 2, 4, 6, 2, 6];
13+
14+
fn is_prime(n: int) -> bool {
15+
if n < 2 { return false; }
16+
if n % 2 == 0 { return n == 2; }
17+
if n % 3 == 0 { return n == 3; }
18+
let d = 5;
19+
while d * d <= n {
20+
if n % d == 0 { return false; }
21+
d += 2;
22+
if n % d == 0 { return false; }
23+
d += 4;
24+
}
25+
return true;
26+
}
27+
28+
// Assumes n is odd.
29+
fn first_prime_factor(n: int) -> int {
30+
if n == 1 { return 1; }
31+
if n % 3 == 0 { return 3; }
32+
if n % 5 == 0 { return 5; }
33+
let k = 7;
34+
let i = 0;
35+
while k * k <= n {
36+
if n % k == 0 {
37+
return k;
38+
} else {
39+
k += inc[i];
40+
i = (i + 1) % 8;
41+
}
42+
}
43+
return n;
44+
}
45+
46+
fn main() {
47+
setlocale(LC_NUMERIC, "");
48+
let blum: [int; 50];
49+
let bc = 0;
50+
let counts: [int; 10];
51+
let digits = [1, 3, 7, 9];
52+
let i = 1;
53+
loop {
54+
let p = first_prime_factor(i);
55+
if p % 4 == 3 {
56+
let q = i / p;
57+
if q != p && q % 4 == 3 && is_prime(q) {
58+
if bc < 50 { blum[bc] = i; }
59+
counts[i % 10]++;
60+
bc++;
61+
if bc == 50 {
62+
println "First 50 Blum integers:";
63+
for j in 0..50 {
64+
print "{blum[j]:3d} ";
65+
if !((j + 1) % 10) { println ""; }
66+
}
67+
println "";
68+
} else if bc == 26_828 || bc % 100_000 == 0 {
69+
println "The {bc:'7d}th Blum integer is: {i:'9d}";
70+
if bc == 400_000 {
71+
println "\n% distribution of the first 400,000 Blum integers:";
72+
for j in digits {
73+
println " {counts[j] / 4000.0:6.3f}% end in {j}";
74+
}
75+
break;
76+
}
77+
}
78+
}
79+
}
80+
i = (i % 5 == 3) ? i + 4 : i + 2;
81+
}
82+
}
83+
```
84+
85+
**Output:**
86+
87+
```
88+
First 50 Blum integers:
89+
21 33 57 69 77 93 129 133 141 161
90+
177 201 209 213 217 237 249 253 301 309
91+
321 329 341 381 393 413 417 437 453 469
92+
473 489 497 501 517 537 553 573 581 589
93+
597 633 649 669 681 713 717 721 737 749
94+
95+
The 26,828th Blum integer is: 524,273
96+
The 100,000th Blum integer is: 2,075,217
97+
The 200,000th Blum integer is: 4,275,533
98+
The 300,000th Blum integer is: 6,521,629
99+
The 400,000th Blum integer is: 8,802,377
100+
101+
% distribution of the first 400,000 Blum integers:
102+
25.001% end in 1
103+
25.017% end in 3
104+
24.997% end in 7
105+
24.985% end in 9
106+
```
107+
108+
---
109+
**Attribution:** This is a community solution for the Rosetta Code task [**Blum integer**](https://rosettacode.org/wiki/Blum_integer) in Zen C.
110+
111+
*This article uses material from the Rosetta Code article **Blum integer**, which is released under the [GNU Free Documentation License 1.3](https://www.gnu.org/licenses/fdl-1.3.html). A list of the original authors can be found in the [page history](https://rosettacode.org/wiki/Blum_integer?action=history).*

rosetta/Brazilian_numbers.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
+++
2+
title = "Brazilian numbers"
3+
+++
4+
5+
# Brazilian numbers
6+
7+
{{trans|Wren}}
8+
9+
```zc
10+
import "locale.h"
11+
12+
fn same_digits(n: int, b: int) -> bool {
13+
let f = n % b;
14+
n /= b;
15+
while n > 0 {
16+
if n % b != f { return false; }
17+
n /= b;
18+
}
19+
return true;
20+
}
21+
22+
fn is_brazilian(n: int) -> bool {
23+
if n < 7 { return false; }
24+
if !(n % 2) && n >= 8 { return true; }
25+
for b in 2..(n - 1) {
26+
if same_digits(n, b) { return true; }
27+
}
28+
return false;
29+
}
30+
31+
fn is_prime(n: int) -> bool {
32+
if n < 2 { return false; }
33+
if n % 2 == 0 { return n == 2; }
34+
if n % 3 == 0 { return n == 3; }
35+
let d = 5;
36+
while d * d <= n {
37+
if n % d == 0 { return false; }
38+
d += 2;
39+
if n % d == 0 { return false; }
40+
d += 4;
41+
}
42+
return true;
43+
}
44+
45+
fn main() {
46+
setlocale(LC_NUMERIC, "");
47+
let kinds = [" ", " odd ", " prime "];
48+
for i, kind in kinds {
49+
println "First 20{kind}Brazilian numbers:";
50+
let c = 0;
51+
let n = 7;
52+
loop {
53+
if is_brazilian(n) {
54+
print "{n} ";
55+
if ++c == 20 {
56+
let nl = i < 2 ? "\n" : "";
57+
println "{nl}";
58+
break;
59+
}
60+
}
61+
if kind == " " {
62+
n++;
63+
} else if kind == " odd " {
64+
n += 2;
65+
} else {
66+
loop {
67+
n += 2;
68+
if is_prime(n) { break; }
69+
}
70+
}
71+
}
72+
}
73+
let c = 0;
74+
let n = 7;
75+
while c < 100_000 {
76+
if is_brazilian(n) { c++; }
77+
n++;
78+
}
79+
println "The 100,000th Brazilian number: {n - 1:'d}";
80+
}
81+
```
82+
83+
**Output:**
84+
85+
```
86+
First 20 Brazilian numbers:
87+
7 8 10 12 13 14 15 16 18 20 21 22 24 26 27 28 30 31 32 33
88+
89+
First 20 odd Brazilian numbers:
90+
7 13 15 21 27 31 33 35 39 43 45 51 55 57 63 65 69 73 75 77
91+
92+
First 20 prime Brazilian numbers:
93+
7 13 31 43 73 127 157 211 241 307 421 463 601 757 1093 1123 1483 1723 2551 2801
94+
The 100,000th Brazilian number: 110,468
95+
```
96+
97+
---
98+
**Attribution:** This is a community solution for the Rosetta Code task [**Brazilian numbers**](https://rosettacode.org/wiki/Brazilian_numbers) in Zen C.
99+
100+
*This article uses material from the Rosetta Code article **Brazilian numbers**, which is released under the [GNU Free Documentation License 1.3](https://www.gnu.org/licenses/fdl-1.3.html). A list of the original authors can be found in the [page history](https://rosettacode.org/wiki/Brazilian_numbers?action=history).*

rosetta/Collections.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
+++
2+
title = "Collections"
3+
+++
4+
5+
# Collections
6+
7+
As well as C style arrays, Zen C's standard library also supports vectors, maps, sets, stacks and queues. The following example uses a vector which is a generic growable array:
8+
9+
```zc
10+
import "std/vec.zc";
11+
12+
fn main() {
13+
let vec = Vec<int>::new();
14+
vec << 1;
15+
vec << 2;
16+
vec << 3;
17+
for v in vec { println "{v}"; }
18+
}
19+
```
20+
21+
**Output:**
22+
23+
```
24+
1
25+
2
26+
3
27+
```
28+
29+
---
30+
**Attribution:** This is a community solution for the Rosetta Code task [**Collections**](https://rosettacode.org/wiki/Collections) in Zen C.
31+
32+
*This article uses material from the Rosetta Code article **Collections**, which is released under the [GNU Free Documentation License 1.3](https://www.gnu.org/licenses/fdl-1.3.html). A list of the original authors can be found in the [page history](https://rosettacode.org/wiki/Collections?action=history).*

rosetta/Common_list_elements.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
+++
2+
title = "Common list elements"
3+
+++
4+
5+
# Common list elements
6+
7+
```zc
8+
import "std/vec.zc"
9+
10+
fn intersection(a: Vec<int>*, b: Vec<int>*) -> Vec<int> {
11+
let res = Vec<int>::new();
12+
for e in *a {
13+
if b.contains(e) { res << e; }
14+
}
15+
return res;
16+
}
17+
18+
fn main() {
19+
let a1 = [2, 5, 1, 3, 8, 9, 4, 6];
20+
let a2 = [3, 5, 6, 2, 9, 8, 4];
21+
let a3 = [1, 3, 7, 6, 9];
22+
let v1 = Vec<int>::new();
23+
let v2 = Vec<int>::new();
24+
let v3 = Vec<int>::new();
25+
for e in a1 { v1 << e; }
26+
for e in a2 { v2 << e; }
27+
for e in a3 { v3 << e; }
28+
let inter1 = intersection(&v1, &v2);
29+
let inter2 = intersection(&inter1, &v3);
30+
print "[";
31+
for v in inter2 { print "{v}, "; }
32+
println "\b\b]";
33+
}
34+
```
35+
36+
**Output:**
37+
38+
```
39+
[3, 9, 6]
40+
```
41+
42+
---
43+
**Attribution:** This is a community solution for the Rosetta Code task [**Common list elements**](https://rosettacode.org/wiki/Common_list_elements) in Zen C.
44+
45+
*This article uses material from the Rosetta Code article **Common list elements**, which is released under the [GNU Free Documentation License 1.3](https://www.gnu.org/licenses/fdl-1.3.html). A list of the original authors can be found in the [page history](https://rosettacode.org/wiki/Common_list_elements?action=history).*

rosetta/Currency.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
+++
2+
title = "Currency"
3+
+++
4+
5+
# Currency
6+
7+
{{libheader|GMP}}
8+
9+
```zc
10+
//> link: -lgmp
11+
12+
import "gmp.h"
13+
14+
fn main() {
15+
let hamburgers: mpf_t;
16+
let milkshakes: mpf_t;
17+
let price1: mpf_t;
18+
let price2: mpf_t;
19+
let tax_pc: mpf_t;
20+
let total_pre_tax: mpf_t;
21+
let total_tax: mpf_t;
22+
let total_after_tax: mpf_t;
23+
let temp: mpf_t;
24+
25+
mpf_init_set_ui(hamburgers, (c_ulong)4000000000000000);
26+
mpf_init_set_ui(milkshakes, (c_ulong)2);
27+
mpf_init_set_str(price1, "5.5", 10);
28+
mpf_init_set_str(price2, "2.86", 10);
29+
mpf_init_set_str(tax_pc, "0.0765", 10);
30+
mpf_inits(total_pre_tax, total_tax, total_after_tax, temp, NULL);
31+
32+
mpf_mul(total_pre_tax, hamburgers, price1);
33+
mpf_mul(temp, milkshakes, price2);
34+
mpf_add(total_pre_tax, total_pre_tax, temp);
35+
mpf_mul(total_tax, total_pre_tax, tax_pc);
36+
mpf_add(total_after_tax, total_pre_tax, total_tax);
37+
gmp_printf("Total price before tax : $ %20.2Ff\n", total_pre_tax);
38+
gmp_printf("Total tax : $ %20.2Ff\n", total_tax);
39+
gmp_printf("Total price after tax : $ %20.2Ff\n", total_after_tax);
40+
mpf_clears(hamburgers, milkshakes, price1, price2, tax_pc,
41+
total_pre_tax, total_tax, total_after_tax, temp, NULL);
42+
}
43+
```
44+
45+
**Output:**
46+
47+
```
48+
Total price before tax : $ 22000000000000005.72
49+
Total tax : $ 1683000000000000.44
50+
Total price after tax : $ 23683000000000006.16
51+
```
52+
53+
---
54+
**Attribution:** This is a community solution for the Rosetta Code task [**Currency**](https://rosettacode.org/wiki/Currency) in Zen C.
55+
56+
*This article uses material from the Rosetta Code article **Currency**, which is released under the [GNU Free Documentation License 1.3](https://www.gnu.org/licenses/fdl-1.3.html). A list of the original authors can be found in the [page history](https://rosettacode.org/wiki/Currency?action=history).*

0 commit comments

Comments
 (0)