mirror of
https://bitbucket.org/Mattrixwv/projecteulerrust.git
synced 2025-12-06 17:43:58 -05:00
Initial commit with a few problems
This commit is contained in:
15
src/Problems/Answer.rs
Normal file
15
src/Problems/Answer.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
//A structure to hold the answer to the problem
|
||||
pub struct Answer{
|
||||
result: String,
|
||||
time: String,
|
||||
}
|
||||
impl std::fmt::Display for Answer{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result{
|
||||
write!(f, "{}\nIt took {} to solve this problem", self.result, self.time)
|
||||
}
|
||||
}
|
||||
impl Answer{
|
||||
pub fn new(result: String, time: String) -> Answer{
|
||||
Answer{result, time}
|
||||
}
|
||||
}
|
||||
27
src/Problems/Problem1.rs
Normal file
27
src/Problems/Problem1.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
extern crate myClasses;
|
||||
|
||||
pub use crate::Problems::Answer::Answer;
|
||||
|
||||
pub fn getDescription() -> &'static str{
|
||||
"What is the sum of all the multiples of 3 or 5 that are less than 1000"
|
||||
}
|
||||
|
||||
pub fn solve() -> Answer{
|
||||
let mut fullSum = 0;
|
||||
let mut timer = myClasses::Stopwatch::Stopwatch::new();
|
||||
timer.start();
|
||||
//Set through every number < 1000 and see if either 3 or 5 divide it evenly
|
||||
for cnt in 1..1000{
|
||||
//If either divides it then add it to the vector
|
||||
if((cnt % 3) == 0){
|
||||
fullSum += cnt;
|
||||
}
|
||||
else if((cnt % 5) == 0){
|
||||
fullSum += cnt;
|
||||
}
|
||||
}
|
||||
timer.stop();
|
||||
|
||||
//Return the answer
|
||||
return Answer::new("The sum of all numbers < 1000 is ".to_string() + &fullSum.to_string(), timer.getString());
|
||||
}
|
||||
27
src/Problems/Problem2.rs
Normal file
27
src/Problems/Problem2.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
extern crate myClasses;
|
||||
|
||||
use crate::Problems::Answer::Answer;
|
||||
|
||||
pub fn getDescription() -> &'static str{
|
||||
"What is the sum of the even Fibonacci numbers less than 4,000,000?"
|
||||
}
|
||||
|
||||
pub fn solve() -> Answer{
|
||||
let mut timer = myClasses::Stopwatch::Stopwatch::new();
|
||||
timer.start();
|
||||
//Get a list of all fibonacci numbers < 4,000,000
|
||||
let fibNums = myClasses::Algorithms::getAllFib(3_999_999);
|
||||
let mut sum = 0;
|
||||
//Set through every element in the list checking if it is even
|
||||
for num in fibNums{
|
||||
//If the number is even add it to the running tally
|
||||
if((num % 2) == 0){
|
||||
sum += num;
|
||||
}
|
||||
}
|
||||
timer.stop();
|
||||
|
||||
//Return the answer
|
||||
return Answer::new("The sum of all even fibonacci numbers <= 3999999 is ".to_string() + &sum.to_string(), timer.getString());
|
||||
}
|
||||
Reference in New Issue
Block a user