Finished projecteuler 087 quite quickly
This commit is contained in:
7
projecteuler/087/Cargo.lock
generated
Normal file
7
projecteuler/087/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
8
projecteuler/087/Cargo.toml
Normal file
8
projecteuler/087/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
62
projecteuler/087/src/main.rs
Normal file
62
projecteuler/087/src/main.rs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
fn primes(max_prime: u32) -> Vec<u32> {
|
||||||
|
let mut prime_bools = vec![true; max_prime as usize];
|
||||||
|
prime_bools[0] = false;
|
||||||
|
prime_bools[1] = false;
|
||||||
|
|
||||||
|
for p in 2..=(max_prime as f32).powf(0.5) as usize + 1 {
|
||||||
|
if !prime_bools[p] {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut i = p;
|
||||||
|
while i + p < max_prime as usize {
|
||||||
|
i += p;
|
||||||
|
prime_bools[i] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
prime_bools
|
||||||
|
.into_iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter_map(|(n, b)| match b {
|
||||||
|
false => None,
|
||||||
|
true => Some(n as u32),
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, this is Patrick!");
|
||||||
|
let now = Instant::now();
|
||||||
|
|
||||||
|
let mut prime_power_triples = HashSet::new();
|
||||||
|
let max_value = 50000000;
|
||||||
|
|
||||||
|
let primes_power_two = primes(f32::powf(max_value as f32, 0.5) as u32 + 1);
|
||||||
|
let primes_power_three = primes(f32::powf(max_value as f32, 1.0 / 3.0) as u32 + 1);
|
||||||
|
let primes_power_four = primes(f32::powf(max_value as f32, 0.25) as u32 + 1);
|
||||||
|
|
||||||
|
for twos in primes_power_two.iter() {
|
||||||
|
for threes in primes_power_three.iter() {
|
||||||
|
for fours in primes_power_four.iter() {
|
||||||
|
let n = twos * twos + threes * threes * threes + fours * fours * fours * fours;
|
||||||
|
if n < max_value {
|
||||||
|
prime_power_triples.insert(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"Number of prime power triples under {}: {:?}",
|
||||||
|
max_value,
|
||||||
|
prime_power_triples.len()
|
||||||
|
);
|
||||||
|
|
||||||
|
// println!("They are: {:?}", prime_power_triples);
|
||||||
|
|
||||||
|
println!("Time passed: {:?}", Instant::now() - now);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user