From 000a15106c0d455b0ba0853a048fbf8413daaeed Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 9 Feb 2020 13:51:15 +0100 Subject: [PATCH] Fix check of divisibility --- .gitignore | 3 +- rotatable.txt | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 22 +++++++++++--- 3 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 rotatable.txt diff --git a/.gitignore b/.gitignore index 4ae822d..aad9246 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target **/*.rs.bk -.idea \ No newline at end of file +.idea +rotatable.txt \ No newline at end of file diff --git a/rotatable.txt b/rotatable.txt new file mode 100644 index 0000000..33aa26e --- /dev/null +++ b/rotatable.txt @@ -0,0 +1,81 @@ +66 +11 +99 +666 +44 +555 +444 +77 +22 +333 +222 +55 +111 +999 +1111 +33 +2222 +777 +3333 +4444 +88 +6666 +888 +5555 +7777 +9999 +11111 +8888 +22222 +33333 +44444 +66666 +55555 +102564 +77777 +88888 +99999 +111111 +153846 +128205 +142857 +222222 +205128 +230769 +444444 +333333 +555555 +666666 +888888 +777777 +999999 +1111111 +2222222 +4444444 +3333333 +6666666 +5555555 +8888888 +7777777 +9999999 +11111111 +22222222 +44444444 +33333333 +66666666 +55555555 +88888888 +77777777 +99999999 +111111111 +22 +222222 +333333333 +444444444 +666666666 +555555555 +777777777 +888888888 +999999999 +1111111111 +2222222222 diff --git a/src/main.rs b/src/main.rs index 239f269..cdd57ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,12 +4,21 @@ use num_traits::{ToPrimitive, Zero}; use std::sync::mpsc::*; use std::thread; use std::time::Instant; +use std::io::BufWriter; +use std::io::prelude::*; +use std::fs::OpenOptions; fn main() { let num_threads: u64 = num_cpus::get() as u64; let (tx, rx) = channel::(); let (sen_timing, rec_timing) = channel::(); let end: BigUint = ubig_pow(10, 100); + let file = OpenOptions::new() + .write(true) + .create(true) + .open("rotatable.txt") + .unwrap(); + let mut buffer = BufWriter::new(file); println!("Starting {} threads...", num_threads); for i in 0u64..num_threads { let tx = tx.clone(); @@ -35,7 +44,12 @@ fn main() { } }); loop { - println!("{}", rx.recv().unwrap()); + let rotatable = rx.recv().unwrap(); + println!("{}", rotatable); + if let Err(e) = buffer.write(&format!("{}\n", rotatable).into_bytes()) { + panic!(e); + } + let _ = buffer.flush(); } } @@ -50,11 +64,11 @@ fn get_rotatable(tx: Sender, sen_time: Sender, start: u64, end: B if num.is_odd() || !(&num % 10 as u64).is_zero() { let mut digits = ubig_digits(num.clone()); if digits.first() >= digits.last() - && !(digits[0] % 2 == 0 && digits[1] % 2 == 1) - && (&num % *digits.first().unwrap() as u64).is_zero() { + && !(digits[0] % 2 == 0 && digits[1] % 2 != 0) { digits.rotate_left(1); + let first = *digits.first().unwrap() as u64; let num_rotated = ubig_from_digits(digits); - if (&num_rotated % &num).is_zero() { + if (first == 0 || (&num_rotated % first).is_zero()) && (&num_rotated % &num).is_zero() { let _ = tx.send(num.clone()); } }