Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- lastNumber = 1024
- maxBase = math.ceil(math.sqrt(lastNumber))
- maxExponent = math.ceil(math.log2(lastNumber))
- numbers = []
- bases = []
- exponents = []
- for number in range(4, lastNumber+1):
- for base in range(2, maxBase+1):
- flag = False;
- for exponent in range(2, maxExponent+1):
- if number == base ** exponent:
- numbers.append(number)
- bases.append(base)
- exponents.append(exponent)
- # The flag will become true, in order to have the chance to break out of the 2nd loop (not only from first)
- flag = True
- break
- if flag == True:
- # With the break-command, we will go out from the outer (2nd) loop, to continue the iteration on 'number'
- break
- for i in range(0, len(numbers)):
- print(str(numbers[i]) + " = " + str(bases[i]) + " ^ " + str(exponents[i]))
- print()
- print("There are " + str(len(numbers)) + " numbers = perfect powers from 4 to " + str(lastNumber) + ".")
- percentage = (lastNumber - 3) / len(numbers)
- print("With other words, 1 such number in " + str(round(percentage, 2)) + " numbers.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement