module solution;
import std.math : sqrt;
import std.stdio: writefln;
export long isqrt(ulong n)
{
writefln("n: %d", n);
return cast(ulong) sqrt(cast(double) n);
}
module solution_test;
import solution : isqrt;
import std.string : format;
import std.stdio: writefln;
version(unittest) import fluent.asserts;
void dotest(ulong n, long expected)
{
isqrt(n)
.should.equal(expected)
.because("n = %d".format(n));
}
@("Random tests")
unittest
{
import std.math : sqrt, pow;
long referenceSolution(ulong n)
{
return cast(ulong) sqrt(cast(double) n);
}
import std.random : Random, unpredictableSeed, uniform;
auto rnd = Random(unpredictableSeed);
ulong n = 3705694685327610000;
// This test never passes
dotest(n, referenceSolution(n));
foreach(_; 0 .. 100)
{
n = pow(uniform(1, 3037000498, rnd), 2);
dotest(n, referenceSolution(n));
}
}