New answers tagged

10

IntegerExponent IntegerExponent[120, #] & /@ {2, 3, 5, 7} (* {3, 1, 1, 0} *)


2

System` is a context, not a library. In other languages, this is called a namespace. Therefore, technically speaking, the fact that a symbol is part of the System` context does not mean that it is built-in. You can easily create symbols in the System` context. In practice, however, this is often a good-enough approximation to find built-in symbols. To ...


2

The problem with some functions and performance is that they unpack packed arrays. When the packed array is large, the unpacked array is larger and may exhaust the memory resources available. Functions such as MemberQ, FreeQ, FirstPosition, etc. unpack, but AnyTrue does not unpack. This is the principal difference in speed on an integer array. Packed ...


2

You are much better off using Not[FreeQ[expr, form]] in place of MemberQ in almost all cases. MemberQ has a number of different problems, including the tests it uses for equality and the depth of its search into expr, and I don't remember the specifics of them, that you are justified in ignoring it completely. This is not to disparage the other answers ...


0

You could post-process the definitions: ClearAll[FixMinusPattern]; SetAttributes[FixMinusPattern, HoldAll]; FixMinusPattern[fn_] := DownValues[fn] = Quiet@ReplaceAll[DownValues[fn], HoldPattern[ Verbatim[HoldPattern][ fn[PatternSequence[otherArgs1___, Verbatim[Pattern][a_, type_], otherArgs2___, -1*Verbatim[Pattern][a_, ...


0

A slightly pointless way of solving the problem with a single variable... G[X : Repeated[_, {2}] /; {X} . {1, 1} == 0] := 0 G[4, -4] (* 0 *)


6

list1 = Range[10^8]; list2 = RandomInteger[10^10, 10^8]; list3 = RandomInteger[10, 10^8]; elem = 5; MemberQ[list1, elem] // AbsoluteTiming MemberQ[list2, elem] // AbsoluteTiming MemberQ[list3, elem] // AbsoluteTiming memberQCompiled = Compile[{{a, _Integer, 1}, {e, _Integer}}, MemberQ[a, e]]; memberQCompiled[list1, elem] // AbsoluteTiming memberQCompiled[...


7

One possibility for lists of integers is to use Clip: iMemberQ[l_, s_] := s == Max @ Clip[l, {s, s}, {s-1, s-1}] For your examples: L = Range[10^8]; iMemberQ[L, 1] //AbsoluteTiming iMemberQ[L, 10^8] //AbsoluteTiming iMemberQ[L, 10^9] //AbsoluteTiming {0.729907, True} {0.648677, True} {0.631598, False} L = RandomSample[Range[10^8]]; iMemberQ[L, 1] //...


4

It's instructive to look at the output of TracePrint. Length[Unevaluated[1 + 1 + 1 + 1]] // TracePrint (* Out: *) Length[Unevaluated[1+1+1+1]] Length Length[1+1+1+1] 4 Whereas f[Unevaluated[1 + 1 + 1 + 1]] // TracePrint (* Out: *) f[Unevaluated[1+1+1+1]] f f[1+1+1+1] Length[1+1+1+1] Length 1+1+1+1 Plus 1 1 1 1 4 Length[4] ...


Top 50 recent answers are included