Created
September 26, 2023 16:39
-
-
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?"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you can't run the code, here is the precomputed output: