From d74a5d66a05791aba88e161ccf35af26827d6630 Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Sat, 7 Sep 2024 00:25:12 -0500 Subject: [PATCH] add "docstrings" --- lua/src/p0001.lua | 60 +++++++++++------ lua/src/p0002.lua | 53 +++++++++------ lua/src/p0006.lua | 44 ++++++++---- lua/src/p0009.lua | 58 ++++++++++------ lua/src/p0017.lua | 167 +++++++++++++++++++++++++--------------------- lua/src/p0836.lua | 25 +++++-- 6 files changed, 251 insertions(+), 156 deletions(-) diff --git a/lua/src/p0001.lua b/lua/src/p0001.lua index 8d32f89e..991838dc 100644 --- a/lua/src/p0001.lua +++ b/lua/src/p0001.lua @@ -1,22 +1,38 @@ -return { - solution = function() - local answer = 0 - - for i = 3,999,3 - do - answer = answer + i - end - - for i = 5,999,5 - do - answer = answer + i - end - - for i = 15,999,15 - do - answer = answer - i - end - - return answer - end -} +-- Project Euler Question 1 +-- +-- I did it the old-fashioned way in this language +-- +-- Revision 1: +-- +-- I found a closed form solution that works in the general case, so it's about an +-- order of magnitude faster now. +-- +-- Problem: +-- +-- If we list all the natural numbers below 10 that are multiples of 3 or 5, we +-- get 3, 5, 6 and 9. The sum of these multiples is 23. +-- +-- Find the sum of all the multiples of 3 or 5 below 1000. + +return { + solution = function() + local answer = 0 + + for i = 3,999,3 + do + answer = answer + i + end + + for i = 5,999,5 + do + answer = answer + i + end + + for i = 15,999,15 + do + answer = answer - i + end + + return answer + end +} diff --git a/lua/src/p0002.lua b/lua/src/p0002.lua index 7c90b7fe..af559a97 100644 --- a/lua/src/p0002.lua +++ b/lua/src/p0002.lua @@ -1,19 +1,34 @@ -return { - solution = function() - local answer = 0 - local a = 1 - local b = 2 - - while b < 4000000 - do - answer = answer + b - - for j = 1,3,1 - do - a, b = b, a + b - end - end - - return answer - end -} +-- Project Euler Problem 2 +-- +-- I modeled this after the C++ solution, because it was by far the simplest +-- +-- Problem: +-- +-- Each new term in the Fibonacci sequence is generated by adding the previous two +-- terms. By starting with 1 and 2, the first 10 terms will be: +-- +-- 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... +-- +-- By considering the terms in the Fibonacci sequence whose values do not exceed +-- four million, find the sum of the even-valued terms. +-- + +return { + solution = function() + local answer = 0 + local a = 1 + local b = 2 + + while b < 4000000 + do + answer = answer + b + + for j = 1,3,1 + do + a, b = b, a + b + end + end + + return answer + end +} diff --git a/lua/src/p0006.lua b/lua/src/p0006.lua index 1e82c5f1..1ec22bcd 100644 --- a/lua/src/p0006.lua +++ b/lua/src/p0006.lua @@ -1,13 +1,31 @@ -return { - solution = function() - local sum = 1 - local sum_of_squares = 1 - for i = 2,100,1 - do - sum = sum + i - sum_of_squares = sum_of_squares + (i * i) - end - return (sum * sum) - sum_of_squares; - end -} - +-- Project Euler Problem 6 +-- +-- This turned out to be really easy +-- +-- Problem: +-- +-- The sum of the squares of the first ten natural numbers is, +-- 1**2 + 2**2 + ... + 10**2 = 385 +-- +-- The square of the sum of the first ten natural numbers is, +-- (1 + 2 + ... + 10)**2 = 55**2 = 3025 +-- +-- Hence the difference between the sum of the squares of the first ten natural +-- numbers and the square of the sum is 3025 − 385 = 2640. +-- +-- Find the difference between the sum of the squares of the first one hundred +-- natural numbers and the square of the sum. + +return { + solution = function() + local sum = 1 + local sum_of_squares = 1 + for i = 2,100,1 + do + sum = sum + i + sum_of_squares = sum_of_squares + (i * i) + end + return (sum * sum) - sum_of_squares; + end +} + diff --git a/lua/src/p0009.lua b/lua/src/p0009.lua index 0eb09cd4..b4cc8e5e 100644 --- a/lua/src/p0009.lua +++ b/lua/src/p0009.lua @@ -1,22 +1,36 @@ -return { - solution = function() - local c = 3 - while true - do - local c_square = c * c - for b = 2,c,1 - do - local b_square = b * b - for a = 1,b,1 - do - local a_square = a * a - if a_square + b_square == c_square and a + b + c == 1000 - then - return a * b * c - end - end - end - c = c + 1 - end - end -} +-- Project Euler Problem 9 +-- +-- This was pretty fun to port +-- +-- Problem: +-- +-- A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, +-- a**2 + b**2 = c**2 +-- +-- For example, 3**2 + 4**2 = 9 + 16 = 25 = 5**2. +-- +-- There exists exactly one Pythagorean triplet for which a + b + c = 1000. +-- Find the product abc. + +return { + solution = function() + local c = 3 + while true + do + local c_square = c * c + for b = 2,c,1 + do + local b_square = b * b + for a = 1,b,1 + do + local a_square = a * a + if a_square + b_square == c_square and a + b + c == 1000 + then + return a * b * c + end + end + end + c = c + 1 + end + end +} diff --git a/lua/src/p0017.lua b/lua/src/p0017.lua index ed90d8b6..9180ecb7 100644 --- a/lua/src/p0017.lua +++ b/lua/src/p0017.lua @@ -1,75 +1,92 @@ -local function to_string_len(n) - if n >= 1000 - then - local thousands = to_string_len(n // 1000 % 100) + 8 - if n % 1000 ~= 0 - then - thousands = thousands + to_string_len(n % 1000) - end - return thousands - end - if n >= 100 - then - local hundreds = to_string_len(n // 100 % 10) + 7 - if n % 100 ~= 0 - then - hundreds = hundreds + 3 + to_string_len(n % 100) - end - return hundreds - end - if n >= 20 - then - local tens_t = { - [2] = 6, - [3] = 6, - [4] = 5, - [5] = 5, - [6] = 5, - [7] = 7, - [8] = 6, - [9] = 6, - } - local tens = tens_t[n // 10] - if n % 10 ~= 0 - then - tens = tens + to_string_len(n % 10) - end - return tens - end - local final = { - [0] = 4, - [1] = 3, - [2] = 3, - [3] = 5, - [4] = 4, - [5] = 4, - [6] = 3, - [7] = 5, - [8] = 5, - [9] = 4, - [10] = 3, - [11] = 6, - [12] = 6, - [13] = 8, - [14] = 8, - [15] = 7, - [16] = 7, - [17] = 9, - [18] = 8, - [19] = 8, - } - return final[n] -end - -return { - solution = function() - local answer = 0 - - for x = 1,1000,1 - do - answer = answer + to_string_len(x) - end - - return answer - end -} +-- Project Euler Problem 17 +-- +-- The key here was remembering I don't need to return the whole string, just the length +-- +-- Problem: +-- +-- If the numbers 1 to 5 are written out in words: one, two, three, four, five, +-- then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. +-- +-- If all the numbers from 1 to 1000 (one thousand) inclusive were written out in +-- words, how many letters would be used? +-- +-- NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and +-- forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 +-- letters. The use of "and" when writing out numbers is in compliance with +-- British usage. + +local function to_string_len(n) + if n >= 1000 + then + local thousands = to_string_len(n // 1000 % 100) + 8 + if n % 1000 ~= 0 + then + thousands = thousands + to_string_len(n % 1000) + end + return thousands + end + if n >= 100 + then + local hundreds = to_string_len(n // 100 % 10) + 7 + if n % 100 ~= 0 + then + hundreds = hundreds + 3 + to_string_len(n % 100) + end + return hundreds + end + if n >= 20 + then + local tens_t = { + [2] = 6, + [3] = 6, + [4] = 5, + [5] = 5, + [6] = 5, + [7] = 7, + [8] = 6, + [9] = 6, + } + local tens = tens_t[n // 10] + if n % 10 ~= 0 + then + tens = tens + to_string_len(n % 10) + end + return tens + end + local final = { + [0] = 4, + [1] = 3, + [2] = 3, + [3] = 5, + [4] = 4, + [5] = 4, + [6] = 3, + [7] = 5, + [8] = 5, + [9] = 4, + [10] = 3, + [11] = 6, + [12] = 6, + [13] = 8, + [14] = 8, + [15] = 7, + [16] = 7, + [17] = 9, + [18] = 8, + [19] = 8, + } + return final[n] +end + +return { + solution = function() + local answer = 0 + + for x = 1,1000,1 + do + answer = answer + to_string_len(x) + end + + return answer + end +} diff --git a/lua/src/p0836.lua b/lua/src/p0836.lua index e63ee887..901d57d9 100644 --- a/lua/src/p0836.lua +++ b/lua/src/p0836.lua @@ -1,5 +1,20 @@ -return { - solution = function() - return "aprilfoolsjoke" - end -} +-- Project Euler Problem 836 +-- +-- Alright, this was funny +-- +-- Problem: +-- +-- Let A be an **affine plane** over a **radically integral local field** F with residual characteristic p. +-- +-- We consider an **open oriented line section** U of A with normalized Haar measure m. +-- +-- Define f(m,p) as the maximal possible discriminant of the **jacobian** associated to the +-- **orthogonal kernel embedding** of U into A. +-- +-- Find f(20230401, 57). Give as your answer the concatenation of the first letters of each bolded word. + +return { + solution = function() + return "aprilfoolsjoke" + end +}