mirror of
https://bitbucket.org/Mattrixwv/csclasses.git
synced 2025-12-06 18:23:58 -05:00
Updated functions to match C# conventions
This commit is contained in:
@@ -30,7 +30,7 @@ using System.Numerics;
|
||||
namespace mee{
|
||||
public class Algorithms{
|
||||
//These functions return a list of all Fibonacci numbers <= goalNumber
|
||||
public static List<int> getAllFib(int goalNumber){
|
||||
public static List<int> GetAllFib(int goalNumber){
|
||||
//Setup the variables
|
||||
List<int> fibNums = new List<int>();
|
||||
|
||||
@@ -52,7 +52,7 @@ namespace mee{
|
||||
fibNums.RemoveAt(fibNums.Count - 1);
|
||||
return fibNums;
|
||||
}
|
||||
public static List<long> getAllFib(long goalNumber){
|
||||
public static List<long> GetAllFib(long goalNumber){
|
||||
//Setup the variables
|
||||
List<long> fibNums = new List<long>();
|
||||
|
||||
@@ -74,7 +74,7 @@ namespace mee{
|
||||
fibNums.RemoveAt(fibNums.Count - 1);
|
||||
return fibNums;
|
||||
}
|
||||
public static List<BigInteger> getAllFib(BigInteger goalNumber){
|
||||
public static List<BigInteger> GetAllFib(BigInteger goalNumber){
|
||||
//Setup the variables
|
||||
List<BigInteger> fibNums = new List<BigInteger>();
|
||||
|
||||
@@ -97,10 +97,10 @@ namespace mee{
|
||||
return fibNums;
|
||||
}
|
||||
//These functions return all factors of goalNumber
|
||||
public static List<int> getFactors(int goalNumber){
|
||||
public static List<int> GetFactors(int goalNumber){
|
||||
//You need to get all the primes that could be factors of this number so you can test them
|
||||
int topPossiblePrime = (int)Math.Ceiling(Math.Sqrt(goalNumber));
|
||||
List<int> primes = getPrimes(topPossiblePrime);
|
||||
List<int> primes = GetPrimes(topPossiblePrime);
|
||||
List<int> factors = new List<int>();
|
||||
|
||||
//YOu need to step through each prime and see if it is a factor in the number
|
||||
@@ -128,10 +128,10 @@ namespace mee{
|
||||
//Return the list of factors
|
||||
return factors;
|
||||
}
|
||||
public static List<long> getFactors(long goalNumber){
|
||||
public static List<long> GetFactors(long goalNumber){
|
||||
//You need to get all the primes that could be factors of this number so you can test them
|
||||
long topPossiblePrime = (long)Math.Ceiling(Math.Sqrt(goalNumber));
|
||||
List<long> primes = getPrimes(topPossiblePrime);
|
||||
List<long> primes = GetPrimes(topPossiblePrime);
|
||||
List<long> factors = new List<long>();
|
||||
|
||||
//YOu need to step through each prime and see if it is a factor in the number
|
||||
@@ -159,10 +159,10 @@ namespace mee{
|
||||
//Return the list of factors
|
||||
return factors;
|
||||
}
|
||||
public static List<BigInteger> getFactors(BigInteger goalNumber){
|
||||
public static List<BigInteger> GetFactors(BigInteger goalNumber){
|
||||
//You need to get all the primes that could be factors of this number so you can test them
|
||||
BigInteger topPossiblePrime = (BigInteger)Math.Exp(BigInteger.Log(goalNumber) / 2);
|
||||
List<BigInteger> primes = getPrimes(topPossiblePrime);
|
||||
List<BigInteger> primes = GetPrimes(topPossiblePrime);
|
||||
List<BigInteger> factors = new List<BigInteger>();
|
||||
|
||||
//YOu need to step through each prime and see if it is a factor in the number
|
||||
@@ -191,7 +191,7 @@ namespace mee{
|
||||
return factors;
|
||||
}
|
||||
//These functions return a list with all the prime number <= goalNumber
|
||||
public static List<int> getPrimes(int goalNumber){
|
||||
public static List<int> GetPrimes(int goalNumber){
|
||||
List<int> primes = new List<int>(); //Holds the prime numbers
|
||||
bool foundFactor = false; //A flag for whether a factor of the current number has been found
|
||||
|
||||
@@ -235,7 +235,7 @@ namespace mee{
|
||||
primes.Sort();
|
||||
return primes;
|
||||
}
|
||||
public static List<long> getPrimes(long goalNumber){
|
||||
public static List<long> GetPrimes(long goalNumber){
|
||||
List<long> primes = new List<long>(); //Holds the prime numbers
|
||||
bool foundFactor = false; //A flag for whether a factor of the current number has been found
|
||||
|
||||
@@ -279,7 +279,7 @@ namespace mee{
|
||||
primes.Sort();
|
||||
return primes;
|
||||
}
|
||||
public static List<BigInteger> getPrimes(BigInteger goalNumber){
|
||||
public static List<BigInteger> GetPrimes(BigInteger goalNumber){
|
||||
List<BigInteger> primes = new List<BigInteger>(); //Holds the prime numbers
|
||||
bool foundFactor = false; //A flag for whether a factor of the current number has been found
|
||||
|
||||
@@ -325,7 +325,7 @@ namespace mee{
|
||||
return primes;
|
||||
}
|
||||
//This function gets a certain number of primes
|
||||
public static List<int> getNumPrimes(int numberOfPrimes){
|
||||
public static List<int> GetNumPrimes(int numberOfPrimes){
|
||||
List<int> primes = new List<int>(); //Holds the prime numbers
|
||||
bool foundFactor = false; //A flag for whether a factor of the current number has been found
|
||||
|
||||
@@ -370,7 +370,7 @@ namespace mee{
|
||||
primes.Sort();
|
||||
return primes;
|
||||
}
|
||||
public static List<long> getNumPrimes(long numberOfPrimes){
|
||||
public static List<long> GetNumPrimes(long numberOfPrimes){
|
||||
List<long> primes = new List<long>(); //Holds the prime numbers
|
||||
bool foundFactor = false; //A flag for whether a factor of the current number has been found
|
||||
|
||||
@@ -415,7 +415,7 @@ namespace mee{
|
||||
primes.Sort();
|
||||
return primes;
|
||||
}
|
||||
public static List<BigInteger> getNumPrimes(BigInteger numberOfPrimes){
|
||||
public static List<BigInteger> GetNumPrimes(BigInteger numberOfPrimes){
|
||||
List<BigInteger> primes = new List<BigInteger>(); //Holds the prime numbers
|
||||
bool foundFactor = false; //A flag for whether a factor of the current number has been found
|
||||
|
||||
@@ -461,34 +461,34 @@ namespace mee{
|
||||
return primes;
|
||||
}
|
||||
//These functions get a value from combining elements in an array
|
||||
public static int getSum(List<int> ary){
|
||||
public static int GetSum(List<int> ary){
|
||||
return ary.Sum();
|
||||
}
|
||||
public static long getSum(List<long> ary){
|
||||
public static long GetSum(List<long> ary){
|
||||
return ary.Sum();
|
||||
}
|
||||
public static BigInteger getSum(List<BigInteger> ary){
|
||||
public static BigInteger GetSum(List<BigInteger> ary){
|
||||
BigInteger sum = 0;
|
||||
foreach(BigInteger num in ary){
|
||||
sum += num;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
public static int getProd(List<int> ary){
|
||||
public static int GetProd(List<int> ary){
|
||||
int prod = 1;
|
||||
foreach(int num in ary){
|
||||
prod *= num;
|
||||
}
|
||||
return prod;
|
||||
}
|
||||
public static long getProd(List<long> ary){
|
||||
public static long GetProd(List<long> ary){
|
||||
long prod = 1;
|
||||
foreach(long num in ary){
|
||||
prod *= num;
|
||||
}
|
||||
return prod;
|
||||
}
|
||||
public static BigInteger getProd(List<BigInteger> ary){
|
||||
public static BigInteger GetProd(List<BigInteger> ary){
|
||||
BigInteger prod = 1;
|
||||
foreach(BigInteger num in ary){
|
||||
prod *= num;
|
||||
|
||||
@@ -31,52 +31,52 @@ namespace mee{
|
||||
public Stopwatch(){
|
||||
}
|
||||
//Returns a long with the elapsed time in nanoseconds. Used by other functions to get the time before converting it to the correct resolution
|
||||
private long getTime(){
|
||||
private long GetTime(){
|
||||
return stopwatch.ElapsedTicks;
|
||||
}
|
||||
//Simultates starting a stopwatch
|
||||
public void start(){
|
||||
public void Start(){
|
||||
stopwatch.Start();
|
||||
}
|
||||
//Simulates stopping a stopwatch
|
||||
public void stop(){
|
||||
public void Stop(){
|
||||
stopwatch.Stop();
|
||||
}
|
||||
//Resets all the variables in teh stopwatch
|
||||
public void reset(){
|
||||
public void Reset(){
|
||||
stopwatch.Reset();
|
||||
}
|
||||
//Returns the timer in nanoseconds
|
||||
public decimal getNano(){
|
||||
public decimal GetNano(){
|
||||
return (decimal)nanosecPerTick * (decimal)stopwatch.ElapsedTicks;
|
||||
}
|
||||
//Returns the timer in microseconds
|
||||
public decimal getMicro(){
|
||||
return getNano() / 1000m;
|
||||
public decimal GetMicro(){
|
||||
return GetNano() / 1000m;
|
||||
}
|
||||
//Returns the timer in milliseconds
|
||||
public decimal getMilli(){
|
||||
return getNano() / 1000000m;
|
||||
public decimal GetMilli(){
|
||||
return GetNano() / 1000000m;
|
||||
}
|
||||
//Returns the timer in seconds
|
||||
public decimal getSecond(){
|
||||
return getNano() / 1000000000m;
|
||||
public decimal GetSecond(){
|
||||
return GetNano() / 1000000000m;
|
||||
}
|
||||
//Returns the timer in minutes
|
||||
public decimal getMinute(){
|
||||
return getNano() / 60000000000m;
|
||||
public decimal GetMinute(){
|
||||
return GetNano() / 60000000000m;
|
||||
}
|
||||
//Returns the timer in hours
|
||||
public decimal getHour(){
|
||||
return getNano() / 3600000000000m;
|
||||
public decimal GetHour(){
|
||||
return GetNano() / 3600000000000m;
|
||||
}
|
||||
//Returns the timer as a string at the 'best' resolution. (Goal is xxx.xxx)
|
||||
public string getStr(){
|
||||
public string GetStr(){
|
||||
//Get the current duration from time
|
||||
return getStr(getNano());
|
||||
return GetStr(GetNano());
|
||||
}
|
||||
//Returns a string of the decimal value passed in (assuming the value in nanoseconds)
|
||||
public static string getStr(decimal nanoseconds){
|
||||
public static string GetStr(decimal nanoseconds){
|
||||
decimal duration = nanoseconds;
|
||||
//Reduce the number to the appropriate number of digits. (xxx.x)
|
||||
//This loop works down to seconds
|
||||
@@ -117,66 +117,66 @@ namespace mee{
|
||||
}
|
||||
//Overrides default tostring. returns getStr
|
||||
public override string ToString(){
|
||||
return getStr();
|
||||
return GetStr();
|
||||
}
|
||||
|
||||
//Tests
|
||||
public static void Main(string[] args){
|
||||
System.Console.WriteLine("Testing start/stop");
|
||||
testStartStop();
|
||||
TestStartStop();
|
||||
System.Console.WriteLine("start/stop completed successfully");
|
||||
System.Console.WriteLine("Testing conversion");
|
||||
testConversion();
|
||||
TestConversion();
|
||||
System.Console.WriteLine("conversion test completed successfully");
|
||||
System.Console.WriteLine("Testing stringConversion");
|
||||
testStringConversion();
|
||||
TestStringConversion();
|
||||
System.Console.WriteLine("stringConversion completed successfully");
|
||||
}
|
||||
public static void testStartStop(){
|
||||
public static void TestStartStop(){
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.start();
|
||||
timer.stop();
|
||||
timer.Start();
|
||||
timer.Stop();
|
||||
//If it gets here without throwing an exception everything went well
|
||||
}
|
||||
public static void testConversion(){
|
||||
public static void TestConversion(){
|
||||
Stopwatch timer = new Stopwatch();
|
||||
int sum = 0;
|
||||
//Start the timer
|
||||
timer.start();
|
||||
timer.Start();
|
||||
//Do something to run some time
|
||||
for(int cnt = 0;cnt < NUM_TO_RUN;++cnt){
|
||||
sum += cnt;
|
||||
}
|
||||
//Stop the timer
|
||||
timer.stop();
|
||||
timer.Stop();
|
||||
//Assert something so the sum isn't ignored during compile
|
||||
System.Diagnostics.Debug.Assert(sum != 0);
|
||||
//Check that the different resolutions work out correctly
|
||||
decimal nano = timer.getNano();
|
||||
System.Diagnostics.Debug.Assert(timer.getMicro() == (nano / 1000m), "Micro resolution test failed");
|
||||
System.Diagnostics.Debug.Assert(timer.getMilli() == (nano / 1000000m), "Milli resolution test failed");
|
||||
System.Diagnostics.Debug.Assert(timer.getSecond() == (nano / 1000000000m), "Second resolution test failed");
|
||||
System.Diagnostics.Debug.Assert(timer.getMinute() == (nano / 60000000000m), "Minute resolution test failed");
|
||||
System.Diagnostics.Debug.Assert(timer.getHour() == (nano / 3600000000000m), "Hour resolution test failed");
|
||||
decimal nano = timer.GetNano();
|
||||
System.Diagnostics.Debug.Assert(timer.GetMicro() == (nano / 1000m), "Micro resolution test failed");
|
||||
System.Diagnostics.Debug.Assert(timer.GetMilli() == (nano / 1000000m), "Milli resolution test failed");
|
||||
System.Diagnostics.Debug.Assert(timer.GetSecond() == (nano / 1000000000m), "Second resolution test failed");
|
||||
System.Diagnostics.Debug.Assert(timer.GetMinute() == (nano / 60000000000m), "Minute resolution test failed");
|
||||
System.Diagnostics.Debug.Assert(timer.GetHour() == (nano / 3600000000000m), "Hour resolution test failed");
|
||||
}
|
||||
public static void testStringConversion(){
|
||||
public static void TestStringConversion(){
|
||||
//Test nanoseconds
|
||||
string results = Stopwatch.getStr(1.0m);
|
||||
string results = Stopwatch.GetStr(1.0m);
|
||||
System.Diagnostics.Debug.Assert(results == "1.000 nanoseconds", "Failed nanoseconds: " + results);
|
||||
//Test microseconds
|
||||
results = Stopwatch.getStr(1.0e3m);
|
||||
results = Stopwatch.GetStr(1.0e3m);
|
||||
System.Diagnostics.Debug.Assert(results == "1.00 microsesconds", "Failed microseconds: " + results);
|
||||
//Test milliseconds
|
||||
results = Stopwatch.getStr(1.0e6m);
|
||||
results = Stopwatch.GetStr(1.0e6m);
|
||||
System.Diagnostics.Debug.Assert(results == "1.00 milliseconds", "Failed milliseconds: " + results);
|
||||
//Test seconds
|
||||
results = Stopwatch.getStr(1.0e9m);
|
||||
results = Stopwatch.GetStr(1.0e9m);
|
||||
System.Diagnostics.Debug.Assert(results == "1.00 seconds", "Failed seconds: " + results);
|
||||
//Test minutes
|
||||
results = Stopwatch.getStr(1.0e12m);
|
||||
results = Stopwatch.GetStr(1.0e12m);
|
||||
System.Diagnostics.Debug.Assert(results == "1.00 minutes", "Failed minutes: " + results);
|
||||
//Test hours
|
||||
results = Stopwatch.getStr(1.0e13m);
|
||||
results = Stopwatch.GetStr(1.0e13m);
|
||||
System.Diagnostics.Debug.Assert(results == "1.00 hours", "Failed hours: " + results);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user