/
problem_005.rs
52 lines (46 loc) · 1.15 KB
/
problem_005.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// -*- compile-command: "rustc -o problem_005_rs problem_005.rs" -*-
// Copyright (c) 2016 Michael Caldwell
fn is_prime(n: u64) -> bool {
if n < 2 {
return false;
}
if n == 2 {
return true;
}
if n % 2 == 0 {
return false;
}
let limit = (n as f64).sqrt() as u64;
for i in 3..(limit+1) {
if n % i == 0 {
return false;
}
}
return true;
}
fn main() {
println!("Project Euler - Problem 5");
println!("What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?\n");
// Start off with a base of all primes less than 20
// Impossible for number to be smaller than this
let mut n = 1;
for i in 1..20 {
if is_prime(i) == true {
n *= i;
}
}
let mut valid = false;
while valid == false {
n += 10;
valid = true;
// No need to check 2 - 10,
// because multiples of each are tested in range of 11-20
for i in 11..21 {
if n % i != 0 {
valid = false;
break;
}
}
}
println!("{}", n);
}