How many right triangles can you find?
You will be given an input represented by
x, which is a string containing at least 3 characters. It will consist only of the standard numeric characters, 0 through 9. Your job is to find and output how many right triangles can be formed with the given numbers.
Numbers must be kept in the order they were given in. No mixing them up!
The numbers for each right triangle must be consecutive.
The order of numbers has to be
b second, and
c third, and must satisfy the formula
a² + b² = c².
a can be greater than or less than
b, as long as it satisfies the formula.
Decimal points may be added between any numbers.
Decimals require one or more numbers to be placed before them, e.g.
.5 cannot be used as a number but
Decimals with at least 4 digits after the decimal point truncated to the third digit, e.g.
1.2345 would truncated to
1.9999 would be truncated to
Numbers can be used more than once in 2 or more different triangles, but cannot be used multiple times in the same triangle.
Multiple representations of the same value can count multiple times.
Repeating zeros are allowed, e.g.
000.5 counts as a number.
All possible combinations must be taken into account for your program to be valid.
Example Inputs and Outputs
This can be split into
5, which, of course, form a right triangle.
While this does include the necessary numbers to form a right triangle, they are not in the correct order. It has to follow the formula
a² + b² = c², but in this case it follows
c² = a² + b². The order of numbers cannot be changed from the original input, so in this case no right triangles can be formed.
This does contain a
4, and a
5, which can form a right triangle, but they are not consecutive; there is a
1 splitting the
5 from the
Because decimals can be added anywhere, it can be changed to
55.67.507, which allows splitting it into
7.507 to form a right triangle. Remember that decimals are truncated to the third digit after the decimal point, which is how we get
The first right triangle is formed by
5. The second one is formed by
5567507 (read the previous example for explanation). Numbers can be used more than once, so the first
5 was used in the first and second triangles.
Because of rule 5, you cannot use
1.25. An integer is required before
.5 for it to work.
Unlike the previous example, there is a number before the first
5, so it is now legal to use
14 would form a right triangle where side
c has a length of approximately
18.43908891458577462000. Because long decimals are truncated to the third digit after the decimal point, we would be left with
18.439. This fits in with the original input,
1.843 counts as a separate combination, thus giving us our second right triangle.
Numbers count separately if they're represented in different ways, so this allows for (1, 00, 1), (1.0, 0, 1), (1, 0, 01), (1, 0.01, 1), (1, 0.01, 1.0), (1, 0.01, 1.00), (1.0, 0.1, 1.005), and (1, 00.1, 1.005).
This is code golf, so shortest answer in bytes wins. Good luck!