Constraint #47

Constraint #47

Constraint to generate any one Prime No. in a given range #1

To generate a prime number, we first need to understand what a prime number is. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In simpler terms, a prime number cannot be formed by multiplying two smaller whole numbers. Examples of prime numbers include 2, 3, 5, 7, 11, and so on.

As per my Knowledge It is difficult or not possible, to directly write a constraint to generate a prime number. Instead, we can find prime numbers and store them in a queue. We can then use this queue for the required constraint.

To achieve this, let's discuss the algorithm to generate all prime numbers in a given range. To determine if a number is prime, we need to start dividing the number from 2 up to that number - 1. If the number is not divisible by any other number, then it is a prime number.

Here's a simple code example to find a prime number:

function prime_numner (int N)
	bit flag = 1; 	
	for(i=2; i<N; i++) begin
		if(i%N == 0) flag = 0;
	end 
	if(flag==1) $display("N is a prime No");
endfunction        

However, this code is not efficient because even after finding that a number is not prime, the loop continues unnecessarily. For example, if we determine that 16 is not a prime number because it is divisible by 2, there is no need to check divisibility by 3, 4, 5, and so on up to 15.

To make this more efficient, we can stop the loop iteration as soon as we find a number that divides the given number by assigning the value of N to i which will make the condition of for loop to fail resulting in saving the Unnecessary iteration after finding that number is prime number. Here's an improved version:

function void prime_number(int N);
    bit flag = 1; 	
    for(int i=2; i<n; i++) begin
        if(i%n == 0) begin
            flag = 0;
            i=N;
        end
    end 
    if(flag==1) $display("N is a prime No");
endfunction        

To find all prime numbers in a given range, we need to iterate over each number in the range from 2 and check if it is prime using the function we discussed earlier. We'll use two nested for loops for this purpose. The outer loop will iterate over each number from 2 to the maximum range, and the inner loop will check if the number is prime.

for(int i=2; i<MAX_RANGE; i++) begin
    bit flag = 1; 	
    for(int j=2; j < i; j++) begin
        if(i%j == 0) begin
            flag = 0;
            j=i;
        end
    end 
    if ( flag == 1) prime_Q.push_back(i); 
end        

This code finds all prime numbers in a given range but As per the Mathematical Law only by checking divisibility up to the square root of a number, we can determine whether it is a prime. This optimization saves unnecessary iterations in the loop. by changing the Second for loop condition as Below

for(int j=2; j <= i**0.5; j++) begin ==> i**0.5 refers to the Square root of i.

for(int i=2; i<MAX_RANGE; i++) begin
    bit flag = 1; 	
    for(int j=2; j <= i**0.5; j++) begin
        if(i%j == 0) begin
            flag = 0;
            j=i;
        end
    end 
    if ( flag == 1) prime_Q.push_back(i); 
end        

Having understood the algorithm to find prime numbers in a given range, we can now proceed to implement the required constraint in the next article.

I appreciate your understanding of the methodology we've utilized to handle constraints and their real-world applications. Your support, through likes and reposts, will indeed encourage further exploration. I'm eagerly looking forward to engaging in meaningful discussions with you. Thank you! ????

srikanth P

Design Verification Engineer

5 个月

if(i%N == 0) flag = 0; may be print mistake, actual is N%i == 0

回复
arshiya sanglikar

Design Verification Engineer @ Bitsilica

8 个月

Really good explanation

要查看或添加评论,请登录

mohamed irsath I的更多文章

  • Types of Assertion

    Types of Assertion

    In this article, we are going to discuss the Types of Assertion. There are two types of assertion, They are 1)…

    4 条评论
  • What is an Assertion and its needs DV

    What is an Assertion and its needs DV

    Assertions are checks which used to verify that your design meets the given requirements. Assertions in design…

    5 条评论
  • Constraint for a Palindrome No.

    Constraint for a Palindrome No.

    In order to solve the above constraints, we first need to understand what a palindrome number is. A palindrome number…

    2 条评论
  • System Verilog Assertions

    System Verilog Assertions

    Dear Followers, We are excited to announce that our upcoming newsletter will be focusing on SystemVerilog Assertions…

    1 条评论
  • Constraint for AXI Strobe Signal

    Constraint for AXI Strobe Signal

    In the AXI protocol, the wstrb signal (write strobe) is used to indicate which bytes of the wdata signal (write data)…

    6 条评论
  • Corner case in constraint #49 Learnings & Solution

    Corner case in constraint #49 Learnings & Solution

    Constraint for a variable i) on every 2nd randomization the value should be the reverse of the previous value ii)…

    4 条评论
  • Constraint for AXI 4kb Boundary

    Constraint for AXI 4kb Boundary

    In order to achieve the above constraint, first, we need to understand what the AXI-4Kb Boundary is. AXI is a parallel…

    6 条评论
  • Constraint #49

    Constraint #49

    Constraint for a variable i) on every 2nd randomization the value should be the reverse of the previous value ii)…

    10 条评论
  • Constraint #48

    Constraint #48

    Constraint to generate any Prime No. in a given range #2 To achieve the given constraint, we utilize the prime_number…

  • Constraint #46

    Constraint #46

    Significance of new() for a RANDC Variable. When using the keyword in SystemVerilog, a variable is declared to have…

社区洞察

其他会员也浏览了