From b8040027c95b094e94b768adcbd271acea622139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Sommer?= Date: Thu, 30 Mar 2017 20:33:16 +0200 Subject: [PATCH] =?UTF-8?q?J=C3=B6rgs=20L=C3=B6sung=20f=C3=BCr=20Euler=208?= =?UTF-8?q?7=20in=20Rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2017-03-28/Makefile | 5 ++- 2017-03-28/jo-so_euler87.rs | 62 +++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 2017-03-28/jo-so_euler87.rs diff --git a/2017-03-28/Makefile b/2017-03-28/Makefile index 968aae5..db17310 100644 --- a/2017-03-28/Makefile +++ b/2017-03-28/Makefile @@ -8,6 +8,9 @@ LDFLAGS=-lm .PHONY: all -all: jo-so_euler87 +all: jo-so_euler87 jo-so_euler87_rust jo-so_euler87: + +jo-so_euler87_rust: jo-so_euler87.rs + rustc -O -g -o $@ $< diff --git a/2017-03-28/jo-so_euler87.rs b/2017-03-28/jo-so_euler87.rs new file mode 100644 index 0000000..c5611e1 --- /dev/null +++ b/2017-03-28/jo-so_euler87.rs @@ -0,0 +1,62 @@ +/* + Copyright © 2017 Jörg Sommer + + License: MIT https://opensource.org/licenses/MIT + */ + +////// +// +// Solution of Euler 87 https://projecteuler.net/problem=87 +// +fn gen_prime_numbers(limit: u32) -> Vec { + let mut ret = vec![2, 3]; + + let mut num = *ret.last().unwrap(); + 'num: while num + 2 <= limit { + num += 2; + for i in 3..((num as f32).sqrt() as u32 + 1) { + if num % i == 0 { + continue 'num; + } + } + ret.push(num); + } + + ret +} + +fn main() { + const LIMIT: u32 = 50_000_000; + + // println!("pn {:?}", gen_prime_numbers(500)); + let pn = gen_prime_numbers((LIMIT as f32).sqrt() as u32); + + println!("There are {} numbers below sqrt(50.000.000)", pn.len()); + + let mut nums = Vec::new(); + + for pn2 in pn.iter() { + let pow2 = pn2.pow(2); + + for pn3 in pn.iter() { + let sum = pow2 + pn3.pow(3); + if sum >= LIMIT { + break; + } + + for pn4 in pn.iter() { + let sum2 = sum + pn4.pow(4); + if sum2 >= LIMIT { + break; + } + + nums.push(sum2); + } + } + } + + nums.sort(); + nums.dedup(); + + println!("Answer {}", nums.len()); +}