diff --git a/CSClasses/Algorithms.cs b/CSClasses/Algorithms.cs index 09b3ddf..c49f97e 100644 --- a/CSClasses/Algorithms.cs +++ b/CSClasses/Algorithms.cs @@ -1,10 +1,10 @@ //C#/CSClasses/Algorithms.cs //Matthew Ellison // Created: 08-23-20 -//Modified: 08-23-20 +//Modified: 06-01-21 //This file contains a class that is used to time the execution time of other programs /* -Copyright (C) 2020 Matthew Ellison +Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -775,5 +775,27 @@ namespace mee{ public static long FindNumOccurrence(string str, char c){ return str.Count(ch => ch == c); } + //Return the factorial of the number passed in + public static int Factorial(int num){ + int fact = 1; + for(int cnt = 1;cnt <= num;++cnt){ + fact *= cnt; + } + return fact; + } + public static long Factorial(long num){ + long fact = 1; + for(long cnt = 1;cnt <= num;++cnt){ + fact *= cnt; + } + return fact; + } + public static BigInteger Factorial(BigInteger num){ + BigInteger fact = 1; + for(BigInteger cnt = 1;cnt <= num;++cnt){ + fact *= cnt; + } + return fact; + } } } diff --git a/TestCSClasses/TestAlgorithms.cs b/TestCSClasses/TestAlgorithms.cs index 5d547f6..26af708 100644 --- a/TestCSClasses/TestAlgorithms.cs +++ b/TestCSClasses/TestAlgorithms.cs @@ -1,10 +1,10 @@ //C#/CSClasses/TestCSClasses/TestAlgorithms.cs //Matthew Ellison // Created: 03-11-21 -//Modified: 03-11-21 +//Modified: 06-01-21 //This file contains the tests for the Algorithms class /* -Copyright (C) 2020 Matthew Ellison +Copyright (C) 2021 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -300,5 +300,35 @@ namespace TestCSClasses{ answer = mee.Algorithms.FindNumOccurrence(testString, testChar); Assert.AreEqual(correctAnswer, answer, "FindNumOccurrence 3 failed"); } + [TestMethod] + public void TestFactorial(){ + //Test 1 + int num = 1; + int correctAnswer = 1; + int answer = mee.Algorithms.Factorial(num); + Assert.AreEqual(correctAnswer, answer, "Factorial 1 failed"); + //Test 2 + num = 10; + correctAnswer = 3628800; + answer = mee.Algorithms.Factorial(num); + Assert.AreEqual(correctAnswer, answer, "Factorial 2 failed"); + //Test 3 + num = -5; + correctAnswer = 1; + answer = mee.Algorithms.Factorial(num); + Assert.AreEqual(correctAnswer, answer, "Factorial 3 failed"); + + //Test 4 + long numLong = 10; + long correctAnswerLong = 3628800; + long answerLong = mee.Algorithms.Factorial(numLong); + Assert.AreEqual(correctAnswerLong, answerLong, "Factorial 4 failed"); + + //Test 5 + BigInteger numBig = 10; + BigInteger correctAnswerBig = 3628800; + BigInteger answerBig = mee.Algorithms.Factorial(numBig); + Assert.AreEqual(correctAnswerBig, answerBig, "Factorial 5 failed"); + } } }