Skip to content

Commit e5f4792

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

47 files changed

Lines changed: 1382 additions & 118 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

rosetta/Additive_primes.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
+++
2+
title = "Additive primes"
3+
+++
4+
5+
# Additive primes
6+
7+
```zc
8+
fn digit_sum(n: int) -> int {
9+
let sum = 0;
10+
while n > 0 {
11+
sum += n % 10;
12+
n /= 10;
13+
}
14+
return sum;
15+
}
16+
17+
fn is_prime(n: int) -> bool {
18+
if n < 2 { return false; }
19+
if n % 2 == 0 { return n == 2; }
20+
if n % 3 == 0 { return n == 3; }
21+
let d = 5;
22+
while d * d <= n {
23+
if n % d == 0 { return false; }
24+
d += 2;
25+
if n % d == 0 { return false; }
26+
d += 4;
27+
}
28+
return true;
29+
}
30+
31+
fn main() {
32+
println "Additive primes less than 500:";
33+
let i = 2;
34+
let count = 0;
35+
while i < 500 {
36+
if is_prime(i) && is_prime(digit_sum(i)) {
37+
print "{i:3d} ";
38+
if !(++count % 10) { println ""; }
39+
}
40+
i = (i > 2) ? i + 2 : 3;
41+
}
42+
println "\n\n{count} such primes found.";
43+
}
44+
```
45+
46+
**Output:**
47+
48+
```
49+
Additive primes less than 500:
50+
2 3 5 7 11 23 29 41 43 47
51+
61 67 83 89 101 113 131 137 139 151
52+
157 173 179 191 193 197 199 223 227 229
53+
241 263 269 281 283 311 313 317 331 337
54+
353 359 373 379 397 401 409 421 443 449
55+
461 463 467 487
56+
57+
54 such primes found.
58+
```
59+
60+
---
61+
**Attribution:** This is a community solution for the Rosetta Code task [**Additive primes**](https://rosettacode.org/wiki/Additive_primes) in Zen C.
62+
63+
*This article uses material from the Rosetta Code article **Additive 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/Additive_primes?action=history).*

rosetta/Angle_difference_between_two_bearings.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ fn main() {
3434
];
3535
3636
println "Differences (to 4dp) between these bearings:";
37-
for pair in pairs {
38-
let (p0, p1) = pair;
37+
for i in 0..pairs.len {
38+
let (p0, p1) = pairs[i];
3939
let diff = subtract(p0, p1);
4040
println "{p0:12.4f}° and {p1:12.4f}° -> {diff:9.4f}°";
4141
}

rosetta/Approximate_equality.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
+++
2+
title = "Approximate equality"
3+
+++
4+
5+
# Approximate equality
6+
7+
```zc
8+
import "std/math.zc"
9+
10+
fn main() {
11+
let tol = 1.0e-16;
12+
let sqrt2 = Math::sqrt(2.0);
13+
let pairs: (f64, f64)[8] = [
14+
(100000000000000.01, 100000000000000.011),
15+
(100.01, 100.011),
16+
(10000000000000.001 / 10000.0, 1000000000.0000001000),
17+
(0.001, 0.0010000001),
18+
(0.000000000000000000000101, 0.0),
19+
(sqrt2 * sqrt2, 2.0),
20+
(-sqrt2 * sqrt2, -2.0),
21+
(3.14159265358979323846, 3.14159265358979324)
22+
];
23+
println "Approximate equality of test cases for a tolerance of {tol:g}:";
24+
for i in 0..pairs.len {
25+
let (p0, p1) = pairs[i];
26+
let res: bool = Math::abs(p0 - p1) < tol;
27+
println " {i + 1} -> {res}";
28+
}
29+
}
30+
```
31+
32+
**Output:**
33+
34+
```
35+
Approximate equality of test cases for a tolerance of 1e-16:
36+
1 -> true
37+
2 -> false
38+
3 -> false
39+
4 -> false
40+
5 -> true
41+
6 -> false
42+
7 -> false
43+
8 -> true
44+
```
45+
46+
---
47+
**Attribution:** This is a community solution for the Rosetta Code task [**Approximate equality**](https://rosettacode.org/wiki/Approximate_equality) in Zen C.
48+
49+
*This article uses material from the Rosetta Code article **Approximate equality**, 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/Approximate_equality?action=history).*

rosetta/Attractive_numbers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn count_factors(n: int) -> int {
4949
}
5050
}
5151
if n > 1 { factors << n; }
52-
return factors.length();
52+
return (int)factors.length();
5353
}
5454
5555
fn is_attractive(n: int) -> bool {

rosetta/Averages_Root_mean_square.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title = "Averages/Root mean square"
77
```zc
88
import "std/math.zc"
99
10-
fn rms(a: f64[], len: const int) -> f64 {
10+
fn rms(a: f64*, len: const int) -> f64 {
1111
let sum = 0.0;
1212
for i in 0..len { sum += a[i] * a[i]; }
1313
return Math::sqrt(sum / len);

rosetta/Catalan_numbers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ fn main() {
2828
setlocale(LC_NUMERIC, "en_US.UTF-8");
2929
println " n Catalan number";
3030
println "------------------";
31-
for i in 0..=15 { printf("%2d %'9llu\n", i, catalan((u64)i)); }
31+
for i in 0..=15 { printf("%2d %'9lu\n", i, catalan((u64)i)); }
3232
println "\nand again using a recursive function:\n";
33-
for i in 0..=15 { printf("%2d %'9llu\n", i, catalan_rec((u64)i)); }
33+
for i in 0..=15 { printf("%2d %'9lu\n", i, catalan_rec((u64)i)); }
3434
}
3535
```
3636

rosetta/Count_the_occurrence_of_each_digit_in_e.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ title = "Count the occurrence of each digit in e"
88

99
```zc
1010
fn count_digits_in_e(n: int) {
11-
autofree let v: int* = malloc(n * sizeof(int));
12-
for i in 0..n { v[i] = 1; }
11+
autofree let l: int* = malloc(n * sizeof(int));
12+
for i in 0..n { l[i] = 1; }
1313
let dc: [int; 10];
1414
dc[2] = 1; // to count the non-fractional digit
1515
for col in 1..(2 * n) {
1616
let a = n + 1;
1717
let c = 0;
1818
for i in 0..n {
19-
c += v[i] * 10;
20-
v[i] = c % a;
19+
c += l[i] * 10;
20+
l[i] = c % a;
2121
c /= a--;
2222
}
2323
dc[c]++;

rosetta/Determine_if_a_string_is_collapsible.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ title = "Determine if a string is collapsible"
1010
import "std/string.zc"
1111
1212
fn collapse(s: string) {
13-
let c = String::new(s).runes();
13+
let ss = String::new(s);
14+
let c = ss.runes();
1415
let le = (int)c.length();
1516
println "original : length = {le:2d}, string = «««{s}»»»";
1617
if le >= 2 {
1718
for let i = le - 2; i >= 0; --i {
1819
if c[i] == c[i + 1] { c.remove(i); }
1920
}
2021
let cl = (int)c.length();
21-
let cs = String::from_runes_vec(c).c_str();
22+
let cs = String::from_runes_vec(c);
2223
println "collapsed: length = {cl:2d}, string = «««{cs}»»»\n";
2324
} else {
2425
println "collapsed: length = {le:2d}, string = «««{s}»»»\n";

rosetta/Determine_if_a_string_is_squeezable.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@ title = "Determine if a string is squeezable"
1010
import "std/string.zc"
1111
1212
fn squeeze(s: string, r: rune) {
13-
let c = String::new(s).runes();
13+
let ss = String::new(s)
14+
let c = ss.runes();
1415
let le = (int)c.length();
15-
let ch = String::from_rune(r).c_str();
16+
let ch = String::from_rune(r);
1617
println "Specified character = '{ch}'";
1718
println "original : length = {le:2d}, string = «««{s}»»»";
1819
if le >= 2 {
1920
for let i = le - 2; i >= 0; --i {
2021
if c[i] == r && c[i] == c[i + 1] { c.remove(i); }
2122
}
2223
let cl = (int)c.length();
23-
let cs = String::from_runes_vec(c).c_str();
24+
let cs = String::from_runes_vec(c);
2425
println "squeezed : length = {cl:2d}, string = «««{cs}»»»\n";
2526
} else {
2627
println "squeezed: length = {le:2d}, string = «««{s}»»»\n";
@@ -43,7 +44,8 @@ fn main() {
4344
let spec = [ " ", "-", "7", ".", " -r", "e", "s", "a", "😍" ];
4445
4546
for i in 0..strings.len {
46-
for ch in String::new(spec[i]) { squeeze(strings[i], ch); }
47+
let s = String::new(spec[i]);
48+
for ch in s { squeeze(strings[i], ch); }
4749
}
4850
}
4951
```

rosetta/Digital_root_Multiplicative_digital_root.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ fn main() {
5858
for v in list[i] { print "{v}, "; }
5959
println "\b\b]";
6060
}
61+
for i in 0..BASE { list[i].free(); }
6162
}
6263
```
6364

0 commit comments

Comments
 (0)