Пример #1
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

#A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

#Find the largest palindrome made from the product of two 3-digit numbers.

#Answer:
	#906609

from time import time; t=time()
from mathplus import palindrome

ret = 0
for i in range(999, 99, -1):
    for j in range(i, 99, -1):
        x = i * j
        if x <= ret: break
        if palindrome(x):
            ret = x

print(ret)#, time()-t
Пример #2
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

#A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

#Find the largest palindrome made from the product of two 3-digit numbers.

#Answer:
#906609

from time import time
t = time()
from mathplus import palindrome

ret = 0
for i in range(999, 99, -1):
    for j in range(i, 99, -1):
        x = i * j
        if x <= ret: break
        if palindrome(x):
            ret = x

print(ret)  #, time()-t
Пример #3
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

#The decimal number, 585 = 10010010012 (binary), is palindromic in both bases.

#Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.

#(Please note that the palindromic number, in either base, may not include leading zeros.)

#Answer:
	#872187

from time import time; t = time()
from mathplus import palindrome

M = 1000000
s = 0
for i in range(1, M, 2):
    if not palindrome(i): continue
    m = bin(i)[2:]
    if int(m[::-1], 2) == i:
        #print i
        s += i

print(s)#, time()-t
Пример #4
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

#The palindromic number 595 is interesting because it can be written as the sum of consecutive squares: 62 + 72 + 82 + 92 + 102 + 112 + 122.

#There are exactly eleven palindromes below one-thousand that can be written as consecutive square sums, and the sum of these palindromes is 4164. Note that 1 = 02 + 12 has not been included as this problem is concerned with the squares of positive integers.

#Find the sum of all the numbers less than 108 that are both palindromic and can be written as the sum of consecutive squares.

#Answer:
	#2906969179

from time import time; t=time()
from mathplus import isqrt, palindrome

M = 10**8
s = isqrt(M/2)
pool = [0]*(s+1)
for i in range(s):
    pool[i+1] = pool[i] + i*i

ss = set()
for u in range(s-1):
    for v in range(u+2, s+1):
        x = pool[v] - pool[u]
        if x >= M: break
        if palindrome(x): ss.add(x)

print(sum(ss)-1)#, time()-t