Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- numbers = list()
- for i in range(10):
- numbers.append(i)
- NUMBERS = [str(number) for number in numbers]
- print(NUMBERS)
- def checkDigitsStep1(n):
- square = n**2
- cube = n**3
- # *****************************************************************************************************
- # 1. I will make a list that will contain the str(digits) of my square and cube
- DIGITS = list()
- SQUARE = str(square)
- CUBE = str(cube)
- SQUAREDIGITS = [SQUARE[i] for i in range(len(SQUARE))]
- CUBEDIGITS = [CUBE[i] for i in range(len(CUBE))]
- # Now if n = 6, square = 36, cube = 216 ----> SQUAREDIGITS = ['3', '6'] and CUBEDIGITS = ['2', '1', '6']
- # *****************************************************************************************************
- # 2. Its time to concatenate the 2 lists in the list 'DIGITS'
- for element in SQUAREDIGITS:
- DIGITS.append(element)
- for element in CUBEDIGITS:
- DIGITS.append(element)
- # *****************************************************************************************************
- # 3. Create a map that counts its digit appearance
- counters = [0 for _ in range(10)]
- # Check every element of my list 'DIGITS'. I will increase the 'counter' in counters list
- for DIGIT in DIGITS:
- for i in range(len(NUMBERS)):
- if DIGIT == NUMBERS[i]:
- counters[i] += 1
- break
- # *****************************************************************************************************
- # 4. Now, my list 'counters' is ready. In my example we should have:
- # 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
- return counters
- def checkDigits(n):
- counters = checkDigitsStep1(n)
- print(n, n**2, n**3, counters)
- # Now, I want all my counters to be 1
- flag = False
- hits = 0
- for i in range(len(counters)):
- if counters[i] == 1:
- hits += 1
- # If hits = len(counters) = 10, that means all the digits from 0 to 9 are once in my square or cube
- if hits == 10:
- flag = True
- return flag
- # MAIN FUNCTION
- limit1 = 10
- limit2 = 100
- for n in range(limit1, limit2):
- if checkDigits(n) == True:
- print()
- print("Solution = " + str(n))
- print("n = " + str(n) + ", n^2 = " + str(n**2) + ", n^3 = " + str(n**3))
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement