Advertisement
dimipan80

Pythagorean Numbers

May 8th, 2015
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. /* George likes math. Recently he discovered an interesting property of the Pythagorean Theorem: there are infinite number of triplets of integers a, b and c (a ≤ b), such that a2 + b2 = c2. Write a program to help George find all such triplets (called Pythagorean numbers) among a set of integer numbers.
  2.  * Print at the console all Pythagorean equations a2 + b2 = c2 (a ≤ b), which can be formed by the input numbers. Each equation should be printed in the following format: "a*a + b*b = c*c". The order of the equations is not important. In case of no Pythagorean numbers found, print "No". */
  3.  
  4. namespace _10.PythagoreanNumbers
  5. {
  6.     using System;
  7.  
  8.     class PythagoreanNumbers
  9.     {
  10.         private static bool _isFoundNum = false;
  11.  
  12.         static void Main(string[] args)
  13.         {
  14.             int count = int.Parse(Console.ReadLine());
  15.             int[] numbers = new int[count];
  16.             for (int i = 0; i < numbers.Length; i++)
  17.             {
  18.                 numbers[i] = int.Parse(Console.ReadLine());
  19.             }
  20.  
  21.             foreach (int a in numbers)
  22.             {
  23.                 foreach (int b in numbers)
  24.                 {
  25.                     if (b >= a)
  26.                     {
  27.                         foreach (int c in numbers)
  28.                         {
  29.                             if (c >= a && c >= b)
  30.                             {
  31.                                 FindPytagoreanNumber(a, b, c);
  32.                             }
  33.                         }
  34.                     }
  35.                 }
  36.             }
  37.  
  38.             if (!_isFoundNum)
  39.             {
  40.                 Console.WriteLine("No");
  41.             }
  42.         }
  43.  
  44.         private static void FindPytagoreanNumber(int numA, int numB, int numC)
  45.         {
  46.             if (((numA * numA) + (numB * numB)) == (numC * numC))
  47.             {
  48.                 _isFoundNum = true;
  49.                 Console.WriteLine("{0}*{0} + {1}*{1} = {2}*{2}", numA, numB, numC);
  50.             }
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement