here is the code for prime number using constraints…
class array;
rand int array[$];
constraint const_array{array.size inside {100};
foreach(array[i])
if(!((i%2==0 && i!=2) || (i%3==0 && i!=3) || (i%4==0 && i!=4) || (i%5==0 && i!=5) || (i%6==0 && i!=6) || (i%7==0 && i!=7) || (i%8==0 && i!=8) || (i%9==0 && i!=9)))
array[i]==i;
else
array[i]==1;}
function void disp();
$display("the array: %p",array);
endfunction
endclass
output:
the array: '{1, 1, 2, 3, 1, 5, 1, 7, 1, 1, 1, 11, 1, 13, 1, 1, 1, 17, 1, 19, 1, 1, 1, 23, 1, 1, 1, 1, 1, 29, 1, 31, 1, 1, 1, 1, 1, 37, 1, 1, 1, 41, 1, 43, 1, 1, 1, 47, 1, 1, 1, 1, 1, 53, 1, 1, 1, 1, 1, 59, 1, 61, 1, 1, 1, 1, 1, 67, 1, 1, 1, 71, 1, 73, 1, 1, 1, 1, 1, 79, 1, 1, 1, 83, 1, 1, 1, 1, 1, 89, 1, 1, 1, 1, 1, 1, 1, 97, 1, 1}
here in the output,the value one is constrained for the rest of the non prime index of the array…i.e array[0]=1;array[1]=1;array[2]=2;array[3]=3;array[4]=1;…so on
how to find the prime numbers from 0 to 100;???
without wasting memory in the array…
expected output:
2,3,5,7,11,13,17,19,23,29,31,…89,97;