How to set different constraints or test cases in Specman e language - specman

I want to use different constraints for din1 and din2. For example, it will execute din1<10 and din2<1000 first 10 times and then execute din1<5 and din2<10 for 10 times.
How can I do this?

this is one simple way to constrain list of structs:
keep insts.size() == 20;
keep for each in insts {
index < 10 => (it.din1 < 10 and it.din2 < 1000);
index >= 10 => (it.din1 < 5 and it.din2 < 10);
};

Related

Count number of repeats in Swift

I want to know how am I supposed to count the number of time a loop has repeated itself? More specifically how do I extract and output the number of repeats?
var x = 20
while x < 100 {
x += 10
}
The loop has executed 8 times in order to get x == 100. Is there a way to extract the number '8' so it can be used somewhere else (e.g. to make it a variable elsewhere)?
You said it yourself: you want to count. So count!
var x = 20
var numtimes = 0
while x < 100 {
x += 10
numtimes += 1 // count!
}
numtimes // 8

speed up prime number generating

I have written a program that generates prime numbers . It works well but I want to speed it up as it takes quite a while for generating the all the prime numbers till 10000
var list = [2,3]
var limitation = 10000
var flag = true
var tmp = 0
for (var count = 4 ; count <= limitation ; count += 1 ){
while(flag && tmp <= list.count - 1){
if (count % list[tmp] == 0){
flag = false
}else if ( count % list[tmp] != 0 && tmp != list.count - 1 ){
tmp += 1
}else if ( count % list[tmp] != 0 && tmp == list.count - 1 ){
list.append(count)
}
}
flag = true
tmp = 0
}
print(list)
Two simple improvements that will make it fast up through 100,000 and maybe 1,000,000.
All primes except 2 are odd
Start the loop at 5 and increment by 2 each time. This isn't going to speed it up a lot because you are finding the counter example on the first try, but it's still a very typical improvement.
Only search through the square root of the value you are testing
The square root is the point at which a you half the factor space, i.e. any factor less than the square root is paired with a factor above the square root, so you only have to check above or below it. There are far fewer numbers below the square root, so you should check the only the values less than or equal to the square root.
Take 10,000 for example. The square root is 100. For this you only have to look at values less than the square root, which in terms of primes is roughly 25 values instead of over 1000 checks for all primes less than 10,000.
Doing it even faster
Try another method altogether, like a sieve. These methods are much faster but have a higher memory overhead.
In addition to what Nick already explained, you can also easily take advantage of the following property: all primes greater than 3 are congruent to 1 or -1 mod 6.
Because you've already included 2 and 3 in your initial list, you can therefore start with count = 6, test count - 1 and count + 1 and increment by 6 each time.
Below is my first attempt ever at Swift, so pardon the syntax which is probably far from optimal.
var list = [2,3]
var limitation = 10000
var flag = true
var tmp = 0
var max = 0
for(var count = 6 ; count <= limitation ; count += 6) {
for(var d = -1; d <= 1; d += 2) {
max = Int(floor(sqrt(Double(count + d))))
for(flag = true, tmp = 0; flag && list[tmp] <= max; tmp++) {
if((count + d) % list[tmp] == 0) {
flag = false
}
}
if(flag) {
list.append(count + d)
}
}
}
print(list)
I've tested the above code on iswift.org/playground with limitation = 10,000, 100,000 and 1,000,000.

Scala for loop value example [duplicate]

