Skip to content

Commit e77619b

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

25 files changed

Lines changed: 1738 additions & 0 deletions

rosetta/Abstract_type.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
+++
2+
title = "Abstract type"
3+
+++
4+
5+
# Abstract type
6+
7+
The position is similar to Rust in that Zen C doesn't have *classes* but does have *structs* and the latter can share behavior by implementing *traits* which are similar in concept to *abstract classes*. Expanding a little on the Rust example:
8+
9+
```zc
10+
import "std/math.zc"
11+
12+
trait Shape {
13+
fn area(self) -> f64;
14+
}
15+
16+
struct Square {
17+
side_length: f64;
18+
}
19+
20+
impl Shape for Square {
21+
fn area(self) -> f64 {
22+
return self.side_length * self.side_length;
23+
}
24+
}
25+
26+
struct Circle {
27+
radius: f64;
28+
}
29+
30+
impl Shape for Circle {
31+
fn area(self) -> f64 {
32+
return Math::PI() * self.radius * self.radius;
33+
}
34+
}
35+
36+
// Prints the area of any Shape.
37+
fn print_area(shape: Shape) {
38+
println "{shape.area()}";
39+
}
40+
41+
fn main() {
42+
let square = Square{side_length: 5.0};
43+
let circle = Circle{radius: 2.5};
44+
print_area(&square);
45+
print_area(&circle);
46+
}
47+
```
48+
49+
**Output:**
50+
51+
```
52+
25.000000
53+
19.634954
54+
```
55+
56+
---
57+
**Attribution:** This is a community solution for the Rosetta Code task [**Abstract type**](https://rosettacode.org/wiki/Abstract_type) in Zen C.
58+
59+
*This article uses material from the Rosetta Code article **Abstract type**, 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/Abstract_type?action=history).*
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
+++
2+
title = "Abundant, deficient and perfect number classifications"
3+
+++
4+
5+
# Abundant, deficient and perfect number classifications
6+
7+
```zc
8+
import "locale.h"
9+
10+
fn proper_divisor_sum(n: int) -> int {
11+
let i = 1;
12+
let k = (n % 2 == 0) ? 1 : 2;
13+
let sum = 0;
14+
while i * i <= n {
15+
if n % i == 0 {
16+
sum += i;
17+
let j = n / i;
18+
if j != i { sum += j; }
19+
}
20+
i += k;
21+
}
22+
return sum - n;
23+
}
24+
25+
fn main() {
26+
def LIMIT = 20_000;
27+
let d = 0;
28+
let a = 0;
29+
let p = 0;
30+
for i in 1..=LIMIT {
31+
let j = proper_divisor_sum(i);
32+
if j < i {
33+
d++;
34+
} else if j == i {
35+
p++;
36+
} else {
37+
a++;
38+
}
39+
}
40+
setlocale(LC_NUMERIC, "");
41+
println "Between 1 and {LIMIT:'d} there are:";
42+
println " {d:'6d} deficient numbers";
43+
println " {a:'6d} abundant numbers";
44+
println " {p:'6d} perfect numbers";
45+
}
46+
```
47+
48+
**Output:**
49+
50+
```
51+
Between 1 and 20,000 there are:
52+
15,043 deficient numbers
53+
4,953 abundant numbers
54+
4 perfect numbers
55+
```
56+
57+
---
58+
**Attribution:** This is a community solution for the Rosetta Code task [**Abundant, deficient and perfect number classifications**](https://rosettacode.org/wiki/Abundant,_deficient_and_perfect_number_classifications) in Zen C.
59+
60+
*This article uses material from the Rosetta Code article **Abundant, deficient and perfect number classifications**, 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/Abundant,_deficient_and_perfect_number_classifications?action=history).*

rosetta/Almost_prime.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
+++
2+
title = "Almost prime"
3+
+++
4+
5+
# Almost prime
6+
7+
{{trans|Go}}
8+
9+
```zc
10+
import "std/vec.zc"
11+
12+
fn k_prime(n: int, k: int) -> bool {
13+
let nf = 0;
14+
for i in 2..=n {
15+
while !(n % i) {
16+
if nf++ == k { return false; }
17+
n /= i;
18+
}
19+
}
20+
return nf == k;
21+
}
22+
23+
fn gen(k: int, n: int) -> Vec<int> {
24+
let r = Vec<int>::new();
25+
let len = n;
26+
n = 2;
27+
for _ in 0..len {
28+
while !k_prime(n, k) { n++; }
29+
r << n++;
30+
}
31+
return r;
32+
}
33+
34+
fn main() {
35+
for i in 1..6 {
36+
print "{i} [";
37+
let res = gen(i, 10);
38+
for j in res { print "{j}, "; }
39+
println "\b\b]";
40+
}
41+
}
42+
```
43+
44+
**Output:**
45+
46+
```
47+
1 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
48+
2 [4, 6, 9, 10, 14, 15, 21, 22, 25, 26]
49+
3 [8, 12, 18, 20, 27, 28, 30, 42, 44, 45]
50+
4 [16, 24, 36, 40, 54, 56, 60, 81, 84, 88]
51+
5 [32, 48, 72, 80, 108, 112, 120, 162, 168, 176]
52+
```
53+
54+
---
55+
**Attribution:** This is a community solution for the Rosetta Code task [**Almost prime**](https://rosettacode.org/wiki/Almost_prime) in Zen C.
56+
57+
*This article uses material from the Rosetta Code article **Almost prime**, 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/Almost_prime?action=history).*
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
+++
2+
title = "Angles (geometric), normalization and conversion"
3+
+++
4+
5+
# Angles (geometric), normalization and conversion
6+
7+
{{trans|Wren}}
8+
9+
```zc
10+
import "std/math.zc"
11+
12+
fn d2d(d: f64) -> f64 { return Math::mod(d, 360.0); }
13+
fn g2g(g: f64) -> f64 { return Math::mod(g, 400.0); }
14+
fn m2m(m: f64) -> f64 { return Math::mod(m, 6400.0); }
15+
fn r2r(r: f64) -> f64 { return Math::mod(r, 2.0 * Math::PI()); }
16+
fn d2g(d: f64) -> f64 { return d2d(d) * 400.0 / 360.0; }
17+
fn d2m(d: f64) -> f64 { return d2d(d) * 6400.0 / 360.0; }
18+
fn d2r(d: f64) -> f64 { return d2d(d) * Math::PI() / 180.0; }
19+
fn g2d(g: f64) -> f64 { return g2g(g) * 360.0 / 400.0; }
20+
fn g2m(g: f64) -> f64 { return g2g(g) * 6400.0 / 400.0; }
21+
fn g2r(g: f64) -> f64 { return g2g(g) * Math::PI() / 200.0; }
22+
fn m2d(m: f64) -> f64 { return m2m(m) * 360.0 / 6400.0; }
23+
fn m2g(m: f64) -> f64 { return m2m(m) * 400.0 / 6400.0; }
24+
fn m2r(m: f64) -> f64 { return m2m(m) * Math::PI() / 3200.0; }
25+
fn r2d(r: f64) -> f64 { return r2r(r) * 180.0 / Math::PI(); }
26+
fn r2g(r: f64) -> f64 { return r2r(r) * 200.0 / Math::PI(); }
27+
fn r2m(r: f64) -> f64 { return r2r(r) * 3200 / Math::PI(); }
28+
29+
fn main() {
30+
let f1 = "%15s %15s %15s %15s %15s\n";
31+
let f2 = "%15.7f %15.7f %15.7f %15.7f %15.7f\n";
32+
let angles: f64[12] = [-2.0, -1.0, 0.0, 1.0, 2.0, 6.2831853, 16.0, 57.2957795, 359.0, 399.0, 6399.0, 1000000.0];
33+
34+
printf(f1, "degrees", "norm degs", "gradians", "mils", "radians");
35+
for a in angles {
36+
printf(f2, a, d2d(a), d2g(a), d2m(a), d2r(a));
37+
}
38+
println "";
39+
40+
printf(f1, "gradians", "norm grds", "degrees", "mils", "radians");
41+
for a in angles {
42+
printf(f2, a, g2g(a), g2d(a), g2m(a), g2r(a));
43+
}
44+
println "";
45+
46+
printf(f1, "mils", "norm mils", "degrees", "gradians", "radians");
47+
for a in angles {
48+
printf(f2, a, m2m(a), m2d(a), m2g(a), m2r(a));
49+
}
50+
println "";
51+
52+
printf(f1, "radians", "norm rads", "degrees", "gradians", "mils");
53+
for a in angles {
54+
printf(f2, a, r2r(a), r2d(a), r2g(a), r2m(a));
55+
}
56+
}
57+
```
58+
59+
**Output:**
60+
61+
```
62+
degrees norm degs gradians mils radians
63+
-2.0000000 -2.0000000 -2.2222222 -35.5555556 -0.0349066
64+
-1.0000000 -1.0000000 -1.1111111 -17.7777778 -0.0174533
65+
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
66+
1.0000000 1.0000000 1.1111111 17.7777778 0.0174533
67+
2.0000000 2.0000000 2.2222222 35.5555556 0.0349066
68+
6.2831853 6.2831853 6.9813170 111.7010720 0.1096623
69+
16.0000000 16.0000000 17.7777778 284.4444444 0.2792527
70+
57.2957795 57.2957795 63.6619772 1018.5916356 1.0000000
71+
359.0000000 359.0000000 398.8888889 6382.2222222 6.2657320
72+
399.0000000 39.0000000 43.3333333 693.3333333 0.6806784
73+
6399.0000000 279.0000000 310.0000000 4960.0000000 4.8694686
74+
1000000.0000000 280.0000000 311.1111111 4977.7777778 4.8869219
75+
76+
gradians norm grds degrees mils radians
77+
-2.0000000 -2.0000000 -1.8000000 -32.0000000 -0.0314159
78+
-1.0000000 -1.0000000 -0.9000000 -16.0000000 -0.0157080
79+
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
80+
1.0000000 1.0000000 0.9000000 16.0000000 0.0157080
81+
2.0000000 2.0000000 1.8000000 32.0000000 0.0314159
82+
6.2831853 6.2831853 5.6548668 100.5309648 0.0986960
83+
16.0000000 16.0000000 14.4000000 256.0000000 0.2513274
84+
57.2957795 57.2957795 51.5662016 916.7324720 0.9000000
85+
359.0000000 359.0000000 323.1000000 5744.0000000 5.6391588
86+
399.0000000 399.0000000 359.1000000 6384.0000000 6.2674773
87+
6399.0000000 399.0000000 359.1000000 6384.0000000 6.2674773
88+
1000000.0000000 0.0000000 0.0000000 0.0000000 0.0000000
89+
90+
mils norm mils degrees gradians radians
91+
-2.0000000 -2.0000000 -0.1125000 -0.1250000 -0.0019635
92+
-1.0000000 -1.0000000 -0.0562500 -0.0625000 -0.0009817
93+
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
94+
1.0000000 1.0000000 0.0562500 0.0625000 0.0009817
95+
2.0000000 2.0000000 0.1125000 0.1250000 0.0019635
96+
6.2831853 6.2831853 0.3534292 0.3926991 0.0061685
97+
16.0000000 16.0000000 0.9000000 1.0000000 0.0157080
98+
57.2957795 57.2957795 3.2228876 3.5809862 0.0562500
99+
359.0000000 359.0000000 20.1937500 22.4375000 0.3524474
100+
399.0000000 399.0000000 22.4437500 24.9375000 0.3917173
101+
6399.0000000 6399.0000000 359.9437500 399.9375000 6.2822036
102+
1000000.0000000 1600.0000000 90.0000000 100.0000000 1.5707963
103+
104+
radians norm rads degrees gradians mils
105+
-2.0000000 -2.0000000 -114.5915590 -127.3239545 -2037.1832716
106+
-1.0000000 -1.0000000 -57.2957795 -63.6619772 -1018.5916358
107+
0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
108+
1.0000000 1.0000000 57.2957795 63.6619772 1018.5916358
109+
2.0000000 2.0000000 114.5915590 127.3239545 2037.1832716
110+
6.2831853 6.2831853 359.9999996 399.9999995 6399.9999927
111+
16.0000000 3.4336294 196.7324722 218.5916358 3497.4661726
112+
57.2957795 0.7471117 42.8063493 47.5626103 761.0017647
113+
359.0000000 0.8584375 49.1848452 54.6498280 874.3972479
114+
399.0000000 3.1593256 181.0160257 201.1289175 3218.0626795
115+
6399.0000000 2.7173573 155.6931042 172.9923380 2767.8774082
116+
1000000.0000000 5.9256211 339.5130823 377.2367581 6035.7881302
117+
```
118+
119+
---
120+
**Attribution:** This is a community solution for the Rosetta Code task [**Angles (geometric), normalization and conversion**](https://rosettacode.org/wiki/Angles_(geometric),_normalization_and_conversion) in Zen C.
121+
122+
*This article uses material from the Rosetta Code article **Angles (geometric), normalization and conversion**, 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/Angles_(geometric),_normalization_and_conversion?action=history).*

rosetta/Anti-primes.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
+++
2+
title = "Anti-primes"
3+
+++
4+
5+
# Anti-primes
6+
7+
{{trans|Wren}}
8+
9+
```zc
10+
fn divisor_count(n: int) -> int {
11+
let i = 1;
12+
let k = (n % 2 == 0) ? 1 : 2;
13+
let count = 0;
14+
while i * i <= n {
15+
if n % i == 0 {
16+
count++;
17+
let j = n / i;
18+
if j != i { count++; }
19+
}
20+
i += k;
21+
}
22+
return count;
23+
}
24+
25+
fn main() {
26+
println "The first 20 anti-primes are:";
27+
let max_div = 0;
28+
let count = 0;
29+
for let n = 1; count < 20; ++n {
30+
let d = divisor_count(n);
31+
if d > max_div {
32+
print "{n} ";
33+
max_div = d;
34+
count++;
35+
}
36+
}
37+
println "";
38+
}
39+
```
40+
41+
**Output:**
42+
43+
```
44+
The first 20 anti-primes are:
45+
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
46+
```
47+
48+
---
49+
**Attribution:** This is a community solution for the Rosetta Code task [**Anti-primes**](https://rosettacode.org/wiki/Anti-primes) in Zen C.
50+
51+
*This article uses material from the Rosetta Code article **Anti-primes**, 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/Anti-primes?action=history).*

0 commit comments

Comments
 (0)