Skip to content

Commit 42ff06a

Browse files
committed
Add id0091 faster
1 parent 82a2840 commit 42ff06a

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ all: \
3232
id0068$(E) id0069$(E) id0070$(E) id0071$(E) id0072$(E) id0073$(E) \
3333
id0074$(E) id0075$(E) id0076$(E) id0077$(E) id0078$(E) id0079$(E) \
3434
id0080$(E) id0081$(E) id0082$(E) id0083$(E) id0084$(E) id0085$(E) \
35-
id0086$(E) id0087$(E) id0089$(E)
35+
id0086$(E) id0087$(E) id0089$(E) id0091$(E) id0092$(E)
3636

3737
libeuler$(A): $(call RECURSE,lib,*.c)
3838
$(RM) *.o
@@ -306,5 +306,11 @@ id0087$(E): src/id0087.c libeuler$(A)
306306
id0089$(E): src/id0089.c libeuler$(A)
307307
$(CC) $(CFLAGS) $< -o $@ $(LEULER) $(LM)
308308

309+
id0091$(E): src/id0091.c libeuler$(A)
310+
$(CC) $(CFLAGS) $< -o $@ $(LEULER) $(LM)
311+
312+
id0092$(E): src/id0092.c libeuler$(A)
313+
$(CC) $(CFLAGS) $< -o $@ $(LEULER) $(LM)
314+
309315
clean:
310316
$(RM) *$(A) *.o

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ which is licensed under the GNU Lesser General Public License v3.0 (`LGPL-3.0`).
122122
| 86 | [Cuboid Route](src/id0086.c) | Geometry | Minimum | |
123123
| 87 | [Prime Power Triples](src/id0087.c) | Number theory | Count | [Hash set](https://en.wikipedia.org/wiki/Hash_table) |
124124
| 89 | [Roman Numerals](src/id0089.c) | Numeral systems | Difference | [Roman numerals](https://en.wikipedia.org/wiki/Roman_numerals) |
125+
| 91 | [Right Triangles with Integer Coordinates](src/id0091.c) | Geometry | Count | |
126+
| 92 | [Square Digit Chains](src/id0092.c) | Number theory | Count | |
125127

126128
## LeetCode
127129

@@ -142,6 +144,7 @@ which is licensed under the GNU Lesser General Public License v3.0 (`LGPL-3.0`).
142144
| 62 | [Unique Paths](src/lc0062.c) | Combinatorics | Binomial coefficient | `binomial` |
143145
| 69 | [Sqrt(x)](src/lc0069.c) | Mathematics | Square root | `sqrt` |
144146
| 75 | [Sort Colors](src/lc0075.c) | Sorting | Sort | `qsort` |
147+
| 273 | [Integer to English Words](src/lc0273.c) | Mathematics | String | `words_to_string` |
145148
| 367 | [Valid Perfect Square](src/lc0367.c) | Mathematics | Boolean | `math_is_polygonal` |
146149
| 2119 | [A Number After a Double Reversal](src/lc2119.c) | Mathematics | Boolean | `math_reverse` |
147150
| 2400 | [Number of Ways to Reach a Position After Exactly k Steps](src/lc2400.c) | Combinatorics | Binomial coefficient | `mod_binomial_range` |

src/id0091.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Licensed under the MIT License.
2+
3+
// Right Triangles with Integer Coordinates
4+
5+
#include "../lib/euler.h"
6+
#include "../lib/euclidean.h"
7+
8+
int main(void)
9+
{
10+
int count = 0;
11+
clock_t start = clock();
12+
13+
for (int x = 1; x <= 50; x++)
14+
{
15+
for (int y = 1; y <= 50; y++)
16+
{
17+
int d = gcd(x, y);
18+
int min = x * d / y;
19+
int other = (50 - y) * d / x;
20+
21+
if (other < min)
22+
{
23+
min = other;
24+
}
25+
26+
count += 2 * min;
27+
}
28+
}
29+
30+
count += 50 * 50 * 3;
31+
32+
return euler_submit(91, count, start);
33+
}

src/id0092.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Licensed under the MIT License.
2+
3+
// Square Digit Chains
4+
5+
#include "../lib/euler.h"
6+
7+
int math_sum_of_square_digits(long n, int results[])
8+
{
9+
if (n < 568 && results[n])
10+
{
11+
return results[n];
12+
}
13+
14+
long result = 0;
15+
16+
for (long k = n; k; k /= 10)
17+
{
18+
int d = k % 10;
19+
20+
result += d * d;
21+
}
22+
23+
return result;
24+
}
25+
26+
int math_square_digit_chain(long m, int results[])
27+
{
28+
long n = m;
29+
30+
while (n != 1 && n != 89)
31+
{
32+
n = math_sum_of_square_digits(n, results);
33+
}
34+
35+
return n;
36+
}
37+
38+
int main(void)
39+
{
40+
long count = 0;
41+
int results[568] = { 0 };
42+
clock_t start = clock();
43+
44+
for (long m = 1; m < 568; m++)
45+
{
46+
results[m] = math_square_digit_chain(m, results);
47+
48+
if (results[m] == 89)
49+
{
50+
count++;
51+
}
52+
}
53+
54+
for (long m = 1; m < 10000000l; m++)
55+
{
56+
long n = math_square_digit_chain(m, results);
57+
58+
if (n == 89)
59+
{
60+
count++;
61+
}
62+
}
63+
64+
return euler_submit(91, count, start);
65+
}

0 commit comments

Comments
 (0)