C.6 Examples for Chapter 9

Example 22

Two functions, nextprime and randprime, can be used to generate prime numbers. The function nextprime takes a number n as input and attempts to find the next prime after n. The function randprime takes a number n as input and attempts to find a random prime between 1 and n. It uses the Miller-Rabin test described in Chapter 9.

>> nextprime(346735)

ans =
   346739

>> randprime(888888)

ans =
   737309

For larger inputs, use symbolic mode:

>> nextprime(10^sym(60))

ans =
   1000000000000000000000000000000000000000000000000000000000007

>> randprime(10^sym(50))

ans =
   58232516535825662451486550708068534731864929199219

It is interesting to note the difference that the ’ ’ makes when entering a large integer:

Get Introduction to Cryptography with Coding Theory, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.