From ec2d00ca9bb9aad510a2bd1640265d20779893e4 Mon Sep 17 00:00:00 2001 From: Fabian Schmidt Date: Mon, 16 Sep 2024 12:42:55 +0200 Subject: [PATCH] solved 24 --- src/bin/problem_24.rs | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/bin/problem_24.rs diff --git a/src/bin/problem_24.rs b/src/bin/problem_24.rs new file mode 100644 index 0000000..eccaa8c --- /dev/null +++ b/src/bin/problem_24.rs @@ -0,0 +1,57 @@ +use std::error::Error; + +fn main() { + let n = 1_000_000; + let digits = vec![1, 0, 2, 3, 4, 5, 6, 7, 8, 9]; + let perm = permutation(digits, n).expect("Should return ok").iter().map(|&digit| digit.to_string()).collect::>().join(""); + println!("{perm}"); + let digits = vec!["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]; + //for n in 1..=factorial(digits.len()) { + let perm = permutation(digits.clone(), n).expect("Should return ok").join(""); + println!("{perm}"); + //} + let digits = vec!["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; + let perm = permutation(digits.clone(), n).expect("Should return ok").join(""); + println!("{perm}"); +} + +/// Explanation +/// +/// there are 10! possible permutations +/// for each first number there are 9!, for each first 2 numbers 8!, etc. +/// we check how many times we have 9! permutations before we're over 1_000_000 +/// aka. 1000000 / 9! +/// we take the remainder and check how many times we have 8! before we?re over it +/// (1000000 % 9!) 8! +/// etc. +/// every iteration we remove the digit by the idx from the original permutation +/// we only check for 999999 permutations because we already have the first one +/// +fn permutation(mut digits: Vec, nth: usize) -> Result, Box> { + digits.sort(); + if nth == 1 { + return Ok(digits); + } + if nth > factorial(digits.len()) || nth == 0 { + return Err(Box::from("Out of bounds")); + } + let mut perm = Vec::new(); + let num_unique_digits = digits.len(); + let mut remainder = nth - 1; + for idx in 1..=digits.len() { + let permutations = remainder / factorial(num_unique_digits-idx); + remainder = remainder % factorial(num_unique_digits-idx); + perm.push(digits[permutations]); + digits.remove(permutations); + } + Ok(perm) +} + +// number of permutations is n! where n is the number of digits +fn factorial(num: usize) -> usize { + let mut fact = 1; + for n in 1..=num { + fact = fact * n; + } + fact +} \ No newline at end of file