Skip to main content
32 votes
Accepted

Simulating molecular dynamics efficiently

Okay, here is a way to compute the forces much faster: We create a CompiledFunction (called getForces). It eats a list of points ...
Henrik Schumacher's user avatar
22 votes
Accepted

Better way to sort course grades

This is a great use for the Association data structure, which makes so many tasks in Mathematica that much more pleasant. First, we can just write out a ranking ...
Pillsy's user avatar
  • 18.6k
20 votes

Is there a more elegant way to write this statement?

Total[Range[CubeRoot[10000]]^3] 53361
Niki Estner's user avatar
  • 36.2k
18 votes
Accepted

Simplifying code that displays a square rolling inside a circle

EDIT 1. Fixed kernel crashing. The problem was WhenEvent choking, because it was continually checking whether the current pivot (which lies exactly on the bounding ...
yawnoc's user avatar
  • 1,235
17 votes
Accepted

Problems with stress distribution using FEM

Your boundary conditions seem to be not quite correct according to the mechanical problem. Sorry, I don't have the time to go through your code today, but I got a version running, although this will ...
Mauricio Fernández's user avatar
16 votes
Accepted

How to avoid procedural loops in this example?

As is demonstrated very well in this post you can use a criteria for your pattern, thereby only applying your function as long as you are searching and not to all elements. Also there is a specific <...
Quantum_Oli's user avatar
  • 8,014
16 votes
Accepted

Sound synthesizer using Manipulate

First let me observe that your coding style makes debugging difficult, I highly recommend breaking giant expressions into manageable pieces. Second, in the code below I have used a different ...
Simon Woods's user avatar
  • 85.2k
16 votes
Accepted

The most efficient algorithm to find all possible integer pairs which sum to a given integer

I think IntegerPartitions[m, {2}, listOfIntegers] does exactly what you want, and seems pretty efficient.
Roman's user avatar
  • 49k
16 votes

Simplify the code

☺lookMaNoLetters☺ = 1 ## & @@@ # & /@ # &; ☺lookMaNoLetters☺ @ mylist {{y1 y2 y3, y3 y4 y5}, {w1 w2 w3, w4 w5 w6}} Further variations: ...
kglr's user avatar
  • 399k
15 votes
Accepted

How to check the validity of an option value

Implementation This is indeed an important problem. It is usually best to have a separate function testing various options. Here is the solution I propose: a wrapper that would factor out the testing ...
Leonid Shifrin's user avatar
14 votes
Accepted

Enneper's minimal surface

The trick here is to use the plotting function to generate the mesh lines, but there is no way to apply a ColorFunction for a ...
Jason B.'s user avatar
  • 69.3k
13 votes

How to avoid procedural loops in this example?

SelectFirst would work: SelectFirst[Range[10], f[#] == 10 &] You can tell that it only executes ...
Gerli's user avatar
  • 1,051
13 votes

Optimizing Mathematica Code

Here is an optimized version of your function ...
TimRias's user avatar
  • 3,180
13 votes
Accepted

Brute Force for solving Diophantine equation

In version 10.1 on my circa 2011 PC: ...
Mr.Wizard's user avatar
  • 272k
12 votes
Accepted

Optimizing Mathematica Code

A version of mmeent's code in a more functional style: ...
Simon Woods's user avatar
  • 85.2k
12 votes
Accepted

Reduce Code Length

since the result of the new computation depends on the previous one we can use NestList ...
Ali Hashmi's user avatar
  • 9,020
12 votes

Simulating molecular dynamics efficiently

To expand on @HenrikSchumacher's comment, compare: ...
Carl Woll's user avatar
  • 131k
12 votes
Accepted

Simplify the code

Since you appear to want to multiply the list only when all of its elements are atomic, how about just mylist /. {s__?AtomQ} :> Times[s] It appears to be the ...
Shredderroy's user avatar
  • 5,281
12 votes

Using Mathematica to derive analytic form of single variable function

If one makes a table of the first few values of $n$, then a pattern becomes evident: Table[{n, f[n]}, {n, 3, 8}] // TableForm One can see that the leading term is ...
JimB's user avatar
  • 42.1k
12 votes
Accepted

Checking if a number is right sorted

f1 = OrderedQ @* Rest @* IntegerDigits; f1 /@ {51369, 51396} {True, False} ...
kglr's user avatar
  • 399k
12 votes
Accepted

Mathematica for Theoretical Physics by Gerd Baumann : Henonmap

HenonMap[{T_, W_}, α_] uses T in args but θ in body ...
I.M.'s user avatar
  • 3,106
11 votes

How to avoid procedural loops in this example?

1 + LengthWhile[Range@10, f@# != 10 &] 5
kglr's user avatar
  • 399k
11 votes

Better way to sort course grades

I had to deal with similar problems many times, and I usually do this with associations. We start by defining an exhaustive list of grades in our desired order. I am not that familiar with the ...
Szabolcs's user avatar
  • 236k
11 votes
Accepted

Is there a faster way to vectorize a matrix than this?

Note that here you are wasting a lot of time by transposing twice (this is the slowest operation here). See the breakdown of timings of your code here: ...
wilsnunn's user avatar
  • 918
11 votes
Accepted

Faster "Closest Pair of Points Problem" implementation?

Here's a refinement of @Henrik's approach. The key difference is that using Nearest[pts->"Distance"] is over an order of magnitude faster than using <...
Carl Woll's user avatar
  • 131k
11 votes
Accepted

Is there a more elegant solution than f[a, b, Sequence@@#3,...]?

It seems to me that there should be a way to avoid constructing lists and deconstructing them immediately Maybe there is a way to avoid lists, but I don't see the motivation. Lists are useful for ...
Jason B.'s user avatar
  • 69.3k
10 votes
Accepted

Computing the twin prime constant with Mathematica

The problem with your code is that N[Product[...]] calls NProduct[...] which turns integers into reals before ...
SonerAlbayrak's user avatar
10 votes
Accepted

Concise way to draw this pyramid

n = 5; Graphics3D[Table[Cuboid[{#[[1]], #[[2]], -i}] & /@ Tuples[Range[-i/2, i/2], 2], {i, 0, n - 1}]] A more concise version (thanks: Vitaliy Kaurov): <...
kglr's user avatar
  • 399k
10 votes
Accepted

Optimization of the following code

The function $x \mapsto P_x([a,b])$ is continuous and piecewise linear, so it can be easily and exactly integrated with the trapezoidal rule. Then the resulting quadrature rule can be compiled for ...
Henrik Schumacher's user avatar
10 votes

Brute Force for solving Diophantine equation

Maybe double loop is not necessary: Reap[Do[If[IntegerQ[Log[2, 5^y + 3]], Sow[y]], {y, 100000}]] Takes around 12 seconds.
yarchik's user avatar
  • 19.2k

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