mirror of
https://bitbucket.org/Mattrixwv/projecteulerrust.git
synced 2025-12-06 17:43:58 -05:00
Added solution to problem 21
This commit is contained in:
@@ -43,3 +43,4 @@ pub mod Problem17;
|
||||
pub mod Problem18;
|
||||
pub mod Problem19;
|
||||
pub mod Problem20;
|
||||
pub mod Problem21;
|
||||
|
||||
101
src/Problems/Problem21.rs
Normal file
101
src/Problems/Problem21.rs
Normal file
@@ -0,0 +1,101 @@
|
||||
//ProjectEulerRust/src/Problems/Problems21.rs
|
||||
//Matthew Ellison
|
||||
// Created: 06-17-20
|
||||
//Modified: 06-17-20
|
||||
//Evaluate the sum of all the amicable numbers under 10000
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses
|
||||
/*
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
extern crate myClasses;
|
||||
use crate::Problems::Answer::Answer;
|
||||
|
||||
pub fn getDescription() -> String{
|
||||
"Evaluate the sum of all the amicable numbers under 10000".to_string()
|
||||
}
|
||||
|
||||
pub fn solve() -> Answer{
|
||||
let LIMIT = 10000;
|
||||
//Setup the variables
|
||||
let mut divisorSum = Vec::<i64>::new();
|
||||
//Make sure the array is filled with 0's
|
||||
while(divisorSum.len() < LIMIT){
|
||||
divisorSum.push(0);
|
||||
}
|
||||
|
||||
//Start the timer
|
||||
let mut timer = myClasses::Stopwatch::Stopwatch::new();
|
||||
timer.start();
|
||||
|
||||
//Generate the divisors of all numbers < 10000, get their sum, and add it to the list
|
||||
for cnt in 1..LIMIT{
|
||||
let mut divisors = myClasses::Algorithms::getDivisors(cnt as i64);
|
||||
if(divisors.len() > 1){
|
||||
divisors.remove(divisors.len() - 1);
|
||||
}
|
||||
divisorSum[cnt] = divisors.iter().sum();
|
||||
}
|
||||
//Check every sum of divisors in the list for matching sum
|
||||
let mut amicable = Vec::<i64>::new();
|
||||
for cnt in 1..divisorSum.len(){
|
||||
let sum = divisorSum[cnt];
|
||||
//If the sum is greater than the number of divisors then it is impossible to be amicable. Skip the number and continue
|
||||
if(sum >= divisorSum.len() as i64){
|
||||
continue;
|
||||
}
|
||||
//We know that divisorSum.at(cnt) == sum, so if divisorSum.at(sum) == cnt we found an amicable number
|
||||
if(divisorSum[sum as usize] == cnt as i64){
|
||||
//A number can't be amicable with itself
|
||||
if(sum == cnt as i64){
|
||||
continue;
|
||||
}
|
||||
//Add it to the array of amicable numbers
|
||||
amicable.push(cnt as i64);
|
||||
}
|
||||
}
|
||||
|
||||
//Stop the timer
|
||||
timer.stop();
|
||||
|
||||
//Sort the array for neatness
|
||||
amicable.sort();
|
||||
|
||||
//Return the results
|
||||
let mut answerString = format!("All amicable numbers less than {} are\n", LIMIT);
|
||||
for cnt in 0..amicable.len(){
|
||||
answerString = format!("{}{}\n", answerString, amicable[cnt]);
|
||||
}
|
||||
answerString = format!("{}The sum of all of these amicable numbers is {}", answerString, amicable.iter().sum::<i64>());
|
||||
return Answer::new(answerString, timer.getString());
|
||||
}
|
||||
|
||||
/* Results:
|
||||
All amicable numbers less than 10000 are
|
||||
220
|
||||
284
|
||||
1184
|
||||
1210
|
||||
2620
|
||||
2924
|
||||
5020
|
||||
5564
|
||||
6232
|
||||
6368
|
||||
The sum of all of these amicable numbers is 31626
|
||||
It took 5.792 milliseconds to solve this problem
|
||||
*/
|
||||
13
src/main.rs
13
src/main.rs
@@ -1,7 +1,7 @@
|
||||
//ProjectEulerRust/src/main.rs
|
||||
//Matthew Ellison
|
||||
// Created: 06-11-20
|
||||
//Modified: 06-15-20
|
||||
//Modified: 06-17-20
|
||||
//This is a driver function for the Project Euler solutions in Rust
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses
|
||||
/*
|
||||
@@ -33,9 +33,9 @@ mod Problems;
|
||||
#[derive(PartialEq)]
|
||||
enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE}
|
||||
|
||||
static problemNumbers: [u32; 21] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
static problemNumbers: [u32; 22] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20];
|
||||
20, 21];
|
||||
|
||||
fn main(){
|
||||
let mut selection = Selections::EMPTY;
|
||||
@@ -212,6 +212,10 @@ fn solveProblem(problemNumber: u32){
|
||||
println!("{}", Problems::Problem20::getDescription());
|
||||
println!("{}", Problems::Problem20::solve());
|
||||
}
|
||||
else if(problemNumber == 21){
|
||||
println!("{}", Problems::Problem21::getDescription());
|
||||
println!("{}", Problems::Problem21::solve());
|
||||
}
|
||||
}
|
||||
fn descriptionMenu(){
|
||||
//Give some extra space to print the description
|
||||
@@ -297,6 +301,9 @@ fn printDescription(problemNumber: u32){
|
||||
else if(problemNumber == 20){
|
||||
println!("{}", Problems::Problem20::getDescription());
|
||||
}
|
||||
else if(problemNumber == 21){
|
||||
println!("{}", Problems::Problem21::getDescription());
|
||||
}
|
||||
}
|
||||
fn getProblemNumber() -> u32{
|
||||
println!("Enter a problem number: ");
|
||||
|
||||
Reference in New Issue
Block a user