mirror of
https://bitbucket.org/Mattrixwv/projecteulerlua.git
synced 2025-12-06 17:43:57 -05:00
Initial commit with existing files
This commit is contained in:
89
Problem30.lua
Normal file
89
Problem30.lua
Normal file
@@ -0,0 +1,89 @@
|
||||
--ProjectEuler/lua/Problem29.lua
|
||||
-- Created: 10-28-19
|
||||
--Modified: 10-28-19
|
||||
--Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.
|
||||
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
|
||||
--[[
|
||||
Copyright (C) 2019 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/>.
|
||||
]]
|
||||
|
||||
|
||||
require "Stopwatch"
|
||||
|
||||
|
||||
--Returns a table with the individual digits of the number passed to it
|
||||
function getDigits(num)
|
||||
local listOfDigits = {}; --This table holds the individual digits of num
|
||||
--The easiest way to get the individual digits of a number is by converting it to a string
|
||||
local digits = tostring(num);
|
||||
--Start with the first digit, convert it to an integer, store it in the table, and move to the next digit
|
||||
for cnt = 1, #digits do
|
||||
table.insert(listOfDigits, tonumber(string.sub(digits, cnt, cnt)));
|
||||
end
|
||||
--Return the table of digits
|
||||
return listOfDigits;
|
||||
end
|
||||
|
||||
--Gets the sum of a table of numbers
|
||||
function getSumOfTable(ary)
|
||||
local sum = 0; --Start the sum at 0 so you can add to it
|
||||
--Add every number in the table to the sum
|
||||
for cnt = 1, #ary do
|
||||
sum = sum + ary[cnt];
|
||||
end
|
||||
--Return the sum
|
||||
return sum;
|
||||
end
|
||||
|
||||
|
||||
--Setup the variables
|
||||
local timer = Stopwatch:create();
|
||||
local TOP_NUM = 1000000; --This is the largest number that will be checked
|
||||
local BOTTOM_NUM = 2; --Starts with 2 because 0 and 1 don't count
|
||||
local POWER_RAISED = 5; --This is the power that the digits are raised to
|
||||
local sumOfFifthNumbers = {}; --This is a table of the numbers that are the sum of the fifth power of their digits
|
||||
|
||||
--Start the timer
|
||||
timer:start();
|
||||
|
||||
--Start with the lowest number and increment until you reach the largest number
|
||||
for currentNum = BOTTOM_NUM, TOP_NUM do
|
||||
--Get the digits of the number
|
||||
local digits = getDigits(currentNum);
|
||||
--Get the sum of the powers
|
||||
local sumOfPowers = 0;
|
||||
for cnt = 1, #digits do
|
||||
sumOfPowers = sumOfPowers + math.pow(digits[cnt], POWER_RAISED);
|
||||
end
|
||||
--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) then
|
||||
table.insert(sumOfFifthNumbers, currentNum);
|
||||
end
|
||||
end
|
||||
|
||||
--Stop the timer
|
||||
timer:stop();
|
||||
|
||||
--Print the results
|
||||
io.write("The sum of all the numbers that can be written as the sum of the fifth powers of their digits is " .. getSumOfTable(sumOfFifthNumbers) .. '\n');
|
||||
io.write("It took " .. timer:getString() .. " to run this algorithm\n");
|
||||
|
||||
|
||||
--[[ 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 2.706 seconds to run this algorithm
|
||||
]]
|
||||
Reference in New Issue
Block a user