Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 9 Online
OpenStudy (anonymous):

Was hoping someone could help me with one of the finger exercises in the companion book to this course. It requires a user to enter an integer and then prints 2 integers, a root and pwr, such that 0

OpenStudy (rsmith6559):

There is an inelegant way to do what you want, an outer loop. Could you post your code so that we can help you towards an elegant solution?

OpenStudy (anonymous):

x = int(raw_input('Enter an integer: ')) #Initial variable root = 0 pwr = 1 while pwr < 6: pwr += 1 root = 0 while root**pwr < x: root += 1 if root**pwr == x: print 'root =', root print 'pwr =', pwr print 'Such that', root, '^', pwr, '=', x if root**pwr != x: print 'No root exists that is an integer'

OpenStudy (anonymous):

I also changed the parameters of the question, which asks for powers between 1 and 6. since everything has a 1st power, i skipped it and began my loop with a second power. Otherwise there would be no need for a conditional where there is no root. At this point, however, I may just cut my loses until I have more experience, change the conditions back to how they were originally stated so that there is always at least 1 answer and i don't have to worry about implementing a scenario where a root and power don't exist. I feel like this is cheating a bit, but the question is also contradictory, which is why i changed the parameters to begin with. Would still love any help, but for the time being am just going to move on.

OpenStudy (rsmith6559):

while statements test a condition to decide whether to execute a block of code. if statements do the same thing. Changing the value of one of the parameters between them can produce some very gnarly problems ( root += 1 ). It's a very useful skill to learn to read your code EXACTLY as it's written. No interpretation, just what it says. That's all that a computer can do.

OpenStudy (anonymous):

As far as finding all the roots of a given positive integer I'm not having any issues. But if there's a better way I'm all for it. Do you have any suggestions? It may help me with excluding 1 as a parameter so that I can introduce a "no root exists' statement.

OpenStudy (rsmith6559):

The simplest way would be to declare a variable, say: found = False in your initial variable section, set it to True in your "if root**pwr == x:" block and only print "No root...." if found is still false.

OpenStudy (anonymous):

All integers have a root and power between 0 and 6. For example the root and power of -7 is -7 and 1, the root and power of 136432 is 136432 and 1. Because of this, I didn't need the print statement that states there is no root and power. If x is negative, then the root will be negative too. I had a condition which decreased the root by 1 if x is negative, and increased the root by 1 if x is positive.

OpenStudy (anonymous):

I don't see how this program solves all roots. It never finds the case root**1. Also, how does it respond to a negative integer such as -27? Will it give you root=-3 and pwr=3? My problem is that my first try, I was only finding the answer root**1. I'm not sure how to make the program find both roots? Or do I just set it up to ignore that root like you did? Anyone have a solution?

OpenStudy (anonymous):

OK, I've been sweating over this for about an hour. I have it working for any x positive, any x negative, and for zero. For zero you should have root=zero and pwrs=1,2,3,4,5,6 correct? But like you I can't for the life of me get the program to print for an integer that doesn't have roots, since for any integer has a root=itself, pwr=1 I think. So, you should really never need that message should you? I can show you my code if you like... I was going to post the question but since you asked something similar, I am putting my reply/question here.

OpenStudy (anonymous):

Yes, that's correct. X positive is a positive root, x negative is a negative root. If x is zero, then the root is zero. X can have the root also be the same value as x and the power be 1. (x**1=x) So you should never get that message.

OpenStudy (anonymous):

Not sure what we are and are not supposed to do on here, but since there is no actual OCW class running on edx as of yet, I am posting my code. It works for all the cases I was talking about. However, I cannot see how to put a line of code in that will execute for no roots, as I don't know of such a case. Let me know if you find a case that the code doesn't work for if you don't mind? You should be able to do a copy an paste into your editor if you want to test it. x = int(raw_input('enter any integer')) pwr = 0 root = 0 ans = 0 while pwr < 6: pwr = pwr + 1 root = -1 while root <= abs(x) or ans <= abs(x): root = root + 1 ans = root**pwr if ans == x: print 'root = ',root print 'pwr = ',pwr elif ans == abs(x) and x < 0 and pwr%2 !=0: print 'root = ',-root print 'pwr = ',pwr

OpenStudy (anonymous):

When I input 0, it gives me back a list of 6 things: root = 0 and power = 1, power = 2, power = 3, etc. When I input 100, it gives me 2 things, root = 100 and power = 1, then root = 10 and power = 2. In my code I only get back root = 10 and power = 2. You can have it throw back multiple roots and powers or just one pair, the problem doesn't specify, it just says have it print 2 integers, power and root, I chose one pair, so I guess that's up to you. Other than that, I got correct answers for all the integers I inputted. I don't have a line of code to execute if there are no roots either, because that just seems mathematically impossible to me, it should always output at least the root and power 1.

OpenStudy (anonymous):

Jscorpio1105. Thanks for your comments. I guess it all goes to your interpretation of the problem statement. I guess the reason that I wrote my code to print all the pairs of integers representing roots is because of the statement "prints two integers, root and pwr, such that 0 < pwr < 6 and root**pwr is equal to the integer entered by the user." To me that meant all roots and powers within the range 1 <= pwr <= 5. The problem never stated "a single root within that range of pwr." I think it was saying that the code should print two integers for each root. I see that if you enter zero using my code, you get a root with pwr = 6, which is incorrect according the the stated range for pwr. So, in my mind, I need to fix that. Looking at the code posted above by JR_Red, if you look closely, he calculates for the range 2 <= pwr <= 6. So he does not find the root for pwr =1, and like me he calculates for pwr = 6. So that is also incorrect in my mind. He also finds no roots for an entry of zero. I plan to take the course for certificate through edx.org so I'll see what they consider correct at MIT. I think the class begins on Jan. 13, and you can get a certificate of completion for $45. I am an old electrical engineer who learned Fortran in the distant past. I know nothing of the "modern languages." So I'm trying to bring myself into the 21st century by doing this. From what I can see, this course will be difficult. I am really impressed that freshman and sophomores at MIT can handle it as an introductory course. Those kids must be damn smart. Happy computing and thanks much for your input! It is nice to have this forum to discuss thoughts about the exercises! I wish I could get my 16 year old daughter interested...

OpenStudy (anonymous):

I took 2 integers to mean root (integer 1) and power (integer 2) (in my program I also had it print the integer entered). And you're completely right, I didn't notice the 6 while running your code. In mine I put the initial value of pwr as 1, test that, and had pwr go up by 1, and once pwr >= 6, I have a command to make pwr 1 again and increase or decrease the root by 1. In case anyone else needs help, I'll paste my code below, along with my comments, feel free to test it. #Finger Exercise 3.1 #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 pair of integers exists, it should print a message to that effect. # x (root) to the power of y (pwr) = z (int entered) #pwr is over 0, terminates once it gets to 6 #if z is neg, the root is negative z = int(raw_input('Enter an integer: ')) root = 0 pwr = 1 while root**pwr != z: pwr = pwr + 1 #pwr has to be less than 6, so when it gets to 6 or above 6, it resets to 1 if pwr >= 6: pwr = 1 #if z is positive, then its root is positive, and the numbers need to decrease if z >= 0: root = root + 1 #if z is negative, then its root is negative, and the numbers need to decrease if z <= 0: root = root - 1 print 'Root =',root,'Power =',pwr,'Integer =',z

OpenStudy (anonymous):

I just noticed a typo in my comment. I meant to write #if z is positive, then its root is positive, and the numbers need to increase

OpenStudy (anonymous):

I see the difference in our approaches. You take each root and raise it to powers 1 through 5. If you don't find a root, you index root to the next integer and evaluate for powers 1 through 5 again. This continues until you find a root. I am evaluating for root varying from 0 through the entered integer for powers 1 through 5 and printing each time I find a root. I went with my approach thinking that I had to find all roots that exist for the range of powers. Yours will stop after finding the first root, only progressing to "root = input" and "pwr = 1" if it doesn't find any other root before getting there. Thanks!

OpenStudy (anonymous):

@Davebezesky I'm not sure if you were referring to my program when you said you weren't able to find root**1. I haven't had that issue, and am wondering if perhaps you are using version 3 and there is some sort of discrepancy. But at that time I hadn't wrote it to include negative roots, which I went back and added. I've tested this on numerous cases and as far as I can tells finds every root between the powers 0 and 6 for any given (+)/(-) integer. x = int(raw_input('Enter an integer: ')) #Initial variables pwr = 0 root = 0 while pwr < 6: pwr += 1 root = 0 while root**pwr < x: root += 1 if root**pwr == x: print 'root =', root print 'pwr =', pwr print 'Such that', root, '^', pwr, '=', x while root**pwr > x: root -= 1 if root**pwr == x: print 'root =', root print 'pwr =', pwr print 'Such that', root, '^', pwr, '=', x elif pwr%2 == 0: break

OpenStudy (anonymous):

I noticed I didn't have a scenario for 0 as an integer, so I added if x == 0 print 'Root = 0^nth pwr Its not elegant, but it does the job. Also, i initially posed the question when excluding the power 1, so that not all integers had a solution (because the question asked me to print a no root scenario if no roots existed). When I couldn't make it work, I changed the parameters to include 1 (like the question initially asked for) so that I didn't have to worry about it. The question isn't written very well, so I don't feel bad about it, lol.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!