New answers tagged

7

I have a few experiments to contribute: I tried to fill the gaps and use up all the "unused" integers in a Recamán series truncated to N entries, so whenever a member of the series is bigger than N, it just jumps to the first integer that is not part of the series yet. Also, I added a final semicircle that connects the last and the first entry to close the ...


16

Just the visualization part using ParametricPlot: recamanSequencePlot1 = ParametricPlot[ Evaluate[Map[RotationTransform[Pi/4] @ {Mean @ # + (#[[2]] - #[[1]])/2 Cos[t], (#[[2]] - #[[1]])/2 Sin[t]} &, Partition[#, 2, 1]]], {t, 0, -Pi}, AspectRatio -> Automatic, Axes -> False, ImageSize -> Large] &; Using ...


11

If you don't care about extra space, we can employ a reverse lookup table: seenQ[0] = True; a[0] = 0; a[n_] := a[n] = With[{prev = a[n - 1]}, (seenQ[#] = True; #) &[ If[prev > n && ! TrueQ[seenQ[prev - n]], prev - n, prev + n] ] ] Block[{$RecursionLimit = ∞}, Do[a[k], {k, 10^6}] // AbsoluteTiming ] {8.55911, Null} a /@ Range[0, ...


17

My own take on this: Clear[f] f[0] = 0; f[n_] := f[n] = If[Or[MemberQ[Last /@ Most[DownValues[f]], f[n - 1] - n], f[n - 1] < n], f[n - 1] + n, f[n - 1] - n] f /@ Range[0, 11] (* {0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22} *) We can draw the figure using With[{list = Table[f[n], {n, 0, 30}]}, Show[Graphics@*Circle @@@ Partition[Flatten[{ ...


2

Since probability to get zero sum at random is quite high, you can generate several random lists and choose one with zero sum. For example using NestWhile: zeroSum[n_] := NestWhile[RandomInteger[{-1, 1}, n]&, 1, (Total[#]!=0)&]; Re-generating large lists is however not very effecient. Thus, you can generate one list and modify several elements (at ...


6

If you want to sample uniformly from all possible tuples that sum to 0, you can do the following: zero[n_] := With[ { ones = RandomChoice[ Table[Multinomial[i,i,n-2i], {i,0,Floor[n/2]}] -> Range[0,Floor[n/2]] ] }, RandomSample @ PadRight[ Join[ConstantArray[1,ones],ConstantArray[-1,ones]], n ] ] For ...


2

Given the lack of details in your specification, I suspect the following very simple approach will be adequate to your needs: zeroSum[n_] := With[{ n3 = Floor[n/3] }, RandomSample@Catenate@{ ConstantArray[-1, n3], ConstantArray[1, n3], ConstantArray[0, n - 2*n3] }]


13

A combination of IntegerPartitions, RandomChoiceand RandomSample: n = 30; RandomSample @ RandomChoice @ IntegerPartitions[0, {n}, {-1, 0, 1}] {-1, 1, 1, 1, 1, -1, -1, 0, -1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, 0, 1, -1, -1, 1, -1, -1, 1, 1, -1, 1} Total @ % 0 You can also do RandomSample @ PadRight[Flatten @ ConstantArray[{1, -1}, RandomChoice[...


Top 50 recent answers are included