# Challenge

Generate $$\n-1\$$ consecutive composite numbers using this prime gap formula

$$n!+2,n!+3,...,n!+n$$

Input

An integer $$\n\$$ such that $$\3 \leq n \leq 50 \$$.

Output

Sequence of $$\n-1\$$ consecutive composite numbers.

# Example

Input

3


Output

8
9


# Rules

• Output should be in integer format.

# Test Cases

For $$\n > 20\$$, the results are very BIG integers (greater than 64-bits) and will most likely require a language that natively supports large numbers or a 3rd party library to handle them.

n $$\n-1\$$ consecutive composites
3 8
9
5 122
123
124
125
21 51090942171709440002
51090942171709440003
51090942171709440004
51090942171709440005
51090942171709440006
51090942171709440007
51090942171709440008
51090942171709440009
51090942171709440010
51090942171709440011
51090942171709440012
51090942171709440013
51090942171709440014
51090942171709440015
51090942171709440016
51090942171709440017
51090942171709440018
51090942171709440019
51090942171709440020
51090942171709440021
• Your last comment makes me wonder: if supporting large integers is a strict requirement (which doesn't sound like a good idea), it should be explicitly mentioned in the challenge. Jan 18 at 0:00
• Please note that "making the challenge trickier by requiring a bignum" is one example of unnecessary fluff and therefore discouraged. Check out the fifth rule on this challenge (which is the implicit default on this site). Jan 18 at 0:06
• So, to be clear: do answers need to support arbiitrarily large integers, or is it acceptable if the results become inexact for large inputs due floating-point issues or maximum representable values? Voting to close until clarified Jan 18 at 11:54
• Actually I read the OEIS gap size wrong. oeis.org/A204665 is for gaps of size 52. So the following 50 numbers after 19609 are composite.
– qwr
Jan 18 at 20:15
• It would be appreciated if a clarification could be made to answerw Luis Mendo's question regarding whether infinite precision is required or if submissions are allowed to become inexact due to language limitations. May 4 at 6:46

# APL(Dyalog Unicode), 5 bytes SBCS

1↓⍳+!


Try it on APLgolf!

A tacit function that takes an integer on the right and returns an array of n-1 composite numbers.

### Explanation

    ! ⍝ factorial
+  ⍝ plus
⍳   ⍝ range
1↓    ⍝ drop the first


There are actually two other permutations of these five characters that produce the same result. See if you can find them. ;)

## 6 bytes, outputs full numbers under $$\10^{34}\$$

⍕1↓⍳+!


Try it! This requires adding (⎕FR⎕PP)←1287 34 to the header to set the float and printing precision.

• How many digits can Dyalog display. I attempted this in APL+WIN and like your answer it defaults to E notation for large numbers. That being the case with a 17 digit limit I could not display the true series as all results were the same for the 21 example i.e. 5.109094217170944E19 5.109094217170944E19 etc Jan 18 at 11:13
• To get an exact answer in Dyalog, you need to have (⎕FR⎕PP)←1287 34 in the header and use ⍕. I'll probably add this as 6 byte solution (usually, defining system variables like ⎕IO← is not counted in bytes).
– Tbw
Jan 18 at 17:40

# Vyxal, 4 bytes

Ḣ?¡+


Try it Online!

Ḣ    # range(2, n+1)
+ # +
?¡  # n!

• 3 bytes # Vyxal Ṫ, 3 bytes  Ḣ?¡  Try it Online! Jan 18 at 6:20

# Funge-98, 29 bytes

