# Square root algorithm

10 messages
Open this post in threaded view
|

## Square root algorithm

 Hi,To help me in learning Haskell I started blogging about some of the things I’ve looked at. One such topic was calculating square roots ‘by hand’ and then deriving a Haskell algorithm. I wrote about the well known  technique herehttp://gitcommit.co.uk/2017/08/25/the-root-of-the-problem-part-1/and it it is really quite a simple method. The second part of the post will be an implementation in Haskell. I then tried implementing it  and got something that works but really its not very pleasant to look at! And its something I don’t want to post! Some parts are fine but I think I locked myself into the notion that it had to be using State and  really the end result is pretty poor. I know this i perhaps a ‘big ask’ but I’d really appreciate any suggestions, solutions, hints etc. I will of course give full attribution. I’ve created a gist of the code herehttps://gist.github.com/banditpigMany ThanksMike_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: Square root algorithm

 I don't know why this email was marked as spam, but it could be worth discussing.On Sun, Sep 3, 2017 at 4:22 PM, mike h wrote:Hi,To help me in learning Haskell I started blogging about some of the things I’ve looked at. One such topic was calculating square roots ‘by hand’ and then deriving a Haskell algorithm. I wrote about the well known  technique herehttp://gitcommit.co.uk/2017/08/25/the-root-of-the-problem-part-1/and it it is really quite a simple method. The second part of the post will be an implementation in Haskell. I then tried implementing it  and got something that works but really its not very pleasant to look at! And its something I don’t want to post! Some parts are fine but I think I locked myself into the notion that it had to be using State and  really the end result is pretty poor. I know this i perhaps a ‘big ask’ but I’d really appreciate any suggestions, solutions, hints etc. I will of course give full attribution. I’ve created a gist of the code herehttps://gist.github.com/banditpigMany ThanksMike_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: Square root algorithm

 In reply to this post by Mike Houghton One approach One function to compute the next iterateAnother function to call the computation function until results are within some tolerance It's usually presented as separation of control and computation 😎--Sent from an expensive device which will be obsolete in a few monthsCaseyOn Sep 3, 2017 1:23 AM, "mike h" <[hidden email]> wrote:Hi,To help me in learning Haskell I started blogging about some of the things I’ve looked at. One such topic was calculating square roots ‘by hand’ and then deriving a Haskell algorithm. I wrote about the well known  technique herehttp://gitcommit.co.uk/2017/08/25/the-root-of-the-problem-part-1/and it it is really quite a simple method. The second part of the post will be an implementation in Haskell. I then tried implementing it  and got something that works but really its not very pleasant to look at! And its something I don’t want to post! Some parts are fine but I think I locked myself into the notion that it had to be using State and  really the end result is pretty poor. I know this i perhaps a ‘big ask’ but I’d really appreciate any suggestions, solutions, hints etc. I will of course give full attribution. I’ve created a gist of the code herehttps://gist.github.com/banditpigMany ThanksMike_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: Square root algorithm

 Thanks I’ll look into that.To recap I have an unattractive but working implementation here https://gist.github.com/banditpigand the algorithm is described here  http://gitcommit.co.uk/2017/08/25/the-root-of-the-problem-part-1/ThanksMikeOn 9 Sep 2017, at 05:49, KC <[hidden email]> wrote:One approach One function to compute the next iterateAnother function to call the computation function until results are within some tolerance It's usually presented as separation of control and computation 😎--Sent from an expensive device which will be obsolete in a few monthsCaseyOn Sep 3, 2017 1:23 AM, "mike h" <[hidden email]> wrote:Hi,To help me in learning Haskell I started blogging about some of the things I’ve looked at. One such topic was calculating square roots ‘by hand’ and then deriving a Haskell algorithm. I wrote about the well known  technique herehttp://gitcommit.co.uk/2017/08/25/the-root-of-the-problem-part-1/and it it is really quite a simple method. The second part of the post will be an implementation in Haskell. I then tried implementing it  and got something that works but really its not very pleasant to look at! And its something I don’t want to post! Some parts are fine but I think I locked myself into the notion that it had to be using State and  really the end result is pretty poor. I know this i perhaps a ‘big ask’ but I’d really appreciate any suggestions, solutions, hints etc. I will of course give full attribution. I’ve created a gist of the code herehttps://gist.github.com/banditpigMany ThanksMike_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners _______________________________________________Beginners mailing list[hidden email]http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: Square root algorithm

 In reply to this post by KC I’m sort of seeing this as a foldr over a list of digit pairs with the seed value of the fold being the first step of the algorithm (which is different from the other steps).The list of pairs will be of indeterminate length as ’00’ is used until the required number of digits in the result is achieved.A minor wrinkle with this is if the input number is a perfect square then a lot of ‘0’s would be in the result.Then, after the fold,  apply some simple function to determine where the decimal point should go.MOn 9 Sep 2017, at 05:49, KC <[hidden email]> wrote:One approach One function to compute the next iterateAnother function to call the computation function until results are within some tolerance It's usually presented as separation of control and computation 😎--Sent from an expensive device which will be obsolete in a few monthsCaseyOn Sep 3, 2017 1:23 AM, "mike h" <[hidden email]> wrote:Hi,To help me in learning Haskell I started blogging about some of the things I’ve looked at. One such topic was calculating square roots ‘by hand’ and then deriving a Haskell algorithm. I wrote about the well known  technique herehttp://gitcommit.co.uk/2017/08/25/the-root-of-the-problem-part-1/and it it is really quite a simple method. The second part of the post will be an implementation in Haskell. I then tried implementing it  and got something that works but really its not very pleasant to look at! And its something I don’t want to post! Some parts are fine but I think I locked myself into the notion that it had to be using State and  really the end result is pretty poor. I know this i perhaps a ‘big ask’ but I’d really appreciate any suggestions, solutions, hints etc. I will of course give full attribution. I’ve created a gist of the code herehttps://gist.github.com/banditpigMany ThanksMike_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners _______________________________________________Beginners mailing list[hidden email]http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: Square root algorithm

 In reply to this post by KC Why is it the sqrt0 function is so much slower than sqrt1. Does the where clause allow intermediate values to be stored?Regards,Patsqrt0 :: Int -> Intsqrt0 0 =  0 sqrt0 1 =  1 sqrt0 n =   ((sqrt0 (n - 1)) + (n `quot` sqrt0 (n-1))) `quot` 2 -- sqrt0 25 several minutessqrt1 :: Int -> Intsqrt1 n  | n ==  0              = 0 | n ==  1              = 1  | otherwise            = div (k + ( div n k)) 2 where k = sqrt1(n-1)-- sqrt1 25 instantOn 9 September 2017 at 05:49, KC wrote:One approach One function to compute the next iterateAnother function to call the computation function until results are within some tolerance It's usually presented as separation of control and computation 😎--Sent from an expensive device which will be obsolete in a few monthsCaseyOn Sep 3, 2017 1:23 AM, "mike h" <[hidden email]> wrote:Hi,To help me in learning Haskell I started blogging about some of the things I’ve looked at. One such topic was calculating square roots ‘by hand’ and then deriving a Haskell algorithm. I wrote about the well known  technique herehttp://gitcommit.co.uk/2017/08/25/the-root-of-the-problem-part-1/and it it is really quite a simple method. The second part of the post will be an implementation in Haskell. I then tried implementing it  and got something that works but really its not very pleasant to look at! And its something I don’t want to post! Some parts are fine but I think I locked myself into the notion that it had to be using State and  really the end result is pretty poor. I know this i perhaps a ‘big ask’ but I’d really appreciate any suggestions, solutions, hints etc. I will of course give full attribution. I’ve created a gist of the code herehttps://gist.github.com/banditpigMany ThanksMike_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners This email originated from DIT. If you received this email in error, please delete it from your system. Please note that if you are not the named addressee, disclosing, copying, distributing or taking any action based on the contents of this email or attachments is prohibited. www.dit.ieIs ó ITBÁC a tháinig an ríomhphost seo. Má fuair tú an ríomhphost seo trí earráid, scrios de do chóras é le do thoil. Tabhair ar aird, mura tú an seolaí ainmnithe, go bhfuil dianchosc ar aon nochtadh, aon chóipeáil, aon dáileadh nó ar aon ghníomh a dhéanfar bunaithe ar an ábhar atá sa ríomhphost nó sna hiatáin seo. www.dit.ieTá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to Grangegorman_______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Open this post in threaded view
|

## Re: Square root algorithm

Open this post in threaded view
|