
__lowerCAmelCase					:			List[str]            =		[sum(int(c, 10) ** 2 for c in i.__str__()) for i in range(100_000)]


def     UpperCAmelCase_  (						__lowerCAmelCase	) ->						int:
    __lowercase		:			Any	    =       0
    while number:
        # Increased Speed Slightly by checking every 5 digits together.
        sum_of_digits_squared += DIGITS_SQUARED[number % 100_000]
        number //= 100_000

    return sum_of_digits_squared


# There are 2 Chains made,
# One ends with 89 with the chain member 58 being the one which when declared first,
# there will be the least number of iterations for all the members to be checked.

# The other one ends with 1 and has only one element 1.

# So 58 and 1 are chosen to be declared at the starting.

# Changed dictionary to an array to quicken the solution
__lowerCAmelCase					:			list[bool | None]            =		[None] * 10_000_000
__lowerCAmelCase					:			int            =		True
__lowerCAmelCase					:			int            =		False


def     UpperCAmelCase_  (						__lowerCAmelCase	) ->						bool:

    if CHAINS[number - 1] is not None:
        return CHAINS[number - 1]  # type: ignore

    __lowercase		:			Optional[Any]	    =       chain(next_number(__lowerCAmelCase	)	)
    __lowercase		:			Tuple	    =       number_chain

    while number < 10_000_000:
        __lowercase		:			Optional[int]	    =       number_chain
        number *= 10

    return number_chain


def     UpperCAmelCase_  (						__lowerCAmelCase = 10_000_000	) ->						int:
    for i in range(1	,					__lowerCAmelCase	):
        if CHAINS[i] is None:
            chain(i + 1	)

    return CHAINS[:number].count(__lowerCAmelCase	)


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 print(F'{solution() = }')




import unittest
from queue import Empty
from threading import Thread

from transformers import AutoTokenizer, TextIteratorStreamer, TextStreamer, is_torch_available
from transformers.testing_utils import CaptureStdout, require_torch, torch_device

from ..test_modeling_common import ids_tensor


if is_torch_available():
 import torch

 from transformers import AutoModelForCausalLM


@require_torch
class 						__lowerCAmelCase	(	unittest.TestCase      ):



      """simple docstring"""







      def 		snake_case_			(      self		:       Union[str, Any]				):
          __lowercase		:			Optional[Any]	    =       AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''				)
          __lowercase		:			Any	    =       AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''				).to(_snake_case				)
          __lowercase		:			List[str]	    =       -1

          __lowercase		:			Optional[Any]	    =       ids_tensor((1, 5)				,       vocab_size=model.config.vocab_size				).to(_snake_case				)
          __lowercase		:			Tuple	    =       model.generate(_snake_case				,       max_new_tokens=10				,       do_sample=_snake_case				)
          __lowercase		:			Optional[int]	    =       tokenizer.decode(greedy_ids[0]				)

          with CaptureStdout() as cs:
              __lowercase		:			Optional[Any]	    =       TextStreamer(_snake_case				)
              model.generate(_snake_case				,       max_new_tokens=10				,       do_sample=_snake_case				,       streamer=_snake_case				)
          # The greedy text should be printed to stdout, except for the final "\n" in the streamer
          __lowercase		:			List[Any]	    =       cs.out[:-1]

          self.assertEqual(_snake_case				,       _snake_case				)




      def 		snake_case_			(      self		:       Optional[int]				):
          __lowercase		:			Optional[int]	    =       AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''				)
          __lowercase		:			Optional[Any]	    =       AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''				).to(_snake_case				)
          __lowercase		:			List[str]	    =       -1

          __lowercase		:			Tuple	    =       ids_tensor((1, 5)				,       vocab_size=model.config.vocab_size				).to(_snake_case				)
          __lowercase		:			List[Any]	    =       model.generate(_snake_case				,       max_new_tokens=10				,       do_sample=_snake_case				)
          __lowercase		:			Optional[Any]	    =       tokenizer.decode(greedy_ids[0]				)

          __lowercase		:			int	    =       TextIteratorStreamer(_snake_case				)
          __lowercase		:			int	    =       {'''input_ids''': input_ids, '''max_new_tokens''': 10, '''do_sample''': False, '''streamer''': streamer}
          __lowercase		:			Optional[Any]	    =       Thread(target=model.generate				,       kwargs=_snake_case				)
          thread.start()
          __lowercase		:			Optional[int]	    =       ''''''
          for new_text in streamer:
              streamer_text += new_text

          self.assertEqual(_snake_case				,       _snake_case				)




      def 		snake_case_			(      self		:       List[str]				):
          __lowercase		:			Dict	    =       AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''				)
          __lowercase		:			int	    =       AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''				).to(_snake_case				)
          __lowercase		:			List[Any]	    =       -1

          __lowercase		:			List[Any]	    =       ids_tensor((1, 5)				,       vocab_size=model.config.vocab_size				).to(_snake_case				)
          __lowercase		:			str	    =       model.generate(_snake_case				,       max_new_tokens=10				,       do_sample=_snake_case				)
          __lowercase		:			List[Any]	    =       greedy_ids[:, input_ids.shape[1] :]
          __lowercase		:			Optional[Any]	    =       tokenizer.decode(new_greedy_ids[0]				)

          with CaptureStdout() as cs:
              __lowercase		:			int	    =       TextStreamer(_snake_case				,       skip_prompt=_snake_case				)
              model.generate(_snake_case				,       max_new_tokens=10				,       do_sample=_snake_case				,       streamer=_snake_case				)
          # The greedy text should be printed to stdout, except for the final "\n" in the streamer
          __lowercase		:			int	    =       cs.out[:-1]

          self.assertEqual(_snake_case				,       _snake_case				)




      def 		snake_case_			(      self		:       Any				):
          # Tests that we can pass `decode_kwargs` to the streamer to control how the tokens are decoded. Must be tested
          # with actual models -- the dummy models' tokenizers are not aligned with their models, and
          # `skip_special_tokens=True` has no effect on them
          __lowercase		:			Dict	    =       AutoTokenizer.from_pretrained('''distilgpt2'''				)
          __lowercase		:			List[str]	    =       AutoModelForCausalLM.from_pretrained('''distilgpt2'''				).to(_snake_case				)
          __lowercase		:			str	    =       -1

          __lowercase		:			Dict	    =       torch.ones((1, 5)				,       device=_snake_case				).long() * model.config.bos_token_id
          with CaptureStdout() as cs:
              __lowercase		:			Optional[Any]	    =       TextStreamer(_snake_case				,       skip_special_tokens=_snake_case				)
              model.generate(_snake_case				,       max_new_tokens=1				,       do_sample=_snake_case				,       streamer=_snake_case				)

          # The prompt contains a special token, so the streamer should not print it. As such, the output text, when
          # re-tokenized, must only contain one token
          __lowercase		:			Dict	    =       cs.out[:-1]  # Remove the final "\n"
          __lowercase		:			Any	    =       tokenizer(_snake_case				,       return_tensors='''pt'''				)
          self.assertEqual(streamer_text_tokenized.input_ids.shape				,       (1, 1)				)




      def 		snake_case_			(      self		:       Any				):
          __lowercase		:			List[str]	    =       AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''				)
          __lowercase		:			Union[str, Any]	    =       AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''				).to(_snake_case				)
          __lowercase		:			Dict	    =       -1

          __lowercase		:			List[Any]	    =       ids_tensor((1, 5)				,       vocab_size=model.config.vocab_size				).to(_snake_case				)
          __lowercase		:			Tuple	    =       TextIteratorStreamer(_snake_case				,       timeout=0.0_01				)
          __lowercase		:			Tuple	    =       {'''input_ids''': input_ids, '''max_new_tokens''': 10, '''do_sample''': False, '''streamer''': streamer}
          __lowercase		:			Optional[int]	    =       Thread(target=model.generate				,       kwargs=_snake_case				)
          thread.start()

          # The streamer will timeout after 0.001 seconds, so an exception will be raised
          with self.assertRaises(_snake_case				):
              __lowercase		:			List[Any]	    =       ''''''
              for new_text in streamer:
                  streamer_text += new_text


