Files
projecteuler/euler51/src/main.rs
T

70 lines
1.6 KiB
Rust

mod primes;
use crate::primes::Primes;
type Int = i64;
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]);
}
}