# Tag Info

15

Using Annuity: pmt /.Solve[TimeValue[Annuity[pmt, 52, 1], .02, 0] == 5000, pmt] 155.545 Exercise left for the reader...

13

Accumulate@Array[b, {3}] (* {b[1], b[1] + b[2], b[1] + b[2] + b[3]} *) therefore: {a, b} = Transpose[list]; Transpose[{a, Accumulate[b]}] Also this will do the job: Rest@FoldList[{#2[[1]], #1[[2]] + #2[[2]]} &, {0, 0}, list] or even easier list[[All,2]]=Accumulate@list[[All,2]]; list

11

You could do something like this: tstart = SessionTime[]; While[(t = SessionTime[] - tstart) < 60, (* do stuff *)]

10

FoldList[{0, 1} # + #2 &, list]

8

The general formula can be derived as follows. First@RSolve[{prin[n] == (1 + int) prin[n - 1] - pay, prin[0] == loan}, prin[n], n]; First@Solve[(prin[n] /. %) == 0, pay] (* {pay -> (int (1 + int)^n loan)/(-1 + (1 + int)^n)} *) where pay is the payment per period, int is the interest per period, and loan is the original principal. For the example given ...

8

I find Flat hard to work with. Despite seeing it in use numerous times I'm never quite sure what it will do until I try it, so maybe I'm not the person to answer your question. Nevertheless I shall try. Consider this related, reduced example: Attributes[foo] = {Flat, OneIdentity}; foo[a_] := "inert" /; Print @ HoldForm[a] foo[X, Y]; foo[X,Y] X Y ...

8

The loop is infinite, because the result of Append is the new list, not the modification of existing. Documentation: Append[expr,elem] gives expr with elem appended. And: AppendTo[s,elem] appends elem to the value of s, and resets s to the result. So, you either need to reassign the value of Append or use AppendTo: list={1}; n=2; While[Last@list<...

7

As an alternative to a manual check of the iterator, as proposed by Kuba, you could refactor to use NestWhile by converting your body and test to functions without parameters: n = 1; NestWhile[(Print[n]; n++) &, , (n > 0) &, 1, 7]; The test n > 0 would normally result in an infinite loop, but here it is limited to 7.

7

Using the shortcut //. together with a MaxIterations options s //. Sequence[s -> s (1 + r), MaxIterations -> 3] $\$(1 + r)^3 s or s //. (s -> s (1 + r)) ~ Sequence ~ (MaxIterations -> 3) An example for an efficient looping construct using ReplaceRepeated is the pattern matching Fibonacci sequence generator fiboSequence2[n_] := Quiet@...

7

a = {14, 10, 19, 17}; b = {16, 10, 15, 11}; c = {3, 13, 7, 6}; eq[a_,b_,c_] := Solve[{e*0.01+f*0.04 +a ==b,e*0.04 +f*0.02 + a == c},{e, f}]; MapThread[eq, {a, b, c}]

7

Total[DistanceMatrix[a]] ...too short to be an acceptable answer (minimum is 30 characters) without some meaningless commentary

7

Use Sow and Reap : counter = 0; Reap @ While[counter < 5, counter = counter + 1; sol = NSolve[{2 x + y - counter == 0, 3 y + 5 x - 2 counter == 0}, {x, y}]; Sow[{x, y, counter} /. sol]]

6

This problem seem to be specific for MacOSX and it has nothing to do with the while-loop. If I do a simple Pause[100] on my OSX, I have a CPU load of about 60% on one processor. A simple While[True,] uses 100% of one CPU core (as expected). My colleague tested it on Windows 8 where the pure while-loop uses one core too. The Pause[100] on the other hand ...

6

First off, don't use capital letters, you'll run into trouble like you did there (E is defined as the natural log unit $e$). You can do what you are going for with a Table, a = {14, 10, 19, 17}; b = {16, 10, 15, 11}; c = {3, 13, 7, 6}; Table[ {e, f} /. First@Solve[{e*0.01 + f*0.04 + a[[n]] == b[[n]], e*0.04 + f*0.02 + a[[n]] == c[[n]]}, {e, f}] ...

6

Mathematica won't run it twice unless you (possibly unknowingly) instruct it to do so. I suspect this is what is happening (please update the question with the details): You put some initialization cells in a notebook. You saved the notebook, closed it an reopened it (or alternatively you quit the kernel). Then you used the Evaluation -> Evaluate ...

6

First of all there is nothing like double in Mathematica, you don't need to define the type of variable here. I don't know what problem are you trying to solve, since you don't describe it, but I at least can point out absolute wrong parts of your code. There is such a thing in Mathematica, called function, so you can parametrize, instead of hard-coding: ...

6

The crux of the matter is how n is changed on each iteration. Math Consider a multi-digit integer $n = a_1 a_2 a_3 \ldots a_N$, where $a_k$ is the $k$-th digit of $n$ in base $10$, and $a_1 \neq 0$. Then, since $a_1 a_2 \ldots a_N = \left( a_1 a_2 \ldots a_{N-1} \right)10 + a_N$, \text{ mod}(n, 10) = \text{ mod}(a_1 a_2 \ldots a_N, 10) = ...

6

n = 10; basis = Table[Cos[k t], {k, 0, n}]; Tuples[{-1, 1}, n + 1].basis

5

Something like this: f[n_] := Module[{s, i}, i = n; s = Range[1000]; While[i > 1, s = Drop[s, {i, Length@s, i}]; s = Accumulate@s; i--; ]; s ] f[3] {1, 8, 27, 64, 125, 216, 343,...} If I was trying to leverage Mathematica I might have written: s = Range[1000]; n = 3; Fold[Accumulate@Drop[#, {#2, Length@#, #2}] &, s, ...

5

All you need is to do Map MemberQ over the second column (a.k.a. [[2]]) of each lines: M = {{A,B,C},{1,{1,3,6,7},0},{1,{3,7,9},1}} MemberQ[#[[2]], 1] & /@ M {False, True, False} An equivalent would be (thanks to Mr.Wizard): MemberQ[#2, 1] & @@@ M

5

This solution will traverse all nested associations regardless of depth and replace each value with the value that corresponds to its key in replacements. f[Rule[key_, assoc_Association]] := Rule[key, AssociationMap[f, assoc]] f[Rule[key_, val_]] := Rule[key, key /. replacements] replacements = { "a" -> 1, "dd" -> 1, "B" -> 1, "aa" -> 0, "...

5

Simon's code CreateDialog[ {(a = RandomInteger[BernoulliDistribution[0.1], {200, 200}]; Dynamic[ Image[a = 1 - Unitize[(# - 3) (# - 12) (# - 13)] &@ ListConvolve[{{1, 1, 1}, {1, 10, 1}, {1, 1, 1}}, a, {2, 2}, 0]]]), DefaultButton[]}, Modal -> True]

5

As alluded to by Daniel Lichtblau you could use TriangleWave with appropriate scaling, e.g. amp = 0.02675; per = 4 amp/0.0001; DiscretePlot[Abs@TriangleWave[{-amp, amp}, x/per], {x, 0, 2 per, 1}, Filling -> None] Compared with @DanielLichtblau comment code: ssk1 = 0.02675; up1 = Table[t, {t, 0, ssk1, .0001}]; down1 = Table[t, {t, ssk1, 0, -.0001}];...

5

thanks to Mike Honeychurch, without pure function: list = {{a1, b1}, {a2, b2}, {a3, b3}}; Thread[{Thread[list][[1]], Rest@FoldList[Plus, 0, Thread[list][[2]]]}] (* {{a1, b1}, {a2, b1 + b2}, {a3, b1 + b2 + b3}} *) and shorter (one expression): Thread[{list[[All, 1]], Accumulate[list][[All, 2]]}]

5

Just to show how to fix the origional loop: amt = 5000; interestrate = .02; nlast = 52; Do[interest = amt*interestrate; amt = Simplify[amt + interest - inst], {n, 1, nlast, 1}]; Solve[amt == 0, inst] inst -> 155.545 Note the Simplify is important here, without it amt looks like this after just 4 weeks.. 5100. + 0.02 (5100. + 0.02 (5100. + 0.02 (...

5

Using the example for an ordinary annuity from here:- Calculating The Present And Future Value Of Annuities The example demonstrates how a present value principal of 4329.48 is paid down by five repayments of 1000 each discounted to present value by the interest rate and period. The calculation can be represented by the following summation: where pis the ...

5

An example using TimeConstrained: TimeConstrained[ n = 1; While[n < 5, Print[n]; n++; Pause[1]], 2 ,(*seconds*) Print["Time is up."]; \$Failed ]

4

I'm not sure exactly what you want, but in an effort to demonstrate some possibilities: Join @@ Table[{{a, b}, NMinimize[{a x + b y, 0.2 x + 0.1 y >= 14, 0.25 x + 0.6 y >= 30, 0.1 x + 0.15 y >= 10, x >= 0, y >= 0}, {x, y}][[2]]}, {a, 0, 3, 1}, {b, 0, 3, 1}] // Column Or: Join @@ Table[{{HoldForm[a] -> a, HoldForm[b] -> b},...

4

As you probably realize by now, your code loops indefinitely because 1 <= Length[WinnerList] + 1 is always true. Now, let's look a better, functional way to solve your problem. Making some data and getting the count of how many items in the list have the maximum value. SeedRandom[42]; scores = RandomInteger[99, 1000]; Count[scores, Max @ scores] 11 ...

4

Using Position: Position[WinnerList, winner] // Flatten

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