def findFactorialDigitSum(List_Of_Factorials): result = [] for number in range(10,1819441): sum_digit_factorials = sum([List_Of_Factorials[x] for x in Common.getDigits(number)]) # calculate the sum of factorial of all digits of the number if (number == sum_digit_factorials): result.append(number) return sum(result)
def findDigitFifthPowers(): DigitfifthPowers = [] for number in range(2,354295): numberDigits = Common.getDigits(number) # find the individual digits of the number sumOfFifthpowers = sum([x**5 for x in numberDigits]) # compute fifth power of each digit of the number and sum them. if number == sumOfFifthpowers: # if number equals the sum DigitfifthPowers.append(number) # add the number to the list. return sum(DigitfifthPowers) # sum all the numbers in the list to get the result.
def findDigitCancellingFractions(): digit_cancelling_fractions = [] for numerator in range(10,100): # since we are interested only in fractions having 2 digits in numerator and denominator. for denominator in range(10,100): # if value of fraction > 1 or if the units place of numerator or denominator has a 0, skip and go to next number. if ( (numerator/denominator >= 1) or (Common.getDigits(numerator)[1] == 0) or (Common.getDigits(denominator)[1] == 0)): pass else: if (Common.getDigits(numerator)[1] == Common.getDigits(denominator)[0]): # Units digit of numerator equals the tens digit of denominator # After digit cancelling, if value of incorrect fraction equals value of correct fraction, we have a winner. if (float(Common.getDigits(numerator)[0]) / float(Common.getDigits(denominator)[1]) == float(numerator)/float(denominator)): digit_cancelling_fractions.append((numerator,denominator)) # find the product of all 4 digit cancelling fractions product_numerator = 1 product_denominator = 1 # calculate the product of all numerators and denominators separately. for fract in digit_cancelling_fractions: product_numerator = product_numerator*fract[0] product_denominator = product_denominator*fract[1] # reduce the fraction to its lowest Common term reducedform = Common.reduceFractionToLowestCommonTerms((product_numerator,product_denominator)) return reducedform
def findMaximumDigitalSumBruteForce(): maxSum = 0 for a in range(2,100): for b in range(2,100): value = a**b if value < 10: continue else: digitalSum = sum(Common.getDigits(value)) if digitalSum > maxSum: maxSum = digitalSum return maxSum
def isLychrel(number): iterations = 1 while (iterations <= 50 ): # for 50 iterations or less reversed_number = "" # Reverse the given number for digit in reversed(Common.getDigits(number)): reversed_number = reversed_number+str(digit) # sum number and its reverse number_added_to_reverse = number+int(reversed_number) # if sum is a palindrome, tell that the given number is not lychrel by returning False. if Common.isPalindrome(str(number_added_to_reverse)): return False iterations +=1 number = number_added_to_reverse return True
def main(): start_time = time.time() print "The sum of the digits in the number 100!: ", sum(Common.getDigits(computeFactorial(100))) print "Problem solved in %s seconds " % (time.time()-start_time)