I'm trying to find the longest consecutive set of composite numbers

You can have a sequence as long as you wish. Consider $n\in\Bbb{N}$ then the set

$$S_n=\{n!+2,n!+3,\cdots,n!+n\}$$

is made of composite consecutive numbers and is of length $n-1$


marwalix's answer is great, but it is possible to 'optimize' the given sequence even more using a very simple 'trick'.

Simply replace $n!$ by $n\#$, the primorial: $$n\#=\prod_{i=1}^{\pi(n)}p_i$$ The sequence now becomes: $$n\#+2,n\#+3,\ldots n\#+n$$

Say you want to find a sequence of length $15$. marwalix's original answer would give you the sequence: $$20922789888002,20922789888003,20922789888004,20922789888005,20922789888006,20922789888007,20922789888008,20922789888009,20922789888010,20922789888011,20922789888012,20922789888013,20922789888014,20922789888015,20922789888016$$ while this way of constructing the sequence gives: $$30032,30033,30034,30035,30036,30037,30038,30039,30040,30041,30042,30043,30044,30045,30046$$ and those numbers are way smaller.

Why does this work? Well say we have some $n,m\in\mathbb{N}$ with $n\#+m$ prime. Then $p\nmid n\#+m$ for all primes $p\le n$, but $p\mid n\#$ for all $p\le n$, so $p\nmid m$ for all $p\le n$. Therefore $m=1$ or $m$ is a prime greater than $n$. In any case, we won't have $2\le m\le n$, so the intergers $n\#+2,n\#+3,\ldots, n\#+n$ are all composite.

A simple algorithm

There is an algorithmic way to 'join' two primegaps together to form a new, larger prime gap. Let me give an example. By a similiar argument as before, for all non-negative integers $k$, the numbers: $$30k+20,30k+21,30k+22$$ and $$30k+24,30k+25,30k+26,30k+27,30k+28$$ are all composite, but $23$ is prime. We would like to restrict the values of $k$ such that $30k+23$ is also composite, say divisible by $7$. We solve $30k+23\equiv 0\pmod 7$: $$30k+23\equiv 0\pmod 7$$ $$2k+ 2\equiv 0\pmod 7$$ $$k\equiv 6\pmod 7$$ So write $k=7m+6$. Now the number $30k+23=30(7m+6)+23$ is divisible by $7$ and therefore composite. We get the sequence of composite numbers: $$210m+200,210m+201,210m+202,\ldots 210m+208$$ for all non-negative $m$. We also find that $210m+198$ is always composite, but $199$ is prime. We would like to restrict $m$ such that $210m+199$ is divisible by $11$. We get: $$210m+199\equiv 0\pmod {11}$$ $$m+1\equiv 0\pmod {11}$$ $$m\equiv 10\pmod {11}$$ So write $m=11k+10$. We now get that for all non-negative integers $k$, the integers $$2310k+2298,2310k+2299,\ldots,2310k+2308$$ are all composite. We can continue this process for as long as we want and there is a chance it will yield even better results than the previous approach, though I don't know for sure. (the best-case result is certainly better and the worst-case result is a lot worse, but I don't know about the average result of the algorithm)


Let me offer a different view to this.

Suppose there was a longest consecutive set of composite numbers. Denote the length by $L$. Then at least every $(1+L)$th natural number must be a prime, so that the density of primes, $$ \lim_{N\to\infty}\frac{\text{number of primes less than }N}{N}, \tag{1} $$ is at least $1/(L+1)$.

However, the density is zero: the bigger $N$ is, the smaller the fraction of primes in the set $\{1,\dots,N\}$. (Well, not exactly. The limit is zero, but the sequence is not monotone. The point should be clear enough, though.) But since $0<1/(L+1)$, we have a contradiction. Therefore there cannot be a longest run of composite numbers.

The only non-elementary part is the fact that the limit (1) is indeed zero. For example, this follows from the prime number theorem, which asserts that the ratio in (1) is roughly $1/\log(N)$.