Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

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

Search the Archive

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


  • 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?