feat(24): complete lexicographic permutations
This commit is contained in:
parent
100f2c03be
commit
508f898e44
4 changed files with 62 additions and 3 deletions
|
@ -17,7 +17,7 @@ Here is a [link to my profile](https://projecteuler.net/progress=newtykins).
|
||||||
|
|
||||||
## Challenge Completion
|
## Challenge Completion
|
||||||
|
|
||||||
### 26 out of 100 public challenges completed.
|
### 27 out of 100 public challenges completed.
|
||||||
|
|
||||||
- [x] 1 - [Multiples of 3 or 5](src/bin/1.rs)
|
- [x] 1 - [Multiples of 3 or 5](src/bin/1.rs)
|
||||||
- [x] 2 - [Even Fibonacci numbers](src/bin/2.rs)
|
- [x] 2 - [Even Fibonacci numbers](src/bin/2.rs)
|
||||||
|
@ -42,7 +42,7 @@ Here is a [link to my profile](https://projecteuler.net/progress=newtykins).
|
||||||
- [x] 21 - Amicable numbers
|
- [x] 21 - Amicable numbers
|
||||||
- [x] 22 - [Names scores](src/bin/22.rs)
|
- [x] 22 - [Names scores](src/bin/22.rs)
|
||||||
- [x] 23 - Non-abundant sums
|
- [x] 23 - Non-abundant sums
|
||||||
- [ ] 24 - Lexicographic permutations
|
- [x] 24 - Lexicographic permutations
|
||||||
- [ ] 25 - 1000-digit Fibonacci number
|
- [ ] 25 - 1000-digit Fibonacci number
|
||||||
- [ ] 26 - Reciprocal cycles
|
- [ ] 26 - Reciprocal cycles
|
||||||
- [x] 27 - [Quadratic primes](src/bin/27.rs)
|
- [x] 27 - [Quadratic primes](src/bin/27.rs)
|
||||||
|
|
54
src/bin/24.rs
Normal file
54
src/bin/24.rs
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
/*
|
||||||
|
Problem 24 - Lexicographic Permutations
|
||||||
|
|
||||||
|
A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:
|
||||||
|
012 021 102 120 201 210
|
||||||
|
What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
|
||||||
|
*/
|
||||||
|
|
||||||
|
fn permutations<T>(array: Vec<T>) -> Vec<Vec<T>>
|
||||||
|
where
|
||||||
|
T: Clone,
|
||||||
|
{
|
||||||
|
let mut out = vec![];
|
||||||
|
|
||||||
|
match array.len() {
|
||||||
|
0 => vec![],
|
||||||
|
1 => vec![array],
|
||||||
|
_ => {
|
||||||
|
for i in 0..array.len() {
|
||||||
|
let mut array = array.clone();
|
||||||
|
let first = array.remove(i);
|
||||||
|
let mut permutations = permutations(array);
|
||||||
|
|
||||||
|
for permutation in permutations.iter_mut() {
|
||||||
|
permutation.insert(0, first.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
out.append(&mut permutations);
|
||||||
|
}
|
||||||
|
|
||||||
|
out
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn nth_lexicographic_permutation(array: Vec<usize>, n: usize) -> Vec<usize> {
|
||||||
|
let mut permutations = permutations(array);
|
||||||
|
permutations.sort();
|
||||||
|
|
||||||
|
permutations[n - 1].clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
let digits = (0..10).collect::<Vec<usize>>();
|
||||||
|
let millionth_permutation = nth_lexicographic_permutation(digits, 1_000_000);
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"The millionth lexicographic permutation of the digits 0-9 is {}!",
|
||||||
|
millionth_permutation
|
||||||
|
.iter()
|
||||||
|
.map(|&d| d.to_string())
|
||||||
|
.collect::<String>()
|
||||||
|
);
|
||||||
|
}
|
|
@ -54,6 +54,8 @@ mod twenty_three;
|
||||||
mod twenty_two;
|
mod twenty_two;
|
||||||
#[path = "../bin/2.rs"]
|
#[path = "../bin/2.rs"]
|
||||||
mod two;
|
mod two;
|
||||||
|
#[path = "../bin/24.rs"]
|
||||||
|
mod twenty_four;
|
||||||
|
|
||||||
pub async fn execute(
|
pub async fn execute(
|
||||||
problem: Option<u8>,
|
problem: Option<u8>,
|
||||||
|
@ -96,6 +98,7 @@ pub async fn execute(
|
||||||
20 => twenty::main(),
|
20 => twenty::main(),
|
||||||
21 => twenty_one::main(),
|
21 => twenty_one::main(),
|
||||||
23 => twenty_three::main(),
|
23 => twenty_three::main(),
|
||||||
|
24 => twenty_four::main(),
|
||||||
_ => {
|
_ => {
|
||||||
exists = false;
|
exists = false;
|
||||||
println!(
|
println!(
|
||||||
|
@ -119,4 +122,4 @@ Time elapsed when executing problem {} is: {:?}",
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
|
@ -30,9 +30,11 @@ struct RunArgs {
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
enum Commands {
|
enum Commands {
|
||||||
/// Handles the initialisation of a new Project Euler Problem
|
/// Handles the initialisation of a new Project Euler Problem
|
||||||
|
#[clap(alias = "n")]
|
||||||
New(NewArgs),
|
New(NewArgs),
|
||||||
|
|
||||||
/// Runs the solution to a problem
|
/// Runs the solution to a problem
|
||||||
|
#[clap(alias = "r")]
|
||||||
Run(RunArgs),
|
Run(RunArgs),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue