Permalink
Browse files

RootSample - support perfect squares with 'i'

  • Loading branch information...
bobbymcr committed Aug 28, 2018
1 parent 3dc2a7d commit 066932d07a46d5d401ee793d6fb4bb63b02c2221
@@ -24,10 +24,16 @@ private RootTerm(int n)
return;
}
bool imag = n < 0;
if (imag)
{
n = -n;
}
int s = (int)Math.Sqrt(n);
if (s * s == n)
{
this.x = 1;
this.x = imag ? -1 : 1;
this.c = s;
return;
}
@@ -54,13 +60,18 @@ private RootTerm(int n)
}
}
this.x = n;
this.x = imag ? -n : n;
}
public static RootTerm Sqrt(int n) => new RootTerm(n);
public override string ToString()
{
if (this.c == 0)
{
return "0";
}
StringBuilder sb = new StringBuilder();
if (this.c != 1)
{
@@ -78,6 +89,15 @@ public override string ToString()
sb.Append(this.x);
sb.Append(')');
}
else if (this.x == -1)
{
if (sb.Length != 0)
{
sb.Append('*');
}
sb.Append("i");
}
if (sb.Length == 0)
{
@@ -19,6 +19,15 @@ public sealed class RootTermTest
[InlineData(2147395600, "46340")]
public void PerfectSquares(int n, string expected) => Test(n, expected);
[Theory]
[InlineData(-1, "i")]
[InlineData(-4, "2*i")]
[InlineData(-9, "3*i")]
[InlineData(-65536, "256*i")]
[InlineData(-1073741824, "32768*i")]
[InlineData(-2147395600, "46340*i")]
public void PerfectSquaresNeg(int n, string expected) => Test(n, expected);
[Theory]
[InlineData(8, "2*sqrt(2)")]
[InlineData(24, "2*sqrt(6)")]

0 comments on commit 066932d

Please sign in to comment.