This question already has answers here:
Get list of elements that are divisible by 3 or 5 from 1 - 1000
(6 answers)
Closed 7 years ago.
How to do it this problem in Scala? Do it in For-loop.
sum of all the multiples of 3 and 5 below 1000;
Example: 1*3+2*5+3*3+4*5+5*3+6*5 ... so on 999*3+1000*5 = How much?
I don't think that 1000*5 is a multiple of 5 below 1000. 1000*5 is 5000 which is not below 1000.
It seems like what you want is:
(1 to 1000).filter(x => x % 3 = 0 || x % 5 == 0).sum
Which doesn't use a "for-loop". A lot of people would cringe at such a term, scala doesn't really have for-loops. if MUST use the for construct, perhaps you would write
(for (x <- 1 to 1000 if x % 3 == 0 || x % 5 == 0) yield x).sum
which is exactly the same thing as above.
you could also (though I would not recommend it) use mutation:
var s = 0
for { x <- 1 to 1000 } { if(x % 3 == 0 || x % 5 == 0) s += x }
s
which could also be
var s = 0
for { x <- 1 to 1000 if (x % 3 == 0 || x % 5 == 0) } { s += x }
s
If you want to use the principles of functional programming you would do it recursive - better you can use tail recursion (sorry that the example is not that good but it's pretty late).
def calc(factorB:Int):Int = {
if(factorB+1 >= 1000)
3*factorB+5*(factorB+1)
else
3*factorB+5*(factorB+1)+calc(factorB+2)
}
In a for-loop you can do it like
var result = 0
for(i <- 1 to 1000){
result += i*(i%2==0?5:3)
}
After the for-loop result yields the calculated value. The downside is that you're using a var instead of val. Iam not sure if the statement i%2==0?5:3 is valid in scala but I don't see any reasons why it shouldn't.

Compare the time duration of two movies

I am going to make a function that takes starting timing of two movies: hr1,hr2,min1,min2, and their durations, durmin1,durmin2 and decides whether we can binge and watch both movies.
The criteria are that they must not overlap and that we are not going to wait more than 30 minutes between the end of one and the beginning of the next. It returns true if the criteria are both met and returns false otherwise. Movie start times are always after 1 pm and before midnight. The first one always starts earlier. The order of the input arguments is: hr1, min1, durmin1, hr2, min2, durmin2
I am unable to understand what will my function will do. What are these timing hr1,hr2? Why duration has been given?
I have tried this:
function mymovies=movies(hr1,min1,dur1,hr2,min2)
h1=hr1+min/60+dur1;
h2=hr2+min/60;
if h2-h1>=30/60 && h2-h1~=0
disp('Ture')
else
disp('False')
end
end
well, with proper variables and a little thinking just one if is sufficient to solve this problem.As far as I have understood, you definitely do not need to consider the duration for the second movie. you only need to worry about the details of the first movie and the start time of the second movie. You also need to "return" the boolean result not display it. So here is what you should do, given the instructions in your question:
First: Convert the total time from when movie one starts till it ends to minutes
Second: convert the total start time of movie two to minutes
Finally, just use the difference to meet the conditions given in the instructions in a simpe if statement. Try that with your grader then let me know what happens then. (preferably of movie2 - movie1) since you are free to assume that movie1 will always start first)
Given your level, this should suffice.
-step 1: convert hr, min to only minutes (past 1pm if you want...)
so:
start_movie_1 = hr1*60 + min1
end_movie_1 = start_movie_1 + durmin1
Similar for movie 2.
-step 2: find if they overlap.
if start_movie_1 < start_movie_2 and end_movie_1 > end_movie_2 => there is overlapping (whole movie 2 is inside movie 1)
if start_movie_1 < start_movie_2 and end_movie_1 > start_movie_2 => there is overlapping (movie 2 will start before movie 1 finish)
if start_movie_2 < start_movie_1 and end_movie_2 > end_movie_1 => there is overlapping (whole movie 1 is inside movie 2)
if start_movie_2 < start_movie_1 and end_movie_2 > start_movie_1 => there is overlapping (movie 1 will start before movie 2 finish)
-step 3: now we know they don't overlap, so we need to check the time inbetween
if start_movie_1 < start_movie_2 => return (start_movie_2 - end_movie_1) <= 30
else (start_movie_2 < start_movie_1)
return (start_movie_1 - end_movie_2) <= 30
Edited for an even more simple answer
function mymovies=movies(hr1,min1,dur1,hr2,min2,dur2)
start_movie_1 = hr1*60 + min1;
end_movie_1 = start_movie_1 + dur1;
start_movie_2 = hr2*60 + min2;
end_movie_2 = start_movie_2 + dur2;
if start_movie_1 < start_movie_2 && end_movie_1 > end_movie_2
disp('FALSE');
else if start_movie_1 < start_movie_2 && end_movie_1 > start_movie_2
disp('FALSE');
else if start_movie_2 < start_movie_1 && end_movie_2 > end_movie_1
disp('FALSE');
else if start_movie_2 < start_movie_1 && end_movie_2 > start_movie_1
disp('FALSE');
else
if start_movie_1 < start_movie_2 && (start_movie_2 - end_movie_1) <= 30
disp('TRUE');
else if (start_movie_2 < start_movie_1) && (start_movie_1 - end_movie_2) <= 30
disp('TRUE');
else
disp('FALSE');
end
end
end

Creating a for-loop that stores values in a new variable

I'm quite new to Matlab so excuse me for the basic question.
I need to make a for-loop that repeats it's self 384 times.
So :
for i=1:384
I now need the for loop to check if 2 certain variables have the value 1 through 10, and then let them store this in a new variable with that value.
So:
if x==1
somevariable = 1
elseif x== 2
saomevariable = 2
..
..
..
elseif y = 1
someothervariable = 1
etc etc.
Is there a way to write this more efficient?
Thank you!
The first think you can do is:
if(x >= 1 && x <= 10)
somevariable=x;
end
if(y >= 1 && y <= 10)
someohtervariable=y;
end
If you could post more information about "x" and "y", perhaps your script can be further "improved".
Hope this helps.