def S(p1, a): b = int(10 ** numLen(p1)) x, _ = extendedGCD(a, b) x *= p1 # y *= p1 t = x // b x -= t * b # y += t * a return a * x
def S(p1, a): b = int(10**numLen(p1)) x, _ = extendedGCD(a, b) x *= p1 # y *= p1 t = x // b x -= t * b # y += t * a return a * x
def main(file): # trick: for every anagram (word1, word2) calculate the character swaps # required to turn word2 into word1 then perform those swaps on a square # number of the same length as word1 and see if the resulting number is # a square. Only caveat is that the eligible square numbers must have # at most one of each digit with open(file, 'r') as f: words = f.read().replace('"', '').split(',') # group the words into lists of anagrams anagrams = dict() for word in words: anagrams.setdefault(''.join(sorted(word)), []).append(word) # group the anagram lists by the length of the word anagramsByLen = dict() for k, v in anagrams.items(): if len(v) > 1: anagramsByLen.setdefault(len(k), []).append(v) # create eligible squares and group them by length squaresByLen = dict() for n in count(4): sq = n * n key = numLen(sq) if key not in anagramsByLen: break if key == len(set(digits(sq))): # no repeated digits squaresByLen.setdefault(key, []).append(sq) def results(): for k in squaresByLen: squares = squaresByLen[k] for words in anagramsByLen[k]: for word1, word2 in combinations(words, 2): sw = list(swaps(word1, word2)) for sq in squares: n = num(apply(digits(sq), sw)) if n in squares: yield max(n, sq) return max(results())
def main(): goodLast = lambda n: pandigital(n % 10**9) goodFirst = lambda n: pandigital(n // 10**(numLen(n) - 9)) good = lambda n: n > 123456789 and goodLast(n) and goodFirst(n) return next(k for k, n in enumerate(fibonaccis()) if good(n))
def dsum(n): n = pow(Decimal(n), Decimal('0.5')) n *= pow(10, 100 - numLen(int(n))) n = int(n) return sum(digits(n))
def main(limit): return next(n for n, f in enumerate(fibonaccis()) if numLen(f) >= limit)
def main(lim): return sum(numLen(n) > numLen(d) for n, d in islice(convergents2(), lim))
def main(): goodLast = lambda n: pandigital(n % 10 ** 9) goodFirst = lambda n: pandigital(n // 10 ** (numLen(n) - 9)) good = lambda n: n > 123456789 and goodLast(n) and goodFirst(n) return next(k for k, n in enumerate(fibonaccis()) if good(n))
def main(): # 10 ^ e has e + 1 digits, so max base is 9 # 9 ^ 22 has 21 digits, so 21 is the max exp return sum(numLen(pow(b, e)) == e for e in range(1, 22) for b in range(1, 10))
def main(): # 10 ^ e has e + 1 digits, so max base is 9 # 9 ^ 22 has 21 digits, so 21 is the max exp return sum( numLen(pow(b, e)) == e for e in range(1, 22) for b in range(1, 10))
def concat(a, b): return a * pow(10, numLen(b)) + b