How to write correct wolfram expression? - wolfram-mathematica

How to write something like this expression on WolframAlpha to get the correct answer?
Solve[Sort[IntegerDigits[2*x]] == Sort[IntegerDigits[3*x]], x]
The first possible x value equals to 1782.
2*1782 = 3564
3*1782 = 5364
Which are the same after sorting.

Sometimes it is possible to know enough Mathematica language notation that this can be used to coax WolframAlpha into giving an answer. This
Select[Table[{x, Sort[IntegerDigits[2*x]],Sort[IntegerDigits[3*x]]}, {x,6000}], #[[2]]==#[[3]]&]
or this
Select[Table[x,{x,6000}],Sort[IntegerDigits[2*#]]==Sort[IntegerDigits[3*#]]&]
works

Related

Simplication using Algebraic modification

Studying for exam and came thru this question:
Simplify using Algebraic modification put result in sum-of-product form with minimum # of literals
F(A,B,C,D)=(C'+AC'(BD+BD'))D+(BC'+(B+C)'+D')'+C(D+AB'(A'+D'))
I have expended it to this:
C'D+ABC'D+ABC'DD'+B'BD+B'CD+BCD+CD+AA'B'D+AB'CD
my final answer is: AD+AC+CD I'm trying to see if someone else have another thought on this.
I figure it out the answer after thoughtful calculation:
=(C'+AC'B)D+(BC'+B'C'+D')'+C(D+AB'D)
=C'D+ABC'D+(C'+D')'+C(D)
=C'D(1+AB)+CD+CD
=C'D+CD
= D

Ruby evaluate without eval?

How could I evaluate at mathematical string without using eval?
Example:
mathstring = "3+3"
Anyway that can be evaluated without using eval?
Maybe something with regex..?
You must either or eval it, or parse it; and since you don't want to eval:
mathstring = '3+3'
i, op, j = mathstring.scan(/(\d+)([+\-*\/])(\d+)/)[0] #=> ["3", "+", "3"]
i.to_i.send op, j.to_i #=> 6
If you want to implement more complex stuff you could use RubyParser (as #LBg wrote here - you could look at other answers too)
I'm assuming you don't want to use eval because of security reasons, and it is indeed very hard to properly sanitize input for eval, but for simple mathematical expressions perhaps you could just check that it only includes mathematical operators and numbers?
mathstring = "3+3"
puts mathstring[/\A[\d+\-*\/=. ]+\z/] ? eval(mathstring) : "Invalid expression"
=> 6
You have 3 options:
In my honest opinion best - parse it to Reverse Polish Notation and then parse it as equation
As you say use RegExps
Fastest, but dangerous and by calling eval but not Kernel#eval
RubyVM::InstructionSequence.new(mathstring).eval
Sure--you'd just want to somehow parse the expression using something other than the bare Ruby interpreter.
There appear to be some good options here: https://www.ruby-toolbox.com/search?q=math
Alternatively, it probably wouldn't be that hard to write your own parser. (Not that I've seriously tried--I could be totally full of crap.)
Dentaku seems (I haven't used it yet) like a good solution - it lets you check your (mathematical and logical) expressions, and to evaluate them.
calculator = Dentaku::Calculator.new
calculator.evaluate('kiwi + 5', kiwi: 2)

Treat # as text in ToExpression Mathematica

I have to run ToExpression["Test#test"] and I want to return test#test, but the
function always return Test[test].
I tried to Unprotect, Clear, ClearAll, Remove ["#"] or [#], but it doesn't work.
Any ideas?
Test#test and Test[test] are two different notations for the very same Mathematica expression. If you convert the string "Test#test" to a Mathematica expression, any information about how it was entered is lost---only the expression structure is retained.
You should tell us why you want to "return test#test", as you said. It seems to me you have some serious confusion about how Mathematica works. Just explain what you want to achieve.

How to get rid of Log[e] from mathematica output?

My mathematica output is
-0.751988 - 0.0708732 Log[e] - 0.0140273 Log[e]^2
But I want mathematica to calculate this expression for me i.e. set Log[e] = 1 and sum the terms. How do I instruct it to do that. I'm assuming it must be treating the functions as complex??
Regards
You probably wanted E or \[ExponentialE] which has the input alias ⋮ee⋮.
Built-in symbols are capitalized, so constants like pi, e are written as Pi, E
If i remember correctly, Mathematica use E as the Neper number, not e. I think you mispelled it, so Log[e] is not expanded.
If not so, with the substitution Log[e]->1 you can achieve what you want.

Boolean Expression Evaluation

I have a string (R(46 - 9900)) AND ( NOT (R(48 - 9900))) where R denotes Range . If you evaluate the expression it results in R(46-47) , considering the logical operators (AND,NOT).
I have a requirement where I need to parse such a string and evaluate it to a correct result . I have to use C++ as a programming tool to achieve this result .
Can anyone suggest a few guide lines as to how do I proceed on this ?
I'm reposting Aftershock's answer to your question the first time you posted it, since it was a good answer and it didn't deserve to be deleted:
You have to write a small interpreter. There are many ways to do it . Here is one. Here is pattern for it: http://en.wikipedia.org/wiki/Interpreter_pattern
This can help too: http://ryanfarley.com/blog/archive/2004/08/19/966.aspx That is about the intersection of data ranges but the problem is similar.
You may also use an operator precedense parser: http://en.wikipedia.org/wiki/Operator-precedence_parser

Resources