def euler36(): ret = 0 for i in range(1, 1000000): if is_palindrome(i): if is_palindrome(bin(i)[2:]): ret += i return ret
def compute(): s = 0 for i in range(1_000_000): # bin(585) will return '0b1001001001' # only take part after b (2nd index until the end) if is_palindrome(str(i)) and is_palindrome(str(bin(i))[2:]): s += i
def palindromo(bot, update): # message.text format "/command text" tmp = update.message.text.split(" ") # first element is command tmp.pop(0) # tebuild text back to its form msg = ' '.join(tmp) if msg is not "": utils.is_palindrome(bot, update, msg) else: update.message.reply_text("Prueba de nuevo.")
def run(): """ Solution: Pretty straightforward search. """ N = 1000000 total = 0 for n in xrange(1, N): if not is_palindrome(n): continue if is_palindrome(to_binary(n)): total += n return total
def prob4(): start = 999 * 999 while (True): start -= 1 if (utils.is_palindrome(start) and utils.has_factors_with_digits(start, 3)): return start
def start_django_project(project_name): if not is_palindrome(project_name): click.echo("Project name is not palindrome") else: management.call_command("startproject", project_name, directory=DIRECTORY_DESTINATION)
def pe36(limit=1000000): """ >>> pe36() 872187 """ return sum(i for i in range(1, limit, 2) if is_palindrome(i) and is_palindrome2(i))
def main(): candidates = [ i for i in range(0, 1000001) if is_palindrome(i) and is_binary_palindrome(i) ] print(candidates) print(sum(candidates))
def find_palindromes(a,b): options = [] for i in reversed(range(a,b)): for j in reversed(range(a,b)): if is_palindrome(i*j): options.append(i*j) return max(options)
def largest_palindrome_product(): palindromes = (x for x in products() if is_palindrome(x)) largest = 0 for product in palindromes: if product > largest: largest = product return largest
def main(): best_result = 0 for i in range(100, 1000): for j in range(i, 1000): prod = i * j if prod > best_result and is_palindrome(prod): best_result = prod print(best_result)
def is_palindrome_in_binary(x): """ >>> is_palindrome_in_binary(585) True """ b = "{0:b}".format(x) # extra work in int conversion but is necessary to use utility function return utils.is_palindrome(int(b))
def is_lychrel(n): counter = 0 while counter < 50: n = reverse_add(n) if is_palindrome(n): return False counter += 1 return True
def palindromes(oddlength, lim=1000000): i = 1 p = mk_palindrome(i, oddlength) while p < lim: if is_palindrome(p, 10): yield p i += 1 p = mk_palindrome(i, oddlength)
def palindromes(oddlength, lim=1_000_000): i = 1 p = mk_palindrome(i, oddlength) while p < lim: if is_palindrome(p, base=10): yield p i += 1 p = mk_palindrome(i, oddlength)
def main(): total = 0 for n in range(MILL): if is_palindrome(n) and is_binary_palindrome(n): total += n return total
def is_lychrel(n, iterations=51): if iterations == 0: return True s = n + int(str(n)[::-1]) if is_palindrome(str(s)): return False return is_lychrel(s, iterations-1)
def euler4(): three_digits = range(100, 1000) ret = 0 for x in three_digits: for y in three_digits: p = x * y if is_palindrome(p) and p > ret: ret = p return ret
def main(): pals = [] for a in range(100, 1000): for b in range(100, 1000): product = a * b if is_palindrome(product): pals.append(product) print(sorted(pals))
def compute(): max_p = 0 for i in range(100, 1000): # Only go to i (inclusive) to eliminate double counting for j in range(100, i + 1): num = i * j if num > max_p and is_palindrome(num): max_p = num return max_p
def problem_four(): largest_palindrome = 0 for i in xrange(100, 999): for j in xrange (100, 999): product = i * j if is_palindrome(product) and product > largest_palindrome: largest_palindrome = product return largest_palindrome
def is_lychrel(n): if n > 10000: raise "n must be < 10000" i = 0 while i < 50: s = str(n) n = int(s) + int(s[::-1]) if utils.is_palindrome(str(n)): return False i += 1 return True
def test_is_palindrome(self): self.assertTrue(is_palindrome(1)) self.assertFalse(is_palindrome(10)) self.assertTrue(is_palindrome(101)) self.assertFalse(is_palindrome(int("10", base=2), base=2)) self.assertTrue(is_palindrome(int("101", base=2), base=2)) self.assertFalse(is_palindrome(int("123", base=4), base=4)) self.assertTrue(is_palindrome(int("12321", base=4), base=4))
def lychrel_number(num, max_rec=50, rec=0): """ return if is lychrel_number """ if is_palindrome(num): return True elif rec >= max_rec: return False else: return lychrel_number(num + int(str(num)[::-1]), rec=rec+1)
def is_lychrel(n, limit): count = 0 while count < limit: count += 1 n_reverse = str(n)[::-1] new = n + int(n_reverse) if is_palindrome(new): return False n = new return True
def add_to_anagrams(items,mapping_func): anagrams = collections.defaultdict(set) sqs_or_ws = collections.defaultdict(set) for item in items: if is_palindrome(item): continue sqs_or_ws[tuple(sorted(item))].add(item) for anags in sqs_or_ws.values(): if len(anags) < 2: continue for pair in itertools.combinations(anags,2): anagrams[mapping_func(pair)].add(pair) return anagrams
def main(): n = 0 for x in range(999, 99, -1): for y in range(x, 99, -1): k = x * y if k <= n: break if is_palindrome(k, 10): n = k return n
def main(): n = 0 for x in range(999, 99, -1): for y in range(x, 99, -1): k = x*y if k <= n: break if is_palindrome(k): n = k return n
def main(): high = 0 for x in range(1000, 100, -1): for y in range(x, 100, -1): t = x * y if is_palindrome(t): if t > high: high = t return high
def lychrel(n, attempts): """ Return True if <n> is a Lychrel number given <attempts> iterations. """ next_try = n for i in range(attempts): p = int(''.join(reversed(str(next_try)))) next_try += p if is_palindrome(next_try): return False return True
def main(): n = int(sys.argv[1]) number_range = lambda n: range((10**n)-1, 10**(n-1), -1) palindromes = set() for x in number_range(n): for y in number_range(n): z = x * y if is_palindrome(z): palindromes.add(z) print(max(palindromes))
def palindromo(bot, update): user = update.message.from_user # message.text format "/command text" tmp = update.message.text.split(" ") # first element is command tmp.pop(0) # tebuild text back to its form msg = ' '.join(tmp) if msg is not "": logger.info("User {} has put /palindromo with text.".format(user.first_name)) utils.is_palindrome(bot, update, msg) return ConversationHandler.END else: logger.info("User {} has put /palindromo with no text.".format(user.first_name)) update.message.reply_text("Prueba a introducir algo de texto, anda. \ Si no quieres, escribe /cancel.") return VACIO
def main(): # examine 1-> 10,000 lychrels = range(1, 10001) for i in xrange(1, 10001): # Fifty iterations per number to try and prove it is non-lychrel r = reverse_and_add(i) for j in xrange(0, 50): if utils.is_palindrome(r): # print "%s is a palindrome of %s on iteration %d" % (r, i, j) lychrels.remove(i) break r = reverse_and_add(r) return len(lychrels)
def solve(self): maxNum = 1000 minNum = 99 result = 0 for a in xrange(maxNum - 1, minNum, -1): for b in xrange(a, minNum, -1): num = a * b if is_palindrome(num): minNum = b result = max(result, num) break return result
def pe4(n=3): """ >>> pe4() 906609 """ first, last = 9*10**(n - 1) + 1, 10**n mx = 0 for x in range(first, last): for y in range(x, last): xy = x * y if xy > mx and is_palindrome(xy): mx = xy return mx
def euler125(): N = 10 ** 8 squares = [x * x for x in range(1, int(math.sqrt(N)))] ret = set() for window_len in range(2, len(squares)): for arr in window(squares, window_len): s = sum(arr) if s >= N: break if is_palindrome(s): ret.add(s) return sum(ret)
def main(): N = 10 ** 8 squares = [n * n for n in range(1, int(sqrt(N)))] palindromes = set() for i in range(len(squares) - 1): s = squares[i] for j in range(i + 1, len(squares)): s += squares[j] if s >= N: break if is_palindrome(s): palindromes.add(s) print(sum(palindromes))
def main(): # create list of Palindromes between 10000 and 998001 palindromes = [x for x in xrange(10000, 998001) if is_palindrome(str(x))] # remove prime palindromes, to reduce the number of options to try palindromes = non_primes(palindromes) # reverse list to start with the highest palindromes first. # find products, once we find a palindrome that meets the problem requirements # return the palindrome and the values used to produce it. palindromes.reverse() for p in palindromes: x, y = find_products(p) if x and y: return p, x, y
def pb4(): """ Problem 4 : Largest palindrome product See utils.is_palindrome for checking if a string is a palindrome. Iterates over {100, ... , 999}^2 and discards if less than current result. Otherwise, checks and updates. """ res = 0 for i in range(100, 1000): for j in range(100, 1000): candidate = i * j if candidate > res: if utils.is_palindrome(str(candidate)): res = candidate return res
def main(): lim = 10**8 squares = [x * x for x in range(1, 7072)] # 7071^2 + 7072^2 > 10^8 seen = set() for i in range(len(squares) - 1): s = squares[i] for j in range(i + 1, len(squares)): s += squares[j] if s >= lim: break if s not in seen and is_palindrome(s): seen.add(s) return sum(seen)
def main(): lim = 10**8 squares = [x*x for x in range(1, 7072)] # 7071^2 + 7072^2 > 10^8 seen = set() for i in range(len(squares)-1): s = squares[i] for j in range(i+1, len(squares)): s += squares[j] if s >= lim: break if s not in seen and is_palindrome(s): seen.add(s) return sum(seen)
def largest_palindrome(ndigits=3): curr0 = 10**ndigits - 1 curr1 = 10**ndigits - 1 largest_palin = 0 while curr1 >=curr0: palin = curr0*curr1 if is_palindrome(palin) and palin > largest_palin: largest_palin = palin if curr1 == curr0: curr0 -= 1 curr1 = 10**ndigits - 1 if largest_palin > curr1*curr0: break elif curr1 > curr0: curr1 -= 1 if curr0 < 10**(ndigits-1): break return largest_palin
def palindrome_subsequences(word): # precalculate dinamic programming N x N of characters coicidences n = len(word) # length 1 dp = [[0 for x in range(n)] for y in range(n)] for i in range(n): dp[i][i] = 1 palindromes = list(word) # length 2 pos = 0 while pos < n - 1: if word[pos] == word[pos + 1]: for i in range(n): dp[pos][pos + 1] = 1 dp[pos + 1][pos] = 1 palindromes.append(word[pos] + word[pos + 1]) pos = pos + 1 # print_matrix(dp) # check words greater > 2 for k in range(3, n + 1): for i in range(n): for j in range(i, n): # check if is_palindrome(word, i, j, j - i): dp[i][j] = 1 dp[j][i] = 1 # print_matrix(dp) # reconstruct words for i in range(1, n - 1): if dp[i][i + 1] == 1: # even words x1 = i y1 = i + 1 palindromes = rebuild_words(x1, y1, word, '', palindromes, dp) if i - 1 > 0 and dp[i - 1][i + 1] == 1: # odd words x1 = i - 1 y1 = i + 1 palindromes = rebuild_words(x1, y1, word, word[i], palindromes, dp) return palindromes
def largest_palindrome(ndigits=3): curr0 = 10**ndigits - 1 curr1 = 10**ndigits - 1 largest_palin = 0 while curr1 >= curr0: palin = curr0 * curr1 if is_palindrome(palin) and palin > largest_palin: largest_palin = palin if curr1 == curr0: curr0 -= 1 curr1 = 10**ndigits - 1 if largest_palin > curr1 * curr0: break elif curr1 > curr0: curr1 -= 1 if curr0 < 10**(ndigits - 1): break return largest_palin
def test_is_palindrome_single_character(): assert is_palindrome("a")
def is_palindrome_in_decimal(x): """ >>> is_palindrome_in_decimal(585) True """ return utils.is_palindrome(x)
def test_is_palinrome(self): cases = {1:True, 22:True, 23:False, "tenet":True, "hello":False} for case in cases.keys(): result = utils.is_palindrome(case) if result != cases[case]: self.fail("is_palindrome failed for %s: it got %s" % (case, cases[case]))
def is_lychrel(n, i = 50): for j in range(i): n = n + reverse(n) if is_palindrome(n): return False return True
""" solve problem 36 """ # pylint: disable=E0611 from numpy import binary_repr from utils import is_palindrome MAX_NUMBER = 1000000 SUM = 0 PALINDROMES = [] # Check to see if palindrome in base10. for number in xrange(MAX_NUMBER+1): if is_palindrome(str(number)): PALINDROMES.append(number) # If palindrome in base10 see if also palindrome in base2. for number in PALINDROMES: if is_palindrome(binary_repr(number)): SUM += number print SUM
import sys sys.path.append('../utils') sys.path.append('utils') from utils import is_palindrome def rev_it(x): return (int(''.join(list(reversed([y for y in str(x)]))))) # could be improved with caching. # if you end up on a number you know goes to palindrome, just stop becuse it ain't lychrel upper = 10000 lychrel = 0 for x in range(1, 10000): iter = 0 x_orig = x while (iter < 50): iter += 1 x = x + rev_it(x) if is_palindrome(x) & (len(str(x)) > 2): break if iter == 50: lychrel += 1 print( str(x_orig) + ' --> ' + str(x) + ' in ' + str(iter) + ' iterations.') print('-----------------------') print('there are ' + str(lychrel) + ' lychrel #s below ' + str(upper))
""" Solves project euler problem 4. """ from itertools import product from utils import is_palindrome MAX = 999 HIGHEST = 0 for num1, num2 in product(range(MAX+1), range(MAX+1)): if is_palindrome(str(num1*num2)) and num1*num2 > HIGHEST: HIGHEST = num1*num2 print HIGHEST
count = 0 #for i in range(943, 944): for i in range(1, 10000): n = i #print "On n: %d" % n if n in num_iter: if 50 <= num_iter[n]: count += 1 else: ms = [n] idx = 0 while idx <= 50: m = ms[idx] m = m + int(str(m)[::-1]) #print "m: %d" % m if is_palindrome(m): #print "FOUND" for j in range(len(ms)): num_iter[ms[j]] = len(ms) - j break else: ms.append(m) idx += 1 if 50 < idx: count += 1 for j in range(len(ms)): num_iter[ms[j]] = 51 #for n in sorted(num_iter): # if n < 10000: #print "n: %d num_iter[n]: %d" % (n, num_iter[n]) print "count: %d" % count
def prob4(): start = 999*999 while (True): start -= 1 if (utils.is_palindrome(start) and utils.has_factors_with_digits(start, 3)): return start
#!/usr/bin/env python from utils import is_palindrome def is_binary_palindrome(n): s = str(bin(n))[2:] return s == s[::-1] sum = 0 for i in range(1, 1000000): if is_palindrome(i) and is_binary_palindrome(i): print i, bin(i)[2:] sum += i print "Answer is", sum
def test_is_palindrome_empty_string(): assert is_palindrome("")
def test_is_palindrome_not_quite(): assert not is_palindrome("abab")