From 5ce9bc821b38ad1f4677ad178b5cad7d1208aef6 Mon Sep 17 00:00:00 2001 From: iByteABit256 Date: Tue, 14 Feb 2023 00:12:21 -0800 Subject: [PATCH] Performance improvement --- Cargo.lock | 2 +- Cargo.toml | 3 ++- src/main.rs | 6 ++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9b53d4d..9d2511a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -284,7 +284,7 @@ dependencies = [ [[package]] name = "mrn-generator" -version = "0.1.0" +version = "0.2.0" dependencies = [ "chrono", "clap", diff --git a/Cargo.toml b/Cargo.toml index 3e1ee32..7a8a864 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,8 @@ [package] name = "mrn-generator" -version = "0.1.0" +version = "0.2.0" edition = "2021" +authors = ["Pavlos Smith "] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/main.rs b/src/main.rs index ee0c176..6133d28 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,10 +28,8 @@ fn is_mrn_valid(mrn: &str) -> Option { let mrn_temp: String = mrn_iter.collect(); - // Powers of 2 - let multipliers: Vec = (0..mrn_temp.len()).map(|n| 1 << n).collect(); - - let multiplied_sum: u32 = mrn_temp.chars().zip(multipliers).map(|(c,m)| check_character_value(c) as u32 * m).sum(); + // Multiply each char value with it's power of 2 and sum them + let multiplied_sum: u32 = mrn_temp.chars().zip(0..mrn_temp.len()).map(|(c,m)| (check_character_value(c) as u32) << m).sum(); let check_digit: u8 = (multiplied_sum % 11).try_into().unwrap(); check_remainder_value(check_digit, last_digit)