Build xy+xz+yz using NAND port - logical-operators

for an homework I have to write xy+xz+yz using only NANDS port.
I will use the notation NAND(x,y) - or other types of bracket to make things clearer -, below my attempt and then an explanation for every step. I'd like to know if i'm doing this right and if there are better ways to do it.
My Solution
NAND[NAND(NAND(NAND(x,y),NAND(x,z)),NAND(NAND(x,y),NAND(x,z))),NAND(NAND(NAND(y,z),NAND(y,z)),NAND(NAND(y,z),NAND(y,z))))]`
I know this looks really impossible to read and keep track of. I'm sorry, didn't know how to make this more beautiful. Hope my explanation will clarify things.
I divided xy+xz+yz in two groups: xy+xz and yz
First Group:
xy+xz = NAND(NAND(x,y),NAND(xz)) = NOT[NOT(xy)*NOT(xz)] = xy+xz
Second Group:
yz = NAND(NAND(y,z),NAND(y,z)) = NOT(NOT(yz)*NOT(yz)) = yz (since yz+yz = yz)
Now I have to combine the first group with the second, for readibility I'll call the first group (in NAND as g1) and the second g2;
g1+g2= NAND[NAND(g1,g1),NAND(g2,g2)] = NOT[NOT(g1)*NOT(g2)] = g1+g2
So at the end:
xy+xz+yz= NAND[NAND(NAND(NAND(x,y),NAND(x,z)),NAND(NAND(x,y),NAND(x,z))),NAND(NAND(NAND(y,z),NAND(y,z)),NAND(NAND(y,z),NAND(y,z))))]
Is my reasoning right? There's a more easy way?
Thanks a lot guys

Your answer is correct (although you have some missing punctuation -- a couple commas and a parenthesis). You can confirm by generating a truth table of all possible outputs as so. I wrote a few lines of C code to confirm. As for your second question to whether there is an easier way, I don't know. Maybe someone else can help out.
x y z xy+xz+yz nands
------------------------------
0 0 0 0 0
0 0 1 0 0
0 1 0 0 0
0 1 1 1 1
1 0 0 0 0
1 0 1 1 1
1 1 0 1 1
1 1 1 1 1

Related

Explanation of 1 mod 3

So i've been looking into modulo recently. I'm trying to improve my math skills, which are not the best if i'm honest. But something i am trying to improve. I understand how this works i think. I am also quite competent with long division. However something is bugging me and i can't seem to find an answer for it online.
I know that 7 % 5 = 2 (5 goes into 7 once, with a remainder of 2).
What i don't understand is this;
1 % 3 = 1
How can this be, 3 goes into 1, 0 times, with a remainder of 3? Surely the answer to 1 % 3 = 3?
Can anyone explain this in its most simplest terms please?
Am i correct in thinking that if the dividend (1) is less than the devisor (3) which we know will equal 0 remainder x, it just uses the dividend as the result?
Thanks for your help.
The remainder in 1%3 refers to what remains of 1 (not 3) after you divide by 3. As you have already said, 3 goes into 1 zero times. So -- when you remove 0 multiples of 3 from 1, all of 1 remains. Thus 1 % 3 = 1.
The result of a modulo operation n % m is just that number r for which q * m + r = n (q may be anything). The only requirement we have is that 0 <= r < m.
So for instance:
7 % 5 --> 1 * 5 + 2 == 7 --> r = 2
1 % 3 --> 0 * 3 + 1 == 1 --> r = 1

Why does recursive fibonacci work?

I was looking at the following code:
function _fibonacci(n) {
if (n < 2){
return 1;
}else{
return _fibonacci(n-2) + _fibonacci(n-1);
}
}
console.log(_fibonacci(5))
I understand HOW this works, but I do not understand WHY this works. Can someone explain to me why this works?
It's quite simple, the fibonacci answer for both location 0 and 1 are both 1 (the sequence looks like 1 1 2 3 5 8 etc...) so when it enters the function with n being 0 or 1 (which can happen for both the n-2 recursive call and the n-1 recursive call), the result is 1. For all other values it just keeps adding the numbers.
(Note that the values for the first 2 in the sequence can be 0 1 or 1 1, depending on your definition of the sequence. For this one it's apparently assumed the first 2 are both 1.)

Counting frequency of runs of consecutive 1s in an array

I have defined an array of arr=[0 0 1 1 1 0 0 0 0 1 1 0 1 1 1].
I would like to count the continuous repetition and store in another array.
For example above, the first repetition is 3 of 1s. So I assume the length of this continuous repetition is 3.
Therefore, the the array should be like arr[length]=1 . The 1 stands for it has been encountered once. The final output should be
arr[3]=2 //it means length of repetition with 3 of 1s has been encountered twice
arr[2]=1 //length of repetition with 2 has been encountered once.
Below is my code progress so far.
err=0;
no_err=0;
flag=0;
arr=[0 0 1 1 1 0 0 0 0 1 1 0 1 1 1] //assume 15
for x=1:15;
val=arr(x);//access array values
if(val==0)//if no error
flag=0; //indicates no error
elseif(val==1) //if there is an error
flag=1; //indicates error
end
if(flag==0)
no_err=no_err+1;//counter of no error
err=0; //reset error to zero
elseif(flag==1)//
err=err+1;//
tmperr=err;//will keep updating with latest err count length
end
end
Some of the logic is reasonable, but this part is strange:
if(flag==0)
....
elseif(flag==1)
This could be simply else. More importantly, the state of flag could have been changed by the earlier part of the loop, which will render the logic of the second if statement invalid.
I suggest considering four scenarios, focusing on counting only repetitions of 1s:
flag is 0 and value is 0: do nothing (no need to have a clause for this)
flag is 1 and value is 1: increment the count of consecutive 1s
flag is 0 and value is 1: set the flag to 1, set the count of consecutive 1s to 1.
flag is 1 and value is 0: set the flag to 0, record the count of 1s in the output array.
Since the second bullet item does not change the state of flag, it can be dealt with first; the two others are considered within if/elseif construction.
Also, the end of array should be ending the current run of 1s. Instead of writing a separate clause for this, it's easier to pad the given array with 0.
err=0
flag=0
arr=[0 0 1 1 1 0 0 0 0 1 1 0 1 1 1]
output = zeros(arr)
paddedarr = [arr 0]
for x=1:length(paddedarr)
val=paddedarr(x)
if (val==1 & flag==1)
err=err+1
end
if (val==1 & flag==0)
flag = 1
err = 1
elseif (val==0 & flag==1)
output(err) = output(err)+1
flag = 0
err = 0
end
end
Output:
0. 1. 2. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
Note that I used length command instead of assuming the length of array.

how do i check even or odd in postscript

I'm trying to check if the number on the top of the stack is even or odd without affecting my stack because I'm using it in a recursive function. I also tried to double pop but this code still messes up the function.
dup 2 0 mod eq
{ 0 0 0 setrgpcolor}
{0.6 0.6 0.6 setrgpcolor}ifelse
pop
You got the order of the arguments wrong. One step at a time (assuming the top stack number is 123):
123
dup 2 0 mod eq
do dup:
123 123 2 0 mod eq
do mod on 2 0 (!)
123 123 --undefined-- eq
and the eq is too little, too late...
Swap the 2 operands to get the right result:
dup 2 mod 0 eq
{ .. number is even ..}
{ .. number is odd ..} ifelse
(Note:
Is setrgpcolor a typo, or is there really a color model named so?)

Is it possible to convert a MathProg MIP file to a format recognised by SCIP?

I've been using GLPK to solve some mixed integer programming problems. Here's a sample input file in MathProg format:
set REACTIONS;
set REACTANTS;
param Ys {i in REACTANTS, j in REACTIONS};
param Gamma {i in REACTANTS, j in REACTIONS};
param eps;
param delt;
var w {i in REACTANTS} >=-delt <=delt;
var R0 {i in REACTIONS} >=0 <=1, integer;
var Rn {i in REACTIONS} >=0 <=1, integer;
minimize z: sum{i in REACTIONS} -Rn[i];
s.t. const1{i in REACTIONS} : sum{k in REACTANTS} w[k]*Gamma[k,i] <= delt*(1-R0[i]);
s.t. const2{i in REACTIONS} : -sum{k in REACTANTS} w[k]*Gamma[k,i] <= delt*(1-R0[i]);
s.t. const3{i in REACTIONS} : Rn[i] <= 1-R0[i];
s.t. const5{i in REACTIONS} : sum{k in REACTANTS} w[k]*Gamma[k,i] <= delt*(1-Rn[i])-eps;
s.t. const6{i in REACTIONS, j in REACTIONS: i <> j} : sum{k in REACTANTS} w[k]*(Ys[k,i]-Ys[k,j]) <= delt*(1-Rn[i]+Rn[j]+R0[j]);
data;
set REACTIONS:= 1 2 3 4 5 6;
set REACTANTS:= 1 2 3 4 5 6;
param Ys: 1 2 3 4 5 6:=
1 1 0 0 0 0 0
2 1 0 0 0 0 0
3 0 1 1 0 0 0
4 0 0 0 1 0 0
5 0 0 0 1 0 0
6 0 0 0 0 1 1;
param Gamma: 1 2 3 4 5 6:=
1 -1 1 0 0 0 1
2 -1 1 1 0 0 0
3 1 -1 -1 0 0 0
4 0 0 1 -1 1 0
5 0 0 0 -1 1 1
6 0 0 0 1 -1 -1;
param eps:=0.1;
param delt:=10;
end;
I've been running into performance problems for bigger problems of this type, and since SCIP claims to be several times faster than GLPK for MIP, it seems worth investigating. However, I haven't been able to make head or tail of the documentation when it comes to input file formats. SCIP's homepage says that it supports AMPL format, and the GLPK's homepagesays that MathProg is a subset of AMPL. Trying to feed the above file into SCIP 3.1.0 via scip -f file.nl returns the following error:
read problem <file.nl>
============
no reader for input file <file.nl> available
I'm not sure whether this is because I've failed to build SCIP with AMPL support, or something else. I found this blog post on building SCIP with AMPL support, but the instructions seem to be outdated as the source zip of SCIP 3.1.0 doesn't contain an interfaces folder.
So, I have two questions:
Is it possible to get SCIP to recognise my MathProg input as is?
If not, can anyone advise on how to convert it to a recognised format? An automated method would be preferable, as I don't really want to have to learn yet another format, but a manual method would be better than nothing.
Thanks for any help and apologies for my ignorance!
As I indicated in my comment above, the Ampl-interface is still included in the SCIP-distribution, and you should be able to compile it and read your problem
as documented in the excellent blog post you cite.
If you feel tempted to try different file formats, I see two options for you:
use glpk for translating your problem into a file format that is recognizable by SCIP. I found methods glp_write_mps() and glp_write_lp. SCIP can read both .lp and .mps-files. Make sure that you use exactly these file extensions, because SCIP doesn't recognize files in .lp-format but ending with .txt instead.
Use Zimpl to formulate your problems instead. The two formats of Zimpl and Ampl are strikingly similar, see the documentation for examples and further reference. Problem descriptions in Zimpl can be translated into .lp-format or read directly by SCIP, if you compile SCIP with the ZIMPL=true-option, which is the default.

Resources