我还没有能够在我的筛选实施中发现错误。我的测试显示以下错误。
// Expected
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
// Mine
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49]
有人可以指出我做错了什么吗?我意识到它与最终条件有关但我还没有能够弄明白
/// Find all prime numbers less than `n`.
/// For example, `sieve(7)` should return `[2, 3, 5]`
pub fn sieve(n: u32) -> Vec<u32> {
// Check assert and then populate vector
assert!(n > 1, "Error n is less than 1");
let mut is_prime: Vec<bool> = vec![true; (n-1) as usize];
let sqrt: u32 = (n as f32).sqrt() as u32 + 1;
for i in 2..sqrt {
if is_prime[i as usize] == true {
let mut k = i * i;
loop {
if k > n {
break;
}
is_prime[(k-2) as usize] = false;
k += i;
println!("i:{} k:{}", i, n);
}
}
}
let mut primes: Vec<u32> = Vec::new();
for i in 0..is_prime.len() {
if is_prime[i] == true {
primes.push((i+2) as u32);
}
}
println!("SQ{}", sqrt);
println!("Vec {:?}", primes);
primes
}
(Github)
这也是一个相对较快的实现,或者我犯了一个大错误(除了打印,那些是用于调试)?
答案 0 :(得分:5)
看起来你的聪明&#34;忽略价值var result = [].concat(...things.map(a => a.match(/\d+|[a-z]+/gi)));
和0
适得其反的诀窍,你并没有不断地上下调整你的偏移量。这样的东西似乎给出了正确的输出:
1