Skip to content

Instantly share code, notes, and snippets.

@tkoki
Created June 10, 2020 13:12
Show Gist options
  • Save tkoki/46e101be9c9ed83ab8426ece559c51a3 to your computer and use it in GitHub Desktop.
Save tkoki/46e101be9c9ed83ab8426ece559c51a3 to your computer and use it in GitHub Desktop.
//
// The Zen Programming Language(tm)
// Copyright (c) 2018-2020 kristopher tate & connectFree Corporation.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//
// This project may be licensed under the terms of the ConnectFree Reference
// Source License (CF-RSL). Corporate and Academic licensing terms are also
// available. Please contact <licensing@connectfree.co.jp> for details.
//
// Zen, the Zen three-circles logo and The Zen Programming Language are
// trademarks of connectFree Corporation in Japan and other countries.
//
// connectFree and the connectFree logo are registered trademarks
// of connectFree Corporation in Japan and other countries. connectFree
// trademarks and branding may not be used without express written permission
// of connectFree. Please remove all trademarks and branding before use.
//
// See the LICENSE file at the root of this project for complete information.
//
//
//
// sieve.zen
// Sieve of Eratosthenes
// 20200610, tkoki
//
const std = @import("std");
const heap = std.heap;
fn isPrimeNumber(n: u32) bool {
var i: u32 = 2;
var is_prime: bool = false;
if (n == 1) {
return false;
} else if (n == 2) {
return true;
}
while (true) {
if (n % i == 0) {
is_prime = false;
break;
} else {
i += 1;
if (i == n) {
is_prime = true;
break;
}
}
}
return is_prime;
}
pub fn main() anyerror!void {
var arena = heap.ArenaAllocator{ .allocator = heap.page_allocator };
defer arena.deinit();
const args = try std.process.argsAlloc(&mut arena);
if (args.len > 1) {
const max_number = try std.fmt.parseInt(u32, args[1], 10);
std.debug.printInfo("max_number = {}", .{max_number});
var i: u32 = 1;
while (i <= max_number) {
if (isPrimeNumber(i)) {
std.debug.printInfo("{} is prime number.", .{i});
}
i += 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment