Hackerrank – Popis problému
Celý popis zadania sa nacháza – Hackerrank.
Riešenie
Riešenie som našiel na StackOverflow. Ako zdroj je možné použiť Wikipédiu
Vytvoril som riešenie v týchto programovacích jazykoch:
Všetky riešenia sú dostupné aj na mojom GitHub profile.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import java.util.Scanner; public class PythagoreanTriplet { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int tests = scanner.nextInt(); for (int i = 0; i < tests; i++) { int n = scanner.nextInt(); System.out.println(findTripletDirty(n)); } scanner.close(); } static long findTriplet(int number) { long maxTriplet = -1; for (int a = 1; a <= number / 3; a++) { int aSquare = a * a; for (int b = a + 1; b <= number / 2; b++) { int c = number - a - b; if(c > 0 && c * c == aSquare + b * b) { long product = (long) a * b * c; if(product > maxTriplet) { maxTriplet = product; } } } } return maxTriplet; } // http://stackoverflow.com/questions/2817848/find-pythagorean-triplet-for-which-a-b-c-1000 static long findTripletDirty(int number) { long max = -1; // sum of integer Pythagorean triplet is always even if(number % 2 == 0) { for (int b = 1; b < number / 2; b++) { if(number * (number / 2 - b) % (number - b) == 0) { int a = number * (number / 2 - b) / (number - b); int c = number - a - b; long product = (long) c * a * b; if(product > max) { max = product; } } } } return max; } } |