Design a Turing Machine to accept {1^n: n is prime number}.
I have this homework to make a recognizer Turing Machine that will be accepted if the occurrences of 1 are equal to any prime number. As of now, I still got no idea how to find the solution related to this prime number.
How should I go about this?
Because we're making a Turing machine and we haven't explicitly said we care about performance, odds are we just care about showing that TMs can solve this problem - so, any solution, no matter how dumb, should suffice. What is a correct, if needlessly tedious, way to show that a number in unary format (e.g., 1^p) is a prime number? One way is to check whether the number of p's is evenly divisible by any number between 2 and p - 1, inclusive. This is actually pretty easy to do for a Turing machine. Since the problem doesn't tell us not to, we can make it even simpler by using a multi-tape Turing machine for our construction.
Let the input be on tape #1 and use tape #2 to record the current thing we are trying to divide the input by. Before we begin, we can verify that our p is greater than 2, as follows:
see if the current tape square is 1, if so, move right, else halt-reject since 0 is not prime
see if the current tape square is 1, if so, move right, else, halt-reject since 1 is not prime
see if the current tape square is 1, if so, reset the tape head and continue on with our division process, knowing p > 2; else, halt-accept since 2 is prime.
If we're continuing at this point, that means we've verified we are looking at the unary encoding of a number greater than 2. We need to do this because 2 is the first number for which we need to check divisibility, and we don't want to say 2 is composite since 2 divides it. At this stage, we can write 11 (unary 2) on tape #2. If you like, you can do this as you are resetting the tape head as mentioned above. Otherwise, you can use some new states specifically for that part of the setup.
We are now looking at a TM configuration like this:
#1111111111111111111111#
^
#11#
^
We want to see if the number represented on the second tape evenly divides the number represented on the first tape. To do this, we can "cross out" numbers on the first tape repeatedly, in groups the size of the second tape, until we run out of numbers on the first tape. If we run out in the middle of crossing out a whole group, then the number represented on the first tape is not evenly divisible by the number represented on the second tape, and we can proceed to check increasing numbers on the second tape. If we run out after having crossed out an entire group, then it is evenly divisible by a number other than 1 and itself, so we can halt-reject as the number is not prime. Processing our example would look like:
=> #1111111# => #x111111# => #xx11111# => #xx11111#
^ ^ ^ ^
#11# #11# #11# #11#
^ ^ ^ ^
=> #xx11111# => #xx11111# => #xx11111# => #xxx1111#
^ ^ ^ ^
#11# #11# #11# #11#
^ ^ ^ ^
=> #xxxx111# => ... reset => #xxxx111# => ... cross
^ tape 2 ^ off another
#11# back to #11# pair of 1s
^ head ... ^ ...
=> #xxxxxx1# => #xxxxxxx#
^ ^
#11# #11#
^ ^
At this stage we see a 1 on the second tape and a blank on the first tape; this means the number was not divisible by our current guess. If we had seen blank/blank, we could have halt-rejected immediately. Instead, we need to continue checking larger possible divisors. At this stage we need to:
reset the first tape head to the beginning of the input, replacing x's with 1's again.
add an extra 1 to the second tape head to increment its value, then reset the head to the beginning of its value.
repeat the divisibility check described above.
If we continue this process, we will eventually find a divisor of the number represented on the input tape, if that number is composite. As it is, however, we will currently halt-reject on prime numbers when the second tape increases to the same number as the input. We will then find that the prime number evenly divides itself. We need to check for this; a good place would be between steps 2 and 3 in the last set of 3 steps above, we can compare tapes #1 and #2 and see if they match exactly. this would be just like the divisibility check, but it would only cross off at most one copy of tape #2 from tape #1, and it would halt-accept if it got to blank/blank rather than halt-rejecting.
Obviously, there are a lot of details to fill out if you want to formally define the TM by giving its transition table. But, it can be done and a procedure like the one outlined here can be used to solve this problem. Again, this is not the most efficient way to solve this, but it is a way to solve it, which generally is good enough when looking for TMs for some problem.
Related
I need to construct linear bounded automaton for the language L = { a^{n!} : n >= 0 }. I know how LBA functions, however, I don't have a thought how it can check the n! that to in the power of a. I might want to hear a few suggestions, as I am experiencing difficulty in developing the specific LBA for it.
A linear bounded automaton is a multi-tape non-deterministic Turing machine whose tape length is bounded by some fixed linear function of the input length. That is, the amount of tape available to work with must be known in advance from the length of the input and that length must grow linearly with input size. If we can determine a Turing machine for this language and show we know exactly how much tape will be used as a function of the input length, and that function is linear. we have shown the TM to be an LBA.
Consider the following multi-tape non-deterministic Turing machine for checking whether the input it a^(n!):
if the input tape is empty, halt-reject
write a on the second tape
scan the input tape and if there is only one instance of a remaining, halt-accept
otherwise, go back to the beginning of input
then, cross off (n-1) instances of a in the input for every n instances you find. to do this, move the second tape head right to count up to n-1, and when you reach the blank after the last a on the second tape, leave the a you are at on the input tape alone, reset the second tape head, and continue the process
if you end up running out of instances of a on the input tape while attempting to cross off all but every n'th one during step 5, halt-reject, since the input tape was greater than (n-1)! but was not evenly divisible by n.
otherwise, if you run out of instances of a at the same time you complete a full counting of the second tape, reset both tape heads, write another a to the end of the second tape, and continue the process from step 3.
Here's an example of how this TM functions:
Input: #aaaaaa# #aaaaaa# #xaaaaa# #xaaaaa#
^ => ^ => ^ => ^
Second: ######## #a###### #a###### #a######
^ ^ ^ ^
#xaxaaa# #xaxaaa# #xaxaxa# #xaxaxa#
=> ^ => ^ => ^ => ^
#a###### #a###### #a###### #aa#####
^ ^ ^ ^
#xxxaxa# #xxxxxa#
=> ^ => ^ => halt-accept since we are at the end
#aa##### #aa##### of the tape and looking at a blank
^ ^ on the second tape and only one a
remains
A simple analysis of the way this TM works shows that the number of additional tape cells used cannot be more than the number of tape cells used by the input. Because we only use additional tape cells for writing down the current divisor, and all divisors of sufficiently large values of n! are smaller than n!, the total number of tape cells in play (including input) is certainly less than 2*|input|. But 2*|input| is a linear function of the input size |input|, so this TM is also a LBA.
I have a problem that I think it's easiest to solve with awk but I wrapped my head around it.
Inside a file I have repeating output like this:
....
Name="BgpIpv4RouteConfig_XXX">
<Ipv4NetworkBlock id="13726"
StartIpList="x.y.z.t"
PrefixLength="30"
NetworkCount="10000"
... other output
then this block will repeat.
a)I want to match on BGPIpv4Route.*, then skip 2 lines (the "n" keyword of awk), then when reaching Prefix Length:
- either replace it with random (25,30)
or
- better but I guess harder (no idea came to mind for keeping track of what was used and looping among /25../30) -> first occurrence /25, second one /26...till /30, then rollback to /25
b) then next line with NetworkCount depending on the new value of PrefixCount calculate it as 65536 / 2^(32-Prefix Count)
eg: if PrefixCount on this occurrence was replaced with /25, then NetworkCount on the line following it = 65536 / 2 ^ 7 = 65536 / 128 = 512
I found some examples with inserting/changing a line after one that matched (or with a counter variable X lines below the match) but I got a bit confused with the value generation part and also with the changing of two lines where one is depending on the other.
Not sure I made any sense...my head is a bit overwhelmed with what I'm finding everywhere right now.
Thanks in advance!
this should do
$ awk 'BEGIN {q="\""; FS=OFS="="; n=split("25=26=27=28=29=30",ps)}
/BgpIpv4Route/ {c=c%n+1}
/PrefixLength/ {$2=q ps[c] q}
/NetworkCount/ {$2=q 65536/2^(32-ps[c]) q}1' file
perhaps minimize computation by changing to 2^(ps[c]-16)
If there are free standing PrefixLength and NetworkCount attributes perhaps you need to qualify them for each BgpIpv4Route context.
I have 2 variables, one for raw p-values and another for adjusted p-values. I need to compute a new variable based on the values of these two variables. What I need to do isn't too complicated, but I have a hard time doing it in SPSS because I can't figure out how I can reference a particular row for an existing variable in SPSS syntax.
The first column lists raw p-values in ascending order. The next column lists adjusted p-values, but these adjusted p-values are still incomplete. I need to compare two adjacent p-values in the adjusted p-values column (e.g., row 1 and 2, row 2 and 3, row 3 and 4, and so forth), and take the p-values whichever is smaller in each of these comparisons and enter those p-values into the following column as values for a new variable.
However, that's not the end of the story. One more condition has to be met. That is, the new p-values have to be in the same order as the raw p-values. However, I cannot ensure this if I start the comparisons from the top row. You can see that (i') is greater than (h') and (g'), and (d') is greater than (c'), (b'), and (a') in the example below (picture).
In order to solve this issue, I would need to start the comparison of the adjusted p-values from the bottom. In addition, I would need to compare the adjusted p-values to the new p-values of one row below. One exception is that I can simply use the value of (a) as the value of (a') since the value of (a) should always be the greatest of all the p-values as a rule. Then, for (b') , I need to compare (b) and (a') and enter whichever is smaller as (b'). For (c'), I need to compare (c) and (b') and enter whichever is smaller as (c'), and so forth. By doing this way, (d') would be 0.911 and (i') would be 0.017.
Sorry for this long post, but I would really appreciate if I can get some help to do this task in SPSS.
Thank you in advance for your help.
Raw p-values | Adjusted p-values (Temporal)| New p-values (Final)
-------------|-----------------------------|---------------------
0.002 | 0.030 (i) | 0.025 (i')
0.003 | 0.025 (h) | 0.017 (h')
0.004 | 0.017 (g) | 0.017 (g')
0.005 | 0.028 (f) | 0.028 (f')
0.023 | 0.068 (e) | 0.068 (e')
0.450 | 1.061 (d) | 1.061 (d')
0.544 | 1.145 (c) | 0.911 (c')
0.850 | 0.911 (b) | 0.911 (b')
0.974 | 0.974 (a) | 0.974 (a')
Another tool that may be convenient is the SHIFT VALUES command. It can move one or more columns of data either forward or backward.
I wonder whether the purpose of this has to do with adjusting p values for multiple testing corrections as with Benjamin-Hochberg FDR or others similar. If that is the case, you might find the STATS PADJUST (Analyze > Descriptives > Calculate adjusted p values) extension command useful. It offers six adjustment methods. You can install it from the Utilities (pre-V24) or Extensions (V24+) menu.
To get you started, here are a few tools that can help you with this task:
The LAG function
you can compare values in this line and the previous one, for example, the following will compare the Pval in each line to the one in the previous one, and put the smaller of the two in the NewPval:
compute NewPVal=min(Pval, lag(Pval)).
If you want to do the same process only start from the bottom, you can easily sort your data in reverse order and do the same.
CREATE + LEAD
if you want to make comparisons to the next line instead of the previous line, you should first create a "lead" variable and then compare to it.
for example, the following syntax will create a new variable that for each line contains the value of Pval in the next line, and then chooses the smaller of the two for the NewPval:
create /LeadPval=LEAD(Pval 1).
compute NewPVal=min(Pval, LeadPval).
Using case numbers
You can use case numbers (line numbers) in calculations and in conditions. For example, the following syntax will let you make different calculations in the first line and the following ones:
if $casenum=1 NewPval=Pval.
if $casenum>1 NewPVal=min(Pval, lag(Pval)).
Okay I need to redraw the pascal's triangle and explain the Fibonacci sequence embedded in it.. And i need to observe over 12 rows of the triangle (which ends on the number 144 in the fibonacci sequence) -- I understand this part as i am just explaining how each row diagonally forms the sum of the Fibonacci numbers.
But I need to use the fact that the rth number in the nth row of the triangle is
C(n, r) = n!/r! n-r!
This last part is whats confusing me.. How can i use C(n,r) to explain the Fibonacci sequence in the triangle??
Please Help. Thanks
Consider the following problem :
In how many ways can you go up a ladder of n steps if you can take either a single step at a time or 2 steps at a time?
Solution 1 : Let's construct a recurrence relation for this problem. It's pretty clear that the recurrence would be something like this : a(n) = a(n-1) + a(n-2); where a(1)=1 and a(2)=2
Thus, the answer for n would be the (n+1)th fibonacci term.
Solution 2 : Each unique way of climbing up the ladder corresponds to a unique sequence of 1's and 2's which adds up to n. The number of such sequences thus would be our answer. Let's start counting such sequences :
Number of sequences without a 2 = $ {n \choose 0 } $.
Number of sequences with one 2 = $ {n-1 \choose 1 } $.
.
.
.
and so on.
In case of even n, the last term would be $ {n/2 \choose n/2 } $.
And for odd n, it would be $ {(n+1)/2 \choose (n-1)/2 } $.
As you can see, These are the diagonal terms in a pascal's triangle.
As these two solutions compute the same result, hence they must be equal. Thus we get the relation between Fibonacci numbers and the diagonals of a pascals triangle.
Refer the link
http://ms.appliedprobability.org/data/files/Articles%2033/33-1-5.pdf
for anymore doubts.
x(n) is given
need x(-n+3)
so to solve it:
first advance the x(n) signal by 3 units(time)
then fold it, or make a reflection of it
are the above steps correct or is the following correct
first fold the x(n) signal
then advance the signal by 3 units
?
Yes, this is a common source of confusion when learning about signals. Here's what I usually do.
Let y[n] = x[-n+3]. Because of -n, y[n] is obviously a time-reversed version of x[n]. But the question about the shift remains.
Notice that y[3] = x[0]. Therefore, y[n] is achieved by first reflecting x[n] about n=0 and then delaying the reflected signal by 3.
For example, let x[n] be the unit step function u[n]. Draw x[n], then draw y[n].
Actually here is what I do:
Let
x(n) = {1,-1,2,4,-3,0,6,-3,-1,2,7,9,-7,5}
^
Suppose origin or n=0 is 6. Note that the ^ symbol indicates the origin. First, we find the folder sequence of x(-n) from x(n). So first we fold or we can say reverse the form of x(n), we get,
The folder sequence of x(-n) from x(n) is
x(-n) = {5,-7,9,7,2,-1,-3,6,0,-3,4,2,-1,1}
^
then shift the sequence of x(-n) towards right hand side by 3 units, we will get
x(-n+3) = {5,-7,9,7,2-1,-3,6,0,-3,4,2,-1,1}
^
Now, the sample 4 is at the origin.
Above steps are correct.
The following steps can be corrected too if these are followed like:
first fold the x(n) signal
then delay the signal by 3 units this will yield x(-n+3).