Tag Info

New answers tagged

4

Dan Fortunato and I made use of Compile for this one. index = Compile[{{p, _Integer}, {r, _Integer}, {n, _Integer}}, If[p <= r, r + Quotient[n, r] - p, Quotient[n, p] ], CompilationTarget -> "C", Parallelization -> True, RuntimeOptions -> "Speed" ]; PrimeSum = Compile[{{n, _Integer}}, Module[{r = Floor[Sqrt[n]], V, S, p = 2, ...


7

Here's a direct translation of the code you linked: Module[{n = 10^9, r, v, p, sp}, r = Floor@Sqrt@n; v = Table[Floor@(n/i), {i, 1, r}] ~Join~ Range[-1 + Floor@(n/r), 1, -1]; ClearAll[s]; Scan[(s[#] = (# (# + 1))/2 - 1) &, v]; For[p = 2, p <= r, ++p, If[s[p] > s[p - 1], Scan[(s[#] -= p (s[Floor[#/p]] - s[p - 1])) &, ...


9

No time for a full answer but with four cores in ParallelSum: ParallelSum[Prime@i, {i, PrimePi[10^9]}] // AbsoluteTiming MaxMemoryUsed[] {55.366167, 24739512092254535} 108337144 Pretty fast and very little RAM required. A modest refactoring of wxffles's code in a more native style including rasher's suggestion: Module[{n = 10^9, r, v, s}, r = ...


2

I have two efficiency increase approaches using existing functions. Distribute the load evenly over the available kernels in parallel. This has gains in speed but memory usage will be about the same. Distribute the load evenly over time. This significantly reduces memory usage but increases time. Capture the number of primes we want to sum. nn = ...


0

I encountered the same question. My approach is to take partial derivatives w.r.t. all variables to be linearized to construct the linearized expression, plus the constant term: (* Expression to be linearized in da, db, dc *) expr = da*db + da^2 + db^3 + dc + da*dc^2 + 12 (* Make List of all vars to be linearized *) ds = {da, db, dc}; (* Create list of ...


1

Pick[ln, Tr /@ StringPosition[nm, #], 1] &@"dow" Pick[ln, Tr /@ StringPosition[nm, #], 1] &@{"dow", _ ~~ "at"} Let's you use single and lists of targets, patterns (unlike straight pick)... assumptions about list correspondence apply, returns results in order of nm.


6

Another option: AssociationThread[nm -> ln]["tat"] If you store AssociationThread[lm -> ln] in a symbol you can use it for many quick lookups without having to recreate the association every time. Without Pick and AssociationThread you might do something like Identity @@ Cases[Transpose[{nm, ln}], {"tat", v_} :> v] Note that I'm using ...


10

if the keys are unique, then this seems to be rather elegant, short and still clear: Pick[ln, nm, "tat"] probably look at the documentation of Pick...


3

How about this? SelectFirst[ln, StringMatchQ[#, "tat" ~~ __] &] Admittedly my method is not as elegant as others, but it's fast if your ln and nm are large: ln = ConstantArray[t, 10^5] /. t :> StringJoin@RandomSample[CharacterRange["A", "z"], 10]; nm = StringTake[#, 3] & /@ ln; The following Timing[SelectFirst[ln, StringMatchQ[#, nm[[1]] ~~ ...



Top 50 recent answers are included