What I think is a "long" type variable is behaving like an "int" type variable. I feel this is the case because as the variable gets larger and larger, it actual flips over into the negative when it tries to go above 2147483647, which is the maximum value for an "int" type. The maximum value for a "long" type is much more accommodating, obviously.
I guess I should paste my code here. Please don't harsh on the code, it has not been refactored at all. Also, you will see many lame attempts to force the code to treat my variable in question as a "long" type, but the totality of those efforts are not successful.
Note, this code deals with "N choose X" or combinations. The inputs are the number of combinations and the N. The code loops through possible values of X until it either finds a match or until it blows past the possibility of being a match (or until the calculated combinations "goes negative".)
Thanks in advance for the help.
public class primativeLongPractice {
private static long fctrl (long num) {
long ans = 1L;
for (long i=num; i>0; i--) ans = ans * i;
return ans;
}
private static long nchoosex (long n, long x) {
long y = n - x;
if (y>x) {
long temp = y;
y=x;
x=temp;
}
long ans = 1L;
for (long i=n; i>x; i--) ans = ans * i;
return ans/fctrl(y);
}
public static long checkchoose(long m, int n) {
long N = (long)n;
long combos = 0L;
long x = 1L; // starting out at 1 and going up
// compute "n choose x" call it combos
combos = nchoosex(N,x);
System.out.println(n + " choose " + x + " equals " + combos + "; m equals " + m);
if (combos==m) return x;
while ((combos>1)&&(combos<m)) {
x = x + 1;
combos = nchoosex(N,x);
System.out.println(n + " choose " + x + " equals " + combos + "; m equals " + m);
if (combos==m) return x;
}
System.out.println("Didn't find anything");
return -1L;
}
public static void main(String[] args) {
long p = 155117520L;
int q = 30;
long r = checkchoose(p,q);
System.out.println("For inputs " + q + " and " + p + " the function returned " + r);
}
}
intin this function because of it's signature:checkchoose(long m, int n)intat some point.