mirror of
https://bitbucket.org/Mattrixwv/projecteulerlua.git
synced 2025-12-06 17:43:57 -05:00
Updated problems to be more in line with conventions
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user