Skip to content

Instantly share code, notes, and snippets.

@KDJDEV
Created September 26, 2023 16:39
Show Gist options
  • Save KDJDEV/6710eb45f8c9792f67bb1141fd6419df to your computer and use it in GitHub Desktop.
Save KDJDEV/6710eb45f8c9792f67bb1141fd6419df to your computer and use it in GitHub Desktop.
Test that my solution works for the first 100 n for the question "If you had n line segments with lengths that correspond to the first n natural numbers, how many unique triangles could you make?"
import itertools
def true_result(n):
natural_numbers = []
for i in range(1, n + 1):
natural_numbers.append(i)
combinations = list(itertools.combinations(natural_numbers, 3))
count = 0
for combo in combinations:
if (combo[0]+combo[1] > combo[2]):
count = count + 1
return count
def double_summation(n):
result = 0
for n3 in range(4, n + 1):
for n2 in range(1, n3):
result += max(0, 2 * n2 - n3 - 1)
return result
for n in range(100):
t = true_result(n)
d = double_summation(n)
print(t, d, t == d)
@KDJDEV
Copy link
Author

KDJDEV commented Sep 26, 2023

If you can't run the code, here is the precomputed output:

0 0 True
0 0 True
0 0 True
0 0 True
1 1 True
3 3 True
7 7 True
13 13 True
22 22 True
34 34 True
50 50 True
70 70 True
95 95 True
125 125 True
161 161 True
203 203 True
252 252 True
308 308 True
372 372 True
444 444 True
525 525 True
615 615 True
715 715 True
825 825 True
946 946 True
1078 1078 True
1222 1222 True
1378 1378 True
1547 1547 True
1729 1729 True
1925 1925 True
2135 2135 True
2360 2360 True
2600 2600 True
2856 2856 True
3128 3128 True
3417 3417 True
3723 3723 True
4047 4047 True
4389 4389 True
4750 4750 True
5130 5130 True
5530 5530 True
5950 5950 True
6391 6391 True
6853 6853 True
7337 7337 True
7843 7843 True
8372 8372 True
8924 8924 True
9500 9500 True
10100 10100 True
10725 10725 True
11375 11375 True
12051 12051 True
12753 12753 True
13482 13482 True
14238 14238 True
15022 15022 True
15834 15834 True
16675 16675 True
17545 17545 True
18445 18445 True
19375 19375 True
20336 20336 True
21328 21328 True
22352 22352 True
23408 23408 True
24497 24497 True
25619 25619 True
26775 26775 True
27965 27965 True
29190 29190 True
30450 30450 True
31746 31746 True
33078 33078 True
34447 34447 True
35853 35853 True
37297 37297 True
38779 38779 True
40300 40300 True
41860 41860 True
43460 43460 True
45100 45100 True
46781 46781 True
48503 48503 True
50267 50267 True
52073 52073 True
53922 53922 True
55814 55814 True
57750 57750 True
59730 59730 True
61755 61755 True
63825 63825 True
65941 65941 True
68103 68103 True
70312 70312 True
72568 72568 True
74872 74872 True
77224 77224 True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment