< Summary

Information
Class: LeetCode.Algorithms.NthTribonacciNumber.NthTribonacciNumberMatrixExponentiation
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\NthTribonacciNumber\NthTribonacciNumberMatrixExponentiation.cs
Line coverage
100%
Covered lines: 36
Uncovered lines: 0
Coverable lines: 36
Total lines: 78
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Tribonacci(...)100%44100%
Power(...)100%44100%
Multiply(...)100%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\NthTribonacciNumber\NthTribonacciNumberMatrixExponentiation.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.NthTribonacciNumber;
 13
 14/// <inheritdoc />
 15public class NthTribonacciNumberMatrixExponentiation : INthTribonacciNumber
 16{
 17    /// <summary>
 18    ///     Time complexity - O(log n)
 19    ///     Time complexity - O(log n)
 20    /// </summary>
 21    /// <param name="n"></param>
 22    /// <returns></returns>
 23    public int Tribonacci(int n)
 624    {
 625        switch (n)
 26        {
 27            case 0:
 128                return 0;
 29            case 1:
 30            case 2:
 231                return 1;
 32        }
 33
 334        int[,] matrix = { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 1, 0 } };
 35
 336        var result = Power(matrix, n - 2);
 37
 338        return result[0, 0] + result[0, 1];
 639    }
 40
 41    private static int[,] Power(int[,] matrix, int power)
 342    {
 343        int[,] result = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
 44
 1145        while (power > 0)
 846        {
 847            if (power % 2 == 1)
 648            {
 649                result = Multiply(result, matrix);
 650            }
 51
 852            matrix = Multiply(matrix, matrix);
 853            power /= 2;
 854        }
 55
 356        return result;
 357    }
 58
 59    private static int[,] Multiply(int[,] a, int[,] b)
 1460    {
 1461        var result = new int[3, 3];
 62
 11263        for (var i = 0; i < 3; i++)
 4264        {
 33665            for (var j = 0; j < 3; j++)
 12666            {
 12667                result[i, j] = 0;
 68
 100869                for (var k = 0; k < 3; k++)
 37870                {
 37871                    result[i, j] += a[i, k] * b[k, j];
 37872                }
 12673            }
 4274        }
 75
 1476        return result;
 1477    }
 78}