From 3f089ad0a43fcedac0dbb1fd07f4f3ceee72b1a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Mon, 1 Mar 2021 19:21:35 +0100 Subject: [PATCH] Better iteration over elements. --- src/main.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 05f2fb2..381b53f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,19 +33,15 @@ fn main() { while primes.len() < n { - let mut i = 0; - let mut test_prime = primes.get(i).unwrap(); let mut is_prime = true; - while *test_prime <= (candidate as f64).sqrt() as u64 + let limit = (candidate as f64).sqrt() as u64; + for i in primes.iter().take_while(|x| **x <= limit) { - if candidate % *test_prime == 0 + if candidate % *i == 0 { is_prime = false; break; } - - i += 1; - test_prime = primes.get(i).unwrap(); } if is_prime