Skip to content

Instantly share code, notes, and snippets.

@sergejusb
Last active December 15, 2015 08:29
Show Gist options
  • Save sergejusb/5231188 to your computer and use it in GitHub Desktop.
Save sergejusb/5231188 to your computer and use it in GitHub Desktop.
namespace BeBetterDeveloper.Algorithms.Math
{
using System;
using NUnit.Framework;
public static class Power
{
public static double Pow(double number, int exp)
{
if (number == 0 && exp < 0) throw new Exception("negative exponent for zero");
if (exp == 0) return 1;
bool positive = true;
if (exp < 0)
{
positive = false;
exp *= -1;
}
double result = 1;
while (exp >= 1)
{
if (exp % 2 != 0)
{
result *= number;
}
number *= number;
exp >>= 1;
}
return positive ? result : 1 / result;
}
}
[TestFixture]
public class PowerTests
{
[Test]
[TestCase(0, 0, 1)]
[TestCase(0, 1, 0)]
[TestCase(1, 2, 1)]
[TestCase(2, 2, 4)]
[TestCase(2, 3, 8)]
[TestCase(2.5, 5, 97.65625)]
[TestCase(5, -2, 0.04)]
[TestCase(-2, 2, 4)]
[TestCase(-2, 3, -8)]
[TestCase(-2.5, 5, -97.65625)]
public void Correctly_Raises_Double_To_Power(double number, int exp, double result)
{
var actual = Power.Pow(number, exp);
Assert.That(actual, Is.EqualTo(result));
}
}
}
namespace BeBetterDeveloper.Algorithms.Math
{
using System;
using NUnit.Framework;
public static class PowerNaive
{
public static double Pow(double number, int exp)
{
if (number == 0 && exp < 0) throw new Exception("negative exponent for zero");
if (exp == 0) return 1;
bool positive = true;
if (exp < 0)
{
positive = false;
exp *= -1;
}
double result = 1;
for (int i = 1; i <= exp; i++)
{
result *= number;
}
return positive ? result : 1 / result;
}
}
[TestFixture]
public class PowerNaiveTests
{
[Test]
[TestCase(0, 0, 1)]
[TestCase(0, 1, 0)]
[TestCase(1, 2, 1)]
[TestCase(2, 2, 4)]
[TestCase(2, 3, 8)]
[TestCase(2.5, 5, 97.65625)]
[TestCase(5, -2, 0.04)]
[TestCase(-2, 2, 4)]
[TestCase(-2, 3, -8)]
[TestCase(-2.5, 5, -97.65625)]
public void Correctly_Raises_Double_To_Power(double number, int exp, double result)
{
var actual = PowerNaive.Pow(number, exp);
Assert.That(actual, Is.EqualTo(result));
}
}
}
namespace BeBetterDeveloper.Algorithms.Math
{
using System;
using NUnit.Framework;
public static class PrimeNumber
{
public static bool IsPrimeNumber(int number)
{
if (number < 1) throw new Exception("number can not be negative or zero");
if (number == 1) return false;
var limit = number >> 1;
for (int i = 2; i <= limit; i++)
{
if (number % i == 0) return false;
}
return true;
}
}
[TestFixture]
class PrimeNumberTests
{
[Test]
[TestCase(1, false)]
[TestCase(2, true)]
[TestCase(3, true)]
[TestCase(19, true)]
[TestCase(100, false)]
[TestCase(101, true)]
[TestCase(121, false)]
public void Correctly_Detects_Prime_Number(int number, bool isPrime)
{
Assert.That(PrimeNumber.IsPrimeNumber(number), Is.EqualTo(isPrime));
}
[Test]
public void Throws_When_Number_Is_Zero()
{
Assert.Throws<Exception>(() => PrimeNumber.IsPrimeNumber(0));
}
[Test]
public void Throws_When_Number_Is_Negative()
{
Assert.Throws<Exception>(() => PrimeNumber.IsPrimeNumber(-1));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment