def cyclic(l): if digits(l[0])[:2] != digits(l[-1])[2:]: return False for i in range(1,len(l)): if digits(l[i])[:2] != digits(l[i-1])[2:]: return False return True
def seq(k): for m in range(1, k + 4): for p in palins(range(10), m): if p[0] == 0: continue n = num(p) for d in primes.divisors(n): if len(digits(d)) == k and len(digits(n // d)) == k: yield n
def problem30(n): """ >>> problem30(200000) 443839 """ return sum([i for i in range(100, n)\ if i == sum(map(lib.call_power, map(lambda x: (x, 5), lib.digits(i))))])
def increasing(n): s = digits(n) for i in range(len(s) - 1): if int(s[i]) > int(s[i+1]): return False return True
def problem20(n): """ >>> problem20(100) 648 """ value = lib.factorial(n) return sum(lib.digits(value))
def problem16(base, pow): """ >>> problem16(2, 1000) 1366 """ value = lib.power(base, pow) return sum(lib.digits(value))
def good(p): if p < 10: return False d = digits(p) for i in range(1, len(d)): if not primes.isPrime(num(d[:i])): return False if not primes.isPrime(num(d[i:])): return False return True
def cycle_length(n): seen = {n:0} i = 0 while True: n = sum(map(fact,map(int,digits(n)))) i += 1 if n in seen: return i else: seen[n] = i
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)
def f89(n): if n == 89: return True elif n <= 1: return False else: d = digits(n) d = list(map(int,d)) d = list(map(sq,d)) d = sum(d) return f89(d)
def seq(): yield 81 mem = [] push = lambda b, e: heappush(mem, (b ** e, b, e)) push(2, 3) while True: n, b, e = heappop(mem) if b == sum(digits(n)): yield n if e == 3: push(b + 1, 3) push(b, e + 1)
def main(limit): sq = [d * d for d in range(10)] mem = [None] * limit mem[0] = False mem[1] = False mem[89] = True for n in range(limit): m = n while mem[m] == None: m = sum(sq[d] for d in digits(m)) mem[n] = mem[m] return sum(mem)
def seq(): yield 81 mem = [] push = lambda b, e: heappush(mem, (b**e, b, e)) push(2, 3) while True: n, b, e = heappop(mem) if b == sum(digits(n)): yield n if e == 3: push(b + 1, 3) push(b, e + 1)
def main(indices): index = 0 pos = 1 rv = 1 for n in count(1): d = digits(n) while indices[index] - pos < len(d): rv *= d[indices[index] - pos] index += 1 if index == len(indices): return rv pos += len(d) return rv
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())
#!/usr/bin/env python3 from lib import digits equal = False x = 1 while not equal: equal = True dig = sorted(digits(x)) for mul in range(2,7): if dig != sorted(digits(mul*x)): equal = False x = x + 1 print(x-1)
def main(lim): return max(sum(digits(pow(a, b))) for a in range(lim) for b in range(lim))
def seq(n, lim=54): for _ in range(lim): n += num(reversed(digits(n))) yield n
#!/usr/bin/env python3 from lib import digits cubes = {} i = 1 while True: if "".join(sorted(digits(i**3))) not in cubes: cubes["".join(sorted(digits(i**3)))] = [] cubes["".join(sorted(digits(i**3)))].append(i) if len(cubes["".join(sorted(digits(i**3)))]) == 5: print(cubes["".join(sorted(digits(i**3)))]) break i += 1 ##print(cubes)
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 bouncy(n): d = digits(n) return not increasing(d) and not decreasing(d)
def pd(n, k): # nth digit in concatenation of all padded k-digit numbers q, r = divmod(n, k) return digits(q)[r]
def main(n): return sum(digits(pow(2, n)))
#!/usr/bin/env python3 from lib import digits count= 0 for exp in range(1,25): for digit in range(1,10): if len(digits(digit**exp)) == exp: print(digit, exp, digit**exp) count += 1 print(count)
def main(lim): n, _ = gen(lim) return sum(digits(n))
def seq(n, lim = 54): for _ in range(lim): n += num(reversed(digits(n))) yield n
def main(n, k): d = digits(n) return max(multiply(d[i: i + k]) for i in range(len(d) - k))
def main(n): return sum(digits(factorial(n)))
def main(nums, k): return num(digits(sum(nums))[:k])
def circular(p): d = digits(p) return all( primes.isPrime(num(d[i:] + d[:i])) for i in range(1, len(d)))
def circular(p): d = digits(p) return all(primes.isPrime(num(d[i:] + d[:i])) for i in range(1, len(d)))
if n == 89: return True elif n <= 1: return False else: d = digits(n) d = list(map(int,d)) d = list(map(sq,d)) d = sum(d) return f89(d) e89 = [-1] * (10**8+1) e89[89] = 1 e89[1] = 0 count = 0 for i in range(1,10**7): n = i #print(n) while e89[n] < 0: d = digits(n) d = list(map(int,d)) d = list(map(sq,d)) n = sum(d) e89[i] = e89[n] if e89[i] > 0: #print(i) count += 1 print(count)
while p < 10000: p = i*(3*i-2) if p >= 1000 and p < 10000: p8.append(int(p)) i += 1 for i3 in p3: for i4 in p4: for i5 in p5: for i6 in p6: l = [i3,i4,i5,i6] ends = set([]) starts = set([]) for i in l: ends.add("".join(digits(i)[2:])) starts.add("".join(digits(i)[:2])) if len(starts & ends) > 0: for i7 in p7: l = [i3,i4,i5,i6,i7] ends = set([]) starts = set([]) for i in l: ends.add("".join(digits(i)[2:])) starts.add("".join(digits(i)[:2])) if len(starts & ends) > 3: for i8 in p8: l = [i3,i4,i5,i6,i7,i8] ends = set([]) starts = set([]) for i in l:
#!/usr/bin/env python3 from lib import digits a = 3 b = 2 count = 0 for it in range(1, 1001): # print(a,b) a,b = 2*b+a,b+a if len(digits(a)) > len(digits(b)): count += 1 print(count)
def nonRepeatLen(n): mem = set() while n not in mem: mem.add(n) n = sum(map(factorial, digits(n))) return len(mem)