Advertisement
makispaiktis

Square and cube must have all digits

Nov 19th, 2020 (edited)
1,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.38 KB | None | 0 0
  1. numbers =  list()
  2. for i in range(10):
  3.     numbers.append(i)
  4. NUMBERS = [str(number) for number in numbers]
  5. print(NUMBERS)
  6.  
  7. def checkDigitsStep1(n):
  8.     square = n**2
  9.     cube = n**3
  10.     # *****************************************************************************************************
  11.     # 1. I will make a list that will contain the str(digits) of my square and cube
  12.     DIGITS = list()
  13.     SQUARE = str(square)
  14.     CUBE = str(cube)
  15.     SQUAREDIGITS = [SQUARE[i] for i in range(len(SQUARE))]
  16.     CUBEDIGITS = [CUBE[i] for i in range(len(CUBE))]
  17.     # Now if n = 6, square = 36, cube = 216 ----> SQUAREDIGITS = ['3', '6'] and CUBEDIGITS = ['2', '1', '6']
  18.  
  19.     # *****************************************************************************************************
  20.     # 2. Its time to concatenate the 2 lists in the list 'DIGITS'
  21.     for element in SQUAREDIGITS:
  22.         DIGITS.append(element)
  23.     for element in CUBEDIGITS:
  24.         DIGITS.append(element)
  25.  
  26.     # *****************************************************************************************************
  27.     # 3. Create a map that counts its digit appearance
  28.     counters = [0 for _ in range(10)]
  29.     # Check every element of my list 'DIGITS'. I will increase the 'counter' in counters list
  30.     for DIGIT in DIGITS:
  31.         for i in range(len(NUMBERS)):
  32.             if DIGIT == NUMBERS[i]:
  33.                 counters[i] += 1
  34.                 break
  35.  
  36.     # *****************************************************************************************************
  37.     # 4. Now, my list 'counters' is ready. In my example we should have:
  38.     # counters = [0, 1, 1, 1, 0, 0, 2, 0, 0, 0], because i have 1 '1's, 1 '2's, 1 '3's and 2 '6's
  39.     return counters
  40.  
  41. def checkDigits(n):
  42.     counters = checkDigitsStep1(n)
  43.     print(n, n**2, n**3, counters)
  44.     # Now, I want all my counters to be 1
  45.     flag = False
  46.     hits = 0
  47.     for i in range(len(counters)):
  48.         if counters[i] == 1:
  49.             hits += 1
  50.     # If hits = len(counters) = 10, that means all the digits from 0 to 9 are once in my square or cube
  51.     if hits == 10:
  52.         flag = True
  53.     return flag
  54.  
  55.  
  56. # MAIN FUNCTION
  57. limit1 = 10
  58. limit2 = 100
  59. for n in range(limit1, limit2):
  60.     if checkDigits(n) == True:
  61.         print()
  62.         print("Solution = " + str(n))
  63.         print("n = " + str(n) + ", n^2 = " + str(n**2) + ", n^3 = " + str(n**3))
  64.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement