Derivation: Discerning difference between arithmetic expression with parenthesis versus without using abstract syntax trees - abstract-syntax-tree

I am trying to illustrate the expression: ( 3 * 4 + 5 * 6 + 7 ) using an abstract syntax tree. I have already illustrated the expression: ( 3 * (4 + 5) * (6 + 7) ).
Could someone please illustrate the expression without parenthesis with order of left-most first. Thanks in advance.
EDIT:
Here is an updated image of what I think may be correct, but please correct me if it is not. Thanks.

Related

Why does Julia return different results for equivalent expressions? 6÷2(1+2) and 6÷2*(1+2)

I typed the following in Julia's REPL:
julia> 6÷2(1+2)
1
julia> 6÷2*(1+2)
9
Why are the different results output?
Presh Talwalkar says 9 is correct in the movie
6÷2(1+2) = ? Mathematician Explains The Correct Answer - YouTube
YouTube notwithstanding, there is no correct answer. Which answer you get depends on what precedence convention you use to interpret the problem. Many of these viral "riddles" that go around periodically are contentious precisely because they are intentionally ambiguous. Not a math puzzle really, it's just a parsing problem. It's no deeper than someone saying a sentence with two interpretations. What do you do in that case in real life? You just ask which one they meant. This is no different. For this very reason, the ÷ symbol isn't often used in real mathematical notation—fraction notation is used instead, which clearly disambiguates this as either:
6
- (1 + 2) = 9
2
or as
6
--------- = 1
2 (1 + 2)
Regarding Julia specifically, this precedence behavior is documented here:
https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/#man-numeric-literal-coefficients
Specifically:
The precedence of numeric literal coefficients is slightly lower than that of unary operators such as negation. So -2x is parsed as (-2) * x and √2x is parsed as (√2) * x. However, numeric literal coefficients parse similarly to unary operators when combined with exponentiation. For example 2^3x is parsed as 2^(3x), and 2x^3 is parsed as 2*(x^3).
and the note:
The precedence of numeric literal coefficients used for implicit multiplication is higher than other binary operators such as multiplication (*), and division (/, \, and //). This means, for example, that 1 / 2im equals -0.5im and 6 // 2(2 + 1) equals 1 // 1.

Differentiation using r studio [duplicate]

This question already has an answer here:
Differentiation using r
(1 answer)
Closed 1 year ago.
I'm new to using R or any type of programming and I'm trying to differentiate the equation below in R:
$x^{t} e^{t(1-x)}$
with respect to x. I've tried this method but R is showing me there is an unexpected symbol
D(expression((x^t)*exp^(t(1-x)),"x")
thanks in advance
The main issue here is a typo as there is no closing bracket on the call to the expression function.
There is an additional issue that the t term needs to be explicitly multiplied by (1-x). This is discussed in more detail in the answers for Differentiation using r.
I have produced the solution with these fixes below.
#Reformatted code to show where the correction occurs
D(
expression(
# Added "*" between t and (1- x)
(x^t)*exp(t *(1-x))
# Added the closing bracket
),
"x"
)
#> x^(t - 1) * t * exp^(t * (1 - x)) - (x^t) * (exp^(t * (1 - x)) *
#> (log(exp) * t))
Created on 2021-04-05 by the reprex package (v2.0.0)

Need help creating the following sequence:

In R, I need to create a vector b = (1, 1+pi, 1+2pi, 1+3pi,...,1+19pi). I am unsure how to do this. I keep trying to use the seq command (i.e. seq(1, 1+npi n = 1:19) and that's totally wrong!), but don't know the proper syntax to make it work, thus it never does.
Any help would be appreciated.
R needs the multiplication operator.
b <- 1+ seq(0,19)*pi
Or slightly faster in situations where speed might matter:
b <- 1+ seq.int(0,19)*pi
You could use the equivalent:
b <- 1+ 0:19*pi
Because the ":" operator has very high precedence ( see ?Syntax), it's reasonable safe. Just be careful that you understand precedence when you use a minus or plus sign where it might be parse as a binary operator (remembering that spaces are ignored and that unary-minus has higher precedence than the single-colon, but binary minus or plus has a lower precedence :
> 1: 5+5
[1] 6 7 8 9 10
You should use simply 0:19 * pi + 1. Using seq is not so nice: seq(1, 1 + 19 * pi, by = pi) or seq(1, 1 + 19 * pi, length = 20).

How to generate random arithmetic expressions for game

i would like to know if you can help me with this problem for my game. I'm currently using lots of switch, if-else, etc on my code and i'm not liking it at all.
I would like to generate 2 random arithmethic expressions that have one of the forms like the ones bellow:
1) number
e.g.: 19
2) number operation number
e.g.: 22 * 4
3) (number operation number) operation number
e.g.: (10 * 4) / 5
4) ((number operation number) operation number) operation number
e.g.: ((25 * 2) / 10) - 2
After i have the 2 arithmetic expresions, the game consist in matching them and determine which is larger.
I would like to know how can i randomly choose the numbers and operations for each arithmetic expression in order to have an integer result (not float) and also that both expression have results that are as close as possible. The individual numbers shouldn't be higher than 30.
I mean, i wouldn't like a result to be 1000 and the other 14 because they would be probably too easy to spot which side is larger, so they should be like:
expresion 1: ((25 + 15) / 10) * 4 (which is 16)
expression 2: (( 7 * 2) + 10) / 8 (which is 3)
The results (16 and 3) are integers and close enough to each other.
the posible operations are +, -, * and /
It would be possible to match between two epxressions with different forms, like
(( 7 * 2) + 10) / 8
and
(18 / 3) * 2
I really appreciate all the help that you can give me.
Thanks in advance!!
Best regards.
I think a reasonable way to approach this is to start with a value for the total and recursively construct a random expression tree to reach that total. You can choose how many operators you want in each equation and ensure that all values are integers. Plus, you can choose how close you want the values of two equations, even making them equal if you wish. I'll use your expression 1 above as an example.
((25 + 15) / 10) * 4 = 16
We start with the total 16 and make that the root of our tree:
16
To expand a node (leaf), we select an operator and set that as the value of the node, and create two children containing the operands. In this case, we choose multiplication as our operator.
Multiplication is the only operator that will really give us trouble in trying to keep all of the operands integers. We can satisfy this constraint by constructing a table of divisors of integers in our range [1..30] (or maybe a bit more, as we'll see below). In this case our table would have told us that the divisors of 16 are {2,4,8}. (If the list of divisors for our current value is empty, we can choose a different operator, or a different leaf altogether.)
We choose a random divisor, say 4 and set that as the right child of our node. The left child is obviously value/right, also an integer.
*
/ \
4 4
Now we need to select another leaf to expand. We can randomly choose a leaf, randomly walk the tree until we reach a leaf, randomly walk up and right from our current child node (left) until we reach a leaf, or whatever.
In this case our selection algorithm chooses to expand the left child and the division operator. In the case of division, we generate a random number for the right child (in this case 10), and set left to value*right. (Order is important here! Not so for multiplication.)
*
/ \
÷ 4
/ \
40 10
This demonstrates why I said that the divisor table might need to go beyond our stated range as some of the intermediate values may be a bit larger than 30. You can tweak your code to avoid this, or make sure that large values are further expanded before reaching the final equation.
In the example we do this by selecting the leftmost child to expand with the addition operator. In this case, we can simply select a random integer in the range [1..value-1] for the right child and value-right for the left.
*
/ \
÷ 4
/ \
+ 10
/ \
25 15
You can repeat for as many operations as you want. To reconstruct the final equation, you simply need to perform an in-order traversal of the tree. To parenthesize as in your examples, you would place parentheses around the entire equation when leaving any interior (operator) node during the traversal, except for the root.

How many possible URLs can you make with the following characters?

I want to make a short URL service for 2 million assets but I want to use the shortest number of possible characters.
What is the math equation that I would need to use to figure it out? I know it has something to do with factorials, right?
It's not a factorial problem, but an exponential one.
If x is the number of possible characters, you need to solve the following equation for y:
x^y = 2000000
If you want to use all numbers and case-sensitive alpha [0-9A-Za-z], you have 62 possible values. This means you need to solve:
62^y = 2000000
y*log(62) = log(2000000)
y = log(2000000) / log(62)
y = 3.5154313828...
Of course, you can't have 3.5 characters in your URL, so you would need 4. If you want to change the character set you are using for your URL's, simply resolve the problem above using the number of values in your set.
Note Solving this equation assumes fixed-length URL's. For variable-length URL's, see Rob's answer.
#jheddings is close, and got the right answer, but the math was not quite correct. Don't forget you are not limited to all the permutations of characters of a specific length. You can also leverage URLs of length 1 through y characters. Therefore we want the closed value of this sum:
x + x^2 + x^3 + ... + x^y = 2000000
Fortunately, there is a closed form for that sum:
x + x^2 + x^3 + ... + x^y = x*(x^y - 1)/(x-1) = 2000000
x is the number of possible characters in our range. For simplicity sake, let's assume it only includes lowercase, uppercase, and numbers (26+26+10 = 62.)
Then we get the following equation:
2000000 = (62^(y+1) - 62)/(62-1)
2000000 = (62^(y+1) - 62)/(61)
2000000 * 61 = 62^(y+1) - 62
122000000 = 62^(y+1) - 62
122000000 + 62 = 62^(y+1)
122000062 = 62^(y+1)
log(122000062) = (y+1)
log(122000062) / log(62) = y+1
4.511492 = y+1
3.511492 = y
And, as you said, 3.5 characters is impossible so 4 are required. Admittedly the difference doesn't matter in this case. However, in certain scenarios (especially when dealing with base 2) it is very important.
Number of possible short URLs = (Number of possible different characters in ID) raised to the power of (Length of ID in url)
For instance, if you're only using lowercase characters (of which there are 26) and your URLs look like http://domain.com/XXXXX (for your unique id's of 5 characters), then you can make 26^5 = 11,881,376 short urls.
If you were using upper and lower case letters, you'd have 52, so 52^5 = 380,204,032 possible short URLs, et cetera.
You need to answer a number of questions, like what kinds of characters you want to allow in your set.
All letters and all digits? base 36 (5 characters can fit 2mil+)
Distinguish between upper and lowercase? That gets you to base 62 (4 characters)
Remove easily-mistaken characters and numbers (e.g. i/l 0/o)? roughly base 32 (also 5 characters)
You can often solve this kind of problem without any math wizardry.
26+26+10 = 62 characters
Try 1. 62 = 62
Try 2. 62*62 = 3,844
Try 3. 62*62*62 = 238,328
Try 4. 62*62*62*62 = 14,776,336
So 4 is your answer :)
According to the HTTP/URI Spec you can additionally use the following "unreserved characters": ALPHA / DIGIT / "-" / "." / "_" / "~"
That adds an additional 4 characters to your radix and thus
Math.log(2000000) / Math.log(66) = 3.4629721616408813
Although this still means you will end up with a 4 character URL path at maximum.

Resources