Advertisement
makispaiktis

Searching for numbers = perfect powers

Dec 30th, 2019 (edited)
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. import math
  2.  
  3. lastNumber = 1024
  4. maxBase = math.ceil(math.sqrt(lastNumber))
  5. maxExponent = math.ceil(math.log2(lastNumber))
  6. numbers = []
  7. bases = []
  8. exponents = []
  9. for number in range(4, lastNumber+1):
  10.     for base in range(2, maxBase+1):
  11.         flag = False;
  12.         for exponent in range(2, maxExponent+1):
  13.             if number == base ** exponent:
  14.                 numbers.append(number)
  15.                 bases.append(base)
  16.                 exponents.append(exponent)
  17.                 # The flag will become true, in order to have the chance to break out of the 2nd loop (not only from first)
  18.                 flag = True
  19.                 break
  20.         if flag == True:
  21.             # With the break-command, we will go out from the outer (2nd) loop, to continue the iteration on 'number'
  22.             break
  23.  
  24. for i in range(0, len(numbers)):
  25.     print(str(numbers[i]) + " = " + str(bases[i]) + " ^ " + str(exponents[i]))
  26. print()
  27. print("There are " + str(len(numbers)) + " numbers = perfect powers from 4 to " + str(lastNumber) + ".")
  28. percentage = (lastNumber - 3) / len(numbers)
  29. print("With other words, 1 such number in " + str(round(percentage, 2)) + " numbers.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement