Example #1
0
        public void IsSumOfFactorialOfDigits_Of145_ReturnsTrue()
        {
            // Arrange
            var number = 145;

            // Act
            var result = FactorialHelper.IsSumOfFactorialOfDigits(number);

            // Assert
            result.Should().BeTrue();
        }
Example #2
0
        public void Factorial_Of5_Returns120()
        {
            // Arrange
            var n = 5;
            var expectedResult = 120;

            // Act
            var result = FactorialHelper.Factorial(n);

            // Assert
            result.Should().Be(expectedResult);
        }
Example #3
0
        public void BinominalCoefficient_Of4Over2_Returns6()
        {
            // Arrange
            var n = 4;
            var k = 2;
            var expectedResult = 6;

            // Act
            var result = FactorialHelper.BinominalCoefficient(n, k);

            // Assert
            result.Should().Be(expectedResult);
        }
Example #4
0
        private string GetNthLexiographicPermutation(BigInteger desiredIndex, List <char> orderedCharacters)
        {
            var numberOfCharacters = orderedCharacters.Count;

            if (numberOfCharacters == 1)
            {
                return(ReturnCharacterIfDesiredIndexIs1(desiredIndex, orderedCharacters));
            }
            BigInteger currentIndex = 0;
            int        indexOfCharacterToUseNext = 0;
            var        numberOfWaysOtherCharacterCanBeOrderedWhenOneIsFixed = FactorialHelper.Factorial(numberOfCharacters - 1);

            while (currentIndex + numberOfWaysOtherCharacterCanBeOrderedWhenOneIsFixed < desiredIndex)
            {
                currentIndex += numberOfWaysOtherCharacterCanBeOrderedWhenOneIsFixed;
                indexOfCharacterToUseNext += 1;
            }
            var characterToUse = orderedCharacters[indexOfCharacterToUseNext];
            var orderedDigitsWithoutDigitUsed = orderedCharacters.Where(character => character != characterToUse).ToList();

            return(characterToUse.ToString() + GetNthLexiographicPermutation(desiredIndex - currentIndex, orderedDigitsWithoutDigitUsed));
        }
Example #5
0
        private static void CalculateWithOperator(Stack stack, OperatorType operatorType)
        {
            double firstOperand;
            double secondOperand;

            switch (operatorType)
            {
            case OperatorType.Addition:
                firstOperand  = double.Parse(Convert.ToString(stack.Pop()));
                secondOperand = double.Parse(Convert.ToString(stack.Pop()));

                stack.Push(firstOperand + secondOperand);

                break;

            case OperatorType.Subtraction:
                firstOperand  = double.Parse(Convert.ToString(stack.Pop()));
                secondOperand = double.Parse(Convert.ToString(stack.Pop()));

                stack.Push(firstOperand - secondOperand);

                break;

            case OperatorType.Multiplication:
                firstOperand  = double.Parse(Convert.ToString(stack.Pop()));
                secondOperand = double.Parse(Convert.ToString(stack.Pop()));

                stack.Push(firstOperand * secondOperand);

                break;

            case OperatorType.Division:
                firstOperand  = double.Parse(Convert.ToString(stack.Pop()));
                secondOperand = double.Parse(Convert.ToString(stack.Pop()));

                if (firstOperand == 0)
                {
                    throw new DivideByZeroException();
                }

                stack.Push(secondOperand / firstOperand);

                break;

            case OperatorType.Modulus:
                firstOperand = double.Parse(Convert.ToString(stack.Pop()));
                stack.Push(firstOperand / 100);

                break;

            case OperatorType.Pow:
                firstOperand  = double.Parse(Convert.ToString(stack.Pop()));
                secondOperand = double.Parse(Convert.ToString(stack.Pop()));

                if (secondOperand == 0 && firstOperand < 0)
                {
                    throw new NotFiniteNumberException(secondOperand);
                }

                stack.Push(Math.Pow(secondOperand, firstOperand));

                break;

            case OperatorType.Factorial:
                firstOperand = double.Parse(Convert.ToString(stack.Pop()));
                stack.Push(FactorialHelper.CalculateFactorial(firstOperand));

                break;

            default:
                throw new InvalidEnumArgumentException(nameof(operatorType), (int)operatorType, typeof(OperatorType));
            }
        }
 public _53_CombinatoricSelections(int countGreaterThan)
 {
     _countGreaterThan = (uint)countGreaterThan;
     _fHelper          = new FactorialHelper();
 }
Example #7
0
 public long ComputeSolution()
 {
     return((long)FactorialHelper.BinominalCoefficient(TOTAL_NUMBER_OF_STEPS, TOTAL_NUMBER_OF_STEPS_DOWNWARDS));
 }
Example #8
0
        public long ComputeSolution()
        {
            var factorial = FactorialHelper.Factorial(NUMBER_TO_COMPUTE_FACTORIAL_OF);

            return(NumberHelper.SumOfDigits(factorial));
        }