DFA for integer in binary that is: 1mod5 - finite-automata

I can create a DFA for 0mod5, but I'm having trouble figuring out how to make the DFA for 1mod5. For examples such as 11=1mod5, my DFA works. However, when I have something like 16, 15 is 01111 and I don't know how to make that into a 16.

Start in state s := 0. Every new symbol n you read, go to state s := (2s + n) % 5. Accept if you are in state s = 1.

Related

Given no modulus or if even/odd function, how would one check for an odd or even number?

I have recently sat a computing exam in university in which we were never taught beforehand about the modulus function or any other check for odd/even function and we have no access to external documentation except our previous lecture notes. Is it possible to do this without these and how?
Bitwise AND (&)
Extract the last bit of the number using the bitwise AND operator. If the last bit is 1, then it's odd, else it's even. This is the simplest and most efficient way of testing it. Examples in some languages:
C / C++ / C#
bool is_even(int value) {
return (value & 1) == 0;
}
Java
public static boolean is_even(int value) {
return (value & 1) == 0;
}
Python
def is_even(value):
return (value & 1) == 0
I assume this is only for integer numbers as the concept of odd/even eludes me for floating point values.
For these integer numbers, the check of the Least Significant Bit (LSB) as proposed by Rotem is the most straightforward method, but there are many other ways to accomplish that.
For example, you could use the integer division operation as a test. This is one of the most basic operation which is implemented in virtually every platform. The result of an integer division is always another integer. For example:
>> x = int64( 13 ) ;
>> x / 2
ans =
7
Here I cast the value 13 as a int64 to make sure MATLAB treats the number as an integer instead of double data type.
Also here the result is actually rounded towards infinity to the next integral value. This is MATLAB specific implementation, other platform might round down but it does not matter for us as the only behavior we look for is the rounding, whichever way it goes. The rounding allow us to define the following behavior:
If a number is even: Dividing it by 2 will produce an exact result, such that if we multiply this result by 2, we obtain the original number.
If a number is odd: Dividing it by 2 will result in a rounded result, such that multiplying it by 2 will yield a different number than the original input.
Now you have the logic worked out, the code is pretty straightforward:
%% sample input
x = int64(42) ;
y = int64(43) ;
%% define the checking function
% uses only multiplication and division operator, no high level function
is_even = #(x) int64(x) == (int64(x)/2)*2 ;
And obvisouly, this will yield:
>> is_even(x)
ans =
1
>> is_even(y)
ans =
0
I found out from a fellow student how to solve this simplistically with maths instead of functions.
Using (-1)^n :
If n is odd then the outcome is -1
If n is even then the outcome is 1
This is some pretty out-of-the-box thinking, but it would be the only way to solve this without previous knowledge of complex functions including mod.

Round to Integer in Smalltalk

I am currently building my first stuff on Smalltalk and I have hit an issue. I have to deal with a user-entered number, and I need to div it by 2 and still be an integer. If an user inputs 10, I will work with 5, if they input 11, I have to work with 6, but I will obviously get 5.5.
If I could get the mod of a number I could simply make sure mod = 0 else add 0.5 and it would do just as good, but I just can't find how to make a mod operation in SmallTalk, all my searches end up in unrelated stuff about actual social smalltalk, which is extremely frustrating.
So if you could tell me how to get the mod of a number it would be great, if you could tell me how to round up with a separate function, even better. Thanks for your help and time beforehand.
UPDATE: After some research, I tried to do it this way:
mod := par rem: 2.
mod = 0 ifFalse: [ par := par + 0.5 ].
where as "mod" is mod of the variable "par", and if it isn't 0, it should add up 0.5 to par.
My issue now is that trying to use par in a timesRepeat brings up a "BoxedFloat64 did not understand #timesRepeat" error. So I am still in the same issue, or just need a way to make a float into an integer.
There are a lot of ways. For example
Add 1 to entered number before div by 2 if entered number is odd
temp := enteredNumber.
temp odd ifTrue: [temp := temp + 1 ].
^temp / 2
Using ceiling method
^(enteredNumber / 2) ceiling
In Smalltalk, we have an operator for integer division (and even two operators):
11 / 2
would answer a Fraction, not a whole Integer
But:
11 // 2
would answer the quotient of division, rounded toward negative infinity
And the corresponding remainder will be:
11 \\ 2
The second operator quo: for quotient and rem: for remainder
The difference is only with negative receiver/operand: the later ones are truncating the quotient toward zero.
-11 // 4 = -3. "floored toward negative infinity"
-11 \\ 4 = 1.
(-11 quo: 4) = -2. "truncated toward zero"
(-11 rem: 4) = -3.
If you want to round the quotient upper (toward positive infinity), then you can write:
(anInteger + 1) // 2.
Or same without parenthesis if you are confident enough in binary operator precedence:
anInteger + 1 // 2.

negative values in integer programming model

I'm new at using the glpk tool, and after writing a model for certain integer problem and running the solver (glpsol) i get negative values in some constraint that shouldn't be negative at all:
No.Row name Activity Lower bound Upper bound
8 act[1] 0 -0
9 act[2] -3 -0
10 act[2] -2 -0
That constraint is defined like this:
act{j in J}: sum{i in I} d[i,j] <= y[j]*m;
where the sets and variables used are like this:
param m, integer, > 0;
param n, integer, > 0;
set I := 1..m;
set J := 1..n;
var y{j in J}, binary;
As the upper bound is negative, i think the problem may be in the y[j]*m parte, of the right side of the inequality.. perhaps something with the multiplication of binarys? or that the j in that side of the constrait is undefined? i dont know...
i would be greatly grateful if someone can help me with this! :)
and excuse for my bad english
thanks in advance!
Sounds like you have an overflow problem. Which values of m and n produced the output shown?

Minimum number of states needed?

Definition of a language L with alphabet { a } is given as following
L = { ank | k > 0 ; and n is a positive integer constant }
What is the number of states needed in a DFA to recognize L?
In my opinion it should be k+1 but I am not sure.
The language L can be recognized by a DFA with n+1 states.
Observe that the length of any string in L is congruent to 0 mod n.
Label n of the states with integers 0, 1, 2, ... n-1, representing each possible remainder. An additional state, S, is the start state. S has a single transition, to state 1. If the machine is currently in state i, on input it moves to state (i+1) mod n. State 0 is
the only accepting state. (If the empty string were part of L, we could eliminate S and make state 0 the start state).
Suppose there were a DFA with fewer than n+1 states that still recognized L. Consider the sequence of states S0, S1, ... Sn encountered while processing the string an. Sn must be an accepting state, since an is in L. But since there are fewer than n+1 distinct states in this DFA, by the pigeonhole principle there must have been some state that was visited at least twice. Removing that loop gives another path (and another accepted string), with length < n, from S0 to Sn. But L contains no strings shorter than n, contradicting our assumption. Therefore no DFA with fewer than n+1 states recognizes L.

How to multiply matrix with its transpose using Oracle database and utl_nla

I'm going nuts with this issue. I can't get the result from the following multiplication:
X^t * X
X is an m * n matrix with m = 36 rows and n = 3 columns which is represented by an utl_nla_array_dbl datatype. The data origins from a table and gets copied by simple pl/sql code.
To solve my problem, I chose the method utl_nla.blas_gemm. It's a matrix-matrix method, in contrast to utl_nla.blas_gemv as a matrix-vector method (I got that one working. I was able to multiply that very matrix X by a vector y and received the right result).
Here is the relevant code, which outputs me a matrix with the right dimension (3X3) but just zeros in it. To make it clearer I hard coded most parameters:
utl_nla.blas_gemm(
transa => 'T',
transb => 'N',
m => 3,
n => 3,
k => 36,
alpha => 1.0,
a => X,
lda => 3,
b => X,
ldb => 3,
beta => 0.0,
c => XtX,
ldc => 3);
The variable XtX is also of type utl_nla_array_dbl and is to hold the result.
Any idea what I'm doing wrong? I'll appreciate every contribution since I'm totally stuck and can't find any help elsewhere on the web.
I had the same problem, and after a few days I'm sure , that the UTL_NLA.BLAS_GEMM procedure is broken.
It was broken in the 10.2g version, and still the same error occurs in version 11.2g.
The problem is in the in wrapper procedure written in PL/SQL.
It does not handle the parameters
M, N, K, LDA, LDB, LDC correctly,
in the case when one or both of the parameters TRANSA, TRANSB are set to 'T'.
Not surprisingly it is working, when the matrix is a sqare matrix,
for example the matrix A is 100x100 and the relevant parameter TRANSA = 'T'.
The procedure UTL_NLS.BLAS_GEMM mishandles the parameters in this case too,
but they are equal, so it has no effect.
The workaround I use is simple: before I call the procedure, I transpose the relevant matrix,
and I use BLAS_GEMM allways with the setting TRANSA = 'N' and TRANSB = 'N'.
Unfortunately there is no transpose procedure in the UTL_NLA package (btw. BLAS has one),
but to write one is not a big deal:
PROCEDURE MatTranspose (nRows IN NUMBER, /* number of rows in A */
nCols IN NUMBER, /* number of columns in A */
mat_A IN utl_nla_array_dbl, /* supposed it is stored column-wise i.e. 'C' */
mat_At IN OUT utl_nla_array_dbl) IS
/* the array can be larger then nRow * nCol, the rest part is not handled in either matrices */
nIii NUMBER;
nJjj NUMBER;
BEGIN
FOR nIii IN 1 .. nRows LOOP
FOR nJjj IN 1 .. nCols LOOP
mat_At (nJjj + nCols * (nIii - 1)) := mat_A (nIii + nRows * (nJjj - 1));
END LOOP;
END LOOP;
END MatTranspose;
For me the real pain was the documentation, e.g. e40758.pdf.
It is full of mistakes too, see for instance p. 232-26 and it misleads me, makes me think I pass the wrong parameters.
I spent couple of hours searching the web for a working example but - of course - in vain.
It is probably a simple error in the BLAS_GEMM procedure which takes half an our to fix,
and yet developers are waiting more then 6 years for a correct version.
After looking at the spec for UTL_NLA and reading the description of BLAS_GEMM, it looks to me like LDA and LDB should be 36. Try changing those and see if it helps.
Share and enjoy.