Skip to content

Commit 89b3311

Browse files
committed
Add two solutions to euler #41.
One of them (41-take2.rs) requires a still-not-accepted patch to the Rust standard library; I'm not sure if it will ever be accepted, either. rust-lang/rust#13761
1 parent c0abab0 commit 89b3311

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

euler/41-take2.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
extern crate exscape;
2+
use exscape::is_prime;
3+
use std::iter::AdditiveIterator;
4+
5+
fn main() {
6+
let num = &mut[7u,6,5,4,3,2,1];
7+
8+
loop {
9+
// Reconstruct array into a number; e.g. 7 * 10^6 + 6 * 10^5 + 5 * 10^4 + 1 * 10^0... = 7654321
10+
let n = num.iter().enumerate().map(|(pos,&val)| val * std::num::pow(10u,num.len() - pos - 1)).sum();
11+
12+
if is_prime(n) {
13+
println!("Answer: {}", n);
14+
break;
15+
}
16+
num.prev_permutation();
17+
}
18+
}

euler/41.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
extern crate exscape;
2+
use exscape::{primes_up_to, is_pandigital};
3+
4+
fn main() {
5+
// 8-digit and 9-digit pandigitals will all be divisible by 3,
6+
// as their digital sums are divisible by 3. Therefore, none of them are prime.
7+
for &p in primes_up_to(7654321).iter().rev() {
8+
if is_pandigital(p) {
9+
println!("Answer: {}", p);
10+
break;
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)