Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Hard Question
Index -> Programming, Turing -> Turing Help
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MannieFresh




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Euphoracle




PostPosted: 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.
MannieFresh




PostPosted: Sat Nov 08, 2008 9:53 am   Post subject: RE:Hard Question

Alright, I'll see if I can do it. Thanks.
Jurica




PostPosted: Sat Nov 08, 2008 10:22 am   Post subject: RE:Hard Question

What don't you understand?
andrew.




PostPosted: 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.
Insectoid




PostPosted: 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)
Vermette




PostPosted: 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...
Insectoid




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
jbking




PostPosted: 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?
andrew.




PostPosted: 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?
OneOffDriveByPoster




PostPosted: 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)
A.J




PostPosted: 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?)
Insectoid




PostPosted: 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.
OneOffDriveByPoster




PostPosted: 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...
delparnel




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 3  [ 32 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: