def ans(): result = 0 p_max = 0 for D in range(2, 1000 + 1): if is_square(D): continue limit = int(sqrt(D)) m = 0 d = 1 a = limit num1 = 1 num = a den1 = 0 den = 1 while num**2 - D * den**2 != 1: m = d * a - m d = (D - m * m) // d a = (limit + m) // d num2 = num1 num1 = num den2 = den1 den1 = den num = a * num1 + num2 den = a * den1 + den2 if num > p_max: p_max = num result = D return result
def handle_one_side(w1: str, w2: str, digits: List[int]) -> int: """ use w1 to create a number for w2 :param w1: first word :param w2: second word :param digits: digits list :return: a number found, -1 if not """ letters_values: Dict[str, int] = dict() digs_used: Set[int] = set() for index, c in enumerate(w1): dig = digits[index] # now set the value for the letter in c. # if there is already a letter with this value, or c already has a value different than dig, break if c in letters_values and letters_values[c] != dig: return -1 if c not in letters_values and dig in digs_used: return -1 # else, it is ok letters_values[c] = dig digs_used.add(dig) else: # if we did not break, calculate the value for the other word: other_word_num_list = [letters_values[c] for c in w2] # if it has a leading zero, go to next if other_word_num_list[0] == 0: return -1 other_word_num = list_to_num(other_word_num_list) if is_square(other_word_num): return other_word_num return -1
def sum_n_digits(num: int, n: int) -> int: """ :param num: input number :param n: number of digits :return: 0 is perfect square, else sum of first n digits of sqrt(num) """ if is_square(num): return 0 return digits_sum(square_root(num, n))
def ans(): """ using calculus, the min distance for a,b,c if sqrt(a^2+(b-c)^2) but we should also choose which of them is the a because a>=b>=c, it is better to put a outside so the squares are more balanced """ count = 0 big_m = 0 while count < 10**6: big_m += 1 a = big_m for bc in range(2, 2 * a + 1): x = bc**2 + a**2 if is_square(x): count += bc // 2 - max( bc - a - 1, 0) # add all pairs (b,c) such that b+c=bs, a>=b>=c>=1 return big_m
def square_root(num: int, digs: int) -> int: """ :param num: input number :param digs: number of wanted digits :return: first "digs" digits in the square root of num including digits before and after the decimal point """ if is_square(num): return int(sqrt(num)) res = 0 for i in range(digs): res *= 10 for j in range(1, 10): # add until we pass the square res += 1 np = num * 10**(2 * i) if res**2 > np: # too much, go back 1 and go for next digit to get more accurate res -= 1 break return res