def ans(): wanted_family_size: int = 8 i: int = 11 # starting number to calc comb: List[List[int]] = [[0]] # starting value for power set primes_checked: Dict[int, bool] = dict() # primes/non primes dict valid_units_digs: Dict[int, int] = { 1: 3, 3: 7, 7: 9, 9: 11 } # only valid units digits, each dig point to the next while True: for digits in comb: # for each subset of the digits indices if need_check(i, digits): # if this is the smallest number possible if family_size(i, digits, wanted_family_size, primes_checked) == wanted_family_size: # we found a solution return first_prime_in_family(i, digits, primes_checked) # increment i to the next number with a valid units digit temp: int = i i = (i - i % 10) + valid_units_digs[i % 10] if num_size(i) > num_size(temp): # if i is now a digit longer # extend the power set comb = sum([[c, c + [num_size(i) - 2]] for c in comb], [[num_size(i) - 2]])
def ans(): count = 0 for i in range(1, 10): n = 1 while num_size( i**n ) >= n: # number of digits sort of decreases the higher the power if num_size(i**n) == n: count += 1 n += 1 return count
def ans(): nums: List[int] = [3, 7] dens: List[int] = [2, 5] # calculate the fractions while len(nums) < 1000: nums.append(2 * nums[len(nums) - 1] + nums[len(nums) - 2]) dens.append(2 * dens[len(dens) - 1] + dens[len(dens) - 2]) count = 0 for i in range(len(nums)): if num_size(nums[i]) > num_size(dens[i]): count += 1 return count
def ans(): prev: int = 1 before_prev: int = 1 index: int = 2 limit: int = 1000 # count numbers until we get a number with at least 1000 digits while num_size(prev) < limit: index += 1 before_prev, prev = prev, before_prev + prev return index
def left_prime(x: int) -> bool: """ :param x: input number :return: True if input number is a left prime """ while x > 0: if not isprime(x): return False x = x % (10**(num_size(x) - 1)) return True
def create_nums(num=0) -> None: """ create all numbers with this property :param num: current number :return: None """ if num_size(num) == 10: # stop all_numbers.add(num) if num == 0: # add digs for i in range(1, 10): create_nums(i) else: for i in digits.difference(num_to_list(num)): # add possible digits if num_size(num * 10 + i) <= 3 or ( (num * 10 + i) % 1000) % primes_list[num_size(num * 10) - 4] == 0: create_nums(num * 10 + i)
def is_circular_prime(x: int) -> bool: """ :param x: input number :return: if x is a circular prime or not """ n_size = num_size(x) for i in range(n_size): x = x // 10 + (x % 10) * 10**(n_size - 1) if not isprime(x): return False return True
def rec(digs_left: Set[int], current_solution: List[int], all_solutions: Set[FrozenSet[int]]): """ :param digs_left: the digits left to handle in set :param current_solution: the current "set" :param all_solutions: all calculated solutions """ if len(digs_left) == 0: all_solutions.add(frozenset(current_solution)) start = 1 if len(current_solution) == 0 else num_size(current_solution[-1]) for i in range(start, len(digs_left) + 1): for c in combinations(digs_left, i): fs_c = frozenset(c) for p in get_primes_from_digits(fs_c): rec(digs_left.difference(c), current_solution + [p], all_solutions)
def ans(): max_n = 10**6 primes = sieve_primes(max_n) # for (2,3)-> 12, for (3,5)-> no solution, start from (5,7): primes = [int(p) for p in primes[2:]] # add one more prime because the given limit is for p1 primes.append(nextprime(primes[-1])) s_sum = 0 for p1, p2 in zip(primes, primes[1:]): """ let tp = 10**(number of digits of p1) so we want smallest s s.t.: s = k * tp + p1, and s % p2 = 0 -> k * tp = -p1 (mod p2) k * tp = p2 - p1 (mod p2) k = (tp ^ -1) * (p2 - p1) (mod p2) """ power_10 = 10**num_size(p1) k = (inverse_mod(power_10, p2) * (p2 - p1)) % p2 s = k * power_10 + p1 s_sum += s return s_sum
def ans(): # board: board_size = 40 square_count = [0] * board_size # number of iterations number_of_iterations = 10**5 # special squares cc_squares = [2, 17, 33] cc_squares_set = set(cc_squares) ch_squares = [7, 22, 36] ch_squares_set = set(ch_squares) r_squares = [5, 15, 25, 35] u_squares = [12, 28] g2j_squares = [30] jail_location = 10 go_location = 0 special_locations = set().union(cc_squares, ch_squares, g2j_squares) # card piles: cc_pile: List[Any] = [go_location, jail_location] cc_pile += [None] * (16 - len(cc_pile)) ch_pile: List[Any] = [ go_location, jail_location, 11, 24, 39, 5, "R", "R", "U", "M3" ] ch_pile += [None] * (16 - len(ch_pile)) random.shuffle(cc_pile) random.shuffle(ch_pile) # dice range: dice_max_number = 4 # player location player_location = 0 # function for card movement: def next_location(card: Union[str, int, None], curr_player_location: int) -> int: """ :param card: card gotten :param curr_player_location: current location :return: next location based on card """ if card is None: return curr_player_location if isinstance(card, str): if card == "R": next_loc = bisect_left(r_squares, curr_player_location) % len(r_squares) if r_squares[next_loc] == curr_player_location: next_loc = (next_loc + 1) % len(r_squares) return r_squares[next_loc] if card == "U": next_loc = bisect_left(u_squares, curr_player_location) % len(u_squares) if u_squares[next_loc] == curr_player_location: next_loc = (next_loc + 1) % len(u_squares) return u_squares[next_loc] if card == "M3": return (board_size + curr_player_location - 3) % board_size raise Exception("got an unknown card:", card) if isinstance(card, int): return card raise Exception("got an unknown card:", card) def is_in_special_location(curr_player_location: int) -> bool: """ :param curr_player_location: current location :return: True if current location is a special location """ return curr_player_location in special_locations def next_location_after_special_location(curr_player_location: int) -> int: """ :param curr_player_location: current location, knowing it is a special location :return: the new location from the current location """ if curr_player_location in g2j_squares: return jail_location if curr_player_location in cc_squares_set: # take one card card = cc_pile[0] # put it back at the end of the pile cc_pile.pop(0) cc_pile.append(card) # do what the card says: return next_location(card, curr_player_location) if curr_player_location in ch_squares_set: # take one card card = ch_pile[0] # put it back at the end of the pile ch_pile.pop(0) ch_pile.append(card) # do what the card says: return next_location(card, curr_player_location) else: raise Exception( "not in special location, yet in special location movement function", curr_player_location) # play the game: for _ in range(number_of_iterations): # arrived here, add 1 to this place square_count[player_location] += 1 # roll dice d1 = random.randint(1, dice_max_number) d2 = random.randint(1, dice_max_number) # go forward player_location += d1 + d2 player_location %= board_size prev_location = -1 while is_in_special_location( player_location) and player_location != prev_location: # may pull M3 card and go back to another special location prev_location = player_location player_location = next_location_after_special_location( player_location) # get stats of placing: final_stats = [] for i in range(board_size): final_stats.append((square_count[i] / number_of_iterations, i)) final_stats.sort() final_stats.reverse() three_most_visited = [ final_stats[0][1], final_stats[1][1], final_stats[2][1] ] # finish up result_str = "" for i in three_most_visited: if num_size(i) == 1: result_str += "0" + str(i) else: result_str += str(i) return result_str
def get_file_number(n: int) -> str: return "0" * (3 - num_size(n)) + str(n)