MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: how many times nestwhile repeats calculations?


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


  • Prev by Date: How can I get this spiked Integral evaluated???
  • Next by Date: Re: how many times nestwhile repeats calculations?
  • Previous by thread: how many times nestwhile repeats calculations?
  • Next by thread: Re: how many times nestwhile repeats calculations?