Sign in to follow this  
Followers 0
Marek(-|-)

confusing squareroot script

10 posts in this topic

I need to make this program that asks for a number to be square rooted. Then the program needs to find the square root of the number. But the catch is that I dont want to to solve the square root unless it is a perfect square. If the number isnt a perfect square then I need it to simplify the number. For example, you input the number 32. Since 32 isnt a perfect square it has to break it down. So it would have to run all of the possible perfect squares agains the number, if the returned value is a decimal then we know that that possibility can be eliminated. Then once we find the highest perfect square that goes into the original number without any decimal remainders we find the square root of the number that worked, and pull that outside the radical. So it would go from (rad)32(endrad) to 4(rad)2(endrad). I cant use the rad symbol in here so that should suffice. I've been working on this one for quite some time now and have had only slight success. If you can donate any information that could help me, that would be great.

I would post the code that I have so far in here but it is over 200 lines and that might be a little pointless as it is mostly repetitive, but if you would like to see what I have then just ask. And another piece of information, I only tested the entered number on the first 30 perfect squares, the first 30 should be enough for the job, so there is no need to exceed that ammount.

Thanks again to anyone who can help me out.


(-|_) -|- ( _|-)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Edit: Fixed for-loop. (I had the lower bound as 4 instead of 2; consequently, Sqrt(8) would not fully simplify.)

;keep entering values until you click cancel...
While 1

   $x = InputBox("Example", "Enter a number")
   If @error Then Exit
      
   If Sqrt($x) = Int(Sqrt($x)) Then 
      MsgBox(4096,"Result", "The square root is " & Sqrt($x))
   Else
      For $i = Int(Sqrt($x)) to 2 step -1
         If $x/($i^2) = Int($x/($i^2)) Then 
            MsgBox(4096,"Result", "The square root is " & $i & " * sqrt(" & $x/($i^2) & ")")
            ExitLoop
         EndIf
      Next
      If $i < 4 Then MsgBox(4096,"Result", "Result is sqrt(" & $x & ")")
   EndIf
   
WEnd
Edited by CyberSlug

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

CyberSlug,

That is like the single most impressive thing I have ever seen. I was working on this for the good part of 4 hours and I had come up with a massive amount of code that obviously was completely irrelevant. I had exactly 204 lines of garbage at the end of those 4 hours when I asked for help. Thanks to you, that 204 lines has been condensed into a neat 15 lines. Now all I am asking is that you explain this wonder so that I can at least claim to understand the 15 lines of magic.

PS: If you would still like to view my code and laugh at it, that is the least I can do.


(-|_) -|- ( _|-)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Oops, I think the for loop should be For $i = Int(Sqrt($x)) to 2 step -1

Sqrt($x) = Int(Sqrt($x)) ;Check if the square root is an integer

If not an integer, then try dividing $x by all perfect squares less than $x. I use a For-loop to do this, but notice that the value I care about is $i^2.

So if my number $x is 32, for example, $i decrements from 5 to 2. Thus $i^2 goes from 25 to 4. Consequently, I the loop would check 32/25, 32/16, 32/9, and 32/4. Actually when it hits 32/16, the loop stops because the result of the division is an integer. Namely32/16 = 2 which is the same as saying 32 = 16 * 2

Mathematically, we want SQRT(32) which we determined equals SQRT(16 * 2) so we just write that as SQRT(16) * SQRT(2) or simply 4 * SQRT(2).

The last condition to check is if the For-loop completes without printing a message. I do this by checking if $i < 4.... In this case, you have a number that cannot be simplified such as Sqrt(21) or something.

Edited by CyberSlug

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

Oops, I think the for loop should be For $i = Int(Sqrt($x)) to 2 step -1

Sqrt($x) = Int(Sqrt($x))  ;Check if the square root is an integer

If not an integer, then try dividing $x by all perfect squares less than $x.  I use a For-loop to do this, but notice that the value I care about is $i^2.

So if my number $x is 32, for example,  $i decrements from 5 to 2.  Thus $i^2 goes from 25 to 4.  Consequently, I the loop would check 32/25, 32/16, 32/9, and 32/4.  Actually when it hits 32/16, the loop stops because the result of the division is an integer.  Namely32/16 = 2 which is the same as saying 32 = 16 * 2

Mathematically, we want SQRT(32) which we determined equals SQRT(16 * 2) so we just write that as SQRT(16) * SQRT(2)  or simply 4 * SQRT(2).

The last condition to check is if the For-loop completes without printing a message.  I do this by checking if $i < 4.... In this case, you have a number that cannot be simplified such as Sqrt(21) or something.

<{POST_SNAPBACK}>

Is not 1 the perfect square root of 1? Shouldn't the loop step down to 1?

Phillip

Share this post


Link to post
Share on other sites

Is not 1 the perfect square root of 1?  Shouldn't the loop step down to 1?

<{POST_SNAPBACK}>

Every number is divisible by one, so it's inefficient. Also it causes undesired behavior on inputs such as 2 and 3 and 5 etc...

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Ok, I think I understand how it works now. My original plan was about the same as yours, except I actually used the perfect squares, so instead of testing the initial value against all perfect squares less than the initial value I tested it against all perfect squares. And then I got all confused about how to solve it after that. Thanks again for all of your help.

There is just one more thing that would make this perfect. I need it to work with fractions and with negative numbers, or imaginary numbers. Originally I have gotten this part to work but haven't been able to incoperate my part with this one. For example the number -32 or 3/4 need to be solved to equal 4isqrt(2) and 2sqrt(3)/4. Also for it to reduce the end fraction, from 2sqrt(3)/4 to sqrt(3)/2. Also if you could include comments or something like you've done before so that I'm not too lost in the new code. Thanks again.

Edited by Marek(-|-)

(-|_) -|- ( _|-)

Share this post


Link to post
Share on other sites

No? I thought you could be able to help me out... thanks anyways.


(-|_) -|- ( _|-)

Share this post


Link to post
Share on other sites

bumP


-I am the giver of life and the bringer of death.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Uhh this is kind of late.. but I was just using your square root code for another project I'm working on.. I just have one problem with it: I don't get how you define the value of $i. I'm not sure if this is a reasonable question, but I don't understand how you were able to make this work. I think the "For $i = Int(Sqrt($x)) to 2 step -1" is what confused the heck out of me. If you could please clarify this that would be great. Thanks again.

Edit: Actually nevermind, I just figured it out after staring at it for a few moments.

Edited by Marek(-|-)

(-|_) -|- ( _|-)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0