Computer Science Canada Hard Question |
| Author: | MannieFresh [ Sat Nov 08, 2008 9:37 am ] |
| Post subject: | Hard Question |
Write a program to keep inputting integers until a perfect square between 40 and 100 is entered. This question is hard for me I'm just learning Turing so if anyone can help me that'd be greatly appreciated. |
|
| Author: | Euphoracle [ Sat Nov 08, 2008 9:47 am ] |
| Post subject: | RE:Hard Question |
It's not hard, but it requires a bit of thought. Work out the cases on paper. You know you're going to need a loop to repeat the input, but you need to determine your exit condition. First, how can you determine if a number is a perfect square, and secondly, how can you determine if it is between 40 and 100? After you determine a method of answering those questions, and transferring that into Turing, the rest of the assignment will write itself. |
|
| Author: | MannieFresh [ Sat Nov 08, 2008 9:53 am ] |
| Post subject: | RE:Hard Question |
Alright, I'll see if I can do it. Thanks. |
|
| Author: | Jurica [ Sat Nov 08, 2008 10:22 am ] |
| Post subject: | RE:Hard Question |
What don't you understand? |
|
| Author: | andrew. [ Sat Nov 08, 2008 10:42 am ] |
| Post subject: | RE:Hard Question |
He doesn't understand how to put that question into Turing. Like Euphoracle said, write it out on paper first and think about how you will get the computer to know whether the number is a perfect square and if it is between 40 and 100. Hint: Perfect squares and square roots are opposites. |
|
| Author: | Insectoid [ Sat Nov 08, 2008 11:06 am ] |
| Post subject: | RE:Hard Question |
You need to use the square root function (sqrt ()?) and modulus inside a loop. (modulus determines the remainder after you divide something) |
|
| Author: | Vermette [ Sat Nov 08, 2008 12:29 pm ] |
| Post subject: | Re: RE:Hard Question |
insectoid @ November 8th 2008, 11:06 wrote: You need to use the square root function (sqrt ()?) and modulus inside a loop. (modulus determines the remainder after you divide something)
There's only 3 perfect squares between 40 and 100... |
|
| Author: | Insectoid [ Sat Nov 08, 2008 12:54 pm ] |
| Post subject: | RE:Hard Question |
The point of the program is to determine if a number is a square or not. Hard-coding in the squares if you already know will teach nothing and not accomplish anything. |
|
| Author: | jbking [ Sat Nov 08, 2008 3:24 pm ] |
| Post subject: | RE:Hard Question |
Here's another question to ask: Is 100 a valid input to terminate the program on, i.e. does between imply a strict inequality or not? |
|
| Author: | andrew. [ Sat Nov 08, 2008 8:06 pm ] |
| Post subject: | RE:Hard Question |
Think about this. Modulus (mod in Turing) returns the remainder after dividing something. So that means if the remainder is 0, then the numbers divide evenly (no decimals). Well, you could check the inputted number modulus another number. What other number would make sense to equal 0 if it is a square root? |
|
| Author: | OneOffDriveByPoster [ Sat Nov 08, 2008 9:24 pm ] |
| Post subject: | Re: RE:Hard Question |
insectoid @ Sat Nov 08, 2008 12:54 pm wrote: The point of the program is to determine if a number is a square or not. Hard-coding in the squares if you already know will teach nothing and not accomplish anything. But you can write code to give you a list of perfect squares, store it and then check against it. Note: no mod.
(Edit: add note) |
|
| Author: | A.J [ Tue Nov 11, 2008 6:14 pm ] |
| Post subject: | Re: Hard Question |
well, i don't know about mods, but there's one observation that might help.... what do we know about perfect squares?.....that their square roots are integers!!! so, to determine if a number is a perfect square, all u would have to do is check if its square root is an integer (hint: sqrt(x) returns the square root of 'x', and round(x) rounds 'x' to the nearest integer...and since round(x) = x only when 'x' is an integer......catching my drift?) |
|
| Author: | Insectoid [ Tue Nov 11, 2008 8:42 pm ] |
| Post subject: | RE:Hard Question |
That's a good take on it, A.J, much simpler than what I had in mind. Just proves that for any problem there are many ways to solve it. |
|
| Author: | OneOffDriveByPoster [ Tue Nov 11, 2008 9:39 pm ] |
| Post subject: | Re: RE:Hard Question |
OneOffDriveByPoster @ Sat Nov 08, 2008 9:24 pm wrote: But you can write code to give you a list of perfect squares, store it and then check against it. Note: no mod. Or should I say no floating point... |
|
| Author: | delparnel [ Tue Nov 11, 2008 11:49 pm ] |
| Post subject: | Re: Hard Question |
THEOREM: An integer M is a perfect square if and only if M is the sum of N consecutive odd integers beginning with 1. Furthermore, M = N^2 . This is an interesting way to look at it if you're not comfortable working with mod. Using this theorem you should be able to derive the algorithm. Although, simply going by the fact that if N mod sqrt ( N ) = 0 then N is a perfect square, is a linear algorithm. |
|
| Author: | noobsauce [ Wed Nov 12, 2008 8:08 am ] |
| Post subject: | Re: Hard Question |
Just think of it this way. Make a counter and square it and then compare with the number user enter if it is smaller continue to add 1 and square again (repeat process) if it is equal, bingo we got a match, if it is bigger then the number is obivously not a root. No need using mod in this question... |
|
| Author: | Vermette [ Wed Nov 12, 2008 1:50 pm ] |
| Post subject: | Re: RE:Hard Question |
insectoid @ November 8th 2008, 12:54 wrote: The point of the program is to determine if a number is a square or not. Hard-coding in the squares if you already know will teach nothing and not accomplish anything.
I was critizing the problem itself; it is too narrowly defined to really accomplish anything and encourages exactly what I mentioned, which if this had to be dealt with in real world code is exaclty what would be done ot save time. Three quick n' dirty XOR comparisons. Why limit the range to 40 through 100? The problem as stated doesn't really ask for perfect squares to be entered, just any of 3 different numbers. IMposing a range was just artifical noise. "Determine if integer x is a perfect sqaure" would be much better incentive to create a generalized and useful solution to the problem. |
|
| Author: | Clayton [ Wed Nov 12, 2008 6:32 pm ] |
| Post subject: | Re: Hard Question |
noobsauce @ Wed Nov 12, 2008 8:08 am wrote: Just think of it this way. Make a counter and square it and then compare with the number user enter if it is smaller continue to add 1 and square again (repeat process)
if it is equal, bingo we got a match, if it is bigger then the number is obivously not a root. No need using mod in this question... That is a rather horridly inefficient way of doing it. If you think about it, you will be iterating for an interval of 1 .. n, a minimum of ceil (sqrt (n)) + ceil (sqrt (n - 1)) + ... + ceil (sqrt (1)) times. That adds up rather quickly once you start getting into ranges of 1 .. 10000+ |
|
| Author: | DanielG [ Wed Nov 12, 2008 9:59 pm ] |
| Post subject: | RE:Hard Question |
The ranges are only between 40 and 100 so manually checking if the number is between 40 and 100 and then checking if it is a perfect square (by for loop) will work. however, I would personally still use the sqrt(x) = round(sqrt(x)) method |
|
| Author: | A.J [ Thu Nov 13, 2008 1:27 pm ] |
| Post subject: | Re: Hard Question |
Thank u DanielG!!! someone likes my idea |
|
| Author: | delparnel [ Thu Nov 13, 2008 1:29 pm ] |
| Post subject: | Re: Hard Question |
A.J @ Thu Nov 13, 2008 1:27 pm wrote: Thank u DanielG!!!
someone likes my idea There are a lot more efficient ways to do it. |
|
| Author: | MannieFresh [ Thu Nov 13, 2008 5:24 pm ] | ||
| Post subject: | RE:Hard Question | ||
I finally finished it, thank you to everyone who helped me, here are the solution to the question:
|
|||
| Author: | OneOffDriveByPoster [ Thu Nov 13, 2008 5:41 pm ] |
| Post subject: | Re: RE:Hard Question |
Ouch... The suggestions here could help you get much better than that. |
|
| Author: | pavol [ Thu Nov 13, 2008 5:48 pm ] | ||
| Post subject: | Re: Hard Question | ||
oh dear...if that is the way you choose to solve your problem one thing you could definitely do is get rid of all those elsifs after you check the number 100 and substitute them for simply
|
|||
| Author: | CodeMonkey2000 [ Thu Nov 13, 2008 6:27 pm ] |
| Post subject: | RE:Hard Question |
0_o My eyes!! They burn!!!!!!!!!! That is a horrible solution!!!!!!! |
|
| Author: | delparnel [ Thu Nov 13, 2008 6:42 pm ] |
| Post subject: | Re: Hard Question |
That is the most disgusting solution I have ever seen. You were given about 3-4 methods of doing it, and you end up hard-coding the answers in? That solution deserves a 0. |
|
| Author: | Insectoid [ Thu Nov 13, 2008 7:29 pm ] |
| Post subject: | RE:Hard Question |
MannieFresh, you know what this assignment was meant to teach you, right? You were supposed to think mathematically, find out under what conditions a number is prime, make the program do the work for you. You have done the opposite. You didn't bother to think about it, and did the work the computer should have done. This program doesn't calculate prime numbers, it takes an input and spits out a pre-defined answer specifically tailored for that input. This is not programming. |
|
| Author: | Insectoid [ Thu Nov 13, 2008 7:31 pm ] |
| Post subject: | RE:Hard Question |
Sorry 'bout that. Was frustrated that after all that help, MannieFresh didn't learn a thing. That both disappoints and irritates me. Makes me wonder if this site is really any good (though past experiences have proved, time and again, that it is a great site) |
|
| Author: | MannieFresh [ Sun Nov 16, 2008 12:19 am ] |
| Post subject: | RE:Hard Question |
Okay, I know you all gave me good solutions, but like I said, I just started this so you think I knew excactly what to put in? And insectoid, not trying to be mean or rude I did learn, didn't I solved it? Yes I did. It's finished now, you guys all have shorter ways, I don't know how to do it but it only took me like 10-15 minutes, didn't really waste my time, enjoyed doing it acutally. |
|
| Author: | Zren [ Sun Nov 16, 2008 2:15 am ] |
| Post subject: | Re: Hard Question |
The solution in your eyes may not be so hard to do. After all, you only had to deal with 60 cases. The whole thing about questions like this is that they are instructional. Like in math class, the problems they give you are always different, but the format is still the same. The solution to finding the zeros (Factoring) of parabulas X^2+5x+1 and 23x^2+345632x+3 both utilize the method of solving (Quadratic formula). Using your method, you'd first have to find out the answers to the questions by punching out the numbers then putting them into a if-else statement. If your making this program for yourself in order to find out the answer to the question, why would you first find out the answer as well as large amount of other test cases. This method is ridiculous and ignores the purpose of needing the computer to solve the question. Although you did learn something, you also missed one of the main goals of the exercise. Congrats on half solving the one of them though. If you want a step by step explanation of our way, just ask. Btw, you never exited the program if x was a perfect number.) |
|
| Author: | DanielG [ Sun Nov 16, 2008 3:18 am ] |
| Post subject: | Re: Hard Question |
delparnel wrote: A.J @ Thu Nov 13, 2008 1:27 pm wrote: Thank u DanielG!!!
someone likes my idea There are a lot more efficient ways to do it. Can you name one? I don't think there is anything better then it, unless you are thinking of using floor or ceil, which wouldn't need to check what comes after the decimal; I can't think of anything else that would be faster, after all, last I checked nothing beats O(1) in terms of efficiency. Of course, compared to what MannieFresh wrote, anything that works is efficient. if you were trying to say that there a lot more of other methods that are also efficient (which now that I think about it makes more sense) then there probably are, but again none best this method. MannieFresh wrote: Okay, I know you all gave me good solutions, but like I said, I just started this so you think I knew excactly what to put in? And insectoid, not trying to be mean or rude I did learn, didn't I solved it? Yes I did. It's finished now, you guys all have shorter ways, I don't know how to do it but it only took me like 10-15 minutes, didn't really waste my time, enjoyed doing it acutally. your way works, yes, however, hard coding is bad! (unless it is a dwite contest and you are too lazy to code the BFS); and as Zren said, you missed the main point of the exercise (unless your teacher is a fan of hard coding). Also, you should never copy and paste when programming, at least not to that degree. Furthermore, I don't see what is hard with: Mod Edit: No just giving the answers. Thanks |
|
| Author: | A.J [ Sun Nov 16, 2008 12:18 pm ] |
| Post subject: | Re: Hard Question |
thats the best way that i can think of too, DanielG! DanielG wrote: delparnel wrote: A.J @ Thu Nov 13, 2008 1:27 pm wrote: Thank u DanielG!!!
someone likes my idea There are a lot more efficient ways to do it. Can you name one? I don't think there is anything better then it, unless you are thinking of using floor or ceil, which wouldn't need to check what comes after the decimal; I can't think of anything else that would be faster, after all, last I checked nothing beats O(1) in terms of efficiency. Of course, compared to what MannieFresh wrote, anything that works is efficient. if you were trying to say that there a lot more of other methods that are also efficient (which now that I think about it makes more sense) then there probably are, but again none best this method. MannieFresh wrote: Okay, I know you all gave me good solutions, but like I said, I just started this so you think I knew excactly what to put in? And insectoid, not trying to be mean or rude I did learn, didn't I solved it? Yes I did. It's finished now, you guys all have shorter ways, I don't know how to do it but it only took me like 10-15 minutes, didn't really waste my time, enjoyed doing it acutally. your way works, yes, however, hard coding is bad! (unless it is a dwite contest and you are too lazy to code the BFS); and as Zren said, you missed the main point of the exercise (unless your teacher is a fan of hard coding). Also, you should never copy and paste when programming, at least not to that degree. Furthermore, I don't see what is hard with: Mod Edit: Removing answer from quotes |
|