1
\$\begingroup\$

Challenge

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.

Rules

  • 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 a first, 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 0.5 and 12.5 can.

  • Decimals with at least 4 digits after the decimal point truncated to the third digit, e.g. 1.2345 would truncated to 1.234 and 1.9999 would be truncated to 1.999.

  • 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

Input: 345
Output: 1
This can be split into 3, 4, and 5, which, of course, form a right triangle.

Input: 534
Output: 0
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.

Input: 3415
Output: 0
This does contain a 3, 4, and a 5, which can form a right triangle, but they are not consecutive; there is a 1 splitting the 5 from the 3 and 4.

Input: 5567507
Output: 1
Because decimals can be added anywhere, it can be changed to 55.67.507, which allows splitting it into 5, 5.6, and 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 7.507.

Input: 345567507
Output: 2
The first right triangle is formed by 3, 4, and 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.

Input: 51125
Output: 0
Because of rule 5, you cannot use .5, 1, and 1.25. An integer is required before .5 for it to work.

Input: 051125
Output: 0
Unlike the previous example, there is a number before the first 5, so it is now legal to use 0.5, 1, and 1.25.

Input: 121418439
Output: 2
The numbers 12 and 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, 121418439. Additionally, 1.2, 1.4, and 1.843 counts as a separate combination, thus giving us our second right triangle.

Input: 10011005
Output: 8
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!

\$\endgroup\$

closed as unclear what you're asking by Jonathan Allan, Sriotchilism O'Zaic, Kirill L., mbomb007, Stephen Apr 14 at 19:15

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ @Tau from what I understood, shortest program in bytes is always the winner for code golf. But I'll edit to mention that explicitly. \$\endgroup\$ – Pika the Wizard of the Whales Apr 14 at 1:44
  • 1
    \$\begingroup\$ @JonathanAllan the rounding you’ve used here is inconsistent with the example used in the question. (1.2345 to 1.234). Pikachu, what rounding rule should be used? Truncation? Rounding 0.xxx5 to even? Rounding 0.xxx5 up? \$\endgroup\$ – Nick Kennedy Apr 14 at 7:20
  • 2
    \$\begingroup\$ And do we round a and b and then round the square root of the sum of squares again? \$\endgroup\$ – Nick Kennedy Apr 14 at 8:33
  • 1
    \$\begingroup\$ With my (3) I was asking the same as Nick has, "And do we round a and b and then round the square root of the sum of squares again?" \$\endgroup\$ – Jonathan Allan Apr 14 at 14:05
  • 1
    \$\begingroup\$ If you keep the option for overlapping prefixes/suffixes, there are 8 possibilities for 10011005: 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; 1, 00.1, 1.005. If you use the Cartesian product of prefixes and suffixes there are 12 possible combinations. \$\endgroup\$ – Nick Kennedy Apr 14 at 15:16

Browse other questions tagged or ask your own question.