minimal number of d flip-flops required for first seven Fibonacci numbers - fibonacci

I encountered a problem while preparing for a test.
What is the minimal number of d flip-flops required (along) with combinational logic to design a counter circuit that outputs the first seven Fibonacci numbers and then wraps around?
A) 3
B) 4
C) 5
D) 6
E) 7
My answer B
Seven Fibonacci numbers => 1 1 2 3 5 8 13.
To count to 13, we will need to 4 flip-flops hence 4 was my choice.
But the correct answer given is solutions was A.
Could someone please explain?

Related

Is there a long method operation for modulo if mod or % is not a supported function/operator?

This is related to the Zeller's Congruence algorithm where there is a requirement to use Modulo to get the actual day of an input date. However, in the software I'm using which is Blueprism, there is no modulo operator/function that is available and I can't get the result I would hope to get.
In some coding language (Python, C#, Java), Zeller's congruence formula were provided because mod is available.
Would anyone know a long method of combine arithmetic operation to get the mod result?
From what I've read, mod is the remainder result from two numbers. But
181 mod 7 = 6 and 181 divided by 7 = 25.857.. the remainder result are different.
There are two answers to this.
If you have a floor() or int() operation available, then a % b is:
a - floor(a/b)*b
(revised to incorporate Andrzej Kaczor's comment, thanks!)
If you don't, then you can iterate, each time subtracting b from a until the remainder is less than b. At that point, the remainder is a % b.

Deterministic Finite Automata divisibility problem

Design a DFA that accepts the string given by L = { w has number of 'a' divisible by 3 and number of 'b' divisible by 2 over the alphabet {a,b} }
Realize that we should have 3 * 2 = 6 states in the DFA. Why? Because one has 3 choices for the number of a's (0 or 1 or 2) [think in terms of remainders] and 2 choices for number of b's (0 or 1 similarly).
Let us name the states axby which means I have found x number of a's and y number of b's till now. For example, if we are in a2b0 and we encounter an a, then we go to a0b0 (hope you see why?). Similarly a1b1 ---b---> a1b0 and a1b1 ---a---> a2b1.
Needless to say a0b0 is the accepting state.
Now, all you have to do is draw the states and keep joining them. I have drawn them on a paper here.

Update model : 3-way interaction terms not dropped

My question is highly related to this one:
R update() interaction term not dropped
However, I don't have multiple categories in my predictor variables, so I don't understand how my issue relates to the answer. Maybe I'm just not understanding it...
I'd like to remove the insignificant 3-way interaction terms in a model reduction process one at a time.
However, the following happens:
model1 <- lme(sum.leafmass ~ stand.td.Sept.2017*stand.wtd.Sept.2017*I((stand.td.Sept.2017)^2)*I((stand.wtd.Sept.2017)^2), random = ~1|block/fence, method="ML", data=subset(Total.CiPEHR, species=="EV"), na.action=na.omit)
model2 <- update(model1,.~.-stand.td.Sept.2017:stand.wtd.Sept.2017:I((stand.td.Sept.2017)^2):I((stand.wtd.Sept.2017)^2))
summary(model2) ##works correctly to eliminate insignificant 4-way interactions
summary(model2)
DF t-value p-value
(Intercept) 4 3.849259 0.0183
stand.td.Sept.2017 4 -1.436666 0.2242
stand.wtd.Sept.2017 4 -2.921806 0.0432
I((stand.td.Sept.2017)^2) 4 4.594303 0.0101
I((stand.wtd.Sept.2017)^2) 4 -0.313197 0.7698
stand.td.Sept.2017:stand.wtd.Sept.2017 4 -1.301935 0.2629
stand.td.Sept.2017:I((stand.td.Sept.2017)^2) 4 1.853451 0.1374
stand.wtd.Sept.2017:I((stand.td.Sept.2017)^2) 4 4.354757 0.0121
stand.td.Sept.2017:I((stand.wtd.Sept.2017)^2) 4 -0.028199 0.9789
stand.wtd.Sept.2017:I((stand.wtd.Sept.2017)^2) 4 1.598564 0.1852
I((stand.td.Sept.2017)^2):I((stand.wtd.Sept.2017)^2) 4 -1.683214 0.1676
stand.td.Sept.2017:stand.wtd.Sept.2017:I((stand.td.Sept.2017)^2) 4 1.972616 0.1198
stand.td.Sept.2017:stand.wtd.Sept.2017:I((stand.wtd.Sept.2017)^2) 4 -1.635314 0.1773
stand.td.Sept.2017:I((stand.td.Sept.2017)^2):I((stand.wtd.Sept.2017)^2) 4 2.190518 0.0936
stand.wtd.Sept.2017:I((stand.td.Sept.2017)^2):I((stand.wtd.Sept.2017)^2) 4 -0.968249 0.3877
##attempt to remove insignificant 3-way interaction
model3 <- update(model2,.~.,-stand.wtd.Sept.2017:I((stand.td.Sept.2017)^2):I((stand.wtd.Sept.2017)^2))
summary(model3)
DF t-value p-value
(Intercept) 4 3.849259 0.0183
stand.td.Sept.2017 4 -1.436666 0.2242
stand.wtd.Sept.2017 4 -2.921806 0.0432
I((stand.td.Sept.2017)^2) 4 4.594303 0.0101
I((stand.wtd.Sept.2017)^2) 4 -0.313197 0.7698
stand.td.Sept.2017:stand.wtd.Sept.2017 4 -1.301935 0.2629
stand.td.Sept.2017:I((stand.td.Sept.2017)^2) 4 1.853451 0.1374
stand.wtd.Sept.2017:I((stand.td.Sept.2017)^2) 4 4.354757 0.0121
stand.td.Sept.2017:I((stand.wtd.Sept.2017)^2) 4 -0.028199 0.9789
stand.wtd.Sept.2017:I((stand.wtd.Sept.2017)^2) 4 1.598564 0.1852
I((stand.td.Sept.2017)^2):I((stand.wtd.Sept.2017)^2) 4 -1.683214 0.1676
stand.td.Sept.2017:stand.wtd.Sept.2017:I((stand.td.Sept.2017)^2) 4 1.972616 0.1198
stand.td.Sept.2017:stand.wtd.Sept.2017:I((stand.wtd.Sept.2017)^2) 4 -1.635314 0.1773
stand.td.Sept.2017:I((stand.td.Sept.2017)^2):I((stand.wtd.Sept.2017)^2) 4 2.190518 0.0936
stand.wtd.Sept.2017:I((stand.td.Sept.2017)^2):I((stand.wtd.Sept.2017)^2) 4 -0.968249 0.3877
##3-way interaction term still there.
Why won't the interaction term drop? The predictor variables are continuous and so should be independent from each other, right..?
Someone please explain if I'm not understanding something basic here...
Solved my own question.
Dummy syntax error. (had an incorrect comma in the .~. portion)
###Incorrect syntax.
model3 <- update(model2,.~.,-stand.wtd.Sept.2017:I((stand.td.Sept.2017)^2):I((stand.wtd.Sept.2017)^2))
###Correct syntax.
model3 <- update(model2,.~.-stand.wtd.Sept.2017:I((stand.td.Sept.2017)^2):I((stand.wtd.Sept.2017)^2))

Desgin DFA that will accept strings having a's and b's divisible by 3?

DFA - DETERMINISTIC FINITE AUTOMATA
for eg-abaabbbbb
number of a's - 3
number of b's - 6
which are divisible by 3
If any string contain a,b & it is divisible by 3 than it has 3 states i.e. q0,q1,q2.
One is having remainder 0
Second is having remainder 1
Third is having remainder2

Greedy algorithm to finish a task with time constraint

This is a question from my midterm today and I wonder how to solve this. All i know is to prove the greedy algorithm using induction.
Question:
You are working on a programming project. There are n Java classes C1, C2, ..., Cn (the bossy architect says so). The architect also says that these classes have to be implemented in order (you are not allowed to implement C2 before you have completed C1 and so on).
Each of the Java classes takes at most 8 hours to implement. You work exactly 8 hours a day, and you should not leave a Java class unfinished at the end of the day.
To complete the project as soon as possible, a strategy is to implement as many classes as you can everyday. Prove that this greedy strategy is indeed the optimal one.
(Hint: let ti be the total number of classes completed in the first i days using the above strategy. The strategy always stays ahead if ti is no less than the total number of classes completed in the first i days using any other strategy)
This problem is similar to the classic task scheduling case where the waiting time in the system must be minimized.
Let C1, C2, ..., Cn your projected classes and c[1], c[2], ..., c[n] their required implementation time. Let's suppose you implement C1, C2, ... Cn in this order. Therefore, the total time (waiting + implementation) for each class Ck will be:
c[1] + c[2] + ... + c[k]
Therefore, we have the total time:
T = n·c[1] + (n – 1)·c[2] + ... + 2*c*[n – 1] + c[n] = sum(k = 1 to n) of (n – k + 1)·c[k]
(Sorry about the presentation — superscripts, subscripts, and math equations aren't supported...)
Let's suppose the implementation times in our permutation are not sorted by ascending order. We can therefore find two integers a and b such that a < b with c[a] > c[b]. If we switch them in the computation of T, we have:
T' = (n – a + 1)·c[b] + (n – b + 1)·c[a] + sum(k = 1 to n except a, b) of (n – k + 1)·c[k]
We finally compute T – T':
T – T' = (n – a + 1)(c[a] – c[b]) + (n – b + 1)(c[b] – c[a]) = (b – a)(c[a] – c[b])
Following our initial hypothesis (a < b and c[a] > c[b]), we have b – a > 0 and c[a] – c[b] > 0 as well, hence T – T' > 0.
This proves that we decrease the total waiting time by switching any pair of tasks so that the shorter one is done first.
Your problem statement is the same, except that before starting implementing a new class, you have to check whether you should start it now (if there is enough time left on the present day) or tomorrow. But the principle proven here holds when it comes to minimizing the total "waiting" time.
This is not a programming question for SO. The problem is not asking for a coding solution, rather its a proof that greedy is optimal. Which can be done with a proof by contradiction (no doubt taught in the class before the midterm).
What you want to do is to calculate the total time taken by greedy (there's only one solution) and disprove that any swaps in day would lead to a better solution. You probably also have to add something that mentions how swapping will allow u to permute the order to the optimal solution, if it exists.
I was going to write some formulae, but i realize Jeff Morin already has the equations, just going in the opposite direction. I think starting from the greedy solution might be easier to explain, since 'in order' is pretty much defined by the problem and you can only shift the work +- which day its done on.
The problem statement is incomplete. There is no indication that any class will take less than 8 hours. Since you can't leave any class unfinished, then you must start each class at the start of the day to be sure to have at least 8 hours to work on it. So if C2 really takes 3 hours and C3 really takes 5 hours, then a greedy algorithm would allow both classes to be done the same day. But after C2 takes 3 hours, you have to wait to day 3 to start C3 to be sure that you have enough time to finish since you don't know how long C3 will take.
So the restrictions really end up dictating that the effort will take n days, 1 day per class. So the implementation algorithm is strictly sequential, not greedy.
Edit Restrictions stated in problem.
(1) There are n Java classes C1, C2, ..., Cn
(2) these classes have to be implemented in order (you are not allowed to implement C2 before you have completed C1 and so on).
(3) Each of the Java classes takes at most 8 hours to implement
But there is no estimate for any class taking less than 8 hours.
(4) You work exactly 8 hours a day
(5) You should not leave a Java class unfinished at the end of the day.
The gist of this (3,4,& 5) is let's assume that I work on class 1 for 5 minutes. I now have 7 hours 55 minutes left. Can I start on Class 2? No because it might take up to 8 hours and I must finish before the end of my 8-hour day. So I must wait to day 2 to start class 2 and so on. Thus the implementation is strictly sequential and will take n days to complete, 1 day per class.
In order to use the Greedy algorithm you'd need additional information.
(6) You also know that each class has a known number of hours needed to code the class - h1, h2, h3, ..., hn. So class 1 takes h1 hours, class 2 takes h2 hours and so on. (From item 3 no class takes more than 8 hours)

Resources