mirror of
https://github.com/ziglang/zig.git
synced 2024-11-26 23:22:44 +00:00
38 lines
982 B
Zig
38 lines
982 B
Zig
const first_25_primes = firstNPrimes(25);
|
|
const sum_of_first_25_primes = sum(&first_25_primes);
|
|
|
|
fn firstNPrimes(comptime n: usize) [n]i32 {
|
|
var prime_list: [n]i32 = undefined;
|
|
var next_index: usize = 0;
|
|
var test_number: i32 = 2;
|
|
while (next_index < prime_list.len) : (test_number += 1) {
|
|
var test_prime_index: usize = 0;
|
|
var is_prime = true;
|
|
while (test_prime_index < next_index) : (test_prime_index += 1) {
|
|
if (test_number % prime_list[test_prime_index] == 0) {
|
|
is_prime = false;
|
|
break;
|
|
}
|
|
}
|
|
if (is_prime) {
|
|
prime_list[next_index] = test_number;
|
|
next_index += 1;
|
|
}
|
|
}
|
|
return prime_list;
|
|
}
|
|
|
|
fn sum(numbers: []const i32) i32 {
|
|
var result: i32 = 0;
|
|
for (numbers) |x| {
|
|
result += x;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
test "variable values" {
|
|
try @import("std").testing.expect(sum_of_first_25_primes == 1060);
|
|
}
|
|
|
|
// test
|