Let Σ = { a; b} How can I define a PDA in JFLAP which recognizes the following? - formal-languages

L = {a^n b^k | 2n >= k}
For example.: abb is element of L, aabbb is element of L, ε is element of L, but babbb is not element of L, abbb is not element of L

The shortest string in L is the empty string, e. Given a string s in the language, the following rules hold:
as is in L
asb is in L
asbb is in L
We can combine these observations to get a context-free grammar:
S := aSbb | aSb | aS | e
By our observations, every string generated by this grammar must be in L. To show that this is a grammar for L, we must show that any string in L can be generated. To get a string a^n b^k we can do the following:
use rule #1 above x times
use rule #2 above y times
use rule #3 above z times
ensure x + y + z = n
ensure y + 2z = k
Setting y = k - 2z and substituting we find x + k - 2z + z = n. Rearranging:
if k > n, then z and x can be chosen however desired so long as k - n = z - x.
if k < n, then z and x can be chosen however desired so long as n - k = x - z.
If k = n, observe we might as well just choose y = n.
Note that we can always choose z and x in our above example since 0 <= x, z <= n and 0 <= k <= 2n.

Related

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.

Other Models of Computation

can anyone explain this concept to me? and help me out in figuring out the answer? as I don't quite get it yet but any help will do, thank you
There exists two really cool primitive recursive functions:
T(z, x1, x2, ..., xn, y) returns 0 if z is an encoding of a Turing Machine, and its computation with
inputs x1, ... xn encodes to y. The function returns 1 otherwise. U(y) returns the result of the computation y.
Goal:Use these two functions and the µ(mu) operator to get a definition of a function f(x1, x2, ...xn) which returns the output of the TM encoded by z
what I have so far:
using these two functions and the µ operator we can then generate a definition for the function f(x1,x2, ...xn) that returns the output of the TM encoded by z such that we have a TM that we can define as T(n) where this computes the number of primes which are less than or equal n. z = 0 which is the number of primes found, y = 1 which is for each y we test whether it is prime, while k <= n:, j = 1 are the possible divisors of y, d = 0 the number of divisors of k found, while j <= n: if y % j == 0:, d = d + 1, j = j + 1, if d== 2:, z= z+1,y=y+1, then return z which is the output of the TM encoded by z

Recursive division algorithm for two n bit numbers

In the below division algorithm, I am not able to understand why multiplying q and r by two works and also why r is incremented if x is odd.
Please give a theoretical justification of this recursive division algorithm.
Thanks in advance.
function divide(x, y)
if x = 0:
return (q, r) = (0, 0)
(q, r) = divide(floor(x/2), y)
q = 2q, r = 2r
if x is odd:
r = r + 1
if r ≥ y:
r = r − y, q = q + 1
return (q, r)
Let's assume you want to divide x by y, i.e. represent x = Q * y + R
Let's assume that x is even. You recursively divide x / 2 by y and get your desired representation for a smaller case: x / 2 = q * y + r.
By multiplying it by two, you would get: x = 2q * y + 2r. Looking at the representation you wanted to get for x in the first place, you see that you have found it! Let Q = 2q and R = 2r and you found the desired Q and R.
If x is odd, you again first get the desired representation for a smaller case: (x - 1) / 2 = q * y + r, multiply it by two: x - 1 = 2q * y + 2r, and send 1 to the right: x = 2q * y + 2r + 1. Again, you have found Q and R you wanted: Q = 2q, R = 2r + 1.
The final part of the algorithm is just normalization so that r < y. r can become bigger than y when you perform multiplication by two.
Algorithm PuzzleSolve(k,S,U) :
Input: An integer k, sequence S, and set U
Output: An enumeration of all k-length extensions to S using elements in U without repetitions
for each e in U do
Add e to the end of S
Remove e from U /e is now being used/
if k == 1 then
Test whether S is a configuration that solves the puzzle
if S solves the puzzle then
return "Solution found: " S
else
PuzzleSolve(k-1,S,U) /a recursive call/
Remove e from the end of S
Add e back to U e is now considered as unused
This algorithm enumerates every possible size-k ordered subset of U, and tests each subset for being
a possible solution to our puzzle. For summation puzzles, U = 0,1,2,3,4,5,6,7,8,9 and each position
in the sequence corresponds to a given letter. For example, the first position could stand for b, the
second for o, the third for y, and so on.

Maximum AND of two numbers

Given two numbers A and B what can be the maximum number that can be formed by AND operation of any two numbers x and y between A and B it means A ≤ x < y ≤ B.
Example : A=1 and B=3 then answer is 2.
I can't go for brute as both A and B can go upto 10^18
Note: this answer assumes A ≥ 0, which is fair considering the question is not tied to a particular language or integer representation that would give meaning to bit operations on negative integers.
If B is odd, the maximum x & y is B-1, obtained by picking y = B and x = B-1.
If B is even and A is low enough to allow to pick y = B-1 and x = B-2, then the maximum is B-2, for this choice of x and y. Otherwise, there is only one choice, B & (B-1), whatever that evaluate to.
To expand on Pascal Cuoq's excellent answer, if we allow A < 0 and assume two's complement arithmetic (which is what essentially all modern computers use for signed integers), then we again have several cases:
If A < 0 ≤ B, then we may choose x = -1 and y = B, in which case x & y = B.
If A < B < 0, then everything works just like for positive numbers: if B is odd, or if A = B-1, then the optimal choice is x = B-1, y = B; otherwise it is x = B-2, y = B-1.
(The reason it works out exactly the same way is because two's complement signed arithmetic is essentially identical to unsigned binary arithmetic, except that we interpret numbers with their highest bit set as negative. Thus, the only really new case is the one where the range from A to B "wraps around" from -1 to 0.)
Summing up these results, we have that, for A ≤ x < y ≤ B, with any given (possibly signed two's complement) A and B, the maximum of x & y is:
If A < 0 ≤ B, then x = -1 and y = B yield x & y = B.
Otherwise, if B is odd, x = B-1 and y = B yield x & y = B-1.
Otherwise, if B > A+1, then x = B-2 and y = B-1 yield x & y = B-2.
If none of the above hold, the only remaining case is B = A+1 (with B even); in this case, the only possible choice is x = A, y = B; whatever x & y equals in this case, it is trivially the maximum.

Prolog: Get values from 0 to K - 1

I'm attempting to create a program where given N, I need Q = 0...N.
So if given N = 1: Q = 0. If given N = 5: Q = 0, Q = 1,...,Q = 4
My attempt so far:
values(N,Q) :- values_helper(0,N,Q).
values_helper(N, N, Q).
values_helper(X,N,Q) :- X0 is X + 1, X0 < N, values_helper(X0,N,X0).
My logic behind it is that I increment X until it reaches the value of N, at which point the program stops. However, I'm not getting any bindings for Q, just an empty set. I also know that I'm neglecting to stop at N - 1.
EDIT: Fixed up ambiguities with the description.
Both of your values_helper clauses don't do what you expect. The first one succeeds if the first 2 arguments are the same, and doesn't impose any constraints on Q. What you actually want is that Q is set equal to the first argument, as long as it's smaller than the second argument:
values_helper(Q, N, Q) :- Q < N.
In the second clause, again you don't use Q anywhere. The recursive call should be values_helper(X0, N, Q), giving:
values_helper(X, N, Q) :- X0 is X + 1, X0 < N, values_helper(X0, N, Q).
These clauses give the expected output:
?- values(5,Q).
Q = 0 ? ;
Q = 1 ? ;
Q = 2 ? ;
Q = 3 ? ;
Q = 4 ? ;
no
Note that for any N <= 0, this terminates without finding any value for Q, which I believe is the expected behavior.

Resources