posted by 써니루루 2007. 7. 17. 20:21

제곱근을 구하기 위해서는 다음과 같은 공식을 만족합니다.


x = root(a) 라면

x^2 = a 입니다.

따라서 x = a/x가 됩니다.

이에 의해서

임의의 x에 의해서 a의 제곱근 값은

x < root(a) < a/x 거나
a/x < root(a) <x 의 범위에 있게 됩니다.

따라서 a와 a/x의 평균값을 구하는

(a+b)/2 공식을 이용해 x = (x + a/x)/2 의 식을 이용하면

대부분의 수는 10번을 돌기 이전에 루트 값을 구할 수 있습니다.

간단하게 아래 C#으로 코딩해본 소스입니다.

using System;
using System.Collections.Generic;
using System.Text;

namespace Sqrt
{
    class SqrtMain
    {
        static double Sqrt(double aa, double xx)
        {
            try
            {
                for (int i = 0; i < 10; i++)
                    Console.WriteLine(xx = (xx + aa / xx) / 2);
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return xx;
        }

        static void Main(string[] args)
        {
            Console.WriteLine(Sqrt(2341, 3));
        }
    }
}

posted by 써니루루 2007. 7. 16. 23:30
마방진 클래스다이어그램


오늘 1시간 시험으로 봤던 마방진 홀수 / 마방진 4의 배수 알고리즘을 이용한 마방진 소스

조금은 어렵지만 한번 분석해보시길 ^ ^

.NET 2.0 C# Console 응용 프로그램으로 제작했습니다.

사용 IDE는 Visual studio 2007 orcas


posted by 써니루루 2007. 7. 13. 12:19

상당히 많은 시행착오를 거쳐 완성된 소스

꽤 걸렸다 ㅠㅠ

4의 배수 크기의 마방진 알고리즘은 한번 찾아보시길 ~

아무튼 소스는 아래와 같습니다.

            int qSize = Size / 4;

            for (int i = 0; i < Size * Size; i++)
            {
                x = i / Size;
                y = i % Size;

                if ((x / qSize == y / qSize) || (x / qSize + y / qSize + 1 == 4))
                    this.data[x, y] = i + 1;
                else
                    this.data[Size - x - 1, Size - y - 1] = i + 1;
            }

posted by 써니루루 2007. 7. 12. 22:46

Sin 을 처리하는 함수를 유사하게 만들어봅니다.

이상하게 255' 이상은 값이 제대로 나오질 않는군요 ㅎㅎ

문제 해결하시게 되면 트랙백 좀 해주세요 ㅎㅎ

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ruru.Math
{
    public class Sin
    {
        public static double GetSin(double x)
        {
            double sum = 0.0;
            double r = 1.0;

            for (int i = 1; i < 13; i++)
            {
                r *= x / (1.0 * i);

                switch (i % 4)
                {
                    case 1: sum += r; break; // 1
                    case 3: sum += (-1) * r; break; // -1
                    case 2:
                    case 4: break; // 0
                }
            }
            return sum;
        }

        public static void PrintSin()
        {
            for (int i = 0; i < 180; i++)
                Console.WriteLine("Sin({0}) = {1}", i, GetSin(System.Math.PI*i/180.0));
        }
    }
}


 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ruru.Math
{
    class SinMain
    {
        static void Main(string[] args)
        {
            Sin.PrintSin();
        }
    }
}


posted by 써니루루 2007. 3. 27. 12:41
선형 콘그루엔셜 방법(Hull. De bull 제안)

1. 하나의 초기 숫자를 설정(x0)
2. 3개의 상수 a, c, m을 수식에 대입해 난수를 발생시킨다.
수식 : x1 = (a * x0 + c) % m
3. 다음 조건을 만족해야 한다.
a < m, c < m, 0 < m


위 알고리즘을 C#으로 표현한 소스는 다음과 같다.