add main loop for 51 (WIP)

This commit is contained in:
timeshifter
2022-08-11 11:25:52 +02:00
parent d19e503d45
commit a9b0c0440b
2 changed files with 65 additions and 4 deletions
+57 -2
View File
@@ -4,11 +4,66 @@ use crate::primes::Primes;
type Int = i64; type Int = i64;
const MIN_VALUE: Int = 10_000_000; const MIN_VALUE: Int = 10_000;
const MAX_VALUE: Int = 10_010_000; const MAX_VALUE: Int = 99_999;
const DIGITS: [Int; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
// const MIN_VALUE: Int = 10_000_000;
// const MAX_VALUE: Int = 10_010_000;
// const MAX_VALUE: Int = 99_999_999; // const MAX_VALUE: Int = 99_999_999;
fn main() { fn main() {
let primes = Primes::get_between(MIN_VALUE, MAX_VALUE); let primes = Primes::get_between(MIN_VALUE, MAX_VALUE);
println!("{}", primes.vector[0]); println!("{}", primes.vector[0]);
for p in primes.vector {
let mut candidates = vec![];
let digits = num_to_digits(p);
for d in DIGITS {
let mut these_digits = digits.clone();
these_digits[3] = d;
these_digits[4] = d;
let num = digits_to_num(these_digits);
if primes.set.contains(&num) {
candidates.push(num)
}
}
println!("candidates");
for c in candidates {
println!("{c}");
}
}
}
fn num_to_digits(n: Int) -> Vec<Int> {
n.to_string()
.chars()
.map(|x| x.to_digit(10).unwrap() as Int)
.collect()
}
fn digits_to_num(vec: Vec<Int>) -> Int {
let mut result = 0;
let mut position = 1;
for digit in vec.iter().rev() {
result += digit * position;
position *= 10;
}
result
}
#[cfg(test)]
mod test {
use crate::{digits_to_num, num_to_digits};
#[test]
fn test_digits_to_num() {
let digits = vec![5, 7, 3, 8, 1];
assert_eq!(digits_to_num(digits), 57381);
}
#[test]
fn test_num_to_digits() {
let num = 83371;
assert_eq!(num_to_digits(num), vec![8, 3, 3, 7, 1]);
}
} }
+8 -2
View File
@@ -27,8 +27,9 @@ impl<T: Num + ToPrimitive + FromPrimitive + Hash + Eq + PartialEq + Copy> Primes
let mut set = HashSet::new(); let mut set = HashSet::new();
for number in *min_value..*max_value { for number in *min_value..*max_value {
if array[(number - min_value) as usize] { if array[(number - min_value) as usize] {
vector.push(FromPrimitive::from_usize(number).unwrap()); let prime = FromPrimitive::from_usize(number).unwrap();
set.insert(FromPrimitive::from_usize(number).unwrap()); vector.push(prime);
set.insert(prime);
} }
} }
Primes { vector, set } Primes { vector, set }
@@ -56,4 +57,9 @@ impl<T: Num + ToPrimitive + FromPrimitive + Hash + Eq + PartialEq + Copy> Primes
} }
result result
} }
pub fn remove(&mut self, num: T) {
let index = self.vector.iter().position(|x| x == &num).unwrap();
self.vector.remove(index);
}
} }