Skip to content

Instantly share code, notes, and snippets.

@Indp-Dustin
Created October 4, 2018 04:18
Show Gist options
  • Save Indp-Dustin/0915cb3a7f769aaac102e44d63bc0de2 to your computer and use it in GitHub Desktop.
Save Indp-Dustin/0915cb3a7f769aaac102e44d63bc0de2 to your computer and use it in GitHub Desktop.
FindM
long findM3(long n, long m)
{
int c = 0;
long v = 1;
bool squared = false;
int va[1000];
while (v * v <= n)
{
if (v * v < n)
{
if (!(n % v))
{
va[c] = v;
if (m == ++c)
{
return v;
}
}
}
else
{
squared = true;
va[c] = v;
if (m == ++c)
{
return v;
}
break;
}
v++;
}
if (squared && (m % 2))
{
return n / va[m - c];
}
else if (!squared && !(m % 2) && (m <= c * 2))
{
return n / va[c - (m - c)];
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment