How to check for a condition that is never met? - logic

Taking an example to clarify, say I have a program that takes in two inputs from the user, a and b. The program increments a and decrements b. The program returns the value at which a and b both become equal. Hence, a has to be smaller and b has to be larger. But what if the user enters the opposite? a as larger and b as smaller? The program would go into an infinite loop obviously. But suppose I want the program to return that "these two numbers can never meet". Then how do I check for it? Please don't answer that check for the numbers and respond respectively. This is just an example. I want to know how to check for a condition that can never be met.
Another example, say comparing two numbers. Suppose I have two numbers and I keep randomising them. The program should return true when they are equal and false when they are not. It should not go into an infinite loop when the numbers are not equal. I can only keep comparing the numbers in each iteration and return true as soon as they are equal. But there are likely chances that they never become equal and the program never terminates. How to check for such a scenario and return something like the numbers can never be equal.

It has to be done on a case-by-case basis; the general problem (given an iterative procedure controlled by a variable condition, determine whether the condition will ever assume a given value, e.g. True) is equivalent to the halting problem, which is uncomputable...

A simple if statement, like this?
if (a <= b)
throw "Condition cannot be met"
else
for ( ; a != b; a--, b++) {
// Do something
}
Example in c++
EDIT I got A and B the wrong way round! Same scenario though.

Related

Loop Invariants with Breaks

I am trying to understand how loop invariants interact with breaks. CLRS 3e (pg19) describes a loop invariant as requiring that
If it is true before an iteration of the loop, it remains true before the next iteration.
So given the following trivial loop
for i = 1 to 5
if i == 3 then break
Would it be fair to say that i < 4 is an invariant property of the loop? The argument being that, since the loop terminates at the break statement, there is no iteration after that property is violated.
Yes, that would be an invariant, for precisely the reason you’ve mentioned. Whether it’s a useful invariant is a separate question that depends on what you’re trying to prove.

Swift: The smallest excutable for-loop

