# Tag Info

26

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 ...

24

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 ...

15

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$ ...

14

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, ...

11

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 ...

10

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, #] & /@ #] & /@ ...

9

Another option: RootApproximant[0.1845095405274387, 1]

9

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

8

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 ...

8

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

8

Still another possibility: Last[Convergents[0.1845095405274387]]

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 ...

8

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. ...

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 *)

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

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 = ...

7

A couple of bits of code for your consideration: FromDigits@#/10^(Length@# - #2) & @@ RealDigits[0.1845095405274387] Rationalize[0.1845095405274387, \$MachineEpsilon]

6

You can enter a number in an arbitrary base using base^^digits: alpha = 2^^0.10101; BaseForm[alpha, 2] BaseForm[alpha^2, 2]

6

I feel like I should be prefacing this answer with three confessions, considering that this is an arithmetic question. First, I had a hard time with the multiplication tables until I was nine years old. Second, even after I finally got the hang of multiplication, I was never a fan of multiplying from right-to-left; I preferred going left-to-right. (Arthur ...

5

Not a full answer since I need to sleep :) but more of an observation, which might help. It seems to have to do with the fact that 0 and 0. are not the same in Mathematica. This simple example shows it UnitConvert[0. + Quantity[5, "Meters"], "Inches"] (*--> UnitConvert[0. + Quantity[5, "Meters"], "Inches"] *) while UnitConvert[0 + ...

4

Independently I arrived at something similar to Michael's answer, yet different. I borrowed his formatting function after seeing it as it works better than what I had. Perhaps this will also be of use: evalFromBottom[expr_, lv_: 1] := If[lv > Depth@expr, expr, With[{ev = Replace[expr, x_ :> RuleCondition[x], {-lv}]}, If[expr === ev, ...

4

Yes, it is possible. Since multiplication of positive integers is repeated addition, we can repeatedly add instead of multiply: n = 10; sum = 0; Do[ Do[ sum = sum + i, {j, 1, i}], {i, 1, n}]; Print[sum]

4

An oddball one using a recursive, memoizing function for the square. Clear[sq]; sq[n_Integer] := sq[n] = sq[n - 1] + n + (n - 1) sq[1] = 1; Sum[sq[n], {n, 6}] 91 It's not something I would directly use for such a goal, but you asked for something without explicit multiplications and you got it. Alternatively, if we don't interpret Dot as some ...

4

Ten ways to beat a dead horse Sunday afternoon on an airplane without wifi and this was the problem I remembered reading at breakfast. Forgive me for the time I had on my hands. All because @Aky resurrected this dead horse. (Thanks by the way. I'm glad to have figured out the CellularAutomaton one, but we landed before I could come up with a good MapAll ...

4

Here's a way to brute-force search for numbers that have the property that the sum of the digits raised to an integer power is equal to the number itself. list = {}; Do[If[Total[IntegerDigits[b^e]] == b, AppendTo[list, {b, e}]], {b, 2, 800}, {e, 2, 100}]; This returns a list of the numbers and powers (here's just the first 50), ordered so that they ...

4

As rasher and the documentation both say, Equal has a certain level of fuzziness. The same is true of SameQ, though it has a more stringent tolerance. The following computations are all done with machine precision numbers. Similar things should hold with arbitrary precision numbers but the analysis might be trickier. (* 12 zeros, difference = ...

4

Please tell me if this simplified function does what you want: f[x_, n_] := Round[x, 10^(1 - n + ⌊ Log10 @ Abs @ x ⌋)] ~SetPrecision~ n Test: Table[f[x*Pi, 4], {x, {1/100, 1/10, 1, 10, 100}}] % // FullForm {0.03142, 0.3142, 3.142, 31.42, 314.2} List[0.031424., 0.31424., 3.1424., 31.424., 314.24.] Update The OP wrote: I understand that ...

3

I'll join the bandwagon with SparseArray: sq[n_Integer] := Tr@SparseArray[{{i_, i_} :> i^2}, n]

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