AP Computer Science A › Recursion
Which of the following is a recursive factorial function?
Recall that an example of a factorial is:
public long factorial(long a) {
if(a <= 1) {
return 1;
}
return a * factorial(a-1);
}
public long factorial(long a) {
a * a-1 * a-2 * a-3 * a-4;
}
public long factorial(long a) {
long ret = 1;
for(int i = 2; i < a; i++) {
ret *= i;
}
return ret;
}
public long factorial(long a) {
return a * factorial(a-1) * factorial(a-2);
}
None of the others
Recall that a recursive function calls itself. Therefore, you can eliminate several of the answers just by the fact that they are not recursive in nature. (Indeed the one with a loop is a correct computation.) Now, you can think of a factorial like this:
And then...
And so forth.
Thus, you have a stopping case at 1, which is equal to 1. For any other value, you will return a recursive value, namely the parameter a and then the factorial method called with a-1.
Which of the following is a recursive factorial function?
Recall that an example of a factorial is:
public long factorial(long a) {
if(a <= 1) {
return 1;
}
return a * factorial(a-1);
}
public long factorial(long a) {
a * a-1 * a-2 * a-3 * a-4;
}
public long factorial(long a) {
long ret = 1;
for(int i = 2; i < a; i++) {
ret *= i;
}
return ret;
}
public long factorial(long a) {
return a * factorial(a-1) * factorial(a-2);
}
None of the others
Recall that a recursive function calls itself. Therefore, you can eliminate several of the answers just by the fact that they are not recursive in nature. (Indeed the one with a loop is a correct computation.) Now, you can think of a factorial like this:
And then...
And so forth.
Thus, you have a stopping case at 1, which is equal to 1. For any other value, you will return a recursive value, namely the parameter a and then the factorial method called with a-1.
Which of the following is a recursive factorial function?
Recall that an example of a factorial is:
public long factorial(long a) {
if(a <= 1) {
return 1;
}
return a * factorial(a-1);
}
public long factorial(long a) {
a * a-1 * a-2 * a-3 * a-4;
}
public long factorial(long a) {
long ret = 1;
for(int i = 2; i < a; i++) {
ret *= i;
}
return ret;
}
public long factorial(long a) {
return a * factorial(a-1) * factorial(a-2);
}
None of the others
Recall that a recursive function calls itself. Therefore, you can eliminate several of the answers just by the fact that they are not recursive in nature. (Indeed the one with a loop is a correct computation.) Now, you can think of a factorial like this:
And then...
And so forth.
Thus, you have a stopping case at 1, which is equal to 1. For any other value, you will return a recursive value, namely the parameter a and then the factorial method called with a-1.
Which of the following is a recursive factorial function?
Recall that an example of a factorial is:
public long factorial(long a) {
if(a <= 1) {
return 1;
}
return a * factorial(a-1);
}
public long factorial(long a) {
a * a-1 * a-2 * a-3 * a-4;
}
public long factorial(long a) {
long ret = 1;
for(int i = 2; i < a; i++) {
ret *= i;
}
return ret;
}
public long factorial(long a) {
return a * factorial(a-1) * factorial(a-2);
}
None of the others
Recall that a recursive function calls itself. Therefore, you can eliminate several of the answers just by the fact that they are not recursive in nature. (Indeed the one with a loop is a correct computation.) Now, you can think of a factorial like this:
And then...
And so forth.
Thus, you have a stopping case at 1, which is equal to 1. For any other value, you will return a recursive value, namely the parameter a and then the factorial method called with a-1.
Which of the following is a recursive factorial function?
Recall that an example of a factorial is:
public long factorial(long a) {
if(a <= 1) {
return 1;
}
return a * factorial(a-1);
}
public long factorial(long a) {
if(a <= 1) {
return 1;
}
return a * factorial(a-1);
}
public long factorial(long a) {
a * a-1 * a-2 * a-3 * a-4;
}
public long factorial(long a) {
a * a-1 * a-2 * a-3 * a-4;
}
public long factorial(long a) {
long ret = 1;
for(int i = 2; i < a; i++) {
ret *= i;
}
return ret;
}
public long factorial(long a) {
long ret = 1;
for(int i = 2; i < a; i++) {
ret *= i;
}
return ret;
}
public long factorial(long a) {
return a * factorial(a-1) * factorial(a-2);
}
public long factorial(long a) {
return a * factorial(a-1) * factorial(a-2);
}
None of the others
None of the others
Recall that a recursive function calls itself. Therefore, you can eliminate several of the answers just by the fact that they are not recursive in nature. (Indeed the one with a loop is a correct computation.) Now, you can think of a factorial like this:
And then...
And so forth.
Thus, you have a stopping case at 1, which is equal to 1. For any other value, you will return a recursive value, namely the parameter a and then the factorial method called with a-1.
int lairotcaf (int n) {
if (n <= 1){
return 1
}
temp = n * lairotcaf(n-1)
return temp;
}
int num;
cin >> num;
``
if (num >= 0){
lairotcaf(n)
} else {
cout << "must use number greater or equal to zero"< endl;
}
What is returned by lairotcaf(5)
?
In recursion, a value is calculated in reverse until a point at which an answer is defined. From that point, the definition is used to calculate forward, evaluating the other definitions which rely upon that base condition.
Recursive functions must have a test that will halt recursion. If the current state of the recursive function matches the base condition, the recursion should stop. Otherwise, recursion should continue.
In case you did not notice from the backwards-naming scheme, this is a definition for the factorial function. The function will not be called if a negative number is passed. If 0 or 1 is passed, 1 is returned. 0 or 1 is the base condition. If the number is greater than one, a chain of calls to lairotcaf()
will initiate until a base condition is reached.
Once lairotcaf(5)
is expanded, we have:
int lairotcaf (int n) {
if (n <= 1){
return 1
}
temp = n * lairotcaf(n-1)
return temp;
}
int num;
cin >> num;
``
if (num >= 0){
lairotcaf(n)
} else {
cout << "must use number greater or equal to zero"< endl;
}
What is returned by lairotcaf(5)
?
In recursion, a value is calculated in reverse until a point at which an answer is defined. From that point, the definition is used to calculate forward, evaluating the other definitions which rely upon that base condition.
Recursive functions must have a test that will halt recursion. If the current state of the recursive function matches the base condition, the recursion should stop. Otherwise, recursion should continue.
In case you did not notice from the backwards-naming scheme, this is a definition for the factorial function. The function will not be called if a negative number is passed. If 0 or 1 is passed, 1 is returned. 0 or 1 is the base condition. If the number is greater than one, a chain of calls to lairotcaf()
will initiate until a base condition is reached.
Once lairotcaf(5)
is expanded, we have:
int lairotcaf (int n) {
if (n <= 1){
return 1
}
temp = n * lairotcaf(n-1)
return temp;
}
int num;
cin >> num;
``
if (num >= 0){
lairotcaf(n)
} else {
cout << "must use number greater or equal to zero"< endl;
}
What is returned by lairotcaf(5)
?
In recursion, a value is calculated in reverse until a point at which an answer is defined. From that point, the definition is used to calculate forward, evaluating the other definitions which rely upon that base condition.
Recursive functions must have a test that will halt recursion. If the current state of the recursive function matches the base condition, the recursion should stop. Otherwise, recursion should continue.
In case you did not notice from the backwards-naming scheme, this is a definition for the factorial function. The function will not be called if a negative number is passed. If 0 or 1 is passed, 1 is returned. 0 or 1 is the base condition. If the number is greater than one, a chain of calls to lairotcaf()
will initiate until a base condition is reached.
Once lairotcaf(5)
is expanded, we have:
public void draw() {
recurs(10);
}
void recurs(int count){
if (count == 0)
return;
else {
System.out.print(count + " ");
int recount = count - 2;
recurs(recount);
return;
}
}
What is the output of the code?
10 8 6 4 2
10 8 6 4 2 0
8 6 4 2 0
8 6 4 2
error, infinite loop
The number 10 is inputed into recurs and prints count,subtracts two from count, then rentered into recurs.
Thus, the first iteration gives, 10.
The second iteration gives as, 10, 8.
It is written this way because it places 8 after the value from the first iteration.
Continuing in this fashion we get,
10 8 6 4 2.
When count reaches 0, nothing is printed.
public void draw() {
recurs(10);
}
void recurs(int count){
if (count == 0)
return;
else {
System.out.print(count + " ");
int recount = count - 2;
recurs(recount);
return;
}
}
What is the output of the code?
10 8 6 4 2
10 8 6 4 2 0
8 6 4 2 0
8 6 4 2
error, infinite loop
The number 10 is inputed into recurs and prints count,subtracts two from count, then rentered into recurs.
Thus, the first iteration gives, 10.
The second iteration gives as, 10, 8.
It is written this way because it places 8 after the value from the first iteration.
Continuing in this fashion we get,
10 8 6 4 2.
When count reaches 0, nothing is printed.