码迷,mamicode.com
首页 > 其他好文 > 详细

projecteuler---->problem=36----Double-base palindromes

时间:2014-08-25 10:06:04      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:projecteuler   python   ruby   

Problem 36

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.)



def isCycle(n):
    if n%10 == 0:
        return False
    tempN = n
    num=list()
    while n > 0:
        num.insert(0,n%2)
        n /= 2
    for i in range(0,int(len(num)/2)):
        if num[i] != num[len(num)-i-1] :
            return False

    value = list()
    while tempN > 0:
        value.insert(0, tempN%10)
        tempN /= 10
    for i in range(0, int(len(value)/2)):
        if value[i] != value[len(value)-1-i]:
            return False
    return True

def main():
    resu = 0
    for i in range(1,1000000):
        if isCycle(i):
            resu += i
    print resu
if __name__ == "__main__":
    main()


class String
    def isCycle?
        self == self.reverse
    end
end

puts (1..1000000).select{|i| i.to_s.isCycle? && i.to_s(2).isCycle? }.reduce(:+)



projecteuler---->problem=36----Double-base palindromes

标签:projecteuler   python   ruby   

原文地址:http://blog.csdn.net/q745401990/article/details/38816345

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!