[Date Index]
[Thread Index]
[Author Index]
Re: count zeros in a number
*To*: mathgroup at smc.vnet.net
*Subject*: [mg121859] Re: count zeros in a number
*From*: Richard Fateman <fateman at eecs.berkeley.edu>
*Date*: Wed, 5 Oct 2011 04:01:07 -0400 (EDT)
*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com
*References*: <201110020636.CAA28027@smc.vnet.net> <j6brvm$8om$1@smc.vnet.net> <201110040530.BAA20698@smc.vnet.net> <op.v2t544pxtgfoz2@bobbys-imac.local>
On 10/4/2011 9:44 AM, DrMajorBob wrote:
> There's nothing "esoteric" about IntegerDigits, Replace, or Repeated,
> as in:
>
> x = 24^24*55^55;
> Replace[IntegerDigits@x, {__, zeroes : 0 ..} :>
> Length@{zeroes}] // Timing
>
> {0.000185, 55}
My feeling is that these ARE esoteric. A person just introduced to
Mathematica would probably not encounter IntegerDigits, Repeated, or
patterns other than the trivial one used in pseudo function definitions as
f[x_]:= x+1.
>
> If we wanted brute-force arithmetic, we might use Fortran.
>
> Your suggested solutions are NOT greatly hindered by "the slow
> implementation of looping constructs in Mathematica", on the other hand:
>
> (n = 0; While[GCD[x, 10^n] == 10^n, n++]; n - 1) // Timing
>
> {0.000029, 0}
huh? I got {0., 55}. Perhaps my computer is faster or has a lower
resolution timer, but we should both get 55.
>
> (n = 0; While[Mod[x, 10] == 0, (x = x/10; n++)]; n) // Timing
>
> {0.000018, 0}
I mention the slow implementation of looping constructs simply to warn
people that if you can resolve your computation by simple composition of
built-in functions e.g. F[G[H[x]]] and don't rely on Mathematica
to efficiently execute loops written as While[] For[] etc, you are
probably going to run faster. Let Mathematica's operation on compound
objects like lists do the job. Length[] is a lot faster than counting
with a While etc.
In my timings, doing the operations 1000 times..
Do[.....,{1000}]//Timing
I found that my solutions were about 10X faster than the IntegerDigits
one, on this particular value of x.
Fortran can't be used unless you manage arbitrary precision integers...
RJF
Prev by Date:
**Re: 3d EPS export files huge on Mac OS X, not so much on PCs**
Next by Date:
**Re: DynamicModule Pure Function**
Previous by thread:
**Re: count zeros in a number**
Next by thread:
**Re: count zeros in a number**
| |