Re: how many times nestwhile repeats calculations?
- To: mathgroup at smc.vnet.net
- Subject: [mg71970] Re: [mg71944] how many times nestwhile repeats calculations?
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Wed, 6 Dec 2006 06:04:03 -0500 (EST)
- References: <200612051104.GAA27219@smc.vnet.net>
On 5 Dec 2006, at 20:04, Arkadiusz.Majka at gmail.com wrote:
> Hi,
>
>> From Help Browser we read
>
> NestWhile[f, expr, test] starts with expr, then repeatedly applies f
> until applying test to the result no longer yields True.
>
> I would like to know how many times f is applied till test stops
> everything. I read already a post in which the following example was
> presented
>
> In[1]:= mysqrt[a_] := NestWhile[{#[[1]]+1, .5(#[[2]]+a/#[[2]])}&,
> {0, Max[1.,N@a]}, #[[2]]^2 > a&]
> In[2]:= mysqrt[2]
> Out[2]= {5, 1.41421}
>
>
> Do you know any other method? I don't want to influence to NestWhile
> (by inserting a "counting" element #[[1]]+1 )
>
> Thanks,
>
> Arek
>
mysqrt[a_] := With[{v = NestWhileList[0.5*(#1 + a/#1) & ,
Max[1., N[a]], #1^2 > a & ]},
{Length[v] - 1, Last[v]}]
mysqrt[2]
{5, 1.414213562373095}
Andrzej Kozlowski
- References:
- how many times nestwhile repeats calculations?
- From: Arkadiusz.Majka@gmail.com
- how many times nestwhile repeats calculations?