If lowerBound is greater than upperBound your code will crash
Why does this run (nothing will be outputted, but still runs, also, floating point can't be inputted so "<1" would seeming be "0"))...
for i in 1..<1 {}
But this doesn't...
for i in 1...0 {}
?
The former expresses "starting at 1 and incrementing by one, if the value is less than one, include it." That is a set with no values in it, but it's a reasonable thing to say.
The later expresses "starting at 1 and incrementing by one, until the value equals 0, include it." That is really a set of all positive integers, but in practice you definitely didn't mean that, and it is is instead explicitly defined to be an error.
Another way to say the same thing is to consider the former to be all integers greater than or equal to 1 and also less than 1. Again, that's an empty set.
The latter is a set of all values greater than or equal to 1 and less than or equal to 0. That's also an empty set, but almost certainly not what you meant, so it's defined to be an error.
The error message tells you the answer:
Can't form Range with upperBound < lowerBound
That is the only rule that matters.
How the range gets formed is irrelevant to that rule. It doesn't matter whether the operator that forms the range is ... or ..<. All that matters is that when we try to obey the operator and instantiate the range, it must turn out that the upper bound is not smaller than the lower bound.
Well, in 1..<1, the upper bound is not smaller than the lower bound. So it's a legal range.
It is also an "empty" range; it contains no integer (neither 0, nor 1, nor 2, nor any other integer). But it's still a range.
Now, if you think about it, that's a very valuable thing. It doesn't look valuable in your example, because you're using literals. But when the lower bound and upper bound come from variables, it's a very good thing that lo..<hi doesn't crash in the corner case where lo and hi happen to be equal! That case arises a lot, and for good reasons.
For example, consider cycling thru the elements of an array. If the array is empty, its indices are (you guessed it) 0..<0. You want it to be legal to cycle thru this array. Nothing happens, but it's not illegal. And that's just what this rule says.

Recursive language

"if a language is recursive, then there exists a method by which the strings in language can be written in some sequence"
I am also told that "if a language can be enumerated in a lexicographic order by some Turing machine, then such a language is called recursive"
First : are the two statements are different?
Second : should it be a lexicographic order only?
Think back to the reason why lexicographical order is necessary for the definition of a recursive language:
A language is recursive if it can be decided. That is to say, for a given word W and a given language L, it is possible to know whether W is a member of L, or not in finitely many steps.
A language is recursively enumerable if it can be accepted. That is to say, for a given word W and a given language L, it is possible to know whether W is a member of L in finitely many steps, but it is not possible to know whether W is not a member of L.
So if a machine just enumerated the words of L in any order, you can check to see if your word W is in that list. If it is, you stop. If it isn't, you have to wait forever to see if your word is eventually output by the machine. The language is recursively enumerable.
If you knew the order, though, you could evaluate whether the machine should have output W by now. If the machine has output a word X, and according to the ordering you know the machine is using, W is before X, you know that the machine will not ever emit W, so you know that W is not a member of L.
Lexicographical order is one of many total orderings of words that satisfy the property that you can tell when your word W should have been output, so if you don't see it by then, you can stop.
Other orders:
https://en.wikipedia.org/wiki/Lexicographical_order#Colexicographic_order
https://en.wikipedia.org/wiki/Kleene%E2%80%93Brouwer_order
So to answer your specific questions:
Are the two statements different?
Yes.
The first statement states "in some sequence", which does not specify that the sequence must be a total order over L's alphabet. Therefore the first statement is incorrect. The first statement defines a recursively enumerable language.
The second statement is correct, but is more restrictive than it needs to be. Lexicographical order is only one total order over an alphabet. Others can be used.
Should it be a lexicographical order only?
No.
As above, as long as the machine guarantees output in any total order over the alphabet, the language is recursive.

Determinate State of Linear Congruential Generator from Results

I am curious on how someone would go about determining the state of a Linear Congruential Generator given its output.
X(n-1) = (aX(n) + c) mod p
Since the values returned are deterministic and the formula is well known, it should be possible to obtain state's value. What exactly is the best way to do this?
Edit:
I was at work when I posted this and this isn't work related, so I didn't spend much time and should have elaborated (much) further.
Assume this is used to generate non-integer values between 0 and 1, but its only visible output is true or false with a 50/50 spread. Assume the implementation is also known, so the values of a, c and p are known, but not X.
Would it be possible, with an finite amount of output, to determine the value of X?
Well, in the simplest case, the output IS the state -- the output is the sequence X0, X1, X2, ... each element of which is the internal state at one step.
More commonly, the LCRNG will be divided to generate uniform numbers in the range [0,k) rather than [0,p) (the values output will be floor(kXn/p),) so will only tell you the upper bits of the internal state. In this case, each output value will give you a range of possible value for the state at that step. By correlating multiple consecutive values, you can narrow down the range.

What is determining the items that make the difference between two arrays called?

I want to find which elements of two arrays make the two arrays different.
For example, if I start off with
known_unacceptable_array = [bad, bad, good, good, good, bad, good]
known_acceptable_array = []
and an array is only unacceptable if there's three bads (but I don't know that at the time), but I'm able to evaluate whether an array is acceptable or unacceptable, I would like to find the smallest array that makes the array unacceptable
possibly_minimal_unacceptable = [bad, bad, bad]
maximal_acceptable = [bad, bad] # Third bad required to make the array unacceptable
What is this problem called, and what algorithms are there for this?
Edit: The elements can't be changed in order, and adding an element can only either change the list from being acceptable to unacceptable or have no effect - it can't change it from being unacceptable to acceptable.
Background: I've randomly generated thousands of instructions that make a ruby interpreter crash, and I want to isolate the specific instructions that cause it to crash, and at the time I thought that multiple bad instructions were required to make it crash. A very naive attempt to determine what the bad instructions is at this link
What is determining the elements that make the difference
between two arrays called?
Differencing is often called
subtraction.
I want to determine which elements of two arrays make the
two arrays different.
Again, that's subtraction(at least
some form of it):
Given A ={ x , y , z } B = { x , y a },
A - B = { z , -a }
or "only A has z and only B has a", or "z and a" make them
different.
For example, if I start off with
known_bad = [bad, bad, good, good, good, bad, good] >
known_good = []
Why start with a full array and an empty one? Isn't this an
extreme case, or are these "two arrays" not two of which you
are trying to determine the "difference."
possibly_minimal_bad = [bad, bad, bad]
maximal_good = [bad, bad] # Third bad required to make the list bad
Is this just a set of rules? Or is this the result of
finding the difference between the two arrays of the previous
(known_good,bad) set?
What is this problem called, and what algorithms are there
for this?
If it isn't called "difference" or "subtraction" then why
introduce it that way?
Is the problem: a. going from
the first two arrays (known_xx) to the second two (min,max);
or is it: b. classifying finite sequences of the words "good"
and "bad."
a) I can't see a relation between the first two
arrays and the second two. How did you get from the first two
to the second?
b) Classifying a sequence of words could be
"parsing a language", or decoding a message, recognizing a
pattern, etc.
Is it "Pattern Recognition"?
It appears that you are looking for a pattern in test input(or test point) data and it's relationship to product failure,
and want to represent the relationship in some codical
form for further analysis. Or searching for a correlation between certain test points and product failure. That makes this question rather
interesting. However, the presentation of the question
is quite confusing. Maybe those groups of
equations could be explained a little more, clarifying if they are related,and if so, then: In what way?
I'm not entirely sure if I understand the question. If my answer is unsatisfactory, please rephrase your question to be more clear. I'll base my answer on this.
I want to determine which elements of two arrays make the two arrays different.
This is a combination of the three set operations union, intersection and difference. Different combinations can achieve the same result.
Complement is the the subset of A which is not in B.
Intersection is the set of elements which is both in A and B, but not just A or B.
Union is the subset which is either in A or B (no duplicates).
It sounds like you want the union of both complements, which is:
A\B ∪ B\A
Or the complement between the intersection and the union:
A∩B \ A∪B
See http://en.wikipedia.org/wiki/Set_operations_(Boolean) for more information.

Resources