"HTMI"4(&:F2+v
\I_@#:-2\I.: <


Try it online!

# Desmos, 10 bytes

[2...n]+n!


Try it in Desmos!

The simplest a solution can get, to be honest.

• Nice creativity using desmos! Jan 18 at 14:53
• @vengy Desmos is literally math notation, so it's very good for challenges like this. Jan 18 at 14:59
• Does not work for n=1, but it's probably unreasonable to expect all answers to output nothing for that case.
– Tbw
Jan 18 at 18:08
• @Tbw n=1 is now not a valid input Jan 18 at 21:36
• Nice answer! But taking input as a hardcoded variable (n in this case) is heavily discouraged by meta consensus. You will need to make it into a function (like lambda in Python) in order for this answer to conform with meta consensus. This can easily be done by adding f(n)= to the beginning of your code, at the cost of 5 bytes. Jan 23 at 7:54

# Python, 51 bytes

f=lambda n:range((n<2or(f(n-1).stop-n)*n)-~n)[1-n:]


Attempt This Online!

Returns a range object.

### Original Python, 56 bytes

f=lambda n:[n*([*f(n-1),3][0]-2)+j+2for j in range(n-1)]


Attempt This Online!

# Jelly, 3 bytes

Ḋ+!


Try it online!

A monadic Link that accepts a positive integer, $$\n\$$, and yields a list of the $$\n-1\$$ consecutive integers.

### How?

Ḋ+! - Link: integer, n
Ḋ   - dequeue {n}      -> [2, 3, 4, ..., n]
! - {n} factorial    -> n!
+  - add (vectorises) -> [2+n!, 3+n!, 4+n!, ..., n+n!]


# 05AB1E, 5 bytes

L¦¤!+


Explanation:

L      # Push a list in the range [1, (implicit) input]
¦     # Remove the first value to make the range [2,input]
¤    # Push the last item (without popping the list): the input
!   # Take its factorial
+  # Add it to each value in the list
# (after which this list is output implicitly as result)

• L¦DP+ also works for the same byte count.
– Neil
Jan 18 at 10:04

# MathGolf, 5 bytes

╒╞\!+


Try it online.

Explanation:

╒      # Push a list in the range [1, (implicit) input]
╞     # Remove the first value to make the range [2,input]
\    # Swap to push the (implicit) input
!   # Take its factorial
+  # Add it to each value in the list
# (after which the entire stack is output implicitly as result)


# JavaScript (Node.js), 52 51 47 46 45 44 bytes

f=(n,i=n)=>--n&&f(n,g=i*n)>console.log(n-~g)


Try it online!

-1 byte from Arnauld/tsh

# JavaScript (Node.js), 38 bytes by Mukundan314

f=(n,i=n)=>--n?[...f(n,g=i*n),n-~g]:[]


Try it online!

• Comparing BigInts with Numbers is fine, so you can just do i>0. Jan 18 at 1:14
• Maybe you need apply above comment again: f=(n,i=n)=>--n&&f(n,g=i*n)>console.log(n-~g)
– tsh
Jan 18 at 9:56
• 38 bytes by returning a list instead of outputting to console Jan 18 at 10:23

# Jelly, 4 bytes

RḊ+!


Try it online!

-1 byte thanks to @emanresu A

Explanation:

RḊ+!­⁡​‎‎⁪⁡⁪⁠⁪⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁪‏‏​⁡⁠⁡‌⁢​‎⁪⁪⁠‎⁪⁡⁪⁠⁪⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁪‏‏​⁡⁠⁡‌­
RḊ    # ‎⁡[2 .. n]
+!  # ‎⁢+ n!
💎


Created with the help of Luminespire.

• 4 Jan 18 at 10:09

# JavaScript (V8), 46 bytes

Prints the results in reverse order.

n=>{for(p=i=n;--i;)p*=i;while(--n)print(p-~n)}


Try it online!

# JavaScript (ES11), 52 bytes

With BigInts.

n=>{for(p=i=n;--i;)p*=i;while(--n)console.log(p-~n)}


Try it online!

• 51 bytes for the ES11 version by using a out argument Jan 18 at 10:34

# J, 7 bytes

!-<:$i:  Attempt This Online! Returns the list of numbers in reverse order. When given a bigint, outputs the exact numbers in bigint. !-<:$i:    input: n, a positive integer
i:    [-n, -n+1, ..., n]
<:       n-1
$take first n-1 items from the list, i.e. [-n, ..., -2] ! n! - n! - each item of [-n, ..., -2] == [n!+n, n!+n-1, ..., n!+2]  # J, 8 bytes !+-.{.i:  Attempt This Online! Returns the list of numbers in the order given. !+-.{.i: input: n, a positive integer i: [-n, -n+1, ..., n] -. 1-n {. since 1-n is negative, take n-1 items from the end; [2, ..., n] !+ add n! to each item of the list  • I'm new to this site...and I always notice these "languages" generate really short code bytes. Are they specifically designed for code golf and how do they minimize the code so much? Thanks. Jan 18 at 0:17 • @vengy J is not a "golflang" (language created for golf); it just happens to be short quite a lot of the time. Jelly, Vyxal, and 05AB1E are some examples of often-used golflangs, all of which minimize code by utilizing all 256 byte values (as opposed to printable ASCII) loaded with built-in functions, among other things. Jelly specifically was inspired by J, and the other two are stack-based. Jan 18 at 0:31 # UiuaSBCS, 9 bytes ↘1+/×.+1⇡  Try it! # Perl 5, 41 bytes sub{@a=1..pop;$"='*';map$_+1+eval"@a",@a}  Try it online! # dc, 56 54 bytes [dlf*sf1-d0!=q]sq1sf?d1-sm[dlf2++p0=m1+dlm!=e]selqxlex  Try it online! This solution isn't the most optimized, you could probably use less registers than I did here. # Python 3.8 (pre-release), 69 62 bytes lambda n:[i-~math.factorial(n)for i in range(1,n)] import math  Try it online! Edit: changed math.prod to math.factorial, thanks to @l4m2 • If you're going to be importing math, then you might as well use math.factorial – qwr Jan 18 at 19:52 • can you show me a shorter solution using math.factorial ? Jan 19 at 8:40 • 62 – l4m2 Jan 19 at 11:31 • No for-loop needed. Cf. @Albert.Lang's answer. Jan 19 at 13:55 # Google Sheets, 32 bytes =SORT(FACT(A1)+SEQUENCE(A1-1)+1)  # C (gcc), 170 bytes Using The GNU Multiple Precision Arithmetic Library f(n){mpz_t f,t;mpz_init(f);mpz_init(t);mpz_set_ui(f,1);for(int i=1;i<=n;mpz_mul_ui(f,f,i++));for(int i=2;i<=n;mpz_add_ui(t,f,i),mpz_out_str(stdout,10,t),puts(""),i++);}  Try it online! • I think you must include both #includes in source unfortunately Jan 18 at 5:50 • 150 bytes Jan 18 at 7:51 # Scala 3, 55 bytes n=>{val a=(1 to n).map(BigInt(_));a.map(_+1+a.product)}  Attempt This Online! # PARI/GP, 15 bytes n->[n!+2..n!+n]  Attempt This Online! # Wolfram Language (Mathematica), 13 bytes #!+2~Range~#&  Try it online! • does wolfram alpha have a similar formula? thanks Jan 18 at 12:05 • @vengy you can use something like 21!+k for k=2..21 Jan 18 at 15:27 • That example worked nicely. Thanks! Jan 18 at 15:44 • Actually this works too: 21!+k,k=2..21 Jan 18 at 15:55 • @vengy or 21!+{2..21} Jan 18 at 16:25 # Retina, 43 bytes .+ * v__+$.&$* L$\d+
$$.(&*_= %~^ .+¶  Try it online! Outputs the numbers in descending order. Explanation: .+ *  Convert n to unary. v__+ .&*  Create an expression n*...*3*2* that evaluates to n! in unary. L\d+$$.($&*_$=


For each integer in that expression, create an expression that adds i and converts to decimal.

%~^
.+¶


Evaluate each resulting expression.

# Charcoal, 11 bytes

≔…·²ＮθＩ⁺θΠθ


Try it online! Link is to verbose version of code.

≔…·²Ｎθ


Create a list from 2 up to n inclusive.

Ｉ⁺θΠθ


Vectorised add the list to its product.

Of course, the given formula is just an existence proof and there are usually lower runs. For instance, the LCM is readily proven to produce a run:

Ｎθ≔…·²θηＷ⌈﹪±θη≧⁺ιθＩ⁺θη


Try it online! Link is to verbose version of code. Or more efficiently:

Ｎθ≔…·²θηＦη≔⌊Φ×⊖ηθ¬﹪κιθＩ⁺θη


Try it online! Link is to verbose version of code.

# Java 10, 134 131 bytes

n->{var f=n.ONE;int N=n.intValue(),i=N;for(;i>1;)f=f.multiply(n.valueOf(i--));for(;i++<N;)System.out.println(f.add(n.valueOf(i)));}


-3 bytes thanks to @ceilingcat.

Input as a BigInteger, outputs on separated lines to STDOUT.

Try it online.

Explanation:

n->{                            // Method with BigInteger as parameter and no return:
var f=n.ONE;                  //  Start f with BigInteger 1
int N=n.intValue(),           //  Set N to the input-BigInteger as integer
i=N;for(;i>1;)                //  Loop i in the range [N,1):
f=f.multiply(n.valueOf(i)); //   Multiply f by i
for(;i++<N;)                  //  Loop i again, this time in the range (1,N]:
System.out.println(         //   Print with trailing newline:
f.add(n.valueOf(i)));}    //    f + i


# Labyrinth, 30 bytes

?:}  @\!
(:{+ ""
*; :=;;
#(;)


Try it online!

Introducing "small-loop-oriented programming" ;)

?    program start; take n from stdin
let's say n = 4
[ 4 | ]

:}   run in the order of :(:}
(:   : dup               [ 4 4 | ]
( decrement         [ 4 3 | ]
: dup               [ 4 3 3 | ]
} move to aux.stack [ 4 3 | 3 ]
loop until the top is 0 after decrement
[ 4 3 2 1 0 | 1 2 3 ]

*;   run in the order of ;*#(
#(   ; drop         [ 4 3 2 1 | 1 2 3 ]
* mul          [ 4 3 2 | 1 2 3 ]
# stack height [ 4 3 2 3 | 1 2 3 ]
( decrement    [ 4 3 2 2 | 1 2 3 ]
loop until the top is 0 after decrement, i.e. stack height is 1
[ 24 0 | 1 2 3 ]

;)   drop 0, and increment the factorial
[ 25 | 1 2 3 ]

{+   run in the order of :=+{
:=   : dup                 [ 25 25 | 1 2 3 ]
= swap the tops       [ 25 1 | 25 2 3 ]
+ add                 [ 26 | 25 2 3 ]
{ move from aux.stack [ 26 25 | 2 3 ]
loop until the top is 0 after swap the tops
[ 26 27 28 25 0 | 25 ]

;;   drop twice [ 26 27 28 | 25 ]

\!   run in the order of ""!\  (" is no-op)
""   ! pop and print as num
\ print a newline
loop until the top is zero, i.e. there is nothing left to print

@    end the program