다음은  이차방정식 x^2 - x - 1  = 0 의 양의 근 즉 황금비율(golden ratio)을 구하는 C# 컨솔 애플리케이션 소스이다. 황금비율을 구하는 비례방정식은   1 : x = x : (x+1) 이며, 이를 이차방정식으로 표현한 것이 x^2 - x - 1  = 0 이다.

See:  Golden ratio - Sajun.org

  1. /*
  2.  *  Filename: TestGoldenRatioApp.cs
  3.  *    황금률(즉, 이차방정식 x^2 - x - 1  = 0 의 양의 근)을 계산한다.
  4.  *
  5.  *   Compile: csc TestGoldenRatioApp.
  6.  *
  7.  *   Execute: TestGoldenRatioApp
  8.  *
  9.  *      Date:  2009/01/16
  10.  *    Author:  PH Kim   [ pkim (AT) scripts.pe.kr ]
  11.  */
  12. using System;
  13. using System.Collections.Generic;
  14. namespace MyTestApplication1 {
  15.     public class TestGoldenRatioApp {
  16.         public static void PrintUsing() {
  17.             Console.WriteLine("Using: TestGoldenRatioApp [-h|-help]");
  18.             Console.WriteLine("This calculates the value of the golden ratio.");
  19.         }
  20.         // 이차방정식 a x^2 + b x + c  = 0 의 근을 구한다.
  21.         public List<double> FindQuadraticRoot(double a, double b, double c) {
  22.             double x1, x2;
  23.             if (a == 0.0) {
  24.                 throw new Exception("Since the highest coefficient is zero, the given equation is not a quadratic equation.");
  25.             }
  26.             else if (b*b - 4*a*c < 0.0) {
  27.                 throw new Exception("Since the discriminant " + (b*b - 4*a*c) + " is negative, the given equation has no real root.");
  28.             }
  29.             x1 = (-b + Math.Sqrt(b*b - 4*a*c)) / (2.0 * a);
  30.             x2 = (-b - Math.Sqrt(b*b - 4*a*c)) / (2.0 * a);
  31.             List<Double> array = new List<double>();
  32.             array.Add(x1);
  33.             array.Add(x2);
  34.             return array;
  35.         }
  36.         // Java 언어의 main 메소드에 해당하는 C# 언어의 Main 메소드
  37.         public static void Main(string[] args) {
  38.             TestGoldenRatioApp app = new TestGoldenRatioApp();
  39.             if (args.Length > 0 && (args[0].Equals("-h") || args[0].Equals("-help"))) {
  40.                 PrintUsing();
  41.                 Environment.Exit(1);
  42.             }
  43.             try {
  44.                 List<double> values = app.FindQuadraticRoot(1.0, -1.0, -1.0);
  45.                 double x1, x2;
  46.                 x1 = values[0];
  47.                 x2 = values[1];
  48.                 if (x1 >= x2) {
  49.                     Console.WriteLine("The bigger root is " + x1 + ", ");
  50.                     Console.WriteLine("and the less root is " + x2 + ".");
  51.                 }
  52.                 else {
  53.                     Console.WriteLine("The bigger root is " + x2 + ", ");
  54.                     Console.WriteLine("and the less root is " + x1 + ".");
  55.                 }
  56.             }
  57.             catch (Exception caught) {
  58.                 Console.WriteLine("" + caught);
  59.             }
  60.         }
  61.     }
  62. }



컴파일> csc TestGoldenRatioApp.cs

실행> TestGoldenRatioApp
The bigger root is 1.618033988749895,
and the less root is -0.6180339887498949.




Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,