C# 소스:

// Filename: GeneratePythagoreanTriplesn.cs
//
// Compile: csc GeneratePythagoreanTriples.cs
//
// Execute: GeneratePythagoreanTriples 40

using System;
using System.Collections.Generic;

namespace GeneralCommandLineApp
{
     class PythagorianTriple {
        Int64 a, b, c;
        public PythagorianTriple(Int64 x) {
             Int64 t;
        	if (x >1 && x % 2 == 1) {
           	    this.a = x;
            	    this.b = (x*x - 1)/2;;
           	    this.c = this.b + 1;;
        	}
        	else if (x >= 2) {
        	    t = x + 2;
            	    this.a = t;
            	    this.b = t*t/4 - 1;
           	    this.c = this.b + 2;;
           	}
       }
        public Int64 GetA() {
        	return this.a;
        }
        public Int64 GetB() {
        	return this.b;
        }
        public Int64 GetC() {
        	return this.c;
        }

        public override string ToString() {
        	return "(" + this.a + ", " + this.b + ", " + this.c + ")";
        }
    }
    
   class Program
    {
         public static void GenerateTriples(List<PythagorianTriple> data, Int64 k)
        {
    	  for (Int64 i = 1; i <= k; i++)
    	  {
    	      data.Add(new PythagorianTriple(2*i + 1));
    	      data.Add(new PythagorianTriple(2*i));
    	  }
        }

        public static void PrintTriplesAsTableForm(List<PythagorianTriple> data)
        {
    	  Int64 c = data.Count;
    	   int j;
              Console.WriteLine("Show Pythagorian triples (a, b, c) each of which satisfies a^2 + b^2 = c^2.");
              Console.WriteLine();
              Console.WriteLine("      +--------+--------+--------+         +--------+--------+--------+");
              Console.WriteLine("      |   Odd  |        |        |         |  Even  |        |        |");
              Console.WriteLine("      |     a  |     b  |     c  |         |     a  |     b  |     c  |");
              Console.WriteLine("      +--------+--------+--------+         +--------+--------+--------+");
               for (int i = 0; i < c; i += 2) {
                   Console.Write("      | {0, 6} | {1, 6} | {2, 6} |", data[i].GetA(), data[i].GetB(), data[i].GetC());
                   j = i + 1;
                   Console.WriteLine("         | {0, 6} | {1, 6} | {2, 6} |", data[j].GetA(), data[j].GetB(), data[j].GetC());
               }
              Console.WriteLine("      +--------+--------+--------+         +--------+--------+--------+");
        }


        static void Main(string[] args)
        {
        	Int64 c;
        	if (args.Length == 1) {
                c = Convert.ToInt64(args[0]);
                if (c < 1) {
                     Console.WriteLine("Sorry, you should a positive integer.");
                     return;
                }
                else {
                    List<PythagorianTriple> data = new List<PythagorianTriple>();
                    GenerateTriples(data, c);
                     PrintTriplesAsTableForm(data);
                     return;
                }
            }
            else
                Console.WriteLine("Usage:  GeneratePyThagoriaTriples  [number]");
        }
    }
}

 

 

 

 

 

실행: 프롬프트> GeneratePythagoreanTriples 40

실행 결과:

더보기

Show Pythagorian triples (a, b, c) each of which satisfies a^2 + b^2 = c^2.

+--------+--------+--------+ +--------+--------+--------+
| Odd | | | | Even | | |
| a | b | c | | a | b | c |
+--------+--------+--------+ +--------+--------+--------+
| 3 | 4 | 5 | | 4 | 3 | 5 |
| 5 | 12 | 13 | | 6 | 8 | 10 |
| 7 | 24 | 25 | | 8 | 15 | 17 |
| 9 | 40 | 41 | | 10 | 24 | 26 |
| 11 | 60 | 61 | | 12 | 35 | 37 |
| 13 | 84 | 85 | | 14 | 48 | 50 |
| 15 | 112 | 113 | | 16 | 63 | 65 |
| 17 | 144 | 145 | | 18 | 80 | 82 |
| 19 | 180 | 181 | | 20 | 99 | 101 |
| 21 | 220 | 221 | | 22 | 120 | 122 |
| 23 | 264 | 265 | | 24 | 143 | 145 |
| 25 | 312 | 313 | | 26 | 168 | 170 |
| 27 | 364 | 365 | | 28 | 195 | 197 |
| 29 | 420 | 421 | | 30 | 224 | 226 |
| 31 | 480 | 481 | | 32 | 255 | 257 |
| 33 | 544 | 545 | | 34 | 288 | 290 |
| 35 | 612 | 613 | | 36 | 323 | 325 |
| 37 | 684 | 685 | | 38 | 360 | 362 |
| 39 | 760 | 761 | | 40 | 399 | 401 |
| 41 | 840 | 841 | | 42 | 440 | 442 |
| 43 | 924 | 925 | | 44 | 483 | 485 |
| 45 | 1012 | 1013 | | 46 | 528 | 530 |
| 47 | 1104 | 1105 | | 48 | 575 | 577 |
| 49 | 1200 | 1201 | | 50 | 624 | 626 |
| 51 | 1300 | 1301 | | 52 | 675 | 677 |
| 53 | 1404 | 1405 | | 54 | 728 | 730 |
| 55 | 1512 | 1513 | | 56 | 783 | 785 |
| 57 | 1624 | 1625 | | 58 | 840 | 842 |
| 59 | 1740 | 1741 | | 60 | 899 | 901 |
| 61 | 1860 | 1861 | | 62 | 960 | 962 |
| 63 | 1984 | 1985 | | 64 | 1023 | 1025 |
| 65 | 2112 | 2113 | | 66 | 1088 | 1090 |
| 67 | 2244 | 2245 | | 68 | 1155 | 1157 |
| 69 | 2380 | 2381 | | 70 | 1224 | 1226 |
| 71 | 2520 | 2521 | | 72 | 1295 | 1297 |
| 73 | 2664 | 2665 | | 74 | 1368 | 1370 |
| 75 | 2812 | 2813 | | 76 | 1443 | 1445 |
| 77 | 2964 | 2965 | | 78 | 1520 | 1522 |
| 79 | 3120 | 3121 | | 80 | 1599 | 1601 |
| 81 | 3280 | 3281 | | 82 | 1680 | 1682 |
+--------+--------+--------+ +--------+--------+--------+

 

 

Posted by Scripter
,