Q694: The Collatz Sequence

以下這個由Lothar Collatz定義的演算法可以產生一連串數列:

Step1:
任選一個正整數A作為這個數列的第一項。
Step2:
如果A=1則停止。
Step3:
如果A為偶數,則A=A/2然後重新回到Step2。
Step4:
如果A為奇數,則A=3*A+1然後重新回到Step2。

這個演算法已經被證明當首項小於等於 109時這個數列最終都會在Step2停止,但是有些A值在這個數列中會超出許多電腦的整數上限。在這個問題中我們想要計算這個數列的長度,而數列的終止有兩種情況:1.最終會在Step2停止或是 2.某一項會在Step4超出一個特定的上限。

Input

輸入包含許多組待測資料,每一列代表一組待測資料,每組待測資料包含兩個正整數,第一個數為首項A,第二個數為這個數列的上限L,無論A或L都不會大於2,147,483,647(32位元有號整數的最大值),且首項A總是小於上限L。當輸入為兩個負數時代表輸入結束。

Output

對每組待測資料必須輸出它為第幾組(從1開始),一個冒號,首項A的值,上限L的值,以及此一數列的項數。(請參考sample output)

Sample Input

 3 100
 34 100
 75 250
 27 2147483647
 101 304
 101 303
 -1 -1

Sample Output

 Case 1: A = 3, limit = 100, number of terms = 8
 Case 2: A = 34, limit = 100, number of terms = 14
 Case 3: A = 75, limit = 250, number of terms = 3
 Case 4: A = 27, limit = 2147483647, number of terms = 112
 Case 5: A = 101, limit = 304, number of terms = 26
 Case 6: A = 101, limit = 303, number of terms = 1