Sha256: 6341779a58ef36d591a0156c1cea362258c2d9fe14455a6f469b3057b769adc0

Contents?: true

Size: 570 Bytes

Versions: 67

Compression:

Stored size: 570 Bytes

Contents

fn is_prime(n: u32) -> bool {
    let mut i: u32 = 3;
    while (i * i) < (n + 1) {
      if n % i == 0 {
         return false;
      }
      i += 1;
   }
   return true;
}

pub fn nth(n: u32) -> Option<u32> {
    match n {
        0 => None,
        1 => Some(2),
        _ => {
            let mut count: u32 = 1;
            let mut candidate: u32 = 1;
            while count < n {
                candidate += 2;
                if is_prime(candidate) {
                    count += 1;
                }
            }
            Some(candidate)
        }
    }
}

Version data entries

67 entries across 67 versions & 1 rubygems

Version Path
trackler-2.2.1.179 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.178 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.177 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.176 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.175 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.174 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.173 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.172 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.171 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.170 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.169 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.167 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.166 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.165 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.164 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.163 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.162 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.161 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.160 tracks/rust/exercises/nth-prime/example.rs
trackler-2.2.1.159 tracks/rust/exercises/nth-prime/example.rs