Author |
Message |
MannieFresh
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Euphoracle
![](http://compsci.ca/v3/uploads/user_avatars/11170373664bf5f25f636f1.png)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
MannieFresh
|
Posted: Sat Nov 08, 2008 9:53 am Post subject: RE:Hard Question |
|
|
Alright, I'll see if I can do it. Thanks. |
|
|
|
|
![](images/spacer.gif) |
Jurica
|
Posted: Sat Nov 08, 2008 10:22 am Post subject: RE:Hard Question |
|
|
What don't you understand? |
|
|
|
|
![](images/spacer.gif) |
andrew.
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: 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) |
|
|
|
|
![](images/spacer.gif) |
Vermette
![](http://compsci.ca/v3/uploads/user_avatars/637825944810b5d4444c6.jpg)
|
Posted: 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... |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
jbking
|
Posted: 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? |
|
|
|
|
![](images/spacer.gif) |
andrew.
|
Posted: 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? |
|
|
|
|
![](images/spacer.gif) |
OneOffDriveByPoster
|
Posted: 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) |
|
|
|
|
![](images/spacer.gif) |
A.J
![](http://compsci.ca/v3/uploads/user_avatars/119833057151651227b0d87.gif)
|
Posted: 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?) |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
OneOffDriveByPoster
|
Posted: 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... |
|
|
|
|
![](images/spacer.gif) |
delparnel
![](http://compsci.ca/v3/uploads/user_avatars/980326151490c8c77b2278.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
|