Compute the weakest precondition - preconditions

Compute the weakest precondition for each of the following assignment statements and postconditions:
a = a + 2 * b - 1 {a > 1}
attempt:
a is on both sides
0 = 2b -1
1/2 = b is the weakest precon
answer:
b > 1 - a/2
how do you get this answer?

Here is answer :
a = a + 2 * b - 1 {a > 1}
We replace a in {a > 1} by a + 2 * b - 1
a + 2 * b - 1 > 1
a + 2 * b > 2
2 * b > 2 - a
b > 1 - a / 2`
I think you found the answer since but for the people who might get here looking for this answer.

Related

what does "Train it to fit the target concept -2 + x1 + x2 > 0." mean?

In machine learning what does "Train it to fit the target concept -2 + x1 + x2 > 0" mean?
is it that if x1 = 0 and x2 = 0 then -2 + 0+ 0 > 0 and since that is impossible then the output is 0?

Simplify equation after solving

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

Solve equation with complex conjugate

When I try to do this:
(%i1) declare (z, complex);
(%o1) done
(%i2) eq1: z^3 + 3 * %i * conjugate(z) = 0;
3
(%o2) 3 %i conjugate(z) + z = 0
(%i3) solve(eq1, z);
1/6 5/6 1/3 1/3
(- 1) (3 %i - 3 ) conjugate(z)
(%o3) [z = - -----------------------------------------,
2
1/6 5/6 1/3 1/3
(- 1) (3 %i + 3 ) conjugate(z)
z = -----------------------------------------,
2
1/6 1/3 1/3
z = - (- 1) 3 conjugate(z) ]
conjugates are not simplified. And the solution for z in terms of z isn't very useful. Is there a way to simplify it?
Also, how can I simplify out the (-1)^(1/6) part?
Also, this equation clearly has 0 as its root, but it's not in the solution set, why?
I don't think solve knows anything about conjugate. Try this to solve it with the real and imaginary parts of z as two variables. Like this:
(%i2) declare ([zr, zi], real) $
(%i3) z : zr + %i*zi $
(%i4) eq1: z^3 + 3 * %i * conjugate(z) = 0;
(%o4) (zr+%i*zi)^3+3*%i*(zr-%i*zi) = 0
(%i5) solve (eq1, [zr, zi]);
(%o5) [[zr = %r1,
zi = (sqrt(9*%r1^2-%i)+3*%r1)^(1/3)-%i/(sqrt(9*%r1^2-%i)+3*%r1)^(1/3)
+%i*%r1],
[zr = %r2,
zi = ((sqrt(3)*%i)/2-1/2)*(sqrt(9*%r2^2-%i)+3*%r2)^(1/3)
-(%i*((-(sqrt(3)*%i)/2)-1/2))/(sqrt(9*%r2^2-%i)+3*%r2)^(1/3)
+%i*%r2],
[zr = %r3,
zi = ((-(sqrt(3)*%i)/2)-1/2)*(sqrt(9*%r3^2-%i)+3*%r3)^(1/3)
-(%i*((sqrt(3)*%i)/2-1/2))/(sqrt(9*%r3^2-%i)+3*%r3)^(1/3)+%i*%r3]]
Note the variables%r1, %r2, and %r3 in the solution. These represent arbitrary values.

using wxMaxima to factor a polynomial

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

Calculated nCr mod m (n choose r) for large values of n (10^9)

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

Resources