From 42ff06aa1b9af8e7d706c23419feaf7095333bda Mon Sep 17 00:00:00 2001 From: Ishan Pranav Date: Wed, 14 Feb 2024 14:55:24 -0500 Subject: [PATCH] Add id0091 faster --- Makefile | 8 ++++++- README.md | 3 +++ src/id0091.c | 33 ++++++++++++++++++++++++++ src/id0092.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/id0091.c create mode 100644 src/id0092.c diff --git a/Makefile b/Makefile index cfbf825..2e606fe 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ all: \ id0068$(E) id0069$(E) id0070$(E) id0071$(E) id0072$(E) id0073$(E) \ id0074$(E) id0075$(E) id0076$(E) id0077$(E) id0078$(E) id0079$(E) \ id0080$(E) id0081$(E) id0082$(E) id0083$(E) id0084$(E) id0085$(E) \ - id0086$(E) id0087$(E) id0089$(E) + id0086$(E) id0087$(E) id0089$(E) id0091$(E) id0092$(E) libeuler$(A): $(call RECURSE,lib,*.c) $(RM) *.o @@ -306,5 +306,11 @@ id0087$(E): src/id0087.c libeuler$(A) id0089$(E): src/id0089.c libeuler$(A) $(CC) $(CFLAGS) $< -o $@ $(LEULER) $(LM) +id0091$(E): src/id0091.c libeuler$(A) + $(CC) $(CFLAGS) $< -o $@ $(LEULER) $(LM) + +id0092$(E): src/id0092.c libeuler$(A) + $(CC) $(CFLAGS) $< -o $@ $(LEULER) $(LM) + clean: $(RM) *$(A) *.o diff --git a/README.md b/README.md index 95909ee..7e9156b 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,8 @@ which is licensed under the GNU Lesser General Public License v3.0 (`LGPL-3.0`). | 86 | [Cuboid Route](src/id0086.c) | Geometry | Minimum | | | 87 | [Prime Power Triples](src/id0087.c) | Number theory | Count | [Hash set](https://en.wikipedia.org/wiki/Hash_table) | | 89 | [Roman Numerals](src/id0089.c) | Numeral systems | Difference | [Roman numerals](https://en.wikipedia.org/wiki/Roman_numerals) | +| 91 | [Right Triangles with Integer Coordinates](src/id0091.c) | Geometry | Count | | +| 92 | [Square Digit Chains](src/id0092.c) | Number theory | Count | | ## LeetCode @@ -142,6 +144,7 @@ which is licensed under the GNU Lesser General Public License v3.0 (`LGPL-3.0`). | 62 | [Unique Paths](src/lc0062.c) | Combinatorics | Binomial coefficient | `binomial` | | 69 | [Sqrt(x)](src/lc0069.c) | Mathematics | Square root | `sqrt` | | 75 | [Sort Colors](src/lc0075.c) | Sorting | Sort | `qsort` | +| 273 | [Integer to English Words](src/lc0273.c) | Mathematics | String | `words_to_string` | | 367 | [Valid Perfect Square](src/lc0367.c) | Mathematics | Boolean | `math_is_polygonal` | | 2119 | [A Number After a Double Reversal](src/lc2119.c) | Mathematics | Boolean | `math_reverse` | | 2400 | [Number of Ways to Reach a Position After Exactly k Steps](src/lc2400.c) | Combinatorics | Binomial coefficient | `mod_binomial_range` | diff --git a/src/id0091.c b/src/id0091.c new file mode 100644 index 0000000..381faf5 --- /dev/null +++ b/src/id0091.c @@ -0,0 +1,33 @@ +// Licensed under the MIT License. + +// Right Triangles with Integer Coordinates + +#include "../lib/euler.h" +#include "../lib/euclidean.h" + +int main(void) +{ + int count = 0; + clock_t start = clock(); + + for (int x = 1; x <= 50; x++) + { + for (int y = 1; y <= 50; y++) + { + int d = gcd(x, y); + int min = x * d / y; + int other = (50 - y) * d / x; + + if (other < min) + { + min = other; + } + + count += 2 * min; + } + } + + count += 50 * 50 * 3; + + return euler_submit(91, count, start); +} diff --git a/src/id0092.c b/src/id0092.c new file mode 100644 index 0000000..9b5a650 --- /dev/null +++ b/src/id0092.c @@ -0,0 +1,65 @@ +// Licensed under the MIT License. + +// Square Digit Chains + +#include "../lib/euler.h" + +int math_sum_of_square_digits(long n, int results[]) +{ + if (n < 568 && results[n]) + { + return results[n]; + } + + long result = 0; + + for (long k = n; k; k /= 10) + { + int d = k % 10; + + result += d * d; + } + + return result; +} + +int math_square_digit_chain(long m, int results[]) +{ + long n = m; + + while (n != 1 && n != 89) + { + n = math_sum_of_square_digits(n, results); + } + + return n; +} + +int main(void) +{ + long count = 0; + int results[568] = { 0 }; + clock_t start = clock(); + + for (long m = 1; m < 568; m++) + { + results[m] = math_square_digit_chain(m, results); + + if (results[m] == 89) + { + count++; + } + } + + for (long m = 1; m < 10000000l; m++) + { + long n = math_square_digit_chain(m, results); + + if (n == 89) + { + count++; + } + } + + return euler_submit(91, count, start); +}