From 598fd83190d28f35c38f18c1c1ca97d15b7e5ccd Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 26 Jan 2020 12:39:25 +0100 Subject: [PATCH] Add primes program --- .gitignore | 4 ++++ Cargo.lock | 35 ++++++++++++++++++++++++++++++ Cargo.toml | 10 +++++++++ src/main.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7ce5aaf --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/target +**/*.rs.bk +primes.txt +*.tar.gz \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..bc635cb --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,35 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hermit-abi" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.66" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "num_cpus" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "primes" +version = "0.1.0" +dependencies = [ + "num_cpus 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[metadata] +"checksum hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eff2656d88f158ce120947499e971d743c05dbcbed62e5bd2f38f1698bbc3772" +"checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" +"checksum num_cpus 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "46203554f085ff89c235cd12f7075f3233af9b11ed7c9e16dfe2560d03313ce6" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..edbde16 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "primes" +version = "0.1.0" +authors = ["trivernis "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +num_cpus = "1.0" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..50d8799 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,61 @@ +extern crate num_cpus; +use std::thread; +use std::fs::{OpenOptions}; +use std::io::prelude::*; +use std::sync::mpsc::{sync_channel, SyncSender}; +use std::env; + +fn main() { + let (tx, rx) = sync_channel::(0); + let num_threads: u64 = num_cpus::get() as u64; + let mut start: u64 = 1; + let args: Vec = env::args().collect(); + if args.len() > 1 { + start = *(&args[1].parse::().unwrap()); + } + if &start % 2 == 0 { + start += 1; + } + let mut file = OpenOptions::new() + .write(true) + .append(true) + .create(true) + .open("primes.txt") + .unwrap(); + println!("Starting {} threads", num_threads); + for i in 0u64..num_threads { + let tx = tx.clone(); + let _child = thread::spawn(move || { + get_primes(&start + (2*&i), &num_threads * 2, &tx); + }); + } + loop { + let prime = rx.recv().unwrap(); + println!("{}", prime); + if let Err(e) = writeln!(file, "{}", prime) { + panic!(e); + } + } +} + +fn get_primes(start: u64, incr: u64, tx: &SyncSender) { + let mut num = start; + loop { + let mut is_prime = true; + if (num < 3) | (&num % 2 == 0) { + num += incr; + continue; + } + for i in (3u64..&num/2).step_by(2) { + if &num % i == 0 { + is_prime = false; + } + } + if is_prime { + if let Err(e) = (*tx).send(num) { + panic!(e); + } + } + num += incr; + } +} \ No newline at end of file