Heres my function, its breaking on line 30:
return this.factorhelper(n, (i + 1), acc);
I'm a unexperienced java programmer, I have some ideas such as getting rid of the accumulator in factorhelper and just use boolean and not but thought some of you might have much better ones.
Update #1: I made the changes I was thinking of.
Update #2: Would translating it into c++ be more effective? I also heard there are ways to increase the memory that java will use.
Update # 3: Btw I would like to have this program for several hours at least. Right now it lasts less then a second lol. Maybe I should just try writing it in c++ instead, I won't be able to right it off the top of my head but I'll just have to read up on it.
Update #4: I think I realized what I need to do, replace my current factor method with a PrimeOmega method. Hmm.. Anyone know much about it?
This is one weird result that I get back.
Then the program makes it up to:
Current: 8784, Difference is: -25
and then throws this error for 50+ times:
New Code
return this.factorhelper(n, (i + 1), acc);
I'm a unexperienced java programmer, I have some ideas such as getting rid of the accumulator in factorhelper and just use boolean and not but thought some of you might have much better ones.
Update #1: I made the changes I was thinking of.
Update #2: Would translating it into c++ be more effective? I also heard there are ways to increase the memory that java will use.
Update # 3: Btw I would like to have this program for several hours at least. Right now it lasts less then a second lol. Maybe I should just try writing it in c++ instead, I won't be able to right it off the top of my head but I'll just have to read up on it.
Update #4: I think I realized what I need to do, replace my current factor method with a PrimeOmega method. Hmm.. Anyone know much about it?
This is one weird result that I get back.
Code:
Current: 8522, Difference is: -17
java.lang.reflect.InvocationTargetExceptionCurrent: 8523, Difference is: -18
Current: 8524, Difference is: -17
Then the program makes it up to:
Current: 8784, Difference is: -25
and then throws this error for 50+ times:
Code:
at OddGreaterEven.factorhelper(OddGreaterEven.java:32)
New Code
Code:
class OddGreaterEven{
OddGreaterEven(){}
// recurses until the difference is equal to 1 (number of evens is greater than the number of odds)
void f (int current, int difference){
System.out.println("Current: " + (current + 1) + ", Difference is: " + difference);
if (difference == 1){
System.out.println("This is the winner! " + (current + 1));
}
else if (this.isOdd(current)){
this.f((current + 1), difference - 1);
}
else this.f((current + 1), difference + 1);
}
// is the number of prime factors for this number odd?
boolean isOdd(int current){
return this.factorhelper(current, 2);
}
// returns the number of prime factors for the given integer
boolean factorhelper(int n, int i){
if ((n == 0) || (n == 1)){
return false;
}
else if ((i <= n) && ((n % i) == 0)){
return !this.factorhelper((n / i), i);
}
else if ((i <= n) && ((n % i) > 0)){
return this.factorhelper(n, (i + 1));
}
else return false;
}
}
Last edited: