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! ????
Design Verification Engineer
5 个月if(i%N == 0) flag = 0; may be print mistake, actual is N%i == 0
Design Verification Engineer @ Bitsilica
8 个月Really good explanation