Updated problems to be more in line with conventions

This commit is contained in:
2020-06-19 19:58:36 -04:00
parent 1bfd097fa3
commit 7d107c1071
31 changed files with 174 additions and 182 deletions

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem1.lua
--Matthew Ellison
-- Created: 02-01-19
--Modified: 03-28-19
--Modified: 06-19-20
--What is the sum of all the multiples of 3 or 5 that are less than 1000
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -25,11 +25,11 @@
require "Stopwatch"
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
TOP_NUMBER = 999; --This is the largest number that you are going to check
sumOfMultiples = 0;
local TOP_NUMBER = 999; --This is the largest number that you are going to check
local sumOfMultiples = 0;
for num = 1, TOP_NUMBER do
if((num % 3) == 0) then
sumOfMultiples = sumOfMultiples + num;

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem10.lua
--Matthew Ellison
-- Created: 02-06-19
--Modified: 03-28-19
--Modified: 06-19-20
--Find the sum of all the primes below two million
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -26,7 +26,7 @@ require "Stopwatch"
require "Algorithms"
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
LARGEST_PRIME = 2000000; --Sum all prime numbers smaller than this

View File

@@ -1,7 +1,7 @@
--ProjectEuler/lua/Problem11.lua
--Matthew Ellison
-- Created: 02-06-19
--Modified: 03-28-19
--Modified: 06-19-20
--What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?
--[[
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
@@ -27,7 +27,7 @@
]]
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -47,7 +47,7 @@
require "Stopwatch"
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
--Setup the grid of numbers
@@ -74,7 +74,7 @@ GRID[19] = {20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 5
GRID[20] = {1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48};
--This function returns the product of all elements in a table
function getTableProduct(numbers)
local function getTableProduct(numbers)
local product = 1; --Start with 1 because you are working with multiplication
--Loop through ever element in a table and multiply them all together
for location=1,#numbers do

View File

@@ -1,11 +1,11 @@
--ProjectEUler/lua/Problem12.lua
--Matthew Ellison
-- Created: 02-07-19
--Modified: 03-28-19
--Modified: 06-19-20
--What is the value of the first triangle number to have over five hundred divisors?
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -26,7 +26,7 @@ require "Stopwatch"
require "Algorithms"
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
GOAL_DIVISORS = 500;

View File

@@ -1,7 +1,7 @@
--ProjectEuler/lua/Problem13.lua
--Matthew Ellison
-- Created: 02-07-19
--Modified: 03-28-19
--Modified: 06-19-20
--Work out the first ten digits of the sum of the following one-hundred 50-digit numbers
--[[
37107287533902102798797998220837590246510135740250
@@ -108,7 +108,7 @@
--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
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
@@ -129,10 +129,10 @@ require "Stopwatch"
local bigint = require "bigint"
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
NUMBERS = {};
local NUMBERS = {};
NUMBERS[#NUMBERS+1] = bigint.new("37107287533902102798797998220837590246510135740250")
NUMBERS[#NUMBERS+1] = bigint.new("46376937677490009712648124896970078050417018260538")
NUMBERS[#NUMBERS+1] = bigint.new("74324986199524741059474233309513058123726617309629")

View File

@@ -1,7 +1,7 @@
--ProjectEuler/lua/Problem14.lua
--Matthew Ellison
-- Created: 02-07-19
--Modified: 03-28-19
--Modified: 06-19-20
--[[
The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even)
@@ -10,7 +10,7 @@ Which starting number, under one million, produces the longest chain?
]]
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -30,13 +30,13 @@ Which starting number, under one million, produces the longest chain?
require "Stopwatch"
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
TOP_NUM = 1000000 --The largest number that you will check against the chain
--This function returns a table of numbers created by the chain
function getChain(startNum)
local function getChain(startNum)
--Put the starting number in the list
local chain = {};
chain[#chain+1] = startNum;

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem15.lua
--Matthew Ellison
-- Created: 02-07-19
--Modified: 03-28-19
--Modified: 06-19-20
--How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down?
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -25,13 +25,13 @@
require "Stopwatch"
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
GRID_WIDTH = 20;
GRID_HEIGHT = 20;
lowestXReached = 20;
function movement(currentX, currentY)
local lowestXReached = 20;
local function movement(currentX, currentY)
--Return 1 if you are at the finish location
if((currentX == GRID_WIDTH) and (currentY == GRID_HEIGHT)) then
return 1;
@@ -59,7 +59,7 @@ function movement(currentX, currentY)
end
--Start the recursion at the correct location and catch what is returned
numberMoves = movement(0, 0);
local numberMoves = movement(0, 0);
timer:stop();

View File

@@ -1,12 +1,12 @@
--ProjectEuler/lua/Problem16.lua
--Matthew Ellison
-- Created: 02-07-19
--Modified: 03-28-19
--Modified: 06-19-20
--What is the sum of the digits of the number 2^1000?
--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
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

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem17.lua
--Matthew Ellison
-- Created: 02-07-19
--Modified: 03-28-19
--Modified: 06-19-20
--If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -25,12 +25,12 @@
require "Stopwatch"
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
--This function takes a number and returns it as a string in english
--This function only works for numbers -1,000,000 < num < 1,000,000
function getStringFromNum(number)
local function getStringFromNum(number)
local numberString = "";
--Starting with the largest digit create a string based on the number passed in
--Check for negative
@@ -144,7 +144,7 @@ function getStringFromNum(number)
end
--This function returns the number of letters in a string, ignoring punctuation, whitespace, and numbers
function getNumberChars(number)
local function getNumberChars(number)
local sumOfLetters = 0;
--Start at location 1 and count the number of letters, ignoring punctuation and whitespace
for location=1,string.len(number) do

View File

@@ -1,7 +1,7 @@
--ProjectEuler/lua/Problem18.lua
--Matthew Ellison
-- Created: 03-12-19
--Modified: 03-28-19
--Modified: 06-19-20
--Find the maximum total from top to bottom
--[[
75
@@ -23,7 +23,7 @@
--This is done using a breadth first search
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -51,7 +51,7 @@ location = {
};
]]
function invert(list)
local function invert(list)
for rowCnt=1,NUM_ROWS do
for colCnt=1,#list[rowCnt] do
list[rowCnt][colCnt] = 100 - list[rowCnt][colCnt];
@@ -59,7 +59,7 @@ function invert(list)
end
end
function foundInList(list, loc)
local function foundInList(list, loc)
for location=1,#list do
if((list[location].xLocation == loc.xLocation) and (list[location].yLocation == loc.yLocation)) then
return true;
@@ -68,8 +68,8 @@ function foundInList(list, loc)
return false;
end
function remove_if(list, loc)
location = 1;
local function remove_if(list, loc)
local location = 1;
while(location <= #list) do
if((list[location].xLocation == loc.xLocation) and (list[location].yLocation == loc.yLocation)) then
table.remove(list, location);
@@ -80,13 +80,13 @@ function remove_if(list, loc)
end
--Create a timer and time the algorithm
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
NUM_ROWS = 15;
local NUM_ROWS = 15;
list = {};
local list = {};
list[1] = {75};
list[2] = {95, 64};
list[3] = {17, 47, 82};
@@ -106,18 +106,18 @@ list[15] = {04, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 04, 23};
--Invert the list so all elements are 100 - element
invert(list);
foundPoints = {}; --This is a table of locations
local foundPoints = {}; --This is a table of locations
foundPoints[1] = {xLocation = 1, yLocation = 1, total = list[1][1], fromRight = false};
possiblePoints = {}; --This is a table of locations
local possiblePoints = {}; --This is a table of locations
--Add the second row as possible points
possiblePoints[1] = {xLocation = 1, yLocation = 2, total = (list[1][1] + list[2][1]), fromRight = true};
possiblePoints[2] = {xLocation = 2, yLocation = 2, total = (list[1][1] + list[2][2]), fromRight = false};
foundBottom = false; --Used when you find a point at the bottom
local foundBottom = false; --Used when you find a point at the bottom
--Loop until you find the bottom
while(not foundBottom) do
--Check which possible point gives us the lowest number. If more than one has the same number simply keep the first one
minLoc = possiblePoints[1];
local minLoc = possiblePoints[1];
for loc=1,#possiblePoints do
if(possiblePoints[loc].total < minLoc.total) then
minLoc = possiblePoints[loc];
@@ -137,8 +137,8 @@ while(not foundBottom) do
--Add to the list of possible points from the point we just found and
--If you are at the bottom raise the flag to end the program
xLoc = minLoc.xLocation;
yLoc = minLoc.yLocation + 1; --Add one because you will always be moving to the next row
local xLoc = minLoc.xLocation;
local yLoc = minLoc.yLocation + 1; --Add one because you will always be moving to the next row
if(yLoc > NUM_ROWS) then
foundBottom = true;
else
@@ -150,7 +150,7 @@ while(not foundBottom) do
end
end
actualTotal = ((100 * (NUM_ROWS)) - foundPoints[#foundPoints].total);
local actualTotal = ((100 * (NUM_ROWS)) - foundPoints[#foundPoints].total);
--Stop the timer
timer:stop();

View File

@@ -1,7 +1,7 @@
--ProjectEuler/lua/Problem19.lua
--Matthew Ellison
-- Created: 03-13-19
--Modified: 03-28-19
--Modified: 06-19-20
--How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
--[[
You are given the following information, but you may prefer to do some research for yourself.
@@ -16,7 +16,7 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles
]]
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -48,20 +48,41 @@ DAYS = {
ERROR = 8;
}
START_YEAR = 1901;
END_YEAR = 2000;
local START_YEAR = 1901;
local END_YEAR = 2000;
local function isLeapYear(year)
local answer;
if(year < 1) then
answer = false;
elseif((year % 100) == 0) then
--This rule only applies at and after 1800
if(year <= 1700) then
answer = true;
elseif((year % 400) == 0) then
answer = true;
else
answer = false;
end
elseif((year % 4) == 0) then
answer = true;
else
answer = false;
end
return answer;
end
--Return the day of the week that the date you pass into it is on
function getDay(month, day, year)
local function getDay(month, day, year)
--Make sure the numbers are within propper bounds
if((month < 1) or (month > 12) or (day < 1) or (day > 31) or (year < 1)) then
return DAYS.ERROR;
end
numDays = 0;
currentYear = 1;
currentMonth = 1;
currentDay = DAYS.SATURDAY;
local numDays = 0;
local currentYear = 1;
local currentMonth = 1;
local currentDay = DAYS.SATURDAY;
day = day - 1;
--Add the correct number of days for every year
@@ -132,29 +153,9 @@ function getDay(month, day, year)
return day;
end
function isLeapYear(year)
if(year < 1) then
answer = false;
elseif((year % 100) == 0) then
--This rule only applies at and after 1800
if(year <= 1700) then
answer = true;
elseif((year % 400) == 0) then
answer = true;
else
answer = false;
end
elseif((year % 4) == 0) then
answer = true;
else
answer = false;
end
return answer;
end
--Setup the variables
timer = Stopwatch:create();
totalSundays = 0;
local timer = Stopwatch:create();
local totalSundays = 0;
--Start the timer
timer:start();
@@ -163,7 +164,7 @@ timer:start();
for year=START_YEAR,END_YEAR do
--Run for all months in the year
for month=1,12 do
day = getDay(month, 1, year);
local day = getDay(month, 1, year);
if(day == DAYS.ERROR) then
io.output("There was an error with the day\n");
os.exit();

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem1.lua
--Matthew Ellison
-- Created: 02-01-19
--Modified: 03-28-19
--Modified: 06-19-20
--The sum of the even Fibonacci numbers less than 4,000,000
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -25,16 +25,16 @@
require "Stopwatch"
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
--Setup the variables
TOP_NUM = 4000000; --The highest looked for Fibonacci numbers
fibSum = 0; --Holds the sum of the Fibonacci numbers
fibNums = {1, 1, 2}; --An array to keep track of the Fibonacci numbers
local fibSum = 0; --Holds the sum of the Fibonacci numbers
local fibNums = {1, 1, 2}; --An array to keep track of the Fibonacci numbers
--Loop to generate the Fibonacci numbers
cnt = 2; --A counter to hold the location in the array of the currently working Fibonacci number
local cnt = 2; --A counter to hold the location in the array of the currently working Fibonacci number
fibNums[(cnt % 3) + 1] = fibNums[((cnt - 1) % 3) + 1] + fibNums[((cnt - 2) % 3) + 1];
while(fibNums[(cnt % 3) + 1] < TOP_NUM) do
--If the number is even add it to the sum, otherwise ignore it

View File

@@ -1,12 +1,12 @@
--ProjectEuler/lua/Problem20.lua
--Matthew Ellison
-- Created: 03-14-19
--Modified: 03-28-19
--Modified: 06-19-20
--What is the sum of the digits of 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
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
@@ -30,7 +30,7 @@ TOP_NUM = 100;
--Start the timer
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
local num = bigint.new(1); --The number to be generated
@@ -41,7 +41,7 @@ for cnt = 1, TOP_NUM do
end
--Get a string of the number because it is easier to pull appart the individual characters to get the sum
numString = bigint.unserialize(num, 's');
local numString = bigint.unserialize(num, 's');
--Run through every character in the string, convert it back to an integer and add it to the running sum
for cnt = 1, string.len(numString) do
sum = sum + tonumber(string.sub(numString, cnt, cnt));

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem21.lua
--Matthew Ellison
-- Created: 03-19-19
--Modified: 03-28-19
--Modified: 06-19-20
--Evaluate the sum of all the amicable numbers under 10000
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -29,26 +29,26 @@ LIMIT = 10000; --The top number that will be evaluated
--Setup the timer
timer = Stopwatch:create();
local timer = Stopwatch:create();
--Setup the variables
divisorSum = {}; --Holds the sum of the factors of the subscript number
local divisorSum = {}; --Holds the sum of the factors of the subscript number
--Start the timer
timer:start();
--Generate the factors of all the numbers < 10000, get their sum, and add it to the list
for cnt = 1, LIMIT do
divisors = getDivisors(cnt); --Get all the divisors of a number
local divisors = getDivisors(cnt); --Get all the divisors of a number
if(#divisors > 1) then
table.remove(divisors); --Remove the last entry because it will be the number itself
end
divisorSum[#divisorSum + 1] = getSum(divisors); --Add the sum of the divisors to the vector
end
--Check every sum of divisors in the list for a matching sum
amicable = {};
local amicable = {};
for cnt = 1, #divisorSum do
sum = divisorSum[cnt];
local 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) then
--We know that divisorSum[cnt] == sum, do if divisorSum[sum] == cnt we have found an amicable number

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem22.lua
--Matthew Ellison
-- Created: 03-20-19
--Modified: 03-28-19
--Modified: 06-19-20
--What is the total of all the name scores in the file?
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -397,9 +397,9 @@ NAMES = {"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUS
--Setup the variables
timer = Stopwatch:create();
sums = {} --Holds the score based on the sum of the characters in the name
prod = {} --Holds the score based on the sum of the characters and the location in alphabetical order
local timer = Stopwatch:create();
local sums = {} --Holds the score based on the sum of the characters in the name
local prod = {} --Holds the score based on the sum of the characters and the location in alphabetical order
--Start the timer
timer:start();

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem23.lua
--Matthew Ellison
-- Created: 03-22-19
--Modified: 03-28-19
--Modified: 06-19-20
--Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -29,7 +29,7 @@ require "Algorithms"
MAX_NUM = 28123; --The highest number that will be evaluated
function isSum(abund, num)
local function isSum(abund, num)
local sumOfNums = 0;
--Pick a number for the first part of the sum
for firstNum = 1, #abund do

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem24.lua
--Matthew Ellison
-- Created: 03-24-19
--Modified: 03-28-19
--Modified: 06-19-20
--What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -29,14 +29,14 @@ NEEDED_PERM = 1000000; --The number of the permutation that you need
--Setup the variables
timer = Stopwatch:create();
nums = "0123456789";
local timer = Stopwatch:create();
local nums = "0123456789";
--Start the timer
timer:start();
--Get all permutations of the string
permutations = getPermutations(nums);
local permutations = getPermutations(nums);
--Stop the timer
timer:stop();

View File

@@ -1,12 +1,12 @@
--ProjectEuler/lua/Problem25.lua
--Matthew Ellison
-- Created: 03-26-19
--Modified: 03-28-19
--Modified: 06-19-20
--What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
--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
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
@@ -28,7 +28,7 @@ require "Algorithms";
local bigint = require "bigint";
NUM_DIGITS = 1000; --The number of digits to calculate up to
local NUM_DIGITS = 1000; --The number of digits to calculate up to
--Setup the variables

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem26.lua
--Matthew Ellison
-- Created: 08-02-19
--Modified: 08-02-19
--Modified: 06-19-20
--Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem27.lua
--Matthew Ellison
-- Created: 09-15-19
--Modified: 09-15-19
--Modified: 06-19-20
--Find the product of the coefficients, |a| < 1000 and |b| <= 1000, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0.
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem28.lua
--Matthew Ellison
-- Created: 09-15-19
--Modified: 10-06-19
--Modified: 06-19-20
--Find the product of the coefficients, |a| < 1000 and |b| <= 1000, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0.
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem29.lua
-- Created: 10-10-19
--Modified: 10-10-19
--Modified: 06-19-20
--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
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

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem3.lua
--Matthew Ellison
-- Created: 02-04-19
--Modified: 03-28-19
--Modified: 06-19-20
--The largest prime factor of 600851475143
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -26,13 +26,13 @@ require "Stopwatch"
require "Algorithms"
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
TARGET_NUMBER = 600851475143;
--Get the factors of the number
factors = getFactors(TARGET_NUMBER);
local factors = getFactors(TARGET_NUMBER);
timer:stop();
--The largest number will be the answer

View File

@@ -1,10 +1,10 @@
--ProjectEuler/lua/Problem29.lua
--ProjectEuler/lua/Problem30.lua
-- Created: 10-28-19
--Modified: 10-28-19
--Modified: 06-19-20
--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
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
@@ -25,7 +25,7 @@ require "Stopwatch"
--Returns a table with the individual digits of the number passed to it
function getDigits(num)
local 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);
@@ -38,7 +38,7 @@ function getDigits(num)
end
--Gets the sum of a table of numbers
function getSumOfTable(ary)
local 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

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem4.lua
--Matthew Ellison
-- Created: 02-05-19
--Modified: 03-28-19
--Modified: 06-19-20
--Find the largest palindrome made from the product of two 3-digit numbers
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -25,14 +25,14 @@
require "Stopwatch"
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
palindromes = {}; --Holds all palindromes the program finds
local palindromes = {}; --Holds all palindromes the program finds
--Loop through every number 100-999 twice, nested, so you can multiply every number by every other number in the array
for num1=100,999 do
for num2=num1,999 do
currentNum = num1 * num2;
local currentNum = num1 * num2;
--If the number is a palindrome add it to the list of palindromes, otherwise ignore it
--Using strings makes it easier to determine a palindrome
if(tostring(currentNum) == string.reverse(tostring(currentNum))) then

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem5.lua
--Matthew Ellison
-- Created: 02-05-19
--Modified: 03-28-19
--Modified: 06-19-20
--What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -25,12 +25,12 @@
require "Stopwatch"
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
--Setup the variables you need
numFound = false;
currentNum = 22; --Start looking at 22 becuase it must be divisible by 2 and greater than 20
local numFound = false;
local currentNum = 22; --Start looking at 22 becuase it must be divisible by 2 and greater than 20
--Start a loop looking for the correct number
while((not numFound) and (currentNum > 0)) do
--Set that you found the number to true because you set this flag when you don't find it

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem6.lua
--Matthew Ellison
-- Created: 02-06-19
--Modified: 03-28-19
--Modified: 06-19-20
--Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -25,12 +25,12 @@
require "Stopwatch"
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
--Create the variables you need
sumOfSquares = 0;
squareOfSum = 0;
local sumOfSquares = 0;
local squareOfSum = 0;
--Loop through all numbers 1-100 to do the appropriate math with them
for num=1,100 do
sumOfSquares = sumOfSquares + num^2; --Get the sum of the squares of the first 100 natural number

View File

@@ -1,7 +1,7 @@
--ProjectEuler/lua/Problem67.lua
--Matthew Ellison
-- Created: 03-26-19
--Modified: 03-28-19
--Modified: 06-19-20
--Find the maximum total from top to bottom
--[[
59
@@ -108,7 +108,7 @@
--This is done using a breadth first search
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -136,7 +136,7 @@ location = {
};
]]
function invert(list)
local function invert(list)
for rowCnt=1,NUM_ROWS do
for colCnt=1,#list[rowCnt] do
list[rowCnt][colCnt] = 100 - list[rowCnt][colCnt];
@@ -144,17 +144,8 @@ function invert(list)
end
end
function foundInList(list, loc)
for location=1,#list do
if((list[location].xLocation == loc.xLocation) and (list[location].yLocation == loc.yLocation)) then
return true;
end
end
return false;
end
function remove_if(list, loc)
location = 1;
local function remove_if(list, loc)
local location = 1;
while(location <= #list) do
if((list[location].xLocation == loc.xLocation) and (list[location].yLocation == loc.yLocation)) then
table.remove(list, location);
@@ -165,13 +156,13 @@ function remove_if(list, loc)
end
--Create a timer and time the algorithm
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
NUM_ROWS = 100;
list = {};
local list = {};
list[1] = {59};
list[2] = {73, 41};
list[3] = {52, 40, 9};
@@ -276,18 +267,18 @@ list[100] ={23, 33, 44, 81, 80, 92, 93, 75, 94, 88, 23, 61, 39, 76, 22, 03, 28,
--Invert the list so all elements are 100 - element
invert(list);
foundPoints = {}; --This is a table of locations
local foundPoints = {}; --This is a table of locations
foundPoints[1] = {xLocation = 1, yLocation = 1, total = list[1][1], fromRight = false};
possiblePoints = {}; --This is a table of locations
local possiblePoints = {}; --This is a table of locations
--Add the second row as possible points
possiblePoints[1] = {xLocation = 1, yLocation = 2, total = (list[1][1] + list[2][1]), fromRight = true};
possiblePoints[2] = {xLocation = 2, yLocation = 2, total = (list[1][1] + list[2][2]), fromRight = false};
foundBottom = false; --Used when you find a point at the bottom
local foundBottom = false; --Used when you find a point at the bottom
--Loop until you find the bottom
while(not foundBottom) do
--Check which possible point gives us the lowest number. If more than one has the same number simply keep the first one
minLoc = possiblePoints[1];
local minLoc = possiblePoints[1];
for loc=1,#possiblePoints do
if(possiblePoints[loc].total < minLoc.total) then
minLoc = possiblePoints[loc];
@@ -301,8 +292,8 @@ while(not foundBottom) do
--Add to the list of possible points from the point we just found and
--If you are at the bottom raise the flag to end the program
xLoc = minLoc.xLocation;
yLoc = minLoc.yLocation + 1; --Add one because you will always be moving to the next row
local xLoc = minLoc.xLocation;
local yLoc = minLoc.yLocation + 1; --Add one because you will always be moving to the next row
if(yLoc > NUM_ROWS) then
foundBottom = true;
else
@@ -312,7 +303,7 @@ while(not foundBottom) do
end
end
actualTotal = ((100 * (NUM_ROWS)) - foundPoints[#foundPoints].total);
local actualTotal = ((100 * (NUM_ROWS)) - foundPoints[#foundPoints].total);
--Stop the timer
timer:stop();

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem7.lua
--Matthew Ellison
-- Created: 02-05-19
--Modified: 03-28-19
--Modified: 06-19-20
--What is the 10001th prime number?
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -26,12 +26,12 @@ require "Stopwatch"
require "Algorithms"
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
NUM_PRIMES = 10001; --The number of the prime number desired
--Get the correct number of primes
primes = getNumPrimes(NUM_PRIMES);
local primes = getNumPrimes(NUM_PRIMES);
timer:stop();

View File

@@ -1,7 +1,7 @@
--ProjectEuler/lua/Problem8.lua
--Matthew Ellison
-- Created: 02-06-19
--Modified: 03-28-19
--Modified: 06-19-20
--Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
--[[
73167176531330624919225119674426574742355349194934
@@ -27,7 +27,7 @@
]]
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -47,7 +47,7 @@
require "Stopwatch"
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
--Put the number into a variable. A string will do for now

View File

@@ -1,11 +1,11 @@
--ProjectEuler/lua/Problem9.lua
--Matthew Ellison
-- Created: 02-06-19
--Modified: 03-28-19
--Modified: 06-19-20
--There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
--All of my requires, unless otherwise listed, can be found at https://bitbucket.org/Mattrixwv/luaClasses
--[[
Copyright (C) 2019 Matthew Ellison
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
@@ -24,7 +24,7 @@
require "Stopwatch"
timer = Stopwatch:create();
local timer = Stopwatch:create();
timer:start();
local foundTriplet = false; --A simple flag to determine if we have found what we are looking for