Problem 60950. Find the longest runs of primes generated by polynomials

Cody Problems 60942, 60943, and 60944 involve polynomials that generate primes. No polynomial can generate only prime numbers, but some can generate sizable runs of primes. For example, for n = 0, 1, 2,…,10, f(n) = 2n^2+11 produces 11, 13, 19, 29, 43, 61, 83, 109, 139, 173, and 211, which are all prime. For n = 11, both terms in the polynomial are divisible by 11, and the result (253) is composite.
Write a function that takes the coefficients of the polynomial in standard Matlab form (i.e., a vector with the coefficients in order of decreasing order of the terms) and returns the length of the longest run of primes as well as a sorted list (low to high) of the distinct primes in the run. Please note the following:
  • Take the absolute value of the output of the polynomial. For example, consider -11, -13, -19, etc. to be prime for this problem, even though negative numbers are not strictly considered to be prime.
  • Round the absolute value to the nearest integer. Although this step is not necessary when the coefficients of the polynomial are integers, it can be necessary when they are not, as in the last two tests.
  • Make sure to list only the primes in the longest run. The polynomials will produce other primes outside of the longest run, but do not include them in the output.

Solution Stats

71.43% Correct | 28.57% Incorrect
Last Solution submitted on Jul 14, 2025

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers4

Suggested Problems

More from this Author310

Problem Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!