I'm new to Mathematica so my question might be trivial.

How do I generate a $200-bit$ prime with the condition that the 10 most significant bits in its binary representation are equal to some binary string (for example $1001110011$)?

I know that I can use RandomPrime[{2^199, 2^200 - 1}] to generate primes in the required range, but how do I guarantee the previous condition?

  • 1
    $\begingroup$ One can use NextPrime. (1) Set your initial bits. (2) Pick a random integer rand between the lowest and highest values that have those initial bits. (3) Take NextPrime[rand]. (4) If it exceeds the max, rinse and repeat. $\endgroup$ Jan 24, 2018 at 17:59

1 Answer 1


You can find the valid range by using BitShiftLeft on the string and its successor:

RandomPrime[{BitShiftLeft[2^^1001110011,     190],
             BitShiftLeft[2^^1001110011 + 1, 190] - 1}]
  • $\begingroup$ @Shadowfirex - don't ask additional questions in a Comment. Start a new Question. If appropriate to add context, link back to the original question. $\endgroup$
    – Bob Hanlon
    Jan 24, 2018 at 19:06
  • $\begingroup$ Alright, moved the comment to a separate question. $\endgroup$ Jan 24, 2018 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.