Hot answers tagged idiomatic-usage
11
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]]
]
...
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., ...
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