Tag Info

Hot answers tagged

27

For general large integers n, I don't know if there's a better method than Min[IntegerExponent[n, 5], IntegerExponent[n, 2]]. Or more compactly, IntegerExponent[n, 10] or IntegerExponent[n].


12

If you are strictly interested in the number of trailing zeros in factorials $n!$, as the example in your question suggests, then consider the number of pairs of 2 and 5 in all the factors of numbers 1 through $n$. There is always a 2 to match a 5, so the number of fives gives the number of zeros. Integers divisible by 5 contribute one 5 to the total. ...


10

Very good question / problem. Generally, this problem seems to belong to the class of problems where Compile is the best choice if maximum efficiency is looked for, since it is, by its nature, not a good fit for the Mathematica paradigm of working with lots of data at once. However, your last solution can be, in a somewhat modified form, brought to the same ...


9

Here is a recursive divide-and-conquer. There are probably nicer ways to code it. trailingZeros[n_, b_] := Module[ {scale=Log[b,N[n]], sqrt, ndigits}, If [scale<1, Return[0]]; sqrt = Ceiling[scale/2]; ndigits = IntegerDigits[n, b^sqrt, 2]; If [Last[ndigits]==0, sqrt + trailingZeros[First[ndigits],b], trailingZeros[Last[ndigits], b]] ] ...


7

Another possibility is definition of your own types ClearAll[nxo, oxn]; nxo /: Transpose[nxo[n_]] := oxn[n]; oxn /: Transpose[oxn[n_]] := nxo[n]; nxo /: A_ .nxo[n_] /; Dimensions[A][[2]] == n := nxo@Dimensions[A][[1]]; oxn /: oxn[n_].A_ /; Dimensions[A][[1]] == n := oxn@Dimensions[A][[2]]; Note the space between A_ and .nxo. You can add much more rules ...


6

First thing that comes to mind is something like LengthWhile[Reverse@IntegerDigits[12345!], # == 0 &] Clearly a compiled version, procedural, must be faster but less MMA elegant


5

Specific to factorials: Tr@Floor@NestWhileList[#/5` &, #/5`, # > 1 &] & @ 12345 3082 Or shorter: Tr@Floor[# / 5`^Range@Log[5, #]] & @ 12345


5

Using Fold with Reap and Sow might be cleaner: ClearAll[f]; f = If[Total[{##}, Infinity] > 1, Sow[Length[Flatten[{##}]]]; ## &[], {##}] &; Fold[f, First@list, Rest@list]; // Reap // Last (* Out[1]= {{3, 2, 3}} *)


4

A little late, I guess, since I had similar ideas to both rm-rf and ybeltukov. I would try using one head to represent an empty matrix, and define what Dot etc. means via TagSetDelayed. You can add error messages for mismatched dimensions in the Condition test -- you'll certainly need them. ClearAll[emptyMatrix]; (* this comes up in the product of an ...


4

You can create $n\times 0$ matrices in Mathematica as follows: nx0[n_Integer] := ConstantArray[{}, n] Dimensions@nx0[10] (* {10, 0} *) This will also obey matrix multiplication (and perhaps a few other operations): IdentityMatrix[10].nx0[10] (* {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}} *) However, I don't know of a good way to natively represent ...


4

Possibly not fastest, but concise and using a simple iteration over the list. Or two iterations, if we do a cleanup step as in the Compile'd variants. subseqlens[ll_] := Reap[Module[{n = 0, tot = 0.}, Map[(n++; tot += #; If[tot > 1, tot = 0.; Sow[n]; n = 0]) &, ll]]][[-1, 1]] subseqlensC = Compile[{{ll, _Real, 1}}, Module[{n = 0, tot = 0., ...


3

Perhaps StringCases[ToString[12345!], {Longest[x : "0" ..] ~~ EndOfString} :> StringLength@x] Or, with regular expressions, StringCases[ToString[12345!], RegularExpression["(0+)$"] :> StringLength["$1"]]


2

You can use NestWhileList together with Accumulate : alist = {0.423768, 0.157558, 0.675251, 0.685209, 0.580772, 0.0230333, 0.927156, 0.506085, 0.0516773, 0.485349} ; This will return the position where you need to split the input list : NestWhileList[ {pos = Position[Accumulate[#[[2]]], _?(# >= 1 &)][[1, 1]]; pos + ...



Only top voted, non community-wiki answers of a minimum length are eligible