Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Generate a sequence of \$n\$ consecutive composite numbers

Definitions

The common methods to generate consecutive composites are

$$\overbrace{(n+1)! + 2, \ (n+1)! + 3, \ \ldots, \ (n+1)! + (n+1)}^{\text{n composites}}$$

$$\overbrace{n!+2,n!+3,...,n!+n}^{\text{n-1 composites}}$$

$$\overbrace{n\#+2,n\#+3,...n\#+n}^{\text{n-1 composites (Primorials)}}$$


A less common method involves the Chinese Remainder Theorem (CRT).

For example, define a list of \$n\$ pairwise coprimes \$p_1, p_2, \ldots, p_n\$.

A set of integers can also be called coprime if its elements share no common positive factor except 1. A stronger condition on a set of integers is pairwise coprime, which means that \$a\$ and \$b\$ are coprime for every pair \$(a, b)\$ of different integers in the set. The set \$\{2, 3, 4\}\$ is coprime, but it is not pairwise coprime since 2 and 4 are not relatively prime.

Create a set of \$n\$ congruences

\begin{align*} x + 1 &\equiv 0 \pmod{p_1} \\ x + 2 &\equiv 0 \pmod{p_2} \\ &\vdots \\ x + n &\equiv 0 \pmod{p_n} \end{align*}

By CRT, there exists a unique solution \$x\$ which satisfies this sequence of \$n\$ consecutive composite numbers:

$$x + 1, x + 2, \ldots, x + n$$

A composite number is a positive integer that can be formed by multiplying two smaller positive integers. Equivalently, it is a positive integer that has at least one divisor other than 1 and itself.


Challenge

Given pairwise coprime inputs, find the smallest range of consecutive composite numbers.

Input

A list of \$n\$ pairwise coprimes.

Output

A sequence of \$n\$ consecutive composite numbers.

Test Cases

These cases were derived from this test.

n pairwise coprimes consecutive composites
2 2, 3 8,9
3 7, 11, 23 1792, 1793, 1794
4 2, 3, 5, 7 158, 159, 160, 161
5 3, 7, 10, 13, 23 47928, 47929, 47930, 47931, 47932

Added a JavaScript (V8) tool to validate if the input numbers are pairwise coprime.

Answer*

Draft saved
Draft discarded
Cancel
1
  • \$\begingroup\$ 446 bytes \$\endgroup\$ Commented Feb 10, 2024 at 5:13

AltStyle によって変換されたページ (->オリジナル) /