add main loop for 51 (WIP)
This commit is contained in:
+57
-2
@@ -4,11 +4,66 @@ use crate::primes::Primes;
|
||||
|
||||
type Int = i64;
|
||||
|
||||
const MIN_VALUE: Int = 10_000_000;
|
||||
const MAX_VALUE: Int = 10_010_000;
|
||||
const MIN_VALUE: Int = 10_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;
|
||||
|
||||
fn main() {
|
||||
let primes = Primes::get_between(MIN_VALUE, MAX_VALUE);
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,8 +27,9 @@ impl<T: Num + ToPrimitive + FromPrimitive + Hash + Eq + PartialEq + Copy> Primes
|
||||
let mut set = HashSet::new();
|
||||
for number in *min_value..*max_value {
|
||||
if array[(number - min_value) as usize] {
|
||||
vector.push(FromPrimitive::from_usize(number).unwrap());
|
||||
set.insert(FromPrimitive::from_usize(number).unwrap());
|
||||
let prime = FromPrimitive::from_usize(number).unwrap();
|
||||
vector.push(prime);
|
||||
set.insert(prime);
|
||||
}
|
||||
}
|
||||
Primes { vector, set }
|
||||
@@ -56,4 +57,9 @@ impl<T: Num + ToPrimitive + FromPrimitive + Hash + Eq + PartialEq + Copy> Primes
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, num: T) {
|
||||
let index = self.vector.iter().position(|x| x == &num).unwrap();
|
||||
self.vector.remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user