Added a solution to problem 30

This commit is contained in:
2020-06-18 00:04:36 -04:00
parent 4b083dd6cc
commit 7eb869547a
3 changed files with 91 additions and 2 deletions

View File

@@ -52,3 +52,4 @@ pub mod Problem26;
pub mod Problem27;
pub mod Problem28;
pub mod Problem29;
pub mod Problem30;

80
src/Problems/Problem30.rs Normal file
View File

@@ -0,0 +1,80 @@
//ProjectEulerRust/src/Problems/Problems30.rs
//Matthew Ellison
// Created: 06-17-20
//Modified: 06-17-20
//Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.
//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{
"Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.".to_string()
}
pub fn solve() -> Answer{
let TOP_NUM = 1_000_000; //This is the largest number that will be checked
let BOTTOM_NUM = 2; //Starts with 2 because 0 and 1 don't count
let POWER_RAISED = 5; //This is the power that the digits are raised to
let mut sumOfFifthNumbers = Vec::<i64>::new(); //Sum of the power of the fifth powers of their digits
//Start the timer
let mut timer = myClasses::Stopwatch::Stopwatch::new();
timer.start();
//Start with the lowest number and increment until you reach the largest number
for currentNum in BOTTOM_NUM..=TOP_NUM{
//Get the digits of the number
let digits = getDigits(currentNum);
//Get the sum of the powers
let mut sumOfPowers = 0;
for num in digits{
sumOfPowers += num.pow(POWER_RAISED);
}
//Check if the sum of the powers is the same as the number
//If it is add it to the list, otherwise continue to the next number
if(sumOfPowers == currentNum){
sumOfFifthNumbers.push(currentNum);
}
}
//Stop the timer
timer.stop();
//Return the results
return Answer::new(format!("The sum of all the numbers that can be written as the sum of the fifth powers of their digits is {}", sumOfFifthNumbers.iter().sum::<i64>()), timer.getString());
}
//Returns a vector with the individual digits of the number passed to it
fn getDigits(num: i64) -> Vec::<i64>{
let mut listOfDigits = Vec::<i64>::new(); //This ArrayList holds the individual digits of num
//The easiest way to get the individual digits of a number is by converting it to a string
let digits = num.to_string();
//Start with the first digit, convert it to an integer, store it in the ArrayList, and move to the next digit
for character in digits.chars(){
listOfDigits.push(character.to_digit(10).unwrap() as i64);
}
//Return the list of digits
return listOfDigits;
}
/* Results:
The sum of all the numbers that can be written as the sum of the fifth powers of their digits is 443839
It took 320.693 milliseconds to solve this problem
*/

View File

@@ -33,9 +33,10 @@ mod Problems;
#[derive(PartialEq)]
enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE}
static problemNumbers: [u32; 30] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
static problemNumbers: [u32; 31] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29];
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30];
fn main(){
let mut selection = Selections::EMPTY;
@@ -248,6 +249,10 @@ fn solveProblem(problemNumber: u32){
println!("{}", Problems::Problem29::getDescription());
println!("{}", Problems::Problem29::solve());
}
else if(problemNumber == 30){
println!("{}", Problems::Problem30::getDescription());
println!("{}", Problems::Problem30::solve());
}
}
fn descriptionMenu(){
//Give some extra space to print the description
@@ -360,6 +365,9 @@ fn printDescription(problemNumber: u32){
else if(problemNumber == 29){
println!("{}", Problems::Problem29::getDescription());
}
else if(problemNumber == 30){
println!("{}", Problems::Problem30::getDescription());
}
}
fn getProblemNumber() -> u32{
println!("Enter a problem number: ");