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:
64
Problem29.lua
Normal file
64
Problem29.lua
Normal file
@@ -0,0 +1,64 @@
|
||||
--ProjectEuler/lua/Problem29.lua
|
||||
-- Created: 10-10-19
|
||||
--Modified: 10-10-19
|
||||
--How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?
|
||||
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
|
||||
--I used the bigint library from https://github.com/empyreuma/bigint.lua
|
||||
--[[
|
||||
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"
|
||||
require "Algorithms"
|
||||
local bigint = require("bigint")
|
||||
|
||||
--Setup the variables
|
||||
local timer = Stopwatch:create();
|
||||
local BOTTOM_A = 2; --The lowest possible value for A
|
||||
local TOP_A = 100; --The highest possible value for A
|
||||
local BOTTOM_B = 2; --The lowest possible value for B
|
||||
local TOP_B = 100; --The highest possible value for B
|
||||
local unique = {}; --A table to hold all of the unique answers for the equation
|
||||
local currentNum = bigint.new(); --Holds the answer to the equation for a particular loop
|
||||
|
||||
--Start the timer
|
||||
timer:start();
|
||||
|
||||
--Start with the lowest A and move towards the largest
|
||||
for currentA = BOTTOM_A, TOP_A do
|
||||
--Start with the lowest B and move towards the largest
|
||||
for currentB = BOTTOM_B, TOP_B do
|
||||
--Get the new number
|
||||
currentNum = bigint.unserialize(bigint.exponentiate(bigint.new(currentA), bigint.new(currentB)), "s");
|
||||
--If the number isn't in the list add it
|
||||
if(not isFound(unique, currentNum)) then
|
||||
table.insert(unique, currentNum);
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--Stop the timer
|
||||
timer:stop();
|
||||
|
||||
--Print the results
|
||||
io.write("The number of unique values generated by a^b for " .. BOTTOM_A .. " <= a <= " .. TOP_A .. " and " .. BOTTOM_B .. " <= b <= " .. TOP_B .. " is " .. #unique .. '\n');
|
||||
io.write("It took " .. timer:getString() .. " to run this algorithm\n");
|
||||
|
||||
--[[ Results:
|
||||
The number of unique values generated by a^b for 2 <= a <= 100 and 2 <= b <= 100 is 9183
|
||||
It took 2.714 minutes to run this algorithm
|
||||
]]
|
||||
Reference in New Issue
Block a user