I am having trouble on some test cases on the "Equations" challenge on hackerrank.com.
This is where the problem is stated: https://www.hackerrank.com/challenges/equations.
I'm pretty sure I understand the logic, but there is still a bug in my program (or a flaw in my logic).
public class Solution
{
public static void mark(boolean[] a, int i, int n) //sieve util
{
int num = i*2;
while(num <= n)
{
a[num] = true;
num += i;
}
}
public static long legendre(int n, int p) //get prime power
{
long ans = 0;
int k = p;
while(n/k != 0)
{
ans += (long)n/k;
k *= p;
}
return ans;
}
public static void main(String[] args)
{
long mod = 1000007;
int n;
Scanner input = new Scanner(System.in);
n = input.nextInt();
boolean[] a = new boolean[n+1];
for(int i = 2; i <= n; ++i) //sieve
{
if(a[i] == false)
{
mark(a,i,n);
}
}
boolean isEven = true;
long ans = 1, power = 1;
for(int i = 2; i <= n; ++i)
{
if(a[i] == false)
{
power = legendre(n,i);
if(power%2 == 1)
{
isEven = false;
}
ans *= (2*power+1);
ans %= mod;
}
}
if(isEven == true) //symmetric solution
ans -= 1;
if(n == 1) //special case
System.out.println(1);
else
System.out.println(ans);
}
}
What I attempted to do: create a table of prime values and then get the power of each prime factor of n!. I used this formula: http://www.cut-the-knot.org/blue/LegendresTheorem.shtml. My program produced the correct output for n = 105387 (which is 629416), but for n = 131399, my output is 598995 (should be 856428).
This question has also been asked before here: Sample testcase for Interviewstreet: Equations Someone asked "what is the output for N=131399, I am getting 598995, but it's giving wrong. – peeyush May 26 '12 at 20:33" Apparently my particular bug is not unique.