Can someone please explain how this recursion function works? [duplicate] - java

This question already has answers here:
How does Recursion actually work in Java?
(4 answers)
Closed 2 years ago.
I just started recursion and happened across a function similar to this, so I adapted it to the question on my homework:
public int getRSquare(int n){
if(n==0){
return 1;
}
return getRSquare(n-1)*n;
}
So when n=5, it returns 120, which is correct, this method outputs the factorial of n. But I don't understand WHY. Doesn't n eventually reach 0, so shouldn't it return 1, not 120? This is java btw, just in case that's needed

on the return value n is updated each time when the method is called. 5-1=4-1=3-1=2-1=2-1=0.
When n is 0 the method stop.

Related

Time complexity for tree [duplicate]

This question already has answers here:
How can I find the time complexity of an algorithm?
(10 answers)
Closed 3 months ago.
I'm having trouble finding the time complexity of a function.
This is the function:
int Mystery(Node root){
if(root==null)
return null;
if(root.leftchild==null)
return null;
return Mystery(root.leftchild)
}
what made me get confused is that there are 2 base conditions
this is what I wrote:
t(n)=t(n/2)+2 (2 stands for the 2 if conditions)
t(n)=t(n/4)+2+2
t(n)=t(n/8)+2+2+2
t(n)=t(n/2^k)+2*k
assume n/2^k=1
so k=log(n)
t(n)=t(1)+2log(n)
so O(log(n))
Is this correct or is there something missing?
There are just two base cases, but each of them imply that n has reached some small, bounded number -- assuming the tree is balanced.
So as long as the tree is balanced, your logic is correct.

puzzle 65-java puzzlers. int overflow can occur in calculations involving subtractions of negative numbers, int.Max_Value, etc How do you avoid it? [duplicate]

This question already has answers here:
How can I check if multiplying two numbers in Java will cause an overflow?
(15 answers)
Closed 1 year ago.
int ans = Integer.MAX_VALUE -(-1); //should I explicitly cast my method parameters in calculation to a wider bit type ?
Found a solution on searching the internet. An article that may help learners like me.
https://www.drdobbs.com/jvm/signalling-integer-overflows-in-java/210500001

Print current line number in intellij [duplicate]

This question already has answers here:
How can we print line numbers to the log in java
(22 answers)
Closed 3 years ago.
I am trying to print the current line number(line number of ide) of a certain code from intellij. How do I do that?
1 System.out.print(getCurrentLineNo());//sample code
2 System.out.print(getCurrentLineNo());//sample code
3 System.out.print(getCurrentLineNo());//sample code
4 System.out.print(getCurrentLineNo());//sample code
One way to do this is to get the stack trace element and call getLineNumber.
public static int getCurrentLineNumber() {
return Thread.currentThread().getStackTrace()[2].getLineNumber();
}
Note that I used an index of 2 because that refers to the frame of the method that calls getCurrentLineNumber. If you are just doing this inline, i.e. like this:
System.out.println(Thread.currentThread().getStackTrace()[1].getLineNumber());
You should use an index of 1 instead.
Note that an index of 0 refers to the frame of the getStackTrace method.
I suppose it too expensive to get StatckTrace for that, but it can help you for some debug.
System.out.println(new Exception().getStackTrace()[0].getLineNumber());
Also, you can look here
You can get line number like this...
public static int getCurrentLineNo() {
return Thread.currentThread().getStackTrace()[2].getLineNumber();
}

What does this lean of code mean? [duplicate]

This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 4 years ago.
capacity = (cap > 0) ? cap : CAPACITY;
I'm just looking through my lecture notes, and I can't figure out what this line of code does. Can someone help me?
It's called conditional expression and in your case it means that if cap is greater than 0, the capacity variable will get the value "cap", if false then it will get the value "CAPACITY".

What does the ++counter mean? [duplicate]

This question already has answers here:
++someVariable vs. someVariable++ in JavaScript
(7 answers)
Closed 7 years ago.
I don't consider myself to be bad at programming, but there's been something troubling me since the past few days.
int counter = 3;
++counter;
Is the following code above the same as counter++;.
It is similar, but not the same.
In your expression it doesn't matter, but if you had something more complicated, like System.out.println(counter++), it would make a big difference.
For example:
int counter = 3;
System.out.println(counter++)
This will print 3, then increment counter to 4.
However, if you do
int counter = 3;
System.out.println(++counter)
it will print 4 because it increments prior to giving the value as a parameter to the print function.
It's a question of when the increment is performed, the prefix performs it before other operations, postfix performs it after. They have different precedences.

Categories

Resources