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'm having a little troubling understanding the code below. I've worked out the vales for each of the variables for each loop and I understand how the values for each variable change after each loop but I'm confused about how int a = b; represents the sum of the two previous values. I was stuck on this problem for a long time and solved the problem only through trial and error.
I really don't understand how int a = b; represents the sum of the two previous values. I was convinced that since int c = a + b; sums both variable a and variable b that was the variable i wanted to print in my program. Can you explain how int a represents the sum of the two previous values and why int c does not.
public class Fibonacci extends ConsoleProgram{
public void run(){
int i = 0;
int a = 0;
int b = 1;
while ( i <= 12) {
println(a);
i++;
int c = a + b;
a = b;
b = c;
}
}
}
I like to think of it as a staircase:
0
0 + 1 = 1
1 + 1 = 2
1 + 2 = 3
2 + 3 = 5
3 + 5 = 8
5 + 8 = 13
An arbitrary step would look like:
a + b = c
b + c = d
After the one step, c acts like b and b acts like a. But what about a and d? Since your solution is iterative, you just say that a becomes d and repeat the process again in a loop:
a + b = c
| b + c = a
|___________|
Or in code:
int a = 0;
int b = 1;
int c = 0;
while (true) {
c = a + b; // `a + b = c` isn't valid, so you have to flip it around.
a = b; // `b` "becomes" `a`
b = c; // `c` "becomes" `b`
c = a; // You don't need this step because `c` is just a temp variable
}
So what happens in this program is:
a = 0, b = 1
c is set to their sum, = 1
a is set to b, = 1
b is set to c, = 1
a = 1, b = 1
c is set to their sum, = 2
a is set to b, = 1
b is set to c, = 2
a = 1, b = 2
c is set to their sum, = 3
a is set to b, = 2
b is set to c, = 3
a = 2, b = 3
c is set to their sum, = 5
a is set to b, = 3
b is set to c, = 5
a = 3, b = 5
... And so on. You should get the idea :)
Related
I came across a class that solves an exponent problem but I couldn't wrap my head around how the method raiseToPower() works. This is the line that I don't understand: resultVal = baseVal * raiseToPower(baseVal,exponentVal-1);
What is baseVal being multiplied by? raiseToPower(baseVal,exponentVal-1) doesn't seem like an expression to me. If baseVal == 2, than what is raiseToPower(baseVal,exponentVal-1)?
I know how to solve 2^3 in my head, I struggle to understand the steps baseVal * raiseToPower(baseVal,exponentVal-1) takes to solve the problem. I know that exponentVal is decremented by 1 each time raiseToPower() is invoked, but I still don't understand how it's holding a value that can be multiplied by baseVal.
I understand that this recursive method behaves like a loop.
public class ExponentMethod {
// calculates the result of raising a number to a power
public static int raiseToPower(int baseVal, int exponentVal) {
int resultVal; // holds the answer to the exponent problem
// sets resultVal to 1
if (exponentVal == 0) {
resultVal = 1;
}
else {
// calculate math problem
resultVal = baseVal * raiseToPower(baseVal,exponentVal-1);
}
// returns when exponentVal == 0
return resultVal;
}
public static void main (String [] args) {
int userBase;
int userExponent;
userBase = 2;
userExponent = 3;
System.out.println(userBase + "^" + userExponent + " = "
+ raiseToPower(userBase, userExponent));
}
}
// output
2^3 = 8
I am aware that a pow() method exists for raising a number to a power
Thanks,
The method is using recursion to raise a specific base to a certain power.
Let us take a simple example of 2^2 and run through the code:
raiseToPower(2, 2) is called
resultVal = 2 * raiseToPower(2, 2 - 1) is run
raiseToPower(2, 1) is called
resultVal = 2 * raiseToPower(2, 1 - 1) is run
raiseToPower(2, 0) is called
Base case is hit and we return 1
Now we go back up the chain!
resultVal = 2 * 1 and 2 is returned
resultVal = 2 * 2 and 4 is returned
So the end result for 2^2 is 4 as expected.
Another way to think about this is suppose someone already gave you the answer to 2^2, can you use that to calculate 2^3? Yes, you can simply do 2 * 2^2!
So: raisePower(2,3) = 2 * raisePower(2,2)
It is important to also have a base case (when power is 0, like in your example above) so that we don't run into an infinite loop! Hope this helps.
What you are missing is that this is a recursive method which calls itself. It continues to do so, storing intermediate results on the call stack until it starts to return, popping those values from the stack to form the answer. Sometimes, a print statement within the method can help you see what is happening.
public static void main(String[] args) {
System.out.println(raiseToPower(3,4));
}
public static int raiseToPower(int baseVal, int exponentVal) {
if (exponentVal == 0) {
return 1;
}
int x = baseVal * raiseToPower(baseVal, exponentVal-1);
System.out.println("exponentVal = " + exponentVal + ", + x = " + x);
return x;
}
prints
exponentVal = 1, + x = 3
exponentVal = 2, + x = 9
exponentVal = 3, + x = 27
exponentVal = 4, + x = 81
81
As the recursive call unwinds when the exponentVal == 0, here is what you get
x = 1;
x = x * baseVal; // x = baseVal
x = x * baseVal; // x = baseVal ^ 2
x = x * baseVal; // x = baseVal ^ 3
x = x * baseVal; // x = baseVal ^ 4
// return x or 81
Lets take example and understand :
baseValue = 2;
exponentialValue = 3;
How we can calculate pow(2,3) , there are ways of that :
baseValue^exponentialValue ---- 2^3 = 8
baseValue x baseValue^exponentialValue-1 ---- 2x2^2 = 8
It's called recursion. The same function is being called recursively with the exponent decreasing each time, multiplied with the base value and added into result. It would run like so:
I have the following code:
public static void main(String[] args) {
int a = 3;
int b = 7;
int x = b; // x=b
int res = a; // res = a
int y = 1;
int invariant = 0;
System.out.println("a|b|x|y|res|invariant");
while (x > 0) {
if (x % 2 == 0) {
y = 2 * y;
x = x / 2;
} else {
res = res + y;
y = 2 * y;
x = (x - 1) / 2;
}
invariant = y + 2;
String output = String.format("%d|%d|%d|%d|%d|%d", a,b,x,y,res,invariant);
System.out.println(output);
}
// < res = a + b >
}
Which gives the following output:
a|b|x|y|res|invariant
3|7|3|2|4|4
3|7|1|4|6|6
3|7|0|8|10|10
However, if I change the numbers, the invariant isn't equal to the res anymore. Therefore my loop invariant for this problem is not correct.
I'm struggling really hard to find the correct loop invariant and would be glad if there's any hint that someone can give me.
My first impression after looking into the code and my results is that the loop invariant changes based on a and b. Let's say both a and b are odd numbers as they are in my example, then my Loop invariant is correct (at least it seems like it)
Is it correct to assume a loop variant like the following?
< res = y - 2 && a % 2 != 0 && b % 2 != 0 >
I did use different numbers and it seems like anytime I change them there's a different loop invariant and I struggle to find any pattern whatsoever.
I would really appreciate if someone can give me a hint or a general idea on how to solve this.
Thanks
This loop computes the sum a+b.
res is initialized to a.
Then, in each iteration of the loop, the next bit of the binary representation of b (starting with the least significant bit) is added to res, until the loop ends and res holds a+b.
How does it work:
x is initialized to b. In each iteration you eliminate the least significant bit. If that bit is 0, you simply divide x by 2. If it's 1, you subtract 1 and divide by 2 (actually it would be sufficient to divide by 2, since (x-1)/2==x/2 when x is an odd int). Only when you encounter a 1 bit, you have to add it (multiplied by the correct power of 2) to the result. y Holds the correct power of 2.
In your a=3, b=7 example, the binary representation of b is 111
In the first iteration, the value of res is a + 1 (binary) == a + 1 = 4
In the second iteration, the value of res is a + 11 (binary) == a + 3 = 6
In the last iteration, the value of res is a + 111 (binary) == a + 7 == 10
You could write the invariant as:
invariant = a + (b & (y - 1));
This takes advantage of the fact the at the end of the i'th iteration (i starting from 1), y holds 2^i, so y - 1 == 2^i - 1 is a number whose binary representation is i 1 bits (i.e. 11...11 with i bits). When you & this number with b, you get the i least significant bits of b.
This question already has an answer here:
Order of operations for compound assignment operators in Java
(1 answer)
Closed 6 years ago.
int a = 2;
int b = 3;
int c = 10;
a +=(a += b) + c; // this will be same as --> a = a+((a= a+b) +c);
/* 2+((2=2+3)+10)
after adding 2+3
now value a = 5;
5 +((5)+10)
5 + (15)
a = 20
*/
System.out.println("a: "+a); // a:17
when i execute above program the answer is a:17 why not a:20 ?
according to me.
After resolving (a += b) now the value of a should be 5 then (a += b) + c would be 15 and after that a +=(a += b) + c; should be 20
please help me understand thanks.
The compound assignment operator stores the original value of the left operand before evaluating the second operand and performing the compound assignment (addition + assignment in this example).
Therefore
a += (a += b) + c;
is equivalent to :
int temp = a;
a = temp + (a += b) + c;
2 + 2 + 3 + 10 = 17
This is a good example of where order of evaluation and precedence don't match. They usually do which makes it confusing when there is a difference, as you mention the expression is evaluated left to right like this
int s = a; // stack
int t = a + b; // second stack value.
s += t;
s += c;
a = t;
a = s;
Note the (a+=b) is the same as (a+b) in this case.
In Java, you can replace a += b with a = a + b. And that is very important.
Hence your expression is equivalent to
a = a + (a = a + b) + c
Note that this will be evaluated as a = Term1 + Term2 + Term3 and in the order left to right.
Term1 is 2.
Term2 is the only tricky one. It is 5 (and has the side-effect of increasing a to 5 but that gets clobbered by the eventual assignment).
Term3 is 10.
That recovers the total, 17.
Note that the behaviour of this expression in C and C++ is undefined.
It is a bit tricky to understand. Although you have a += b on the right-hand side, that doesn't change the value of a that's used for the += at the beginning of the expression; that will still be 2. E.g.:
a += (a += b) + c;
a = 2 + (a[still 2] += 3) + 10
a = 2 + (5) + 10
a = 17
The a += b on the right-hand side is really just a a + b, since this is all on the right-hand side of a += already directed at a.
Details in JLS§15.26.2: Compound Assignment Operators.
First you do a+=b so 2+3 you have 5. Then a+= your answer (here 5) so 2+=5 = 7. Then 7+10 = 17
This question already has answers here:
Explain this dynamic programming climbing n-stair code
(3 answers)
Closed 6 years ago.
Question is: You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
And I saw a java code which is correct, but I don't understand the logic. Can anyone explain it to me? What's a,b,c stand of?
public int climbStairs(int n) {
if (n<2) return 1;
int a = 1;
int b = 1;
int c = 1;
for (int i=2; i<=n; i++){
c = b;
b = a + b;
a = c;
}
return b;
}
The code itself is basically a fibonacci-number generator.
int a = 1;
int b = 1;
int c = 1;
for (int i=2; i<=n; i++){
c = b;
b = a + b;
a = c;
}
creates the nth fibonacci-number, starting with 1 for n = 0.
So the more important question:
How does the number of possible ways correspond to the fibonacci-row?
For n = 0 and n = 1, the answer is pretty simple: there's exactly one way: don't move (0), take a single step (1). For any other n, we can use a recursive approach: there are two ways to reach step n: take a short step from n - 1 or a long step from n - 2. Which is the same as the fibonacci-sequence: fib(n + 2) = fib(n + 1) + fib(n).
The recursive formula is
f(n) = f(n-1) + f(n-2)
f(0) = f(1) = 1
Translated to a, b, c in the code
if (n<2) return 1;
int a = 1;
int b = 1;
int c = 1;
The above defines f(0) = f(1) = 1 and below calculates f(n) = f(n-1) + f(n-2) for n >= 2
for (int i=2; i<=n; i++){
c = b; // f(i-1) is temporary saved in c
b = a + b; // f(i-2) + f(i-1) is saved in b
a = c; // f(i-1) is saved in a for the next iteration
}
I had come across a question for which I am not able to find out why its output is coming as
7
when I calculate mathematically it can produce output as 7, 8 or any other number So my question is on what basis its coming as 7 only
interface InterfaceA
{
int A = InterfaceB.B * 2;
}
interface InterfaceB
{
int B = InterfaceC.C + 1;
}
interface InterfaceC extends InterfaceA
{
int C = A + 1;
}
public class TestInterface implements InterfaceA, InterfaceB, InterfaceC {
public static void main(String[] args) {
System.out.println(A + B + C);
}
}
Obviously code like this should never actually occur. It's horrendous. I don't think you should spend too much time worrying about why it gives 7, but it's actually not too hard to see why.
The first field value to be evaluated is InterfaceA.A, so the VM starts to initialize InterfaceA. That requires InterfaceB.B, so it starts to initialize InterfaceB. That requires InterfaceC.C, so it starts to initialize InterfaceC.
Now although InterfaceC.C refers to InterfaceA.A, the VM is already initializing InterfaceA, so it just proceeds regardless (as per section 12.4.2 of the JLS):
If the Class object for C indicates that initialization is in progress for C by the current thread, then this must be a recursive request for initialization. Release LC and complete normally.
So InterfaceA.A is still 0 (we're still trying to work out what value it should have, and 0 is the default value for int), and InterfaceC.C gets a value of 1 (0 + 1). Then InterfaceB.B gets a value of 2 (1 + 1), and InterfaceA.A gets a value of 4 (2 * 2).
Sum all of those field values, and you end up with 7.
If you use a different expression, you'll get a different value because you'll see a different interface being initialized last, although it only depends on the first field which you refer to:
A + B + C = 7 (A = 4, B = 2, C = 1)
A + C + B = 7 (A = 4, B = 2, C = 1)
B + A + C = 3 (A = 0, B = 2, C = 1)
B + C + A = 3 (A = 0, B = 2, C = 1)
C + A + B = 6 (A = 2, B = 1, C = 3)
C + B + A = 6 (A = 2, B = 1, C = 3)
(You have to replace the existing line of code of course, as this is about type initialization - if you just add more System.out.println lines you'll get the same answer for all the above expressions.)
System.out.println(A + B + C);
When you asked for A ( InterfaceB.B * 2;) , you need B
So, B needs to be resolve,
int B = InterfaceC.C + 1;
When you asked for B ( InterfaceC.C + 1) , you need C
So, C needs to be resolve,
int C = A + 1; // 0+1 =1
A is not yet resolved and default is 0
So int C is 1.
Now , You need B.
int B = InterfaceC.C + 1; // 1+1 =2
Now
int A = InterfaceB.B * 2; // 2*2 =4
Finally
1+2+4 =7
When you run the code,then execution starts from the main method.In main its System.out.println(A+B+C) and as you know control goes from right to left.
Here in this case C is executed first(Because it is present at extreme right) meaning c=A+1 but A is 0 so C=1.
Now B is present to left of C so B will be executed this means B=C+1 so B=2(because c is already 1)
As A is at extreme left so Now A is executed this means A=B*2
So finally its 4+2+1=7
Similarly if you print System.out.println(B+C+A); then you will get 3