Skip to content

Commit

Permalink
Add id0091 faster
Browse files Browse the repository at this point in the history
  • Loading branch information
ishanpranav committed Feb 14, 2024
1 parent 82a2840 commit 42ff06a
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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` |
Expand Down
33 changes: 33 additions & 0 deletions src/id0091.c
Original file line number Diff line number Diff line change
@@ -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);
}
65 changes: 65 additions & 0 deletions src/id0092.c
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 42ff06a

Please sign in to comment.