Rust Implementation of Problem 41

View source code here on GitHub!

Includes

Problem Solution

pub fn problems::p0041::p0041() -> utils::Answer
 1/*
 2Project Euler Problem 41
 3
 4Once I found out where the end was, it seemed to be relatively easy
 5
 6Problem:
 7
 8We shall say that an n-digit number is pandigital if it makes use of all the
 9digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is
10also prime.
11
12What is the largest n-digit pandigital prime that exists?
13*/
14use crate::include::primes::primes;
15use crate::include::utils::Answer;
16
17pub fn p0041() -> Answer {
18    let mut answer: i64 = -1;
19    for p in primes::<i64>() {
20        let cur_digits = p.to_string();
21        let num_digits = cur_digits.len();
22        if num_digits > 7 {
23            break;
24        }
25        if cur_digits.bytes().any(|b| ((b - b'0') as usize > num_digits || cur_digits.bytes().filter(|c| *c == b).count() != 1)) {
26            continue;
27        }
28        if p > answer {
29            answer = p;
30        }
31    }
32    return Answer::Int(answer.into());
33}

Tags: pandigital, prime-number, rust-iterator