Lambda calculus help - lambda-calculus

So i'm totally stuck on this one part of a problem. It would be awesome if someone could help.........
Show that the term ZZ where Z is λz.λx. x(z z x) satisfies
the requirement for fixed point combinators that ZZM =β M(ZZM).

This is completely trivial.
You just apply the definition of β-reduction two times:
Z Z M = (λz.λx. x(z z x)) Z M > (λx. x(Z Z x)) M > M (Z Z M)
where > is the β-reduction.
Therefore Z Z M β-reduces to M (Z Z M) in two steps, hence Z Z M =β M (Z Z M).

Related

How to find the number of solutions of modular equation?

Find the number of solutions of 𝑥 = x (mod m)
Let p and q be the primes.
You can break a modular equation into separate equations if the factors are coprime.
This means that 𝑥**2 = x (mod m) is equivalent to 𝑥**2 = x (mod p) and 𝑥**2 = x (mod q).
Each of these can be factorized as x(x-1)=0 => x=0 or x=1.
So you know that x is 0 or 1 modulo p, and x is 0 or 1 modulo q. Each choice has 1 solution modulo m by the chinese remainder theorem so there will be 4 solutions.
2 are easy (x=0 and x=1). The other two can be found with the extended Euclidean algorithm as follows:
def egcd(a, b):
x,y = 0,1
lx,ly = 1,0
while b != 0:
q = a/b
(a, b) = (b, a%b)
(lx, x) = (x, lx-q*x)
(ly, y) = (y, ly-q*y)
return (lx, ly)
p=7
q=11
m=p*q
(lx, ly) = egcd(p,q)
print lx*p%m,ly*q%m

calculate x ^ (1 / y) mod m fast (modular root)

How can I solve x ^ ( 1 / y ) mod m fast, where x, y, m are all positive integers?
This is to reverse the calculation for x ^ y mod m. For example
party A hands party B agree on positive integer y and m ahead of time
party A generates a number x1 (0 < x1 < m), and hands party B the result of x1 ^ y mod m, call it x2
party B calculates x2 ^ ( 1 / y ) mod m, so that it gets back x1
I know how to calculate x1 ^ y mod m fast, but I don't know how to calculate x2 ^ (1 / y) mod m fast. Any suggestions?
I don't know how to call this question. Given x ^ y mod m is called modular exponentiation, is this called modular root?
I think you're asking this question: Given y, m, and the result of x^y (mod m) find x (assuming 0 <= x < m).
In general, this doesn't have a solution -- for example, for y=2, m=4, 0^2, 1^2, 2^2, 3^2 = 0, 1, 0, 1 (mod 4), so if you're given the square of a number mod 4, you can't get back the original number.
However, in some cases you can do it. For example, when m is prime and y is coprime to m-1. Then one can find y' such that for all 0 <= x < m, (x^y)^y' = x (mod m).
Note that (x^y)^y' = x^(yy'). Ignoring the trivial case when x=0, if m is prime Fermat's Little Theorem tells us that x^(m-1) = 1 (mod m). Thus we can solve yy' = 1 (mod m-1). This has a solution (which can be found using the extended Euclidean algorithm) assuming y and m-1 are coprime.
Here's working code, with an example with y=5, m=17. It uses the modular inverse code from https://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm
def egcd(a, b):
if a == 0: return b, 0, 1
g, x, y = egcd(b%a, a)
return g, y - (b//a) * x, x
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise AssertionError('no inverse')
return x % m
def encrypt(xs, y, m):
return [pow(x, y, m) for x in xs]
def decrypt(xs, y, m):
y2 = modinv(y, m-1)
return encrypt(xs, y2, m)
y = 5
m = 17
e = encrypt(range(m), y, m)
print decrypt(e, y, m)
RSA is based on the case when m is the product of two distinct primes p, q. The same ideas as above apply, but one needs to find y' such that yy' = 1 (mod lcm((p-1)(q-1))). Unlike above, one can't do this easily only given y and m, because there are no known efficient methods for finding p and q.

Write a loop invariant for partial correctness of Hoare Triple

I am new to the world of logic. I am learning Hoare Logic and Partial & Total correctness of programs. I tried alot to solve the below question but failed.
Write a loop invariant P to show partial correctness for the Hoare triple
{x = ¬x ∧ y = ¬y ∧ x >= 0} mult {z = ¬x * ¬y}
where mult is the following program that computes the product of x and y and stores it in z:
mult:
z := 0;
while (y > 0) do
z := z + x;
y := y - 1;
One of my friend gave me the following answer but I don't know if it correct or not.
Invariant P is (¬x * ¬y = z + ¬x * y) ∧ x = ¬x. Intuitively, z holds the part of the result that is already computed, and (¬x * y) is what remains to compute.
Please teach me step by step of how to solve this question.

Combinations of three positive numbers x, y, z so that x + y, x - y, y + z, y - z, x + z and x - z are perfect squares

Good morning, I'm new here and I bring a small problem. I'm having trouble develop efficient an algorithm for the following problem:
I need to find combinations of three positive numbers x, y and z so that x + y, x - y, y + z, y - z, x + z and x - z are perfect squares.
The issue is to develop an algorithm that finds all combinations of x, y and z between 1
and 2,000,000.
Currently I use a for within a for that certainly will not end before I have my grandchildren.
The basic idea to begin with a substitution, like:
u = x + y
v = x - y
w = y + z
Then x + y, x - y, y + z, y - z, x + z and x - z becomes
u, v, w, u - v - w, v + w, u - w [all have to be squares]
Then with another substitution, u = a², v = b², w = c², you get:
a², b², c², a² - b² - c², b² + c², a² - c² [all have to be squares]
now you can enumerate all a, b, c-s which may already be fast enough.
Further ideas could be to first enumerate all b², c², b²+c² using Pythagorean triples (by substituting it to m and n, enumerating all coprime (m,n) and then using Euclid formula) and then find for given (b,c) the as in a similar way (e.g. change a² - c² = x² to a² = x² + c² and use the triples again).
Extending BeniBela's answer,
x + y = (x - z) + (y + z)
x + y = (x + z) + (y - z)
So, triplets are valid only if the hypotenuse can be represented in two different forms.
Further filtering can be done by observing that (x - z) and (x + z) also form the hypotenuse of a Pythagorean triplet.

Fixed point of K combinator

The K combinator is K := (λxy.x) and the fixed point combinator is Y := λf.(λx.f x x) (λx.f x x). I tried to calculate YK:
YK = (λx.Kxx)(λx.Kxx) = (λx.x)(λx.x) = (λx.x) = I
So because YK is the fixed point of K:
K(YK) = YK
KI = I
KIe = Ie = e
for any e. But KIe should be equal to I!
You're not starting with the correct definition of the Y-combinator. It should be Y := λf.(λx.f (x x)) (λx.f (x x)) (note the parentheses around x x).
Since lambda-calculus is left-associative, f x x is equal to (f x) x, which obviously doesn't work.
Using the correct definition, we get
Y K := (λf.(λx.f (x x)) (λx.f (x x))) K
(λx.K (x x)) (λx.K (x x))
K ((λx.K (x x)) (λx.K (x x)))
K (Y K)
Since Y K doesn't reduce to I, the following substitution is not allowed.
K (Y K) = Y K
K I = I
So, K I e is simply
K I e := (K I) e
((λx.λy.x) I) e
(λy.I) e
I

Resources