Currently using:
While[! Divisible[n = Quotient[n, 10], 10]]
Currently using:
While[! Divisible[n = Quotient[n, 10], 10]]
Fred,
Fails if n is divisible by 10.
n = 1234;
While[! Divisible[n = Quotient[n, 10], 10]]; n
(* 0 *)
n = 1204;
While[! Divisible[n = Quotient[n, 10], 10]]; n
(* 120 *)
n = 1230;
While[! Divisible[n = Quotient[n, 10], 10]]; n
(* 0 *)
I have not benchmarked, but these alternatives are probably slower for large n
IntegerDigits[n] // Position[0]
ToString[n] // StringPosition["0"]
I am not sure what you are trying to do with your code, it does not seem to work. Perhaps:
MemberQ[RealDigits[N[Pi, 10]][[1]], 0]
False
MemberQ[RealDigits[N[Pi, 50]][[1]], 0]
True
or maybe to see where 0s are:
Position[RealDigits[N[Pi, 100]][[1]], 0]
{{33}, {51}, {55}, {66}, {72}, {78}, {86}, {98}}
For the specific application I’m using n is never a multiple of 10,also n is 100 of thousand of digit long so those 2 implementation are horribly slower. Code is something like this:
For[i = 100000, i < 200000, i++, n = 2^i;
While[! Divisible[n = Quotient[n, 10], 10]];
If[IntegerLength[n] == 0, Print[i]]]
DigitCount[3^10232222,10,0]//AbsoluteTiming
In 0.8 seconds fast enough? I think it’s fast for numbers having a zero somewhere at the beginning
n = Prepend[Table[RandomInteger[{1, 9}], 400000], {1, 0}] // Flatten //
FromDigits;
runs in 0.03 seconds but your procedure takes very long (aborted)
DigitCount[3^10232222, 10, 0] // AbsoluteTiming
takes 1.7s in my computer vs 0.003s for
n = 3^10232222;
AbsoluteTiming[While[! Divisible[n = Quotient[n, 10], 10]]]
I don’t need the number of 0 in the number really just if theres any 0 in it.
n = Prepend[Table[RandomInteger[{1, 9}], 400000], {1, 0}] // Flatten // FromDigits;
Then compare
DigitCount[n, 10, 0] // AbsoluteTiming
and
AbsoluteTiming[While[! Divisible[n = Quotient[n, 10], 10]]]
in the vast majority of case you will hit a 0 in the first few division for a normal number with random digit, the case of 400000 digit all of then no 0 will basically never happen.
Then I would propose to use the IntegerString combined with StringContainsQ
A 1000000 digit number to string convertion takes 0.13 Seconds to convert.
Using StringContainsQ is almost instantanous.
(s = IntegerString[2^4000000, 10];
StringContainsQ[s, "0"];) // AbsoluteTiming
takes 0.136 seconds
The highly improbable no zero 1000000 digits
range=CharacterRange["1","9"];
s2 = RandomChoice[range, 1000000] // StringJoin;
StringContainsQ[s2, "0"] // AbsoluteTiming
is False within 0.00009 seconds. Pretty impressive I would think
a 500000 digit number
(s = IntegerString[3^999999, 10];
StringContainsQ[s, "0"];) // AbsoluteTiming
in 0.049 seconds