Advertisement
makispaiktis

ABC = A! + B! + C! Solution

Nov 9th, 2019 (edited)
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. '''
  2.  
  3. I have to find the 3 digits A,B and C, so the number:
  4. ABC = 100A + 10B + C = A! + B! + C!
  5. Example: For A = 2, B = 5, C = 6, we want: 256 = 2! + 5! + 6!, which is not true
  6. Note: NONE OF THE 3 DIGITS (A, B and C) IS EQUAL TO 0
  7.  
  8. '''
  9.  
  10. # Factorial Function
  11. def factorial(n):
  12.     if n >= 1:
  13.         return n * factorial(n-1)
  14.     return 1
  15.  
  16. # MAIN FUNCTION
  17. print("I have to find the 3 digits A,B and C, so the number:")
  18. print("ABC = 100A + 10B + C = A! + B! + C!")
  19. print("Example: For A = 2, B = 5, C = 6, we want: 256 = 2! + 5! + 6!, which is not true")
  20. print("Note: NONE OF THE 3 DIGITS (A, B and C) IS EQUAL TO 0")
  21. print()
  22. print()
  23. counterOfArrangements = 0
  24. threeDigitNumberIJK = 0
  25. sum = 0
  26. solutionList = []
  27. counterWhenIJKBiggerThanSumOfFactorials = 0
  28. for i in range(1, 10):
  29.     for j in range(1, 10):
  30.         for k in range(1, 10):
  31.             counterOfArrangements += 1
  32.             threeDigitNumberIJK = 100 * i + 10 * j + k
  33.             sum = factorial(i) + factorial(j) + factorial(k)
  34.             # IF I FIND AN ARRANGEMENT = SOLUTION OF THE PROBLEM
  35.             if threeDigitNumberIJK == sum:
  36.                 print(str(counterOfArrangements) + ":     " + str(threeDigitNumberIJK) + " = " + str(sum) + " , SOLUTION")
  37.                 solutionList.append(i)
  38.                 solutionList.append(j)
  39.                 solutionList.append(k)
  40.  
  41.             # ELIF - CASE (When (ABC = 100A + 10B + C) > A! + B! + C!)
  42.             elif threeDigitNumberIJK > sum:
  43.                 print(str(counterOfArrangements) + ":     " + str(threeDigitNumberIJK) + " > " + str(sum) + " , NOT SOLUTION")
  44.                 counterWhenIJKBiggerThanSumOfFactorials += 1
  45.             # ELSE - CASE
  46.             else:
  47.                 print(str(counterOfArrangements) + ":     " + str(threeDigitNumberIJK) + " < " + str(sum) + " , NOT SOLUTION")
  48.  
  49.  
  50.  
  51. # Printing out the solutions and the statistics of the survey
  52. # 1. Firstly, I will find out how many solutions are there
  53. # I remember that my solutionList is in the following form:
  54. # solutionList = [solution1I, solution1J, solution1K, solution2I, solution2J, solution2K, ......, solutionNI, solutionNJ, solutionNK]
  55. # The length can be divided with 3 - Solutions are sets of 3 consecutive numbers in the list
  56. print()
  57. print()
  58. step = 3
  59. for i in range(0, len(solutionList), step):
  60.     print("Solution " + str(i+1) + ":   Numbers " + str(solutionList[i]) + ", " + str(solutionList[i+1]) + ", " + str(solutionList[i+2]))
  61.     # print("Because:   " + str(100 * solutionList[i] + 10 * solutionList[i+1] + solutionList[i+2]) + " = " + str(solutionList[i]) + "! + " + str(solutionList[i+1]) + "! + " + str(solutionList[i+2]) + " = " + str(factorial(solutionList[i]))) + " + " + str(solutionList[i+2] + " = " + str(factorial(solutionList[i+1])) + " + " + str(solutionList[i+2]) + " = " + str(factorial(solutionList[i+2])))
  62.     first = solutionList[i]
  63.     second = solutionList[i+1]
  64.     third = solutionList[i+2]
  65.     print(" Because:     " + str(100 * first + 10 * second + third) + " = " + str(first) + "! + " + str(second) + "! + " + str(third) + "! = " + str(factorial(first)) + " + " + str(factorial(second)) + " + " + str(factorial(third)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement