Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, January 31, 2016

Searching for two integers root**pwr = integer(user's input)

Searching for two integers root**pwr = integer(user's input)


A question from a book I have found.

Write a program that asks the user to input an integer and prints two integers, root and pwr, such that 0 < pwr < 6 and root**pwr is equal to the integer user entered. If no such pair exists, print that it is impossible to find such a pair.

integer = 3 #there will be raw_input but I use it as an example  root = 0    for pwr in range(1,6):        if root**pwr != integer:          pwr += 1          print pwr        else:          print root, pwr        if pwr > 5:               pwr = 1          root += 1  

I did not complete program yet because I cannot get loop right. The problem is that I receive output 2, 3, 4, 5, 6 and then loop terminates. However, I did use restart on pwr variable in the last if statement code block you see. However, it stops to execute anyway. What is the problem here?

Answer by amit for Searching for two integers root**pwr = integer(user's input)


integer ** 1 always suffices the condition.

An alternative (assuming you actually want 1 < pwr < 6):

Check if for a certain base a and a number n: ceil(a ** log_a(n)) == n. If so - then a ** log_a(n) is your answer.
Repeat for all possible a's in range.

(In here log_a(n) is the logarithm with the base a, which can be computed as log(n)/log(a))

Answer by zenpoy for Searching for two integers root**pwr = integer(user's input)


Another option, with "simple math".

integer = 3    for power in range(1,6):      a = (integer ** (1.0/power))      if math.ceil(a) == a:          print a, power    >> 3.0 1  

Answer by mbattifarano for Searching for two integers root**pwr = integer(user's input)


In general, it's not a good idea to modify what you are looping over inside of the loop. There is no reason to fiddle with pwr when you are using it to iterate over range(1,6).

What your code is trying to do is test root ** pwr == integer for successive values of pwr and a fixed value of root until pwr reaches 6. Then you want it to add one to root and repeat. This is most naturally phrased as two nested for loops:

for root in range(0,integer):      for pwr in range(1,6):          if root ** pwr == integer:              print root, pwr  

In any case, this is a fairly expensive way to go about it and so I would recommend looking into some of the other solutions here. However, you should keep this in mind because it's a good example of how to use for loops.

To answer your question about why the loop was terminating you have to consider how python treats iterators. When the code block inside of a for loop terminates, python sets pwr to the value returned by the iterators next() method (which does exactly what you would think). When there are no more values left in the iterator next() will raise a StopIteration exception and Python will exit the loop. The key is that Python doesn't modify the value of pwr by adding 1 each iteration, it overwrites the value. So you loop will run exactly 5 times because that's how many items there are in range(1,6)

For clarification run the following code:

for i in range(0,9):      print i      i += 5      print i  

Answer by archery1234 for Searching for two integers root**pwr = integer(user's input)


This is the finger exercise from Chapter 3 of 'Introduction to Computation and Programming Using Python' by John Guttag and reads:

Write a program that asks the user to enter an integer and prints two integers, root and pwr, such that 0 < pwr < 6 and root**pwr is equal to the integer entered by the user. If no such pairs exists, it should print a message to that effect.

Given the author indicates that there may be no pairs that exist, I've assumed that the conditions for pwr should be 1 < pwr < 6 otherwise there will always be a solution (integer**1). My answer, using only the concepts that have been introduced in the book to that point, is:

num = int(raw_input('Enter a positive integer: '))  pwr = 2  root = 1  ans = ''  while pwr < 6:      while root**pwr <= num:          if root**pwr == num:                     print 'the root is ',root,              print 'the power is ', pwr              ans = True          root += 1              pwr += 1      root = 1  if ans != True:      print'No such pair of integers exist'  

Answer by Per-Mikael Bruvik for Searching for two integers root**pwr = integer(user's input)


First, if we enter an integer, e.g 1024, then we have 1024^1 = 1024. So there will always be a solution to the problem:

n = int(input('Enter an integer: '))  pwr = 1  root = 1  found = False    while root <= n:      if root**pwr == n:          print('root =',root, 'pwr =', pwr)          found = True      pwr = pwr + 1      if pwr == 6:          pwr = 1          root = root + 1    if not found:      print('No such pair exist.')  

Answer by Sean Collins for Searching for two integers root**pwr = integer(user's input)


This is an answer based on the submission by archery1234. Thanks to archery1234 a I am so new to this I could not get started without the help. I liked the answer provided because it only used the concepts explained up to that point in the book so I felt it was close to what Dr. Guttag probably wanted us to learn (while loop within a while loop). The following code meets the condition that 0 < pwr < 6 without always just terminating at the integer entered as the root and pwr = 1.

integer = abs(int(raw_input("Enter an integer: ")))  pwr = 1  root = 2  ans = ' '  if abs(integer) == 1:      print "No such pair of integers exists"  else:      while root < abs(integer):          while root**pwr <= integer:              if root**pwr == integer:                  print "The root is ",root,";"                  print "The power is ",pwr,"."                  ans = True              pwr += 1          root += 1          pwr = 1      if ans != True:          print "No such pair of integers exist"  

Answer by Luke D for Searching for two integers root**pwr = integer(user's input)


I was working on this problem, my answer works:

number = int(raw_input("Enger a number: "))      i = 1        pwr = 1      test = 0      while i <= number/2:          while pwr < 6:              if i ** pwr == number:                  print i                  test = 1              pwr = pwr + 1          i = i + 1          pwr = 1      if test == 0:          print 'no'  

Answer by Peter Duang for Searching for two integers root**pwr = integer(user's input)


This is my answer:

usr = int(raw_input("Enter an integer > "))  paired = False  for pwr in range(1,6):      root = 0      while (usr - root**pwr > 0):          root += 1      if usr == root**pwr:          paired = True          print "root is " + str(root) + " and ",          print "power is " + str(pwr)          break  if paired == False:      print "No pair"  

Answer by BlackStar for Searching for two integers root**pwr = integer(user's input)


I have managed to solve this problem with the knowledge you gain from chapter 1 up until 3.1 in the book by Gattug. This finger exercise took me two weeks to do. I am also just started coding.

here is my solution:

num = int(raw_input('Enter an integer: '))   root = num   power = 1     if root**power == abs(num):     print ' First pair of integers which equal the integer:', num, 'is root = ', root, 'power = ', power     root -= 1  while root**power != abs(num) and root > 0:  while power < 6 and root**power != abs(num):        if not(root**power == abs(num)):           power +=1        if not(root**power == abs(num)):           power +=1   if power >= 6 and root**power != abs(num):      power = 1      root -= 1  if root**power == abs(num):  print 'Second pair of integers which equal the integer:', num, 'is root = ', root, 'power = ', power  elif root**power != abs(num) and root == 0:  print ' there is no other pair of integers that equal', num  

I have done it and it is the best book to teach teaching you coding


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.