mirror of
https://bitbucket.org/Mattrixwv/csclasses.git
synced 2025-12-06 18:23:58 -05:00
Added functions to determine if a number is prime and create a string from a list/array
This commit is contained in:
@@ -486,6 +486,49 @@ namespace mee{
|
|||||||
primes.Sort();
|
primes.Sort();
|
||||||
return primes;
|
return primes;
|
||||||
}
|
}
|
||||||
|
//This function determines whether the number passed into it is a prime
|
||||||
|
public static bool IsPrime(int possiblePrime){
|
||||||
|
if(possiblePrime <= 3){
|
||||||
|
return possiblePrime > 1;
|
||||||
|
}
|
||||||
|
else if(((possiblePrime % 2) == 0) || ((possiblePrime % 3) == 0)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for(int cnt = 5;(cnt * cnt) <= possiblePrime;cnt += 6){
|
||||||
|
if(((possiblePrime % cnt) == 0) || ((possiblePrime % (cnt + 2)) == 0)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public static bool IsPrime(long possiblePrime){
|
||||||
|
if(possiblePrime <= 3){
|
||||||
|
return possiblePrime > 1;
|
||||||
|
}
|
||||||
|
else if(((possiblePrime % 2) == 0) || ((possiblePrime % 3) == 0)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for(long cnt = 5;(cnt * cnt) <= possiblePrime;cnt += 6){
|
||||||
|
if(((possiblePrime % cnt) == 0) || ((possiblePrime % (cnt + 2)) == 0)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public static bool IsPrime(BigInteger possiblePrime){
|
||||||
|
if(possiblePrime <= 3){
|
||||||
|
return possiblePrime > 1;
|
||||||
|
}
|
||||||
|
else if(((possiblePrime % 2) == 0) || ((possiblePrime % 3) == 0)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for(BigInteger cnt = 5;(cnt * cnt) <= possiblePrime;cnt += 6){
|
||||||
|
if(((possiblePrime % cnt) == 0) || ((possiblePrime % (cnt + 2)) == 0)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
//These functions return all the divisors of goalNumber
|
//These functions return all the divisors of goalNumber
|
||||||
public static List<int> GetDivisors(int goalNumber){
|
public static List<int> GetDivisors(int goalNumber){
|
||||||
List<int> divisors = new List<int>();
|
List<int> divisors = new List<int>();
|
||||||
@@ -901,5 +944,29 @@ namespace mee{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//Print a list
|
||||||
|
public static string PrintList<T>(List<T> list){
|
||||||
|
string listString = "[";
|
||||||
|
for(int cnt = 0;cnt < list.Count;++cnt){
|
||||||
|
listString += list[cnt];
|
||||||
|
if(cnt < list.Count - 1){
|
||||||
|
listString += ", ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
listString += "]";
|
||||||
|
return listString;
|
||||||
|
}
|
||||||
|
//Print an array
|
||||||
|
public static string PrintArray<T>(T[] array){
|
||||||
|
string arrayString = "[";
|
||||||
|
for(int cnt = 0;cnt < array.Length;++cnt){
|
||||||
|
arrayString += array[cnt];
|
||||||
|
if(cnt < array.Length - 1){
|
||||||
|
arrayString += ", ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
arrayString += "]";
|
||||||
|
return arrayString;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,6 +120,56 @@ namespace TestCSClasses{
|
|||||||
CollectionAssert.AreEqual(bigCorrectAnswer, bigAnswer, "GetNumPrimes BigInteger failes");
|
CollectionAssert.AreEqual(bigCorrectAnswer, bigAnswer, "GetNumPrimes BigInteger failes");
|
||||||
}
|
}
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
|
public void TestIsPrime(){
|
||||||
|
//Test 1
|
||||||
|
int num = 2;
|
||||||
|
bool correctAnswer = true;
|
||||||
|
bool answer = mee.Algorithms.IsPrime(num);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "IsPrime 1 failed");
|
||||||
|
//Test 2
|
||||||
|
num = 97;
|
||||||
|
correctAnswer = true;
|
||||||
|
answer = mee.Algorithms.IsPrime(num);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "IsPrime 2 failed");
|
||||||
|
//Test 3
|
||||||
|
num = 1000;
|
||||||
|
correctAnswer = false;
|
||||||
|
answer = mee.Algorithms.IsPrime(num);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "IsPrime 3 failed");
|
||||||
|
|
||||||
|
//Test 4
|
||||||
|
long longNum = 2;
|
||||||
|
correctAnswer = true;
|
||||||
|
answer = mee.Algorithms.IsPrime(longNum);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "IsPrime long 1 failed");
|
||||||
|
//Test 5
|
||||||
|
longNum = 97;
|
||||||
|
correctAnswer = true;
|
||||||
|
answer = mee.Algorithms.IsPrime(longNum);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "IsPrime long 2 failed");
|
||||||
|
//Test 6
|
||||||
|
longNum = 1000;
|
||||||
|
correctAnswer = false;
|
||||||
|
answer = mee.Algorithms.IsPrime(longNum);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "IsPrime long 3 failed");
|
||||||
|
|
||||||
|
//Test 7
|
||||||
|
BigInteger bigNum = 2;
|
||||||
|
correctAnswer = true;
|
||||||
|
answer = mee.Algorithms.IsPrime(bigNum);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "IsPrime Big 1 failed");
|
||||||
|
//Test 8
|
||||||
|
bigNum = 97;
|
||||||
|
correctAnswer = true;
|
||||||
|
answer = mee.Algorithms.IsPrime(bigNum);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "IsPrime Big 2 failed");
|
||||||
|
//Test 9
|
||||||
|
bigNum = 1000;
|
||||||
|
correctAnswer = false;
|
||||||
|
answer = mee.Algorithms.IsPrime(bigNum);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "IsPrime Big 3 failed");
|
||||||
|
}
|
||||||
|
[TestMethod]
|
||||||
public void TestGetDivisors(){
|
public void TestGetDivisors(){
|
||||||
//Test 1
|
//Test 1
|
||||||
List<int> correctAnswer = new List<int>(){1, 2, 4, 5, 10, 20, 25, 50, 100};
|
List<int> correctAnswer = new List<int>(){1, 2, 4, 5, 10, 20, 25, 50, 100};
|
||||||
@@ -422,5 +472,63 @@ namespace TestCSClasses{
|
|||||||
}
|
}
|
||||||
CollectionAssert.AreEqual(bigCorrectAnswer, bigAnswer, "SieveOfEratosthenesBig failed");
|
CollectionAssert.AreEqual(bigCorrectAnswer, bigAnswer, "SieveOfEratosthenesBig failed");
|
||||||
}
|
}
|
||||||
|
[TestMethod]
|
||||||
|
public void TestPrintList(){
|
||||||
|
//Test 1
|
||||||
|
List<int> nums = new List<int>();
|
||||||
|
string correctAnswer = "[]";
|
||||||
|
string answer = mee.Algorithms.PrintList(nums);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "PrintList<int> 1 failed");
|
||||||
|
//Test 2
|
||||||
|
nums = new List<int>(){1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||||
|
correctAnswer = "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]";
|
||||||
|
answer = mee.Algorithms.PrintList(nums);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "PrintList<int> 2 failed");
|
||||||
|
//Test 3
|
||||||
|
nums = new List<int>(){-3, -2, -1, 0, 1, 2, 3};
|
||||||
|
correctAnswer = "[-3, -2, -1, 0, 1, 2, 3]";
|
||||||
|
answer = mee.Algorithms.PrintList(nums);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "PrintList<int> 3 failed");
|
||||||
|
|
||||||
|
//Test 4
|
||||||
|
List<string> strings = new List<string>(){"A", "B", "C"};
|
||||||
|
correctAnswer = "[A, B, C]";
|
||||||
|
answer = mee.Algorithms.PrintList(strings);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "PrintList<string> 1 failed");
|
||||||
|
//Test 5
|
||||||
|
strings = new List<string>(){"abc", "def", "ghi"};
|
||||||
|
correctAnswer = "[abc, def, ghi]";
|
||||||
|
answer = mee.Algorithms.PrintList(strings);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "PrintList<string> 2 failed");
|
||||||
|
}
|
||||||
|
[TestMethod]
|
||||||
|
public void TestPrintArray(){
|
||||||
|
//Test 1
|
||||||
|
int[] nums = new int[]{};
|
||||||
|
string correctAnswer = "[]";
|
||||||
|
string answer = mee.Algorithms.PrintArray(nums);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "PrintArray<int> 1 failed");
|
||||||
|
//Test 2
|
||||||
|
nums = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||||
|
correctAnswer = "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]";
|
||||||
|
answer = mee.Algorithms.PrintArray(nums);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "PrintArray<int> 2 failed");
|
||||||
|
//Test 3
|
||||||
|
nums = new int[]{-3, -2, -1, 0, 1, 2, 3};
|
||||||
|
correctAnswer = "[-3, -2, -1, 0, 1, 2, 3]";
|
||||||
|
answer = mee.Algorithms.PrintArray(nums);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "PrintArray<int> 3 failed");
|
||||||
|
|
||||||
|
//Test 4
|
||||||
|
string[] strings = new string[]{"A", "B", "C"};
|
||||||
|
correctAnswer = "[A, B, C]";
|
||||||
|
answer = mee.Algorithms.PrintArray(strings);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "PrintArray<string> 1 failed");
|
||||||
|
//Test 5
|
||||||
|
strings = new string[]{"abc", "def", "ghi"};
|
||||||
|
correctAnswer = "[abc, def, ghi]";
|
||||||
|
answer = mee.Algorithms.PrintArray(strings);
|
||||||
|
Assert.AreEqual(correctAnswer, answer, "PrintArray<string> 2 failed");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user