원시 피타고라스 삼조(primitive pythagorea triplet)를 생성하는
명령줄 어플(Command Line Application) C# 소스
C# 소스:
// Filename: GeneratePrimitivePythagoreanTriplets.cs
//
// Compile: csc GeneratePrimitivePythagoreanTriplets.cs
//
// Execute: GeneratePrimitivePythagoreanTriplets 7
using System;
using System.Collections.Generic;
namespace GeneralCommandLineApp
{
class Program
{
public static Int64 GetGCD(Int64 xa, Int64 xb)
{
Int64 a = Math.Abs(xa);
Int64 b = Math.Abs(xb);
Int64 c;
if (b > a) {
c = b;
b = a;
a = c;
}
Int64 r;
r = b;
while (r > 0)
{
r = a % b;
a = b;
b = r;
}
return a;
}
public static void GeneratePrimitivePythagoreanTriplets(List<PythagoreanTriplet> data, Int64 k)
{
for (Int64 m = 2; m <= k; m++)
{
for (Int64 n = 1; n < m; n++)
{
if (GetGCD(m, n) == 1 && ((m % 2 == 0 && n % 2 != 0) || (m % 2 != 0 && n % 2 == 0))) {
data.Add(new PythagoreanTriplet(m, n));
}
}
}
}
public static void PrintTripletsAsTableForm(List<PythagoreanTriplet> data)
{
Int64 k = data.Count;
Int64 a, b, c, m, n;
Console.WriteLine("Print out some primitive Pythagorean triplets (a, b, c)\neach of which satisfies a^2 + b^2 = c^2.");
Console.WriteLine();
Console.WriteLine(" +--------+--------+-----------+-----------+-----------+");
Console.WriteLine(" | | | m^2 - n^2 | 2mn | m^2 + n^2 |");
Console.WriteLine(" | m | n | a | b | c |");
Console.WriteLine(" +--------+--------+-----------+-----------+-----------+");
for (int i = 0; i < k; i++) {
a = data[i].GetA();
b = data[i].GetB();
c = data[i].GetC();
m = (Int64) Math.Sqrt((a + c)/2);
n = b/(2*m);
Console.WriteLine(" | {0, 6} | {1, 6} | {2, 9} | {3, 9} | {4, 9} |", m, n, a, b, c);
}
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 as an upper boud.");
return;
}
else {
List<PythagoreanTriplet> data = new List<PythagoreanTriplet>();
GeneratePrimitivePythagoreanTriplets(data, c);
PrintTripletsAsTableForm(data);
return;
}
}
else
Console.WriteLine("Usage: GeneratePrimitivePythagoreanTriplets [UpperBound]");
}
}
class PythagoreanTriplet {
Int64 a, b, c;
public PythagoreanTriplet(Int64 m, Int64 n) {
this.a = m*m - n*n;
this.b = 2*m*n;;
this.c = m*m + n*n;
}
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 + ")";
}
public bool IsPrimitive(Int64 xa, Int64 xb, Int64 xc)
{
return GCD(xa, xb, xc) == 1;
public Int64 GCD(Int64 xa, Int64 xb)
{
Int64 a = Math.Abs(xa);
Int64 b = Math.Abs(xb);
Int64 c;
if (b > a) {
c = b;
b = a;
a = c;
}
Int64 r;
r = b;
while (r > 0)
{
r = a % b;
a = b;
b = r;
}
return a;
}
public Int64 GCD(Int64 xa, Int64 xb, Int64 xc)
{
Int64 t = GCD(xa, xb);
Int64 y = GCD(t, xc);
return y;
}
}
}
명령 GeneratePrimitivePythagoreanTriplets 7 에 의한 실행 결과:
Print out some primitive Pythagorean triplets (a, b, c)
each of which satisfies a^2 + b^2 = c^2.
+--------+--------+-----------+-----------+-----------+
| | | m^2 - n^2 | 2mn | m^2 + n^2 |
| m | n | a | b | c |
+--------+--------+-----------+-----------+-----------+
| 2 | 1 | 3 | 4 | 5 |
| 3 | 2 | 5 | 12 | 13 |
| 4 | 1 | 15 | 8 | 17 |
| 4 | 3 | 7 | 24 | 25 |
| 5 | 2 | 21 | 20 | 29 |
| 5 | 4 | 9 | 40 | 41 |
| 6 | 1 | 35 | 12 | 37 |
| 6 | 5 | 11 | 60 | 61 |
| 7 | 2 | 45 | 28 | 53 |
| 7 | 4 | 33 | 56 | 65 |
| 7 | 6 | 13 | 84 | 85 |
+--------+--------+-----------+-----------+-----------+
'프로그래밍 > C#' 카테고리의 다른 글
Visual Studio 2019 에서 유닛 테스트 작성하는 예 (0) | 2020.04.21 |
---|---|
C# 의 세제곱근 구하는 함수 Cbrt() (0) | 2020.03.25 |
피타고라스 수를 생성하는 C# 소스 (0) | 2020.02.29 |
이진 파일을 읽어서 16진수로 보여주는 HexView 소스 with C# (0) | 2013.08.05 |
C# 언어로 작성하여 실행해 본 OpenGL 예제: Redbook 의 Teapots (0) | 2013.05.17 |