From 8e9427d1172bf06f29e26f36c46f3e4c0c0b53c4 Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 9 Oct 2024 18:10:09 +0100 Subject: [PATCH] feat(problem): 5 - smallest multiple --- src/bin/5.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/bin/5.rs diff --git a/src/bin/5.rs b/src/bin/5.rs new file mode 100644 index 0000000..19d8f3e --- /dev/null +++ b/src/bin/5.rs @@ -0,0 +1,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) -> usize { + if start > end { + panic!("You can not start on a value higher than your end value!"); + } + + let mut result: usize = 1; + + for i in start..(end + 1) { + result = (result * i) / gcd(result, i); + } + + return result; +} + +fn main() { + let number = first_value_divisible_by(1, 20); + + println!("The smallest number that is divisible by all integers between 1 and 20 is {number}"); +}