Exemple #1
0
def main():

    rollingTotal = 1  # 1 ** 2 is special as it's palindromic, but it's only 1 number.
    # so start the total at 1 to avoid special casiing in the algo.
    palindromes = set([])
    LIMIT = 10**8
    END = int(floor((sqrt(4 - 4 * 2 * (1 - LIMIT)) + 2) /
                    4))  # solve x ** 2 + (x - 1) ** 2 = LIMIT
    # this establishes the very last squared number
    # that, when added to the 2nd-to-last squared number,
    # produces a number < LIMIT.
    for i in xrange(2, END + 1):

        rollingTotal += i**2

        if rollingTotal < LIMIT and isNumberPalindromic(rollingTotal):
            palindromes.add(rollingTotal)

        copyOfTotal = rollingTotal

        for j in xrange(1, i - 1):  # the sum is of at least two values.
            copyOfTotal -= j**2
            if copyOfTotal < LIMIT and isNumberPalindromic(copyOfTotal):
                palindromes.add(copyOfTotal)

    print "Sum of all palindromes:", sum(palindromes)
def main():
    
    rollingTotal = 1        # 1 ** 2 is special as it's palindromic, but it's only 1 number. 
                            # so start the total at 1 to avoid special casiing in the algo.
    palindromes = set([])
    LIMIT = 10 ** 8
    END = int(floor((sqrt(4 - 4 * 2 * (1 - LIMIT)) + 2) / 4))   # solve x ** 2 + (x - 1) ** 2 = LIMIT
                                                                # this establishes the very last squared number
                                                                # that, when added to the 2nd-to-last squared number,
                                                                # produces a number < LIMIT.
    for i in xrange(2, END + 1):
        
        rollingTotal += i ** 2
        
        if rollingTotal < LIMIT and isNumberPalindromic(rollingTotal): 
            palindromes.add(rollingTotal)
        
        copyOfTotal = rollingTotal
        
        for j in xrange(1, i - 1):                              # the sum is of at least two values.
            copyOfTotal -= j ** 2
            if copyOfTotal < LIMIT and isNumberPalindromic(copyOfTotal): 
                palindromes.add(copyOfTotal)
                
    print "Sum of all palindromes:", sum(palindromes)
def main():
    start = time()
    solutions = set([])
    for i in xrange(1, LIMIT):
        if isNumberPalindromic(i) and isNumberPalindromic(str(bin(i))[2:]):
            solutions.add(i)
    
    print "Sum of solution set: ", sum(solutions)
    end = time()
    print "Runtime: ", end - start, " seconds."
def main():
    start = time()
    solutions = set([])
    for i in xrange(1, LIMIT):
        if isNumberPalindromic(i) and isNumberPalindromic(str(bin(i))[2:]):
            solutions.add(i)

    print "Sum of solution set: ", sum(solutions)
    end = time()
    print "Runtime: ", end - start, " seconds."
def main():
    
    start = time()
    solutionSet = set([])
    numbersLeadingToPalindromes = set([])
    
    for i in xrange(1, LIMIT):
        iterationNumber = 0
        candidate = i
        possibleLychrel = True
        iterationIntermediates = set([i])
        
        while iterationNumber < ITERATION_LIMIT:
            
            mirroredNumber = int(str(candidate)[::-1])
            
            newCandidate = candidate + mirroredNumber
            iterationIntermediates.add(newCandidate)
            
            if newCandidate in numbersLeadingToPalindromes or isNumberPalindromic(newCandidate):
                possibleLychrel = False
                numbersLeadingToPalindromes |= iterationIntermediates
                break
            
            candidate = newCandidate
            iterationNumber += 1
        
        if possibleLychrel:
            solutionSet.add(i)
    
    end = time()
    
    print "Potential Lychrel numbers < ", LIMIT, " : ", len(solutionSet)
    print "Runtime: ", end - start, " seconds."
def main():

    start = time()
    solutionSet = set([])
    numbersLeadingToPalindromes = set([])

    for i in xrange(1, LIMIT):
        iterationNumber = 0
        candidate = i
        possibleLychrel = True
        iterationIntermediates = set([i])

        while iterationNumber < ITERATION_LIMIT:

            mirroredNumber = int(str(candidate)[::-1])

            newCandidate = candidate + mirroredNumber
            iterationIntermediates.add(newCandidate)

            if newCandidate in numbersLeadingToPalindromes or isNumberPalindromic(
                    newCandidate):
                possibleLychrel = False
                numbersLeadingToPalindromes |= iterationIntermediates
                break

            candidate = newCandidate
            iterationNumber += 1

        if possibleLychrel:
            solutionSet.add(i)

    end = time()

    print "Potential Lychrel numbers < ", LIMIT, " : ", len(solutionSet)
    print "Runtime: ", end - start, " seconds."