add more code for 51 (WIP)

This commit is contained in:
Dr. Matthias Ratajczak
2022-08-11 14:16:40 +02:00
parent a9b0c0440b
commit 4b8c4729a5
2 changed files with 27 additions and 10 deletions
+21 -7
View File
@@ -14,8 +14,9 @@ const DIGITS: [Int; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
fn main() {
let primes = Primes::get_between(MIN_VALUE, MAX_VALUE);
// let mut new_primes = primes.clone();
println!("{}", primes.vector[0]);
for p in primes.vector {
for p in primes.vector.iter() {
let mut candidates = vec![];
let digits = num_to_digits(p);
for d in DIGITS {
@@ -27,14 +28,21 @@ fn main() {
candidates.push(num)
}
}
println!("candidates");
for c in candidates {
println!("{c}");
if candidates.len() > 4 {
println!("candidates");
for c in &candidates {
// new_primes.remove(c);
println!("{c}");
}
}
}
// println!("new primes");
// for p in new_primes.vector.iter() {
// println!("{p}");
// }
}
fn num_to_digits(n: Int) -> Vec<Int> {
fn num_to_digits(n: &Int) -> Vec<Int> {
n.to_string()
.chars()
.map(|x| x.to_digit(10).unwrap() as Int)
@@ -53,7 +61,7 @@ fn digits_to_num(vec: Vec<Int>) -> Int {
#[cfg(test)]
mod test {
use crate::{digits_to_num, num_to_digits};
use crate::{digits_to_num, num_to_digits, primes::Primes, Int, MAX_VALUE, MIN_VALUE};
#[test]
fn test_digits_to_num() {
@@ -64,6 +72,12 @@ mod test {
#[test]
fn test_num_to_digits() {
let num = 83371;
assert_eq!(num_to_digits(num), vec![8, 3, 3, 7, 1]);
assert_eq!(num_to_digits(&num), vec![8, 3, 3, 7, 1]);
}
#[test]
fn test_example_prime() {
let num: Int = 56003;
assert!(Primes::get_between(MIN_VALUE, MAX_VALUE).set.contains(&num));
}
}
+6 -3
View File
@@ -6,6 +6,7 @@ use num::ToPrimitive;
use std::collections::HashSet;
use std::hash::Hash;
#[derive(Clone)]
pub struct Primes<T: Num + ToPrimitive> {
pub vector: Vec<T>,
pub set: HashSet<T>,
@@ -58,8 +59,10 @@ 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);
pub fn remove(&mut self, num: &T) {
if let Some(index) = self.vector.iter().position(|x| x == num) {
self.vector.remove(index);
self.set.remove(num);
};
}
}