UVa 944

From Algorithmist
Jump to navigation Jump to search

944 - Happy Numbers[edit]

Summary[edit]

Read two positive integers between 1 and 99999 (inclusive) each; the first integer, L, is the low limit of the closed interval; the second one, H, is the high limit (L ≤ H). The output is composed of the happy numbers that lie in the interval [L,H], together with the number of iterations required for the corresponding sequences of squares to reach 1.

Explanation[edit]

Instructions are clear. If you start with


If you reach 1 then is Happy else it's Unhappy!

Gotcha's[edit]

  • is unhappy when you meet a again and it means a loop, so it never meets 1.
  • ``if is happy (unhappy), then any number in the sequence will also be happy (unhappy) , attention , if you know is happy (unhappy) don’t waste time and terminate it because the rest of sequence will be happy (unhappy).
  • the number of iterations for ,if you meet a happy number in the sequence , simply add the size of current sequence to the number of iterations for , then = ( the number of iterations for ) - 1 and so on.

Input[edit]

7 10
1 7
10 4
10 10
1 1
99970 99999

Output[edit]

7 6
10 2

1 1
7 6

10 2

1 1

99971 7
99973 4
99978 8
99987 8