Soluție HackerRank pentru Pythagorean Triple, subdomeniul Algebra, în Python 3. Include cerința formatată, exemple, explicația pașilor și cod sursă.

  • Problemă: Pythagorean Triple
  • Domeniu: Algebra
  • Limbaj: Python 3

Challenge: Pythagorean Triple

Subdomeniu: Algebra (algebra)

Scor cont: 20.0 / 20

Submission status: Accepted

Submission score: 1.0

Submission ID: 464721950

Limbaj: python3

Link challenge: https://www.hackerrank.com/challenges/pythagorean-triple/problem

Cerință

A *Pythagorean triple* consists of three positive integers a, b, and c, such that a^2 + b^2 = c^2. Such a triple is commonly written as (a, b, c). This term comes from the [Pythagorean theorem](http://en.wikipedia.org/wiki/Pythagorean_theorem), which says that a Pythagorean Triple will be the lengths of the sides of a [right-angled triangle](http://en.wikipedia.org/wiki/Right_triangle).

You have been given an integer a which represents the length of one of [cathetus](https://en.wikipedia.org/wiki/Cathetus) of a right-angle triangle.

![image](https://s3.amazonaws.com/hr-assets/0/1496133929-d6c8a1a890-1428670428-5294116a8c-HRRT.JPG)

You need to find the lengths of the remaining sides. There may be multiple possible answers; any one will be accepted.

*Hints:*

- Every odd number 2k+1 can be represented as (k+1)^2 - k^2.
- If m and n are integers and m > n, then (m^2-n^2)^2 + (2mn)^2 = (m^2+n^2)^2.

Input Format

The first line contains an integer a denoting the length of one of cathetus of the right-angled triangle.

Output Format

A single line containing the possible values of a, b and c. You may print them in any order.

Constraints

+ 5 ≤ a < 10^9

Cod sursă

#!/bin/python3

import math
import os
import random
import re
import sys

def pythagoreanTriple(a):
    if(a%2==0):
        b=int(int(a**2)//4)-1
        c=int(b)+2
    else:
        c=((a**2)+1)//2
        b=c-1
    return a,int(b),int(c)
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    a = int(input().strip())

    triple = pythagoreanTriple(a)

    fptr.write(' '.join(map(str, triple)))
    fptr.write('\n')

    fptr.close()
HackerRank Algebra – Pythagorean Triple