~janbaudisch/euler-rs

e2fc659ca292aeebc705dd735b6496c61cccfec3 — Jan Baudisch 5 years ago 7a76546
add: problem 35
4 files changed, 51 insertions(+), 1 deletions(-)

M Cargo.lock
M Cargo.toml
A problem_035/Cargo.toml
A problem_035/src/main.rs
M Cargo.lock => Cargo.lock +7 -0
@@ 55,3 55,10 @@ dependencies = [
name = "problem_019"
version = "0.1.0"

[[package]]
name = "problem_035"
version = "0.1.0"
dependencies = [
 "common 0.1.0",
]


M Cargo.toml => Cargo.toml +2 -1
@@ 8,7 8,8 @@ members = [
    "problem_006",
    "problem_007",
    "problem_010",
    "problem_019"
    "problem_019",
    "problem_035"
]

[profile.release]

A problem_035/Cargo.toml => problem_035/Cargo.toml +8 -0
@@ 0,0 1,8 @@
[package]
name = "problem_035"
version = "0.1.0"
authors = ["Jan Baudisch <dev@baudisch.xyz>"]
edition = "2018"

[dependencies]
common = { path = "../common" }

A problem_035/src/main.rs => problem_035/src/main.rs +34 -0
@@ 0,0 1,34 @@
use common::input;
use common::math::IsPrime;

fn main() {
    println!("[INPUT] calculate primes until:");
    let limit = u64::from_str_radix(&input::read_line(), 10).expect("Could not parse input!");

    let mut count = 0;

    for n in 2..limit {
        if n.is_prime() {
            let mut digits = n.to_string().chars().into_iter().collect::<Vec<char>>();

            let mut flag = true;

            for _ in 0..digits.len() {
                digits.rotate_right(1);
                let x =
                    u64::from_str_radix(&digits.clone().iter().collect::<String>(), 10).unwrap();

                if !x.is_prime() {
                    flag = false;
                    break;
                }
            }

            if flag {
                count += 1;
            }
        }
    }

    println!("[SOLUTION] circular primes below {}: {}", limit, count);
}