



'''simple docstring'''
import random
from typing import List, Optional, Union

import numpy as np
import torch

from ..state import AcceleratorState
from .constants import CUDA_DISTRIBUTED_TYPES
from .dataclasses import DistributedType, RNGType
from .imports import is_npu_available, is_tpu_available, is_xpu_available


if is_tpu_available(check_device=False):
					import torch_xla.core.xla_model as xm






def A__					(  UpperCAmelCase_						,						UpperCAmelCase_ = False							):
						if device_specific:
												seed += AcceleratorState().process_index
						random.seed(UpperCAmelCase_							)
						np.random.seed(UpperCAmelCase_							)
						torch.manual_seed(UpperCAmelCase_							)
						if is_xpu_available():
												torch.xpu.manual_seed_all(UpperCAmelCase_							)
						elif is_npu_available():
												torch.npu.manual_seed_all(UpperCAmelCase_							)
						else:
												torch.cuda.manual_seed_all(UpperCAmelCase_							)
						# ^^ safe to call this function even if cuda is not available
						if is_tpu_available():
												xm.set_rng_state(UpperCAmelCase_							)






def A__					(  UpperCAmelCase_ = None						,						UpperCAmelCase_ = None							):
						# Get the proper rng state
						if rng_type == RNGType.TORCH:
												_UpperCamelCase      :	List[str]				       =     torch.get_rng_state()
						elif rng_type == RNGType.CUDA:
												_UpperCamelCase      :	Tuple				       =     torch.cuda.get_rng_state()
						elif rng_type == RNGType.XLA:
												assert is_tpu_available(), "Can't synchronize XLA seeds on an environment without TPUs."
												_UpperCamelCase      :	Tuple				       =     torch.tensor(xm.get_rng_state()							)
						elif rng_type == RNGType.NPU:
												assert is_npu_available(), "Can't synchronize NPU seeds on an environment without NPUs."
												_UpperCamelCase      :	List[str]				       =     torch.npu.get_rng_state()
						elif rng_type == RNGType.XPU:
												assert is_xpu_available(), "Can't synchronize XPU seeds on an environment without XPUs."
												_UpperCamelCase      :	Tuple				       =     torch.xpu.get_rng_state()
						elif rng_type == RNGType.GENERATOR:
												assert generator is not None, "Need a generator to synchronize its seed."
												_UpperCamelCase      :	Union[str, Any]				       =     generator.get_state()

						# Broadcast the rng state from device 0 to other devices
						_UpperCamelCase      :	Optional[Any]				       =     AcceleratorState()
						if state.distributed_type == DistributedType.TPU:
												_UpperCamelCase      :	List[Any]				       =     rng_state.to(xm.xla_device()							)
												xm.collective_broadcast([rng_state]							)
												xm.mark_step()
												_UpperCamelCase      :	List[str]				       =     rng_state.cpu()
						elif (
						    state.distributed_type in CUDA_DISTRIBUTED_TYPES
						    or state.distributed_type == DistributedType.MULTI_NPU
						    or state.distributed_type == DistributedType.MULTI_XPU
						):
												_UpperCamelCase      :	Tuple				       =     rng_state.to(state.device							)
												torch.distributed.broadcast(UpperCAmelCase_						,						0							)
												_UpperCamelCase      :	Any				       =     rng_state.cpu()
						elif state.distributed_type == DistributedType.MULTI_CPU:
												torch.distributed.broadcast(UpperCAmelCase_						,						0							)

						# Set the broadcast rng state
						if rng_type == RNGType.TORCH:
												torch.set_rng_state(UpperCAmelCase_							)
						elif rng_type == RNGType.CUDA:
												torch.cuda.set_rng_state(UpperCAmelCase_							)
						elif rng_type == RNGType.NPU:
												torch.npu.set_rng_state(UpperCAmelCase_							)
						elif rng_type == RNGType.XPU:
												torch.xpu.set_rng_state(UpperCAmelCase_							)
						elif rng_type == RNGType.XLA:
												xm.set_rng_state(rng_state.item()							)
						elif rng_type == RNGType.GENERATOR:
												generator.set_state(UpperCAmelCase_							)






def A__					(  UpperCAmelCase_						,						UpperCAmelCase_ = None							):
						for rng_type in rng_types:
												synchronize_rng_state(RNGType(UpperCAmelCase_							)						,						generator=UpperCAmelCase_							)







'''simple docstring'''
import math






def A__					(  UpperCAmelCase_							):

						if 1 < number < 4:
												# 2 and 3 are primes
												return True
						elif number < 2 or number % 2 == 0 or number % 3 == 0:
												# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
												return False

						# All primes number are in format of 6k +/- 1
						for i in range(5						,						int(math.sqrt(UpperCAmelCase_							) + 1							)						,						6							):
												if number % i == 0 or number % (i + 2) == 0:
																		return False
						return True






def A__					(  UpperCAmelCase_ = 1_0_0_0						,						UpperCAmelCase_ = 1_0_0_0							):
						_UpperCamelCase      :	str				       =     [0, 0, 0]  # length, a, b
						for a in range((a_limit * -1) + 1						,						UpperCAmelCase_							):
												for b in range(2						,						UpperCAmelCase_							):
																		if is_prime(UpperCAmelCase_							):
																								_UpperCamelCase      :	List[str]				       =     0
																								_UpperCamelCase      :	Union[str, Any]				       =     0
																								while is_prime((n**2) + (a * n) + b							):
																														count += 1
																														n += 1
																								if count > longest[0]:
																														_UpperCamelCase      :	Any				       =     [count, a, b]
						_UpperCamelCase      :	Dict				       =     longest[1] * longest[2]
						return ans


if __name__ == "__main__":
					print(solution(1000, 1000))


