Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.
I am not clear on above desription. what it mean by computing recursively?
We have a number of bunnies and each bunny has two big floppy ears. We want to compute the total number of ears across all the bunnies recursively (without loops or multiplication).
bunnyEars(0) → 0
bunnyEars(1) → 2
bunnyEars(2) → 4
The idea of calling one function from another immediately suggests the possibility of a function calling itself. The function-call mechanism in Java supports this possibility, which is known as recursion.
Your first recursive program. The "Hello, World" for recursion is the factorial function, which is defined for positive integers n by the equation
n!=n×(n−1)×(n−2)×…×2×1
n!=n×(n−1)×(n−2)×…×2×1
The quantity n! is easy to compute with a for loop, but an even easier method in Factorial.java is to use the following recursive function:
public static long factorial(int n) {
if (n == 1) return 1;
return n * factorial(n-1);
}
We can trace this computation in precisely the same way that we trace any sequence of function calls.
factorial(5)
factorial(4)
factorial(3)
factorial(2)
factorial(1)
return 1
return 2*1 = 2
return 3*2 = 6
return 4*6 = 24
return 5*24 = 120
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
How to get time difference in minutes and seconds only between 2 dates | 2 | 41 | |
Is this impact soundworks website to buy Logic Pro add-on's a shady site? | 2 | 30 | |
null output | 3 | 24 | |
Cygwin - GNU GPL License | 1 | 23 |
Join the community of 500,000 technology professionals and ask your questions.