Browse Source

problem 7, readme

canon
Hazel Levine 1 year ago
parent
commit
bf288ac739
3 changed files with 38 additions and 4 deletions
  1. +6
    -2
      README.md
  2. +2
    -2
      src/bin/prob2.rs
  3. +30
    -0
      src/bin/prob7.rs

+ 6
- 2
README.md View File

@ -1,3 +1,7 @@
# euler
# Project Euler solutions
solutions to project euler problems in rust
They're solutions to Project Euler. In Rust. For my own learning experience.
Limitations of this challenge:
- No problems past #100 will be published per the wishes of the authors
- No external libraries that implement mathematical functions can be used

+ 2
- 2
src/bin/prob2.rs View File

@ -15,8 +15,8 @@ fn fib(n: i32) -> i64 {
}
fn compute(n: i64) -> i64 {
(0..).map(|x| fib(x)).take_while(|&x| x < n)
.filter(|&x| x % 2 == 0).sum()
// this is super cool wtf
(0..).map(fib).take_while(|&x| x < n).filter(|&x| x % 2 == 0).sum()
}
fn main() {


+ 30
- 0
src/bin/prob7.rs View File

@ -0,0 +1,30 @@
// Problem 7:
//
// By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see
// that the 6th prime is 13.
// What is the 10,001st prime number?
fn sieve(n: usize) -> Vec<u32> {
let mut prime_idx = vec![true; n];
prime_idx[0] = false;
prime_idx[1] = false;
// for i in 2, 3, 4... not exceeding sqrt(n)...
let slimit = (n as f64).sqrt() as usize;
for i in 2..slimit {
// if A[i] is true
if prime_idx[i] {
for j in (i.pow(2)..n).step_by(i) {
prime_idx[j] = false;
}
}
}
let mut ret = Vec::new();
ret.extend((0..n as u32).filter(|x| prime_idx[*x as usize]));
ret
}
fn main() {
println!("the 10,001st prime is: {:?}", sieve(200_000)[10000]);
}

Loading…
Cancel
Save