We’re rewarding the question askers & reputations are being recalculated! Read more.

# Tag Info

39

Oleksandr is correct about the way evaluation works. a/b seems to be interpreted (parsed) directly as Times[a, Power[b,-1]], or more readably: $a\times b^{-1}$. Divide[a,b] is interpreted as is. Evaluation then proceeds from these forms, and the arithmetic is carried out differently for the two cases: either $a\times (1/b)$ or $a/b$. Here are some ...

30

In a sense described below, this answer finds $422716$ distinct solutions. The innovations presented here are using postfix operators to eliminate problems with parentheses; avoiding having to deal with unary negation; initially computing "too many" solutions, some of which make no sense, and eliminating them at the end (rather than writing more ...

23

A non brute-force approach is the following, similar to my answer for the Zebra Puzzle. Both puzzles are examples of constrainst satisfaction problems, that can be solved with Reduce/Minimize/Maximize or, more efficiently, with LinearProgramming. The good about this approach is that you can easily extend and apply to many similar problems. The common part:...

20

The usual way to multiply all elements of a list is Times @@ list. The less trivial problem is to calculate the product as fast as possible. Some performance tests (from slowest to fastest): SeedRandom[0]; a = RandomReal[{0.999, 1.001}, 10000000]; Det@DiagonalMatrix[a] // AbsoluteTiming (* I don't have 1 PB RAM :) *) Product[a[[i]], {i, Length[a]}] // ...

16

EDIT: As @Rojo points out in the comments, my code doesn't really find all solutions. For example, a term of the form a * (b * c + d) can't be represented with "precedence plus/minus" operators. I'm not sure if it is salvageable, but as it is, the code below does not find all solutions. A very simple solution would be to define two new operators $\oplus$ ...

15

While it would've been nice if the package handled it automatically, it can be fixed with a simple overloading of Quantity: Unprotect@Quantity; Quantity /: (0 | 0.) Quantity[_, unit_] := Quantity[0, unit] Protect@Quantity; You can add this to your init.m, so that you don't have to define it each time. You can test your examples with this: 0. Quantity[1, "...

13

Can't think out a better method than brute force, it'll be conciser in Mathematica of course: g = 10 # + #2 &; Pick[#, g[#, #2] #3 == g[#4, #5] == g[#8, #9] - g[#6, #7] & @@@ #] &@Permutations@Range@9 {{1, 7, 4, 6, 8, 2, 5, 9, 3}}

12

An extended comment. I'm not sure if this has been realized, please correct me if it has. The result of the Divide[a,b] operation is not the same as the first 3 which are identical. {a, b} = List @@ RandomReal[{-50, 50}, {2, 1*^7}]; x1 = a/b; x2 = a b^-1; x3 = a/b; x4 = Divide[a, b]; Now... Tally[x1 - x2] Tally[x2 - x3] Both give 10^7 zeros. Tally[x3 ...

12

You can input numbers in any base up to 36 using the notation base^^digits. Digits over 9 are represented using a, b, c, ... You can print numbers in any base up to 36 using BaseForm. Thus, In[1]:= a=2^^0.10101 Out[1]= 0.65625 In[2]:= BaseForm[a^2,2] Out[2]//BaseForm= Subscript[0.0110111001, 2] Note that the internal representation of numbers doesn't ...

11

Ah well, for fun and posterity: Tr[#, Times]&

11

The Pauli vector: σ = Table[PauliMatrix[i], {i, 1, 3}] MatrixForm /@ σ One can define a dot product of matrices (in spaces of arbitrary dimension): dot[a_, b_] := Total @ MapThread[Dot, {a, b}] Then dot[σ, σ] // MatrixForm For the cross product of matrices, I literally took the cross product of vectors Cross[{a, b, c}, {x, y, z}] /. {Times[A_Symbol, ...

10

Here's one idea. Hold the expression unevaluated and go up the expression tree from (near) the bottom, level by level, and evaluate. expr = HoldForm[1/((a + 2 b)/c^2)] /. {a -> 1, b -> 2, c -> 3} out = ToExpression@ToString[FullForm@#] & /@ (ReplacePart[expr, # -> Extract[expr, #] & /@ #] & /@ GatherBy[Position[...

10

10

There isn't really such a thing as binary arithmetic (at least in Mathematica). Numbers can be represented in any base, and this user-visible representation is completely independent from how arithmetic is done. Try this: BaseForm[(2^^1010101011)*(2^^1111101110), 2] Things to look up: BaseForm Digits in numbers

10

To make the brute force solution a bit more pleasant for the eye of the observer: testFunc = And[ FromDigits @ {#1, #2} #3 == FromDigits @ {#4, #5}, FromDigits @ {#4, #5} + FromDigits @ {#6, #7} == FromDigits @ {#8, #9} ]& ; sol = Range[9] // RightComposition[ Permutations, SelectFirst[ testFunc @@ # & ] ] {1, 7, 4, 6, 8, 2, 5, 9,...

10

This is a good question for uncovering an important difference between listability and vectorization. Sometimes these terms are used interchangeably, but I would like to make a technical distinction. In both Listable and vectorized functions, the value of the function on a list is equal to values of the function on each element of the list. From the ...

9

Here's a possibility nextops = HoldForm /@ {Plus, Times, Divide, Subtract}; (nextOp[#1] = #2) & @@@ Most@Transpose@{nextops, RotateLeft@nextops}; nextchildren = True; SetAttributes[{nextPlus, nextTimes}, Flat]; next[{i_}] := False; next[l_List] := HoldForm[Plus][{l[[1]]}, l[[2 ;;]]]; next[op_[arg1_, arg2_]] /; nextchildren := With[{res = next[...

9

Calculating eigenvalues involves solving for the roots of the characteristic polynomial, which is of degree equal to the order of the size of the matrix. When you input real numbers, it can search for the roots of the polynomial using numerical techniques. When you input exact integers (or rationals, probably) it tries to find exact answers for the roots of ...

9

Someone certainly had to write this recursive one: Clear[f] f[x_Integer, y_Integer] := x + f[x, y - 1] f[x_Integer, 0] := 0 f[x_Integer] := f[x, x] + f[x - 1] f[0] := 0

9

Another option: RootApproximant[0.1845095405274387, 1]

9

Range[9] // Permutations // Select[(10 #[[1]] + #[[2]])*#[[3]] == 10 #[[4]] + #[[5]] &] // SelectFirst[ 10 #[[4]] + #[[5]] + 10 #[[6]] + #[[7]] == 10 #[[8]] + #[[9]] &] So,17*4=68；68+25=93。 好玩吧

9

Consider the following: 2.84852+3 2.85 This syntax means "write 2.84852 with 3 digits of precision". You're saying that this is equivalent to 2.84852+3 5.84852 Which it is not. Documentation: http://reference.wolfram.com/language/tutorial/NumericalPrecision.html

8

Try this : s[n_] := Total[ Range[n]^2] to check how it works, e.g. : s[5] // Trace There is also a purely symbolic approach, e.g. : $\quad n^2$ ~ Sum ~$n\quad$ (see Infix notation) : (n^2) ~ Sum ~ n 1/6 (-1 + n) n (-1 + 2 n) Note : Sum[ n^2, n] returns the same as Sum[ i^2, {i, n-1}] does, i.e. indefinite sums starts at 0 while definite ones ...

8

Still another possibility: Last[Convergents[0.1845095405274387]]

8

This is at best a partial answer. I don't know in full detail what is going on with FullForm or InputForm but I will venture to guess they are influenced in some way by the $XXXPrecision settings.$MaxPrecision = 5; $MinPrecision = 5; InputForm[x = SetPrecision[10^(-10), 5]] (* Out[6]//InputForm= 1.5.*^-10 *) InputForm[y = 1 + x] (* Out[7]//InputForm= 1.... 8 No need to do anything fancy. This is just how ordinary list multiplication works: {a1, a2, a3} {b1, b2, b3} yields {a1 b1, a2 b2, a3 b3} 7 A couple of bits of code for your consideration: FromDigits@#/10^(Length@# - #2) & @@ RealDigits[0.1845095405274387] Rationalize[0.1845095405274387,$MachineEpsilon]

7

Figuring out what the following snippet does is left as an exercise for the reader: With[{n = 9}, s = t = 0; j = 1; Do[ t += j; s += t; j += 2, {n}]; s ]

7

Total@Flatten[ConstantArray[#, #] & /@ Range[9]] I think this exercise is somewhat of a Rorschach test… I don't know what the above says about me, though :)

7

A Mathematica minded answer: HarmonicNumber[n, -2] So: Simplify[Sum[i^2, {i, n}] == HarmonicNumber[n, -2]] (* True *)

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