This is a (really) hard exercise for my next exam. It's intended for scoping, binding and parameter-passing rules undestanding. I can't figure out even the first 3 numbers outputted, this is driving me crazy.
What is the output of this C-like program with dynamic scope, shallow binding and expression evaluation from left to right?
1 A : {
2
3 int x = 5;
4 int y = 7;
5
6 proc P(ref int y, valueresult int z, int R(name int)) {
7 z = y-- + R(++x + ++y);
8 write(x, y, z);
9 z = R(z++);
10 }
11
12 B : {
13
14 int x = 3;
15
16 int Q(name int w) {
17 return (w + x++ + y++);
18 }
19
20 P(x, y, Q); // start here
21 write(y++, x++);
22 }
23
24 write(y, x);
25
26 }
A.x = 5
A.y = 7
B.x = 3
Calls P(3, 7, Q) - Inside P, y is a ref to B.x, which value is 3, z is 7. Due to dynamic scope, inside P, x is B.x.
Inside P, in the first instruction, we have the post-decrement y-- evaluated first, so the evaluated value would be 3 and B.x will change to 2.
R is called by name, so the return (w + x++ + y++) will become return ++x + ++y + x++ + y++. The entire first expression is expanded to something equivalent to z = y-- + (++x + ++y + x++ + y++).
In the expression ++x + ++y + x++ + y++, we have a pre-increment ++x, which will change B.x to 3 and give 3 as result. The ++y will change B.x to 4 and evaluate 4. The x++ will evaluate to 4 and change to 5 and the y++ will evaluate to 5 and change to 6.
So the returned value from Q is 3 + 4 + 4 + 5 = 16. This will be added to the previous z-- value which is 3, so 19 will be assigned to z and B.x will be 6.
The write will print x which is due to dynamic scope B.x, so will print 6. The y is a reference to the same B.x, so will print 6. The z is 19. So it will print 6, 6, 19.
In the third instruction of the P, we have z = R(z++) which due to call by name will expand to something equivalent to z = (z++ + x++ + y++).
So we get the z value, which is 19 and increment to 20 (post-increment). Add it to the value of B.x (6) and change B.x to 7. Add the 7 from y and increment y (which is B.x) to 8.
So z = 19 + 6 + 7 = 32. B.x is 8.
Due to the valueresult of z, the A.y will be changed to 32.
Outside P, the write(y++, x++) will get the A.y value, which is 32 and print it. Then A.y will change to 33. B.x which is 8 will be printed and changed to 9. So it will print 32, 8.
Finally, the A.y will be printed (its value is 33). The A.x will be printed, which is 5. So it will print 33, 5.
Concluding, the program prints 6, 6, 19, 32, 8, 33, 5.
Here's what I've come up with - in a line-by-line trace:
before P(x,y,Q) A.x = 5 A.y = 7 B.x = 3
entering P A.x = 5 A.y = 7 B.x = 3 y = 3 z = 7
before z = y-- + R(++x + ++y) A.x = 5 A.y = 7 B.x = 3 y = 3 z = 7
entering R w = 9 A.x = 6 A.y = 7 B.x = 3
return (w + x++ + y++)
leaving R w = 9 A.x = 6 A.y = 8 B.x = 4 returns 19
after z = y-- + R(++x + ++y) A.x = 6 A.y = 8 B.x = 4 y = 4 z = 22
write(x, y, z)
6 4 22
before z = R(z++) A.x = 6 A.y = 8 B.x = 4 y = 4 z = 22
entering R w = 22 A.x = 6 A.y = 8 B.x = 4
return (w + x++ + y++)
leaving R w = 22 A.x = 6 A.y = 9 B.x = 5 returns 34
after z = R(z++) A.x = 6 A.y = 9 B.x = 5 y = 5 z = 34
after P(x,y,Q) A.x = 6 A.y = 34 B.x = 5
write(y++, x++)
34 5
after write(y++,x++) A.x = 6 A.y = 35 B.x = 6
write(y, x)
35 6
Related
In Maxima I put:
q_d: 1000-5*p_d;
q_s: -250+2*p_s;
p_d: (1+t)*p_s;
eq:q_d = q_s;
solve(eq,p_s);
EC: 10*q_d + 0.01 * (q_d**2);
get the result
p_s=1250/(5*t+7)
0.01*(1000-5*p_s*(t+1))^2+10*(1000-5*p_s*(t+1))
How do I further simplify EC in term of 't' only?
One way to go about this is to express all the relations you listed as equations, and then solve the equations for the variables you want to eliminate, then you get expressions in terms of t which you can substitute into EC to get a result only in terms of t.
(%i2) e1: q_d = 1000-5*p_d;
(%o2) q_d = 1000 - 5 p_d
(%i3) e2: q_s = -250+2*p_s;
(%o3) q_s = 2 p_s - 250
(%i4) e3: p_d = (1+t)*p_s;
(%o4) p_d = p_s (t + 1)
(%i5) e4: q_d = q_s;
(%o5) q_d = q_s
(%i6) solns: solve ([e1, e2, e3, e4], [q_d, q_s, p_d, p_s]);
1250 t - 750 1250 t - 750
(%o6) [[q_d = - ------------, q_s = - ------------,
5 t + 7 5 t + 7
1250 t + 1250 1250
p_d = -------------, p_s = -------]]
5 t + 7 5 t + 7
Now in %o6 I have a list of equations for the variables to be eliminated.
(%i7) EC: 10*q_d + 0.01 * (q_d**2);
2
(%o7) 0.01 q_d + 10 q_d
I'll substitute into EC to get a result in terms of t only.
(%i8) subst (solns[1], EC);
2
0.01 (1250 t - 750) 10 (1250 t - 750)
(%o8) -------------------- - -----------------
2 5 t + 7
(5 t + 7)
I'll use ratsimp to simplify the result.
(%i9) ratsimp (%);
rat: replaced 0.01 by 1/100 = 0.01
2
46875 t + 68750 t - 58125
(%o9) - --------------------------
2
25 t + 70 t + 49
I was wondering if there is an easy way to represented the following dependencies in Z3:
x = 0
y = x + 1
y = y + 10
Thanks!
y = y + 10 is always unsatisfiable. I'm guessing you intend these to model a series of assignments, as in an imperative programming language? In that case, you should convert it to static single assignment form (https://en.wikipedia.org/wiki/Static_single_assignment_form). That is, model it as follows:
x0 = 1
y0 = x0 + 1
y1 = y0 + 10
Suppose that I define some function, then make a change of variable and expand, as in the following lines:
declare(a,real); declare(k,real); declare(z,real);
myFun(a,k,z):=(1-1/2*((k-a)/2)^2)*z - 1 + 1/2* ((k+3*a)/2)^2;
myFun(a,k,z),simp,a=x0+x1*k;
expand(%);
What I would like to do now is to obtain a polynomial in k, i.e. collect the terms in each power of k with one command so that it shows something like:
(...)k^2 + (...)k + (...)
declare(a,real); declare(k,real); declare(z,real);
myFun(a,k,z):=(1-1/2*((k-a)/2)^2)*z - 1 + 1/2* ((k+3*a)/2)^2;
myFun(a,k,z),simp,a=x0+x1*k;
P: expand(%);
rat(P, k);
gives
2 2 2
(%o7)/R/ - (((x1 - 2 x1 + 1) z - 9 x1 - 6 x1 - 1) k
2 2
+ ((2 x0 x1 - 2 x0) z - 18 x0 x1 - 6 x0) k + (x0 - 8) z - 9 x0 + 8)/8
coeff returns each of the coefficients
coeff(P, k^2);
2 2
x1 z x1 z z 9 x1 3 x1 1
(%o8) - ----- + ---- - - + ----- + ---- + -
8 4 8 8 4 8
Now that CodeSprint 3 is over, I've been wondering how to solve this problem. We need to simply calculate nCr mod 142857 for large values of r and n (0<=n<=10^9 ; 0<=r<=n). I used a recursive method which goes through min(r, n-r) iterations to calculate the combination. Turns out this wasn't efficient enough. I've tried a few different methods, but they all seem to not be efficient enough. Any suggestions?
For non-prime mod, factor it (142857 = 3^3 * 11 * 13 * 37) and compute C(n,k) mod p^q for each prime factor of the mod using the general Lucas theorem, and combine them using Chinese remainder theorem.
For example, C(234, 44) mod 142857 = 6084, then
C(234, 44) mod 3^3 = 9
C(234, 44) mod 11 = 1
C(234, 44) mod 13 = 0
C(234, 44) mod 37 = 16
The Chinese Remainder theorem involves finding x such that
x = 9 mod 3^3
x = 1 mod 11
x = 0 mod 13
x = 16 mod 37
The result is x = 6084.
Example
C(234, 44) mod 3^3
First convert n, k, and n-k to base p
n = 234_10 = 22200_3
k = 44_10 = 1122_3
r = n-k = 190_10 = 21001_3
Next find the number of carries
e[i] = number of carries from i to end
e 4 3 2 1 0
1 1
r 2 1 0 0 1
k 1 1 2 2
n 2 2 2 0 0
Now create the factorial function needed for general Lucas
def f(n, p):
r = 1
for i in range(1, n+1):
if i % p != 0:
r *= i
return r
Since q = 3, you will consider only three digits of the base p representation at a time
So
f(222_3, 3)/[f(210_3, 3) * f(011_3, 3)] *
f(220_3, 3)/[f(100_3, 3) * f(112_3, 3)] *
f(200_3, 3)/[f(001_3, 3) * f(122_3, 3)] = 6719344775 / 7
Now
s = 1 if p = 2 and q >= 3 else -1
Then
p^e[0] * s * 6719344775 / 7 mod 3^3
e[0] = 2
p^e[0] = 3^2 = 9
s = -1
p^e[0] * s * 6719344775 = -60474102975
Now you have
-60474102975 / 7 mod 3^3
This is a linear congruence and can be solved with
ModularInverse(7, 3^3) = 4
4 * -60474102975 mod 27 = 9
Hence C(234, 44) mod 3^3 = 9
Here's the question:
Solve the recurrence by obtaining a theta bound for T(n) given that T(1) = theta(1).
T(n) = n + T(n-3)
Attempted Solution:
T(n) = T(n-6) + (n-3) + n
= T(n-9) + (n-6) + (n-3) + n
= T(n-(n-1)) + [(n-n) + (n-(n-3)) + (n-(n-6)) + ... + n]
= T(1) + [0 + 3 + 6 + ... + n]
= theta(1) = 3[1 + 2 + 3 + ... + n/3]
= theta(1) + [(n/3)(n/3 + 1)]/2
= theta(1) + (n^2+3n)/6
When I double check to see if the solution fits the recurrence, it doesn't work.
The issue was that you were getting the wrong summation.
It doesn't start at 0, since your last T function was T(n - (n-1)) , which means previous one was T(n-(n-4)). So the summation starts at 4, and goes up till n.
If you don't know how to find the summation of this, I'd suggest you look at some of the proofs from the summation formula. This is what the solution looks like.
T(n) = T(n-3) + n
= T(n-6) + (n-3) + n
= T(n-(n-1)) + [ (n-(n-4)) + (n-(n-7)) + ... + n]
= T(1) + [4 + 7 + ... + n]
= theta(1) + (4 + n) * (n - 1)/6