How to fix a stack overflow in my java program? - java

So, I am studying java, and as a first exercise I decided to write two small and composable functions, namely a fibbonacci and a factorial function, which have a common implementation detail, however I received a stack overflow exception when I tried to separate their common part into a separate function. Any tips on what am I doing wrong?
public class BaseFunctions{
static Integer factorial(Integer num) {
return factorial(comComponent(num)-1)*num;
}
static Integer fibbonacci(Integer num) {
return fibbonacci(comComponent(num)-1) + fibbonacci(comComponent(num)-2);
}
static Integer comComponent(Integer num) {
if(num == 1 || num == 0) {
return 1;
}else if(num < 0){
throw new ArithmeticException("Num must be > 0");
}else return num;
}
}

When you reach num = 0, comComponent will return 1, calling your function with num = 0 again, thus getting stuck in an infinite loop.

A recursive function must have an exit point (which must be checked first before any recursive call), if not this leads to Stack Overflow.
What you do is leave the function without that exit point, be delegating (or so you think) this functionality to comComponent().
But take a look at the code inside factorial():
return factorial(comComponent(num)-1)*num;
Why should it ever stop? There is no return statement without a recursive call.
So your logic is wrong.
Drop comComponent() and stick to the traditional way:
static Integer factorial(Integer num) {
if (num == 1 || num == 0)
return 1;
else if (num < 0)
throw new ArithmeticException("Num must be > 0");
else
return factorial(num - 1) * num;
}
static Integer fibbonacci(Integer num) {
if (num == 1 || num == 0)
return 1;
else if (num < 0)
throw new ArithmeticException("Num must be > 0");
else
return fibbonacci(num - 1) + fibbonacci(num - 2);
}
As you can see both functions have an exit point:
if (num == 1 || num == 0)
return 1;

Related

Digit amount in an integer [duplicate]

This question already has answers here:
Way to get number of digits in an int?
(29 answers)
Closed 24 days ago.
How can I find the amount of digits in an integer? Mathematically, and by using functions if there are any.
I don't really know how to do that, since I'm a somewhat beginner.
Another option would be to do it iteratively by dividing number by 10, until result is 0.
int number = ...;
int count = 1;
while ((number /= 10) != 0) {
count++;
}
In this program we use a for loop without any body.
On each iteration, the value of num is divided by 10 and count is incremented by 1.
The for loop exits when num != 0 is false, i.e. num = 0.
Since, for loop doesn't have a body, you can change it to a single statement in Java as such:
for(; num != 0; num/=10, ++count);
public class Main {
public static void main(String[] args) {
int count = 0, num = 123456;
for (; num != 0; num /= 10, ++count) {
}
System.out.println("Number of digits: " + count);
}
}
There are many ways to calculate the number of digits in a number. The main difference between them is how important performance is to you. The first way is to translate a number into a string and then take its length:
public static int countDigitsFoo(int x) {
if (x == Integer.MIN_VALUE) {
throw new RuntimeException("Cannot invert Integer.MIN_VALUE");
}
if (x < 0) {
return countDigitsFoo(-x); // + 1; if you want count '-'
}
return Integer.toString(x).length();
}
This method is bad for everyone, except that it is easy to write. Here there is an extra allocation of memory, namely the translation of a number into a string. That with private calls to this function will hit performance very hard.
The second way. You can use integer division and sort of go by the number from right to left:
public static int countDigitsBoo(int x) {
if (x == Integer.MIN_VALUE) {
throw new RuntimeException("Cannot invert Integer.MIN_VALUE");
}
if (x < 0) {
return countDigitsBoo(-x); // + 1; if you want count '-'
}
int count = 0;
while (x > 0) {
count++;
x /= 10;
}
return count;
}
but even this method can be improved. I will not write it in full, but I will give part of the code.
P.S. never use this method, it is rather another way to solve this problem, but no more
public static int countDigitsHoo(int x) {
if (x == Integer.MIN_VALUE) {
throw new RuntimeException("Cannot invert Integer.MIN_VALUE");
}
if (x < 0) {
return countDigitsHoo(-x); // + 1; if you want count '-'
}
if (x < 10) {
return 1;
}
if (x < 100) {
return 2;
}
if (x < 1000) {
return 3;
}
// ...
return 10;
}
You also need to decide what is the number of digits in the number. Should I count the minus sign along with this? Also, in addition, you need to add a condition on Integer.MIN_VALUE because
Integer.MIN_VALUE == -Integer.MIN_VALUE
This is due to the fact that taking a unary minus occurs by -x = ~x + 1 at the hardware level, which leads to "looping" on -Integer.MIN_VALUE
In Java, I would convert the integer to a string using the .toString() function and then use the string to determine the number of digits.
Integer digit = 10000;
Integer digitLength = abs(digit).toString().length();

How to reverse an integer?

I have a code to reverse an integer, but it does not work, can't seem to find the bug.
public static void test(int N) {
int enable_print = N % 10;
while (N > 0) {
if (enable_print == 0 && N % 10 != 0) {
enable_print = 1;
} else if (enable_print == 1) {
System.out.print(N % 10);
}
N = N / 10;
}
}
Sometimes it is easier and/or better to rewrite instead of debugging.
Write or think of your algorithm in pseudocode where each high level step is then broken down into more pseudocode. Your conditions seem strange and therefore hard to debug.
It is best not to embed a print directly into the heart of a loop. Rather, build a string and return it. Let the caller print a string.
System.out.println (reverseInt (12345));
public static String reverseInt (anInt) {
Initialize a StringBuffer with an empty string.
while (anInt > 0) {
Get last digit by modulo 10 and put in StringBuffer.
Prepend digit in StringBuffer.
Chop off last digit by doing integer divide.
}
return StringBuffer's .toString ();
}
An alternate algorithm would call reverseInt recursively to build an ever growing string.
if and else if both works together in a sequence from top to down
if(true) { execute } else if() { done execute even if condition is true } else { done execute}
if(false) else if(check condition) { if true execute other wise go to next condition}
So on..
in your case, this is going to be solution
public static void test(int N) {
int enable_print = N % 10;
while (N > 0) {
if (enable_print == 0 && N % 10 != 0) {
enable_print = 1;
}
if (enable_print == 1) {
System.out.print(N % 10);
}
N = N / 10;
}
}

How to list of exponents when calculating Sum of power for a number

I am completely lost. I want to express one integer into a sum of power form in java. eg. 17= 2^4+2^0
But I don't know the way, Can someone please guide me.
I tried the following like power(17,2) but didn't work
int power(int num, int n)
{
if (n == 0)
return 1;
else if (n%2 == 0)
return power(num, n/2)*power(num, n/2);
else
return num*power(num, n/2)*power(num, n/2);
}
Edit: Now i understand the above function is not correct as I want a list of exponents in the result.
I may be misinterpreting your question, but from the method signature I assume you are asking how to implement power(num, exponent) recursively. If that's the case, the way to do it is to call the method recursively, multiplying the number by the (n - 1)th power until you reach n == 0, the recursion ends and the final result is returned.
int power(int num, int n) {
if (n < 0) {
throw new UnsupportedOperationException("Negative powers not supported");
} else if (n == 0) {
return 1;
} else {
return power(num, n - 1) * num;
}
}
Test it with:
System.out.println(power(5, 0));
System.out.println(power(5, 1));
System.out.println(power(5, 2));
System.out.println(power(5, 3));
Produces:
1
5
25
125

Fixing Exception in thread "main" java.lang.StackOverflowError

What is the reason of the StackOverflowError? I was trying it for a while but still can't get why it happens, and how to fix it.
The formula used in the code is a requirement.
public static long fib(long n){
if(n == 1 || n == 2)
return 1;
else if(n > 2 && n%2 == 0)//even
return fib((n/2+1)*(n/2+1)) - fib((n/2-1)*(n/2-1));
else //odd
return fib(((n+1)/2)*((n+1)/2)) + fib(((n-1)/2)*((n-1)/2));
}
public static void main(String[] args){
for(int i = 1; i <= 10; i++)
System.out.println(fib(i)+" ");
}
Not really sure what you are trying to achieve with your method, but by just adding some System.out.println to your code you can see what you are getting in each recursive call. For what I see your "odd" call is getting into an infinite loop and that is why you got that StackOverflowError error, Typically this is caused when your recursive functions doesn't have the correct termination condition, that is to say, it ends up calling itself forever.
For the way you start your code and the "fib" method name, it looks like you are trying to perform Fibonacci, which can be done in a simpler way:
public static int getFibonacci(int n) {
if (n == 1) {
return 1;
}
if (n == 2) {
return 1;
}
return getFibonacci(n - 1) + getFibonacci(n - 2);
}

Java boolean method needs extra return statement?

I am starting with java and while I was writing a way to identify whether a number was prime I wrote a method like this
public static boolean checkPrime(int n){
int x = 2;
while (((n % x) != 0) && (n > x)){
x = x + 1;
}
if(((n % x) == 0) && (n == x)){
return !Prime;
}
else if(((n % x) == 0) && (n > x)){
return Prime;
}
else {
return Prime;
}
}
What I couldn't figure out was the necessity of the last else statement. If I do not put it, I get an error message. However I don't think it is necessary since all possibilities are covered by the previous loops, with their respecting return statements. Or am I missing something?
You don't need the else. What you are being told by the compiler is the method must return SOMETHING. Your last else block could replaced by this:
return PrimeOrNot;
In fact, your method could look like this:
public static boolean checkPrime(int n){
int x = 2;
while (((n % x) != 0) && (n > x)){
x = x + 1;
}
if(((n % x) == 0) && (n == x)){
return !(PrimeOrNot);
}
return (PrimeOrNot);
}
In any case your very last statement block cannot be an else if.
The method has a return type of boolean.
The compiler is scared by the possibility in which none of the 'if' cases are met. In this situation, the method know what to return. This method needs to return something, so just give it a 'return true' before the method ends. It won't ever be read, but it will make the compiler happy.
The conditional expressions within the if/else-if are only evaluated at runtime. Normally, the compiler wouldn't know what the result would be, because they are not evaluated at compile-time. Only, situation when the compiler can figure what the result of the expression would be is when it's some compile-time constant (like if(true) {).
public static boolean checkPrime(int n){
boolean PrimeOrNot = false;
int x = 2;
while (((n % x) != 0) && (n > x)){
x = x + 1;
}
if(((n % x) == 0) && (n == x)){
return !(PrimeOrNot);
}
else if(((n % x) == 0) && (n > x)){
return (PrimeOrNot);
}
return PrimeOrNot;
}
A method which returns a value will be compilable if it returns a value in all its possible code paths.
Imagine for a moment that you're the compiler. You see this code:
int myMethod()
{
if (cond)
return anInt;
}
While you may know that cond is in fact always true, the compiler will not know that. It can only be sure about the result of a boolean expression if it is an expression which can be evaluated at compile time only.
Note that the vast majority of "code optimization" in Java is in fact done at run time (JIT: Just In Time).
The compiler only checks to see if there are valid return paths from your method. The compiler isn't "smart" enough to inspect the conditional statements and determine whether the conditions can be logically met -- the compiler simply checks to make sure that some value is returned to respect the contract of the method declaration.
Some would argue that the following is a cleaner structure for the method (but I think it is just a matter of taste):
public static boolean checkPrime(int n){
int x = 2;
while (((n % x) != 0) && (n > x)){
x = x + 1;
}
if(((n % x) == 0) && (n == x)){
return !(PrimeOrNot);
}
return (PrimeOrNot);
}

Categories

Resources