Rust Implementation of Problem 37

View source code here on GitHub!

Includes

Problem Solution

pub fn problems::p0037::p0037() -> utils::Answer
 1/*
 2Project Euler Problem 37
 3
 4I was surprised how fast my brute-force solution went, but it worked
 5
 6Problem:
 7
 8The number 3797 has an interesting property. Being prime itself, it is possible
 9to continuously remove digits from left to right, and remain prime at each
10stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797,
11379, 37, and 3.
12
13Find the sum of the only eleven primes that are both truncatable from left to
14right and right to left.
15
16NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
17*/
18use crate::include::primes::{is_prime,primes};
19use crate::include::utils::Answer;
20
21pub fn p0037() -> Answer {
22    let mut answer: u64 = 0;
23    let mut count: u64 = 0;
24    for p in primes::<u64>() {
25        if count == 11 {
26            break;
27        }
28        else if p < 10 {
29            continue;
30        }
31        let mut left = p;
32        let mut right = p;
33        while is_prime(right) {
34            right /= 10;
35        }
36        if right != 0 {
37            continue;
38        }
39        while is_prime(left) {
40            let mut x = 10;
41            while x < left {
42                x *= 10;
43            }
44            left %= x / 10;
45        }
46        if left != 0 {
47            continue;
48        }
49        answer += p;
50        count += 1;
51    }
52    return Answer::Int(answer.into());
53}

Tags: prime-number, digit-manipulation, rust-iterator