-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.rs
52 lines (40 loc) · 1.1 KB
/
5.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
/*
Problem 5 - Smallest multiple
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
*/
// Euclidean algorithm for finding GCD
fn gcd(a: usize, b: usize) -> usize {
let mut x = a;
let mut y = b;
let mut r: usize = 1; // temporary initial value
while r != 0 {
if x < y {
let temp = x;
x = y;
y = temp;
}
r = x % y;
if r == 0 {
break;
} else {
x = y;
y = r;
}
}
return y;
}
fn first_value_divisible_by(start: usize, end: usize) -> Option<usize> {
if start > end {
return None;
}
let mut result: usize = 1;
for i in start..(end + 1) {
result = (result * i) / gcd(result, i);
}
return Some(result);
}
pub fn main() {
let number = first_value_divisible_by(1, 20).unwrap();
println!("The smallest number that is divisible by all integers between 1 and 20 is {number}");
}