Example #1
0
    public long GetFabonacciNumber(int n)
    {
        if (n <= 0)
        {
            throw new ArgumentOutOfRangeException("n");
        }

        if (n == 1)
        {
            return(m_FirstNumber);
        }

        if (n == 2)
        {
            return(m_SecondNumber);
        }

        n -= 2;

        MatrixFibonacci result = new MatrixFibonacci(1, 0, 0, 1);
        MatrixFibonacci temp   = new MatrixFibonacci(1, 1, 1, 0);

        while (n != 0)
        {
            if ((n & 1) == 1)
            {
                result.Multiply(temp);
            }

            temp.Multiply(temp);
            n >>= 1;
        }

        return(result[0] * m_SecondNumber + result[1] * m_FirstNumber);
    }
Example #2
0
    public void Multiply(MatrixFibonacci matrix)
    {
        var v11 = m_v11 * matrix.m_v11 + m_v12 * matrix.m_v21;
        var v12 = m_v11 * matrix.m_v12 + m_v12 * matrix.m_v22;
        var v21 = m_v21 * matrix.m_v11 + m_v22 * matrix.m_v21;
        var v22 = m_v21 * matrix.m_v12 + m_v22 * matrix.m_v22;

        m_v11 = v11;
        m_v12 = v12;
        m_v21 = v21;
        m_v22 = v22;
    }