mirror of
https://bitbucket.org/Mattrixwv/csclasses.git
synced 2025-12-06 18:23:58 -05:00
Added function to get factorial of number
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
//C#/CSClasses/Algorithms.cs
|
//C#/CSClasses/Algorithms.cs
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 08-23-20
|
// 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
|
//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
|
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
|
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){
|
public static long FindNumOccurrence(string str, char c){
|
||||||
return str.Count(ch => ch == 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
//C#/CSClasses/TestCSClasses/TestAlgorithms.cs
|
//C#/CSClasses/TestCSClasses/TestAlgorithms.cs
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-11-21
|
// Created: 03-11-21
|
||||||
//Modified: 03-11-21
|
//Modified: 06-01-21
|
||||||
//This file contains the tests for the Algorithms class
|
//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
|
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
|
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);
|
answer = mee.Algorithms.FindNumOccurrence(testString, testChar);
|
||||||
Assert.AreEqual(correctAnswer, answer, "FindNumOccurrence 3 failed");
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user