It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Please explain how recursion works, in the simplest way you can.
As been pointed out, recursion is when a function calls itself. Here illustrated using factorial, where factorial(5) mathematically evalutates to values 5 * 4 * 3 * 2 * 1.
public int factorial(int x) {
if (x == 1) return 1;
else return x * factorial (x - 1);
}
// Try routine
factorial(3);
Which evaluates as
factorial(1) = 1 = 1
factorial(2) = 2 * factoral(1) = 2 * 1
factorial(3) = 3 * (2 * factorial(2) * (factorial(1)) = 3 * 2 * 1
...
Usually, it's a function that calculates one result itself, and calls itself to get the rest of the results.
For example, to get all positive numbers less or equal to 3, the function says, "One result is 3, and the rest of the results are all positive numbers less or equal to 2. Let's call myself with 2 and add that result to the one I calculated."
Of course, recursive functions need to be careful to have an "end condition" or they will never return a result. In the case of this example, the end condition would be when the function is called with 0, it should just return 0.
Here's a simple example of recursive method: -
public int recur(int count) {
if (count < 10) {
return count + recur(count++);
}
return count;
}
System.out.println(recur(0)); // Invoke first time
Recursion is a method which calls itself from within itself.
Example:
public void recur(int x)
recur(10);
end
Recursion is when a function calls itself:
int factorial(int integer)
{
if (integer == 1)
return 1;
else
return (integer*(factorial(integer-1)));
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
Given base and n that are both 1 or more, compute recursively (no
loops) the value of base to the n power, so powerN(3, 2) is 9 (3
squared).
The answer is
public int powerN(int base, int n) {
if(n == 1)
return base;
return base * powerN(base, n - 1);
}
I am confused because when I look at this because is this not saying multiply the base against the returned number of powerN()?
powerN(3,3);
= 3
= 3*powerN(base, n-1) = 6
= 3*powerN(base, n-1) = 9
Which would multiply 9*6*3?
I do not see why we would have to multiply the base by the function?
Should the method not just return the answer as the base never changes and once n==1 the base case executes
Let's calculate powerN(3,3) as you did.
powerN(3,3) = 3 * powerN(3,2)
3 * powerN(3,2) = 3 * 3 * powerN(3,1)
3 * 3 * powerN(3,1) = 3 * 3 * 3 = 27
So it was just wrong the way you calculated.
Let's say we have powerN(2,3)
powerN(2,3):
call#1> return 2*powerN(2,3-1)
call#2> return 2*powerN(2,2-1)
call#3> (n==1)return 2*1
So,
call#3 returns 2 to call#2 [call#2 is now returning 2*2=4]
call#2 returns 4 to call#1 [call#1 is now returning 2*4=8]
That's it.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a set =(1, 2, 3), and I need to get all possible subsets of set with different sequences (with repeating elements).The out put look:
1
2
3
1,2
1,3
2,1
2,3
3,1
3,2
1,2,3
1,3,2
2,1,3
2,3,1
3,1,2
3,2,1
please can someone help me with that? thxx
If you want subsets, then Google Guava has a method for you:
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Sets.html#powerSet(java.util.Set)
But in your example you have some duplicate sets (remember, sets are unordered). So you might want to get all the possible permutations of each subset as well:
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Collections2.html#permutations(java.util.Collection)
The problem can be solved by finding all combinations using bitwise operations.
The idea is: Generate all the subsets of a given array (set), this set is known as a power set .For each of the subset(Combination), find its permutation too.
Refer the following tutorials, to learn, how can you find all combinations using Bit Wise Operations. http://www.codechef.com/wiki/tutorial-bitwise-operations
void PERMUTE()
{
/*PERMUTATION FUNCTION THAT PERMUTES THE SUBSET ARRAY*/
}
public static void main(String[] args)
{
// TODO code application logic here
int Set[]={1,2,3};
int n=Set.length;
for (int i = 0; i <= (1 << n); ++i)
{
System.out.print("[");
int subsetSz=0;
int A[]=new int[100];
for (int j = 0; j < n; ++j)
{
if ((i & 1 << j)!=0)
{
System.out.print(Set[j]+",");
A[subsetSz++]=Set[j];
}
}
System.out.println("]");
/*Permute the subset*/
if(subsetSz>1)
{
PERMUTE(A);
}
}
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I need to print the first k digits and the last k digits in
n^n (n to the power of n, where n is an integer)
For example :
Input Output
n k First k digits Last k digits
4 2 --> 25 56
9 3 --> 387 489
I sense that it requires some clever mathematics, however I am unable to think of anything as such. Please suggest a way to approach the problem.
The last k digits are easy, you just need to calculate it modulo 10^k. To do this, after every multiplication, just apply the modulo, ie. intermediate_result %= 10^k.
Of course, you will need to calculate 10^k using some other method, because ^ does not mean power of in C or Java.
To find the first k digits, see first n digits of an exponentiation.
Thanks everyone for their help. My final code is
#include <stdio.h>
#include <math.h>
long int lastKdigits(long long n,int k)
{
long long i,res=1,div=pow(10,k);
for(i=1;i<=n;i++)
{
res=(res*n)%div;
}
return res;
}
long int firstKdigits(long long n,int k)
{
long double x, y;
x = n*log10(n);
y = floor(pow(10,x-floor(x) +k-1));
return ((int)y);
}
int main()
{
long long n;
int k;
scanf("%lld %d",&n,&k);
printf("%ld\t",firstKdigits(n,k));
printf("%ld\n",lastKdigits(n,k));
}
return 0;
}
For last k digits it's pretty easy you just need to calculate n^n (mod 10^k) but I don't know any solution for the other k diggits!
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
public void mystery1(int n) {
if (n <= 1) {
System.out.print(n);
} else {
mystery1(n / 2);
System.out.print(", " + n);
}
}
What gives this code for odd numbers. Becuase when we divide it it will not be an integer.
There is not mystery, because result of the integer division in Java is integer.
In Java or most other programming languages, when you divide an integer by an integer, the result will be an integer. If a decimal number occurs, say for example:
5/2=2.5
then, the number before the decimal point will be treated as the integer and 2 will be chosen.
In case you want to explicitly convert the integer into float or double, you can use any of the following conversions:
(float) 3/2;
(double) n/2;
The above explicitly converts it to a decimal.
n / 2, this is an integer division, where the fraction part will be ignored.
System.out.println(3/2); // prints 1
System.out.println(3.0/2); // prints 1.5
System.out.println(3/2.0); // prints 1.5
System.out.println(3.0/2.0); // prints 1.5
Param will rounded to int, for example if param will be 5, the next call the function will be with param 2
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
This is the code that I have a problem wit understanding. I know so far that this code here passes in int a and int b. Then it checks if int b is equal to 0 and if it is then it returns 1. Then it checks if b is 1 and if so then it returns int a. But I dont get the last part of this code. Its something with recursion but I dont get it.
public static int mystery(int a, int b) {
if(b == 0) {
return 1;
else if(b==1) {
return a;
return a * mystery(a,b-1);
This code looks like that for exponentiation where a is the base and b is the exponent. So in the case of b = 0, obviously, the answer would be 1 by the rules of exponentiation. When b = 1, then return a because a ^ 1 = a. Otherwise, use multiplication to get the result recursively where the base case for recursion is b = 1 (b = 0 condition will not be the base case if b > 0). By the way, you need to put proper parenthesis on the code for it to compile (the curly braces need to be closed).
i.e.,
public static int mystery(int a, int b) {
if(b == 0) {
return 1;
} else if(b==1) {
return a;
}
return a * mystery(a,b-1);
}
e.g.,
mystery(2, 6) returns 64 as the result.
Run it on paper:
mystery(10, 20)
b == 0 (false) b == 1 (false) so return 10 * mystery(10, 19)
mystery(10, 19)
boils down to return 10 * mystery(10, 18)
and so on.
Try with different values e.g. mystery(20, 10) mystery(5,5);