MySQL Blog
http://mysqlearner.blogspot.com/
Haani Naz wrote:Hi guys,
Reading up on some the loops to get a firm understanding. Can somebody please clarify the following for me:
In the following examples which do you think best decribes the result:
result: will finish
result: sometimes runs forever
result: unknown
(1).
The above looks like it will finish.
(2).
(3).
I'm struggling with the last two. Can somebody shed some light on this? thanks.
Jeff Verdegan wrote:I will say, though, that the last one looks like it might be the Collatz conjecture.
Haani Naz wrote:
Jeff Verdegan wrote:I will say, though, that the last one looks like it might be the Collatz conjecture.
Thanks did a look up on that and i found my answer. Will focus on the 2nd one now!
Jeff Verdegan wrote:
Haani Naz wrote:
Jeff Verdegan wrote:I will say, though, that the last one looks like it might be the Collatz conjecture.
Thanks did a look up on that and i found my answer. Will focus on the 2nd one now!
Be careful though. If you start Collatz with, say 987654321 on paper you will NOT get the same next value as you will in Java.
Also, did you figure out the case where the first one never finishes?
Jeff Verdegan wrote:
(1).
The above looks like it will finish.
There is exactly one case where it will run forever.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
What does that mean about its terminating in Java™?Jeff Verdegan wrote: . . . If you start Collatz with, say 987654321 on paper you will NOT get the same next value as you will in Java. . . .
Campbell Ritchie wrote:What about No 2: What if you start with n = 2000000000;? What will happen then?
Go through the execution with pencil and paper for 32 iterations and tell us the values of i and n at that stage. Then work out whether the loop will terminate.
How did you get 2147483648? There is no such int. Have you missed out a -?Haani Naz wrote: . . .
i was 2147483648
n was 2000000031 . . .
I still think a pencil and paper would have been better. I also think you only ran it 31 times. Without running the program again, work out what the value of i would be after 32 iterations. Then you can work out whether the loop will terminate.Haani Naz wrote: . . . I did one better, i ran the program. . . .
Campbell Ritchie wrote:
What does that mean about its terminating in Java™?Jeff Verdegan wrote: . . . If you start Collatz with, say 987654321 on paper you will NOT get the same next value as you will in Java. . . .
Haani Naz wrote:
Jeff Verdegan wrote:
Also, did you figure out the case where the first one never finishes?
Nah Jeff, i can't seem to figure it out. feel kinda stupid now lol.
Haani Naz wrote:
In the following examples which do you think best decribes the result:
result: will finish
result: sometimes runs forever
result: unknown
(1).
Haani Naz wrote:So i believe the 2nd one (below) will finish. Seeing how n only increments by 1 and i will multiplication of its own value - at some point it will be greater than n. I am right correct?
Jeff Verdegan wrote:
Haani Naz wrote:So i believe the 2nd one (below) will finish. Seeing how n only increments by 1 and i will multiplication of its own value - at some point it will be greater than n. I am right correct?
Yes, it will finish, but your reasoning applies more to actual integers than to Java ints or longs. (And, like I said, unless you come back and tell me otherwise, I'm going to assume we're dealing with Java primtives here.)
One very simple way to answer this question is to keep in mind that multiplying an int by 2 is the same as doing a bitwise left-shift of 1.
Think about what happens to the bit pattern when you do that. Better yet, follow Campbell's suggestion and work it out on pencil and paper. Do it with a single 8-bit byte to make your life simpler.
We find this kind of rampant individuality very disturbing. But not this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|