From 4a564ecbb4c6a451e914b3ce92ad55f99460dfb9 Mon Sep 17 00:00:00 2001 From: Arka Poddar Date: Wed, 19 Feb 2025 01:06:07 +0530 Subject: [PATCH 01/13] Solutions Uploaded Signed-off-by: Arka Poddar --- main.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 main.rs diff --git a/main.rs b/main.rs new file mode 100644 index 0000000..3d2fe3d --- /dev/null +++ b/main.rs @@ -0,0 +1,49 @@ +use rand::Rng; +use std::io; + +fn main() { + // Step 1: Read N from user + println!("Enter a number between 1 and 100:"); + + let mut input = String::new(); + io::stdin().read_line(&mut input).expect("Failed to read line"); + + let n: usize = input.trim().parse().expect("Please enter a valid number"); + + if n < 1 || n > 100 { + println!("Number out of range! Please enter a number between 1 and 100."); + return; + } + + let mut rng = rand::rng(); + let mut trials: Vec = Vec::new(); + + for _ in 0..100 { // Repeat the experiment 100 times + // Step 2: Generate N random numbers and store them + let mut numbers: Vec = (0..n) + .map(|_| rng.random_range(0.0..1.0)) + .collect(); + + let mut count = 0; + + // Step 3: Keep generating numbers until we remove all from the list + while !numbers.is_empty() { + count += 1; + let rand_num = rng.random_range(0.0..1.0); + + // Remove elements that match (allow small precision tolerance) + numbers.retain(|&x| (x - rand_num).abs() > 1e-5); + } + + trials.push(count); + } + + // Step 4: Compute min, max, and avg + let min = *trials.iter().min().unwrap(); + let max = *trials.iter().max().unwrap(); + let avg: f32 = trials.iter().sum::() as f32 / trials.len() as f32; + + println!("Minimum iterations: {}", min); + println!("Maximum iterations: {}", max); + println!("Average iterations: {:.2}", avg); +} From f711eb9e9ad41502b889403805e11751e06df929 Mon Sep 17 00:00:00 2001 From: Arka Poddar Date: Wed, 19 Feb 2025 01:06:49 +0530 Subject: [PATCH 02/13] Create main.rs Signed-off-by: Arka Poddar --- solutions/main.rs | 1 + 1 file changed, 1 insertion(+) create mode 100644 solutions/main.rs diff --git a/solutions/main.rs b/solutions/main.rs new file mode 100644 index 0000000..7490fcb --- /dev/null +++ b/solutions/main.rs @@ -0,0 +1 @@ +solutions From 8e18735caa31bc3fe19ff6f836467ebe53e45f71 Mon Sep 17 00:00:00 2001 From: Arka Poddar Date: Wed, 19 Feb 2025 01:07:30 +0530 Subject: [PATCH 03/13] Update main.rs Signed-off-by: Arka Poddar --- solutions/main.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/solutions/main.rs b/solutions/main.rs index 7490fcb..3d2fe3d 100644 --- a/solutions/main.rs +++ b/solutions/main.rs @@ -1 +1,49 @@ -solutions +use rand::Rng; +use std::io; + +fn main() { + // Step 1: Read N from user + println!("Enter a number between 1 and 100:"); + + let mut input = String::new(); + io::stdin().read_line(&mut input).expect("Failed to read line"); + + let n: usize = input.trim().parse().expect("Please enter a valid number"); + + if n < 1 || n > 100 { + println!("Number out of range! Please enter a number between 1 and 100."); + return; + } + + let mut rng = rand::rng(); + let mut trials: Vec = Vec::new(); + + for _ in 0..100 { // Repeat the experiment 100 times + // Step 2: Generate N random numbers and store them + let mut numbers: Vec = (0..n) + .map(|_| rng.random_range(0.0..1.0)) + .collect(); + + let mut count = 0; + + // Step 3: Keep generating numbers until we remove all from the list + while !numbers.is_empty() { + count += 1; + let rand_num = rng.random_range(0.0..1.0); + + // Remove elements that match (allow small precision tolerance) + numbers.retain(|&x| (x - rand_num).abs() > 1e-5); + } + + trials.push(count); + } + + // Step 4: Compute min, max, and avg + let min = *trials.iter().min().unwrap(); + let max = *trials.iter().max().unwrap(); + let avg: f32 = trials.iter().sum::() as f32 / trials.len() as f32; + + println!("Minimum iterations: {}", min); + println!("Maximum iterations: {}", max); + println!("Average iterations: {:.2}", avg); +} From b31ba99705daeb91d7fef8f5316c58bcfd1b8a1c Mon Sep 17 00:00:00 2001 From: Arka Poddar Date: Wed, 19 Feb 2025 01:09:19 +0530 Subject: [PATCH 04/13] Delete main.rs Signed-off-by: Arka Poddar --- main.rs | 49 ------------------------------------------------- 1 file changed, 49 deletions(-) delete mode 100644 main.rs diff --git a/main.rs b/main.rs deleted file mode 100644 index 3d2fe3d..0000000 --- a/main.rs +++ /dev/null @@ -1,49 +0,0 @@ -use rand::Rng; -use std::io; - -fn main() { - // Step 1: Read N from user - println!("Enter a number between 1 and 100:"); - - let mut input = String::new(); - io::stdin().read_line(&mut input).expect("Failed to read line"); - - let n: usize = input.trim().parse().expect("Please enter a valid number"); - - if n < 1 || n > 100 { - println!("Number out of range! Please enter a number between 1 and 100."); - return; - } - - let mut rng = rand::rng(); - let mut trials: Vec = Vec::new(); - - for _ in 0..100 { // Repeat the experiment 100 times - // Step 2: Generate N random numbers and store them - let mut numbers: Vec = (0..n) - .map(|_| rng.random_range(0.0..1.0)) - .collect(); - - let mut count = 0; - - // Step 3: Keep generating numbers until we remove all from the list - while !numbers.is_empty() { - count += 1; - let rand_num = rng.random_range(0.0..1.0); - - // Remove elements that match (allow small precision tolerance) - numbers.retain(|&x| (x - rand_num).abs() > 1e-5); - } - - trials.push(count); - } - - // Step 4: Compute min, max, and avg - let min = *trials.iter().min().unwrap(); - let max = *trials.iter().max().unwrap(); - let avg: f32 = trials.iter().sum::() as f32 / trials.len() as f32; - - println!("Minimum iterations: {}", min); - println!("Maximum iterations: {}", max); - println!("Average iterations: {:.2}", avg); -} From 3ac4d9bc471e628172c722448eb5226b12f2ecaf Mon Sep 17 00:00:00 2001 From: Arka Poddar Date: Wed, 19 Feb 2025 19:20:42 +0530 Subject: [PATCH 05/13] Create lib.rs Signed-off-by: Arka Poddar --- solutions/lib.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 solutions/lib.rs diff --git a/solutions/lib.rs b/solutions/lib.rs new file mode 100644 index 0000000..237a16e --- /dev/null +++ b/solutions/lib.rs @@ -0,0 +1,15 @@ +use rand::Rng; + +pub fn random_removal(n: usize) -> usize { + let mut rng = rand::rng(); + let mut numbers: Vec = (0..n).map(|_| rng.random_range(0.0..1.0)).collect(); + let mut count = 0; + + while !numbers.is_empty() { + count += 1; + let rand_num = rng.random_range(0.0..1.0); + numbers.retain(|&x| (x - rand_num).abs() > 1e-5); + } + + count +} From 3dab7ebb970f75864837c65162fb8946c265dab3 Mon Sep 17 00:00:00 2001 From: Arka Poddar Date: Wed, 19 Feb 2025 19:21:01 +0530 Subject: [PATCH 06/13] Update main.rs Signed-off-by: Arka Poddar --- solutions/main.rs | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/solutions/main.rs b/solutions/main.rs index 3d2fe3d..9f31adb 100644 --- a/solutions/main.rs +++ b/solutions/main.rs @@ -1,8 +1,8 @@ -use rand::Rng; use std::io; +use std::time::Instant; +use cryptoMiningAssignment::random_removal; // Import from lib.rs fn main() { - // Step 1: Read N from user println!("Enter a number between 1 and 100:"); let mut input = String::new(); @@ -15,35 +15,30 @@ fn main() { return; } - let mut rng = rand::rng(); - let mut trials: Vec = Vec::new(); + let mut trials = Vec::new(); + let mut times = Vec::new(); - for _ in 0..100 { // Repeat the experiment 100 times - // Step 2: Generate N random numbers and store them - let mut numbers: Vec = (0..n) - .map(|_| rng.random_range(0.0..1.0)) - .collect(); - - let mut count = 0; - - // Step 3: Keep generating numbers until we remove all from the list - while !numbers.is_empty() { - count += 1; - let rand_num = rng.random_range(0.0..1.0); - - // Remove elements that match (allow small precision tolerance) - numbers.retain(|&x| (x - rand_num).abs() > 1e-5); - } + for _ in 0..100 { + let start_time = Instant::now(); + let count = random_removal(n); + let duration = start_time.elapsed().as_secs_f64(); trials.push(count); + times.push(duration); } - // Step 4: Compute min, max, and avg let min = *trials.iter().min().unwrap(); let max = *trials.iter().max().unwrap(); - let avg: f32 = trials.iter().sum::() as f32 / trials.len() as f32; + let avg = trials.iter().sum::() as f32 / trials.len() as f32; + + let min_time = times.iter().cloned().fold(f64::INFINITY, f64::min); + let max_time = times.iter().cloned().fold(f64::NEG_INFINITY, f64::max); + let avg_time = times.iter().sum::() / times.len() as f64; println!("Minimum iterations: {}", min); println!("Maximum iterations: {}", max); println!("Average iterations: {:.2}", avg); + println!("Minimum time taken: {:.6} seconds", min_time); + println!("Maximum time taken: {:.6} seconds", max_time); + println!("Average time taken: {:.6} seconds", avg_time); } From 41535662899217bfc94f7ed6455136a7850fcffe Mon Sep 17 00:00:00 2001 From: Arka Poddar Date: Wed, 19 Feb 2025 19:21:53 +0530 Subject: [PATCH 07/13] Create benchmark.rs Signed-off-by: Arka Poddar --- solutions/benches/benchmark.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 solutions/benches/benchmark.rs diff --git a/solutions/benches/benchmark.rs b/solutions/benches/benchmark.rs new file mode 100644 index 0000000..e592696 --- /dev/null +++ b/solutions/benches/benchmark.rs @@ -0,0 +1,10 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use cryptoMiningAssignment::random_removal; + +fn benchmark(c: &mut Criterion) { + c.bench_function("random_removal 50", |b| b.iter(|| random_removal(black_box(50)))); + c.bench_function("random_removal 100", |b| b.iter(|| random_removal(black_box(100)))); +} + +criterion_group!(benches, benchmark); +criterion_main!(benches); From 7a5be7f36ece2bd1888827cec998824598aa7808 Mon Sep 17 00:00:00 2001 From: Arka Poddar Date: Wed, 19 Feb 2025 19:22:46 +0530 Subject: [PATCH 08/13] Create main.rs Signed-off-by: Arka Poddar --- solutions/src/main.rs | 1 + 1 file changed, 1 insertion(+) create mode 100644 solutions/src/main.rs diff --git a/solutions/src/main.rs b/solutions/src/main.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/solutions/src/main.rs @@ -0,0 +1 @@ + From 6141c054b3f561087b38816a9a217f43469df871 Mon Sep 17 00:00:00 2001 From: Arka Poddar Date: Wed, 19 Feb 2025 19:23:00 +0530 Subject: [PATCH 09/13] Create lib.rs Signed-off-by: Arka Poddar --- solutions/src/lib.rs | 1 + 1 file changed, 1 insertion(+) create mode 100644 solutions/src/lib.rs diff --git a/solutions/src/lib.rs b/solutions/src/lib.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/solutions/src/lib.rs @@ -0,0 +1 @@ + From fd91536ddb2ab7771d5b33dd4bee27e1660c303b Mon Sep 17 00:00:00 2001 From: Arka Poddar Date: Wed, 19 Feb 2025 19:23:19 +0530 Subject: [PATCH 10/13] Update lib.rs Signed-off-by: Arka Poddar --- solutions/src/lib.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/solutions/src/lib.rs b/solutions/src/lib.rs index 8b13789..a7f866a 100644 --- a/solutions/src/lib.rs +++ b/solutions/src/lib.rs @@ -1 +1,16 @@ +use rand::Rng; + +pub fn random_removal(n: usize) -> usize { + let mut rng = rand::rng(); + let mut numbers: Vec = (0..n).map(|_| rng.random_range(0.0..1.0)).collect(); + let mut count = 0; + + while !numbers.is_empty() { + count += 1; + let rand_num = rng.random_range(0.0..1.0); + numbers.retain(|&x| (x - rand_num).abs() > 1e-5); + } + + count +} From fe3a5a0614e7fe621d245d44b873836d732180f1 Mon Sep 17 00:00:00 2001 From: Arka Poddar Date: Wed, 19 Feb 2025 19:23:40 +0530 Subject: [PATCH 11/13] Update main.rs Signed-off-by: Arka Poddar --- solutions/src/main.rs | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/solutions/src/main.rs b/solutions/src/main.rs index 8b13789..484303d 100644 --- a/solutions/src/main.rs +++ b/solutions/src/main.rs @@ -1 +1,45 @@ +use std::io; +use std::time::Instant; +use cryptoMiningAssignment::random_removal; // Import from lib.rs + +fn main() { + println!("Enter a number between 1 and 100:"); + + let mut input = String::new(); + io::stdin().read_line(&mut input).expect("Failed to read line"); + + let n: usize = input.trim().parse().expect("Please enter a valid number"); + + if n < 1 || n > 100 { + println!("Number out of range! Please enter a number between 1 and 100."); + return; + } + + let mut trials = Vec::new(); + let mut times = Vec::new(); + + for _ in 0..100 { + let start_time = Instant::now(); + let count = random_removal(n); + let duration = start_time.elapsed().as_secs_f64(); + + trials.push(count); + times.push(duration); + } + + let min = *trials.iter().min().unwrap(); + let max = *trials.iter().max().unwrap(); + let avg = trials.iter().sum::() as f32 / trials.len() as f32; + + let min_time = times.iter().cloned().fold(f64::INFINITY, f64::min); + let max_time = times.iter().cloned().fold(f64::NEG_INFINITY, f64::max); + let avg_time = times.iter().sum::() / times.len() as f64; + + println!("Minimum iterations: {}", min); + println!("Maximum iterations: {}", max); + println!("Average iterations: {:.2}", avg); + println!("Minimum time taken: {:.6} seconds", min_time); + println!("Maximum time taken: {:.6} seconds", max_time); + println!("Average time taken: {:.6} seconds", avg_time); +} From 9ef9550807a985d61b29d82937c363662c806c1e Mon Sep 17 00:00:00 2001 From: Arka Poddar Date: Wed, 19 Feb 2025 19:26:30 +0530 Subject: [PATCH 12/13] Delete solutions/lib.rs Signed-off-by: Arka Poddar --- solutions/lib.rs | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 solutions/lib.rs diff --git a/solutions/lib.rs b/solutions/lib.rs deleted file mode 100644 index 237a16e..0000000 --- a/solutions/lib.rs +++ /dev/null @@ -1,15 +0,0 @@ -use rand::Rng; - -pub fn random_removal(n: usize) -> usize { - let mut rng = rand::rng(); - let mut numbers: Vec = (0..n).map(|_| rng.random_range(0.0..1.0)).collect(); - let mut count = 0; - - while !numbers.is_empty() { - count += 1; - let rand_num = rng.random_range(0.0..1.0); - numbers.retain(|&x| (x - rand_num).abs() > 1e-5); - } - - count -} From 77965f54ec9f64a993ef7ff7d23b8c531e13c482 Mon Sep 17 00:00:00 2001 From: Arka Poddar Date: Wed, 19 Feb 2025 19:26:42 +0530 Subject: [PATCH 13/13] Delete solutions/main.rs Signed-off-by: Arka Poddar --- solutions/main.rs | 44 -------------------------------------------- 1 file changed, 44 deletions(-) delete mode 100644 solutions/main.rs diff --git a/solutions/main.rs b/solutions/main.rs deleted file mode 100644 index 9f31adb..0000000 --- a/solutions/main.rs +++ /dev/null @@ -1,44 +0,0 @@ -use std::io; -use std::time::Instant; -use cryptoMiningAssignment::random_removal; // Import from lib.rs - -fn main() { - println!("Enter a number between 1 and 100:"); - - let mut input = String::new(); - io::stdin().read_line(&mut input).expect("Failed to read line"); - - let n: usize = input.trim().parse().expect("Please enter a valid number"); - - if n < 1 || n > 100 { - println!("Number out of range! Please enter a number between 1 and 100."); - return; - } - - let mut trials = Vec::new(); - let mut times = Vec::new(); - - for _ in 0..100 { - let start_time = Instant::now(); - let count = random_removal(n); - let duration = start_time.elapsed().as_secs_f64(); - - trials.push(count); - times.push(duration); - } - - let min = *trials.iter().min().unwrap(); - let max = *trials.iter().max().unwrap(); - let avg = trials.iter().sum::() as f32 / trials.len() as f32; - - let min_time = times.iter().cloned().fold(f64::INFINITY, f64::min); - let max_time = times.iter().cloned().fold(f64::NEG_INFINITY, f64::max); - let avg_time = times.iter().sum::() / times.len() as f64; - - println!("Minimum iterations: {}", min); - println!("Maximum iterations: {}", max); - println!("Average iterations: {:.2}", avg); - println!("Minimum time taken: {:.6} seconds", min_time); - println!("Maximum time taken: {:.6} seconds", max_time); - println!("Average time taken: {:.6} seconds", avg_time); -}