3
\$\begingroup\$

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
\$\endgroup\$
13
  • 5
    \$\begingroup\$ 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. \$\endgroup\$
    – Arnauld
    Commented Jan 18 at 0:00
  • 8
    \$\begingroup\$ 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). \$\endgroup\$
    – Bubbler
    Commented Jan 18 at 0:06
  • 2
    \$\begingroup\$ 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 \$\endgroup\$
    – Luis Mendo
    Commented Jan 18 at 11:54
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – qwr
    Commented Jan 18 at 20:15
  • 2
    \$\begingroup\$ It would be appreciated if a clarification could be made to answer Luis Mendo's question regarding whether infinite precision is required or if submissions are allowed to become inexact due to language limitations. \$\endgroup\$
    – hyperneutrino
    Commented May 4 at 6:46

28 Answers 28

5
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – Graham
    Commented Jan 18 at 11:13
  • \$\begingroup\$ 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). \$\endgroup\$
    – Tbw
    Commented Jan 18 at 17:40
5
\$\begingroup\$

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!

\$\endgroup\$
4
\$\begingroup\$

Vyxal, 4 bytes

Ḣ?¡+

Try it Online!

Ḣ    # range(2, n+1)
   + # + 
 ?¡  # n!
\$\endgroup\$
1
4
\$\begingroup\$

Funge-98, 29 bytes

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

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Desmos, 15 bytes

f(n)=[2...n]+n!

Try it in Desmos!

The simplest a solution can get, to be honest.

\$\endgroup\$
5
  • \$\begingroup\$ Nice creativity using desmos! \$\endgroup\$
    – vengy
    Commented Jan 18 at 14:53
  • \$\begingroup\$ @vengy Desmos is literally math notation, so it's very good for challenges like this. \$\endgroup\$
    – Joao-3
    Commented Jan 18 at 14:59
  • 1
    \$\begingroup\$ Does not work for n=1, but it's probably unreasonable to expect all answers to output nothing for that case. \$\endgroup\$
    – Tbw
    Commented Jan 18 at 18:08
  • \$\begingroup\$ @Tbw n=1 is now not a valid input \$\endgroup\$
    – Joao-3
    Commented Jan 18 at 21:36
  • \$\begingroup\$ Another issue with this is that the numbers get inaccurate for large n due to floating point inaccuracies. As an example, n=21 gives a list of all 51090942171709460000. It is unclear whether or not OP is fine with such inaccuracies, but if not, this answer would technically be invalid. \$\endgroup\$
    – Aiden Chow
    Commented Jan 23 at 8:00
3
\$\begingroup\$

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!]
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 5 bytes

L¦¤!+

Try it online or verify all test cases.

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)
\$\endgroup\$
1
  • 1
    \$\begingroup\$ L¦DP+ also works for the same byte count. \$\endgroup\$
    – Neil
    Commented Jan 18 at 10:04
3
\$\begingroup\$

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)
\$\endgroup\$
3
\$\begingroup\$

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!

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

Jelly, 4 bytes

RḊ+!

Try it online!

Port of Vyxal answer.

-1 byte thanks to @emanresu A

Explanation:

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

Created with the help of Luminespire.

\$\endgroup\$
1
  • \$\begingroup\$ 4 \$\endgroup\$
    – emanresu A
    Commented Jan 18 at 10:09
2
\$\begingroup\$

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!

\$\endgroup\$
1
2
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – vengy
    Commented Jan 18 at 0:17
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – Bubbler
    Commented Jan 18 at 0:31
2
\$\begingroup\$

Uiua SBCS, 9 bytes

↘1+/×.+1⇡

Try it!

\$\endgroup\$
1
2
\$\begingroup\$

Perl 5, 41 bytes

sub{@a=1..pop;$"='*';map$_+1+eval"@a",@a}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
4
  • \$\begingroup\$ If you're going to be importing math, then you might as well use math.factorial \$\endgroup\$
    – qwr
    Commented Jan 18 at 19:52
  • \$\begingroup\$ can you show me a shorter solution using math.factorial ? \$\endgroup\$ Commented Jan 19 at 8:40
  • 2
    \$\begingroup\$ 62 \$\endgroup\$
    – l4m2
    Commented Jan 19 at 11:31
  • \$\begingroup\$ No for-loop needed. Cf. @Albert.Lang's answer. \$\endgroup\$
    – loopy walt
    Commented Jan 19 at 13:55
2
\$\begingroup\$

Google Sheets, 32 bytes

=SORT(FACT(A1)+SEQUENCE(A1-1)+1)
\$\endgroup\$
2
\$\begingroup\$

Haskell, 29 bytes

f n=map(product[2..n]+)[2..n]

Try it online

\$\endgroup\$
1
1
\$\begingroup\$

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!

\$\endgroup\$
2
  • \$\begingroup\$ I think you must include both #includes in source unfortunately \$\endgroup\$
    – AZTECCO
    Commented Jan 18 at 5:50
  • 2
    \$\begingroup\$ 150 bytes \$\endgroup\$
    – ceilingcat
    Commented Jan 18 at 7:51
1
\$\begingroup\$

Scala 3, 55 bytes

n=>{val a=(1 to n).map(BigInt(_));a.map(_+1+a.product)}

Attempt This Online!

\$\endgroup\$
1
\$\begingroup\$

PARI/GP, 15 bytes

n->[n!+2..n!+n]

Attempt This Online!

\$\endgroup\$
1
\$\begingroup\$

Wolfram Language (Mathematica), 13 bytes

#!+2~Range~#&

Try it online!

\$\endgroup\$
8
  • \$\begingroup\$ does wolfram alpha have a similar formula? thanks \$\endgroup\$
    – vengy
    Commented Jan 18 at 12:05
  • \$\begingroup\$ @vengy you can use something like 21!+k for k=2..21 \$\endgroup\$
    – ZaMoC
    Commented Jan 18 at 15:27
  • \$\begingroup\$ That example worked nicely. Thanks! \$\endgroup\$
    – vengy
    Commented Jan 18 at 15:44
  • \$\begingroup\$ Actually this works too: 21!+k,k=2..21 \$\endgroup\$
    – vengy
    Commented Jan 18 at 15:55
  • \$\begingroup\$ @vengy or 21!+{2..21} \$\endgroup\$
    – ZaMoC
    Commented Jan 18 at 16:25
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 11 bytes

≔…·²NθI⁺θΠθ

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

≔…·²Nθ

Create a list from 2 up to n inclusive.

I⁺θΠθ

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:

Nθ≔…·²θηW⌈﹪±θη≧⁺ιθI⁺θη

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

Nθ≔…·²θηFη≔⌊Φ×⊖ηθ¬﹪κιθI⁺θη

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

\$\endgroup\$
1
\$\begingroup\$

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`
\$\endgroup\$
0
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Japt, 6 bytes

ò2È+UÊ

Try it here

\$\endgroup\$
1
\$\begingroup\$

Raku (Perl 6) (rakudo), 19 bytes

{2..$_ X+[*] 1..$_}

Attempt This Online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.