Permalink
Browse files

RootSample - divide out factors of 9

  • Loading branch information...
bobbymcr committed Aug 27, 2018
1 parent 69c7763 commit 21f8633ccb2d412c2bd42366cfa0e7c3d3408a05
@@ -5,10 +5,13 @@
namespace RootSample
{
using System;
using System.Collections.Generic;
using System.Text;
public struct RootTerm
{
private static readonly List<ushort> Primes = new List<ushort>() { 2, 3 };
private readonly int c;
private readonly int x;
@@ -18,33 +21,35 @@ public RootTerm(int n)
{
this.c = 0;
this.x = 0;
return;
}
else
this.c = 1;
foreach (ushort p in Primes)
{
this.c = 1;
int r;
while (true)
{
int r;
int m = Math.DivRem(n, 4, out r);
int m = Math.DivRem(n, p * p, out r);
if (r != 0)
{
break;
}
n = m;
this.c *= 2;
this.c *= p;
}
}
int s = (int)Math.Sqrt(n);
if (s * s == n)
{
this.x = 1;
this.c *= s;
}
else
{
this.x = n;
}
int s = (int)Math.Sqrt(n);
if (s * s == n)
{
this.x = 1;
this.c *= s;
}
else
{
this.x = n;
}
}
@@ -31,5 +31,14 @@ public void FactorsOfFour(int n, string expected)
{
new RootTerm(n).ToString().Should().Be(expected, "sqrt({0}) is {1}", n, expected);
}
[Theory]
[InlineData(18, "3*sqrt(2)")]
[InlineData(243, "9*sqrt(3)")]
[InlineData(2147483646, "3*sqrt(238609294)")]
public void FactorsOfNine(int n, string expected)
{
new RootTerm(n).ToString().Should().Be(expected, "sqrt({0}) is {1}", n, expected);
}
}
}

0 comments on commit 21f8633

Please sign in to comment.