Advertisement
user_137

Find Roots

Feb 8th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. # and pwr, such that 0<pwr<6 and root^pwr is equal to the integer entered by
  2. # the user. If no such pair of integers exists, it should print a message to
  3. # that effect.
  4.  
  5. rawUserNum = int(raw_input('Enter an integer: '))
  6. userNum = abs(rawUserNum)
  7. root = 0
  8. pwr = 5
  9. i = root
  10. while i < userNum:
  11.     j = pwr
  12.     while j > 0:
  13.         if i ** j == userNum:
  14.             root = i
  15.             pwr = j
  16.             i = userNum
  17.             break
  18.         j -= 1
  19.     i += 1
  20.  
  21. if root ** pwr == userNum:
  22.     print ''
  23.     print 'User entered:', userNum
  24.     if rawUserNum < 0:
  25.         print 'root is:', -root
  26.         print 'pwr is:', pwr
  27.         print str(-root) + '^' + str(pwr) + ' = ' + str(-root ** pwr) + \
  28.               ', which is equal to the integer the user entered.'
  29.     else:
  30.         print 'root is:', root
  31.         print 'pwr is:', pwr
  32.         print str(root) + '^' + str(pwr) + ' = ' + str(root ** pwr) + \
  33.               ', which is equal to the integer the user entered.'
  34. else:
  35.     print 'No such pair of integers exist, such that: 0<pwr<6 and root^pwr' \
  36.           ' is equal to the integer enetered by the user.'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement