add permutation.rs for 51 (WIP)
This commit is contained in:
+4
-2
@@ -1,3 +1,4 @@
|
|||||||
|
mod permutation;
|
||||||
mod primes;
|
mod primes;
|
||||||
|
|
||||||
use crate::primes::Primes;
|
use crate::primes::Primes;
|
||||||
@@ -12,10 +13,12 @@ const DIGITS: [Int; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|||||||
// const MAX_VALUE: Int = 10_010_000;
|
// const MAX_VALUE: Int = 10_010_000;
|
||||||
// const MAX_VALUE: Int = 99_999_999;
|
// const MAX_VALUE: Int = 99_999_999;
|
||||||
|
|
||||||
|
const LENGTH_OF_NUMBERS: usize = 5;
|
||||||
|
const MIN_LENGTH: usize = 2;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let primes = Primes::get_between(MIN_VALUE, MAX_VALUE);
|
let primes = Primes::get_between(MIN_VALUE, MAX_VALUE);
|
||||||
// let mut new_primes = primes.clone();
|
// let mut new_primes = primes.clone();
|
||||||
println!("{}", primes.vector[0]);
|
|
||||||
for p in primes.vector.iter() {
|
for p in primes.vector.iter() {
|
||||||
let mut candidates = vec![];
|
let mut candidates = vec![];
|
||||||
let digits = num_to_digits(p);
|
let digits = num_to_digits(p);
|
||||||
@@ -41,7 +44,6 @@ fn main() {
|
|||||||
// println!("{p}");
|
// println!("{p}");
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn num_to_digits(n: &Int) -> Vec<Int> {
|
fn num_to_digits(n: &Int) -> Vec<Int> {
|
||||||
n.to_string()
|
n.to_string()
|
||||||
.chars()
|
.chars()
|
||||||
|
|||||||
@@ -0,0 +1,75 @@
|
|||||||
|
struct PositionPermut {
|
||||||
|
max_length: usize,
|
||||||
|
number_of_digits: usize,
|
||||||
|
idx: usize,
|
||||||
|
total_number_of_iterations: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for PositionPermut {
|
||||||
|
type Item = Vec<i64>;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
if self.idx == (self.total_number_of_iterations) {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
let pos1 = self.idx % self.number_of_digits;
|
||||||
|
let pos0 = 0;
|
||||||
|
let x = vec![pos0 as i64, pos1 as i64];
|
||||||
|
let result = Some(x);
|
||||||
|
self.idx += 1;
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PositionPermut {
|
||||||
|
fn new(max_length: usize) -> Self {
|
||||||
|
assert!(max_length >= 3);
|
||||||
|
let total_number_of_iterations = (1..max_length + 1).sum();
|
||||||
|
Self {
|
||||||
|
max_length,
|
||||||
|
number_of_digits: 2,
|
||||||
|
idx: 0,
|
||||||
|
total_number_of_iterations,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn test(&mut self) {
|
||||||
|
// length_of_numbers = 4
|
||||||
|
// number_of_digits = 2
|
||||||
|
[0, 1];
|
||||||
|
[0, 2];
|
||||||
|
[0, 3];
|
||||||
|
[1, 2];
|
||||||
|
[1, 3];
|
||||||
|
[2, 3];
|
||||||
|
// number_of_digits = 3
|
||||||
|
[0, 1, 2];
|
||||||
|
[0, 1, 3];
|
||||||
|
[0, 2, 3];
|
||||||
|
[1, 2, 3];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
#[test]
|
||||||
|
fn test_permutation() {
|
||||||
|
use crate::permutation::PositionPermut;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
let permutator = PositionPermut::new(3);
|
||||||
|
let result: HashSet<_> = permutator.into_iter().collect();
|
||||||
|
let mut solution = HashSet::new();
|
||||||
|
solution.insert(vec![0, 1]);
|
||||||
|
solution.insert(vec![0, 2]);
|
||||||
|
solution.insert(vec![0, 3]);
|
||||||
|
solution.insert(vec![1, 2]);
|
||||||
|
solution.insert(vec![1, 3]);
|
||||||
|
solution.insert(vec![2, 3]);
|
||||||
|
// solution.insert(vec![0, 1, 2]);
|
||||||
|
// solution.insert(vec![0, 1, 3]);
|
||||||
|
// solution.insert(vec![0, 2, 3]);
|
||||||
|
// solution.insert(vec![1, 2, 3]);
|
||||||
|
// assert_eq!(result.len(), solution.len());
|
||||||
|
assert_eq!(result, solution);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,7 +12,10 @@ pub struct Primes<T: Num + ToPrimitive> {
|
|||||||
pub set: HashSet<T>,
|
pub set: HashSet<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Num + ToPrimitive + FromPrimitive + Hash + Eq + PartialEq + Copy> Primes<T> {
|
impl<T> Primes<T>
|
||||||
|
where
|
||||||
|
T: Num + ToPrimitive + FromPrimitive + Hash + Eq + PartialEq + Copy,
|
||||||
|
{
|
||||||
pub fn get_between(min_value: T, max_value: T) -> Self {
|
pub fn get_between(min_value: T, max_value: T) -> Self {
|
||||||
let max_value_usize = max_value.to_usize().unwrap();
|
let max_value_usize = max_value.to_usize().unwrap();
|
||||||
let min_value_usize = min_value.to_usize().unwrap();
|
let min_value_usize = min_value.to_usize().unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user