Skip to main content

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
Asked
Modified 2 days ago
Viewed 894 times
20
\$\begingroup\$

I was looking for a simple sequence that's not yet referenced on the OEIS and came up with this one(*):

  • \$a_1=2\$
  • \$a_2=3\$
  • For \$n>2\$, \$a_n\$ is the smallest number of the form \$a_i\times a_j+1\$ (with \$1\le i\le j<n\$) that is not yet included in the sequence.

The first few terms are:

2 3 5 7 10 11 15 16 21 22 23 26 31 33 34 36 43 45 46 47 49 50 51 53 56 63 64 67 69 70 71 73 76 78 79 81 87 91 93 94 95 99

Your task is to output this sequence.

This is and standard rules apply.


(*) As pointed out by ovs and Jonathan Allan, the OEIS does include two sequences in the same spirit: A009293 and A009388.

\$\endgroup\$
4
  • \$\begingroup\$ And 5 in the sequence from where came from? \$\endgroup\$
    Rosario
    –  Rosario
    2025-10-09 19:11:12 +00:00
    Commented Oct 9 at 19:11
  • 3
    \$\begingroup\$ @Rosario 2x2+1; i and j can be equal. \$\endgroup\$
    Giuseppe
    –  Giuseppe
    2025-10-09 19:11:34 +00:00
    Commented Oct 9 at 19:11
  • 3
    \$\begingroup\$ A009293 is a variant of this starting with just 2 \$\endgroup\$
    ovs
    –  ovs
    2025-10-09 19:13:56 +00:00
    Commented Oct 9 at 19:13
  • 2
    \$\begingroup\$ A009388 is the variant with \$-1\$ rather than \$+1\$. \$\endgroup\$
    Jonathan Allan
    –  Jonathan Allan
    2025-10-09 23:52:32 +00:00
    Commented Oct 9 at 23:52

14 Answers 14

9
\$\begingroup\$

R, 62 60 57 bytes

\(n)Reduce(\(g,.)c(g,min(setdiff(g%o%g+1,g))),1:n,2:3)[n]

Attempt This Online!

-3 bytes thanks to M--.

Uses Reduce to iterate n times, resulting in a list of length n+2, then extracts the nth element.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ 57 bytes: \(n)Reduce(\(g,.)c(g,min(setdiff(g%o%g+1,g))),1:n,2:3)[n]. \$\endgroup\$
    M--
    –  M--
    2025-10-10 16:36:29 +00:00
    Commented Oct 10 at 16:36
  • \$\begingroup\$ @M-- very nice. I don't know that I've ever seen . as an argument before, though it seems it can (of course) be replaced by anything else. \$\endgroup\$
    Giuseppe
    –  Giuseppe
    2025-10-10 18:09:36 +00:00
    Commented Oct 10 at 18:09
6
\$\begingroup\$

Jelly,  13  11 bytes

2×þ`‘jƊ¡QṢḣ

A monadic Link that accepts a positive integer, \$n\$, and yields a list of the first \$n\$ values in the sequence

Don't try it online! - anything above \$n=3\$ will time out :D
(It constructs a list of \$a_n=a_{n-1}(2a_{n-1}-1)\$ integers where \$a_1=5\$.)


Faster version,  15  13 bytes

2×þ`‘FḟƲṂṭƊ¡ḣ

Try it online!

How?

11:

2×þ`‘jƊ¡QṢḣ - Link: N
2           - two (starting value of Current)
       ¡    - repeat {N} times:
      Ɗ     -   last three links as a monad - f(Current):
 ×þ`        -     multiplication table of {Current} with itself
                    N.B. Initially Current is the integer 2, the range [1,2]
                         is made implicitly, so we get [[1, 2], [2, 4]]
    ‘       -     increment all values                 [[2, 3], [3, 5]]
     j      -     join with {Current}                  [2, 3, 2, 3, 5]      
        Q   - deduplicate
         Ṣ  - sort
          ḣ - head to 1-index {N}

13:

2×þ`‘FḟƲṂṭƊ¡ḣ - Link: N
2             - two (starting value of Current)
           ¡  - repeat {N} times:
          Ɗ   -   last three links as a monad - f(Current):
       Ʋ      -     last four links as a monad - f(Current):
 ×þ`          -     multiplication table of {Current} with itself
                      N.B. Initially, Current is the integer 2, the range [1,2]
                           is made implicitly, so we get [[1, 2], [2, 4]]
    ‘         -     increment all values                 [[2, 3], [3, 5]]
     F        -     flatten                              [2, 3, 3, 5]
      ḟ       -     discard those in {Current}           [5]
        Ṃ     -   minimum                                5
         ṭ    -   tack to {Current}                      [2, 3, 5]
            ḣ - head to 1-index {N}
\$\endgroup\$
5
\$\begingroup\$

APL(NARS), 39 chars

{⍵≤1:,2⋄⍵{⍺≤≢⍵:⍵⋄⍺∇⍵,⌊/⍵∼⍨1+,⍵∘.×⍵}2,3}

This function has as input one positive integer 'w', as output a list of integers of length 'w'.

Test:

  f←{⍵≤1:,2⋄⍵{⍺≤≢⍵:⍵⋄⍺∇⍵,⌊/⍵∼⍨1+,⍵∘.×⍵}2,3}
  f 1
2 
  f 2
2 3 
  f 3
2 3 5 
  f 4
2 3 5 7 
  f 10
2 3 5 7 10 11 15 16 21 22 
  f 100
2 3 5 7 10 11 15 16 21 22 23 26 31 33 34 36 43 45 46 47 49 50 51 53 56 63 64 67 69 70 71 
  73 76 78 79 81 87 91 93 94 95 99 100 101 103 106 107 109 111 113 116 122 127 129 
  130 131 135 136 139 141 142 143 147 148 151 153 154 155 156 157 159 160 161 162 163 
  166 169 171 175 177 181 183 187 189 190 191 193 199 201 202 203 207 208 211 213 214 
  215 216 218 219 
\$\endgroup\$
5
\$\begingroup\$

Charcoal, 23 bytes

FN⊞υ⎇‹ι²⁺²ι⌊⁻⊕ΣEυ×κυυIυ

Attempt This Online! Link is to verbose version of code. Outputs the first n terms. Explanation:

FN

Input n and loop that many times.

⊞υ⎇‹ι²⁺²ι⌊⁻⊕ΣEυ×κυυ

Push 2 and 3 as the first two terms to the predefined empty list, then for the remaining terms vectorised multiply the list by each of its elements, concatenate the results, remove elements already in the list, and push the minimum.

Iυ

Output the terms.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ @JonathanAllan seems to be the byte count of the literate program, not the SBCS version. \$\endgroup\$
    lyxal
    –  lyxal
    2025-10-09 22:39:57 +00:00
    Commented Oct 9 at 22:39
  • 1
    \$\begingroup\$ @JonathanAllan I'm used to TIO where I can paste the succinct version into the post (and update the byte count) before copying and pasting the post into CGSE, whereas with ATO I have to click the copy button and paste the post, then copy and paste the succinct version, and then also update the byte count. \$\endgroup\$
    Neil
    –  Neil
    2025-10-09 23:45:18 +00:00
    Commented Oct 9 at 23:45
4
\$\begingroup\$

Google Sheets, 109 bytes

=let(f,lambda(f,a,if(rows(a)=A1,a,let(b,sort(tocol(a*torow(a)+1)),f(f,{a;+filter(b,b>max(a))})))),f(f,{2;3}))

A recursive function that expects \$n ≥ 2\$ in cell A1 and outputs the sequence up to that index.

screenshot

Ungolfed:

=let(
  f, lambda(f, a, 
    if(rows(a) >= A1, a, let(
      b, sort(tocol(a * torow(a) + 1)),
      c, single(filter(b, b > max(a))),
      f(f, { a; c })
    ))
  ),
  f(f, { 2; 3 })
)
\$\endgroup\$
4
\$\begingroup\$

Japt, 18 bytes

Outputs the first n terms

@Zï* mÄ kZ rm}h23ì

Try it

@Zï* mÄ kZ rm}h23ì     :Implicit input of integer U
@                      :Function taking an array Z as argument
 Zï                    :  Cartesian product of Z
   *                   :  Reduce each pair by multiplication
     m                 :  Map
      Ä                :    Add 1
        kZ             :  Remove all elements of Z
           r           :  Reduce by
            m          :    Minimum
             }         :End function
              h        :Starting with the following array, run it through the function and push the result until it reaches length U
               23ì     :  23 converted to digit array
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 11 bytes

23SλλãP>λKß

(Lazily) outputs the infinite sequence.

Try it online.

Explanation:

   λ         # Create a recursive environment,
             # to implicitly output the infinite sequence afterwards
23S          # Starting with a(0)=2 and a(1)=3
             # Where every following a(n) is calculated as:
    λ        #  Push the list of all previous values: [a(0),a(1),...]
     ã       #  Take its cartesian product to create all possible pairs
      P      #  Take the product of each inner pair
       >     #  Increase each by 1
        λK   #  Remove all previous values from this list
          ß  #  Then pop and push the minimum
\$\endgroup\$
3
\$\begingroup\$

Python 3.8+, 68 bytes

c={2,3}
s=c-c
while 1:print(x:=min(c-s));s|={x};c|={x*y+1for y in s}

Attempt This Online! (modified to stop)

s holds a set of numbers already output. c holds a set of numbers eligible for the sequence: the initial 2 and 3, and later products of numbers in s plus 1.

c-c is a short way to produce the empty set ({} gives a dict, not a set).

Each iteration of the loop finds the lowest number in c that is not in s, outputs it, adds it to s, and adds to c the products involving that number, plus 1.

\$\endgroup\$
3
\$\begingroup\$

C (clang), 136 bytes

c(*a,*b){return*a-*b;}f(*a,n){int b[n*n],i,k=a[i=0]=2;for(a[1]=3;k<n;++i/k/k?a[k++]=b[wcsspn(b,a)]:qsort(b,i,4,c))b[i]=a[i/k]*a[i%k]+1;}

Try it online!

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

Vyxal 3, 13 bytes

23⎄#¤D▦×f›$⦰g

Vyxal It Online!

23⎄#¤D▦×f›$⦰g­⁡​‎‎⁡⁠⁣‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁤⁡‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁣⁤‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁣⁣‏‏​⁡⁠⁡‌⁢⁢​‎⁠⁠⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌⁢⁣​‎‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁢⁤​‎‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌­
  ⎄            # ‎⁡a generator
23             # ‎⁢with initial values 2,3
            g  # ‎⁣that returns the minimum of
           ⦰   # ‎⁤the set difference
   #¤D    $    # ‎⁢⁡of each previous term
      ▦×f      # ‎⁢⁢and the inner product of
   #¤D         # ‎⁢⁣the list of previous terms, with itself,
         ›     # ‎⁢⁤incremented
💎

Created with the help of Luminespire.

run snippet for 30 first terms, then you can provide an argument to specify how many terms:

<script type="vyxal3">
23⎄#¤D▦×f›$⦰g  ]⊖
</script>
<script>
    args=[["30"]]
</script>
<script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>

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

APL+WIN, 43 bytes

Prompts for the number of terms to display:

n←2 3⋄⍎∊(⎕-2)⍴⊂'n←n,↑(m[⍋m←1+,n∘.×n])~n⋄'⋄n

Try it online! Thanks to Dyalog APL Classic

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

Maple, 82 bytes

proc(n)s:={2,3};to n-2 do s:=s union{min({seq(seq(i*j+1,i=s),j=s)}minus s)}od;end;

Takes n and produces a set containing the sequence. Straightforward code.

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

Husk, 16 bytes

!Ou!¹ƒGS+×o→*d23

Performance-optimized version (outputs up to 700th element without timing out)

16 bytes codegolf (times out for n > 4.)

Inputs \$n\$ = the element's index in the sequence. Outputs the number at the position \$n\$. The short version times out if the input is > 4.

The program constructs an infinite list of (finite) lists starting from the outer product of \$[2,3]\$ with itself, then joins the first list to the result and so on. Note that the original lists are kept in place. This way the sublists can be sorted, which is not the case for an infinite list.

The problem is that the sublists are missing some numbers. For example, in the 5th list the 41st element (94) is followed by 99, which is the 43rd element. To overcome this, I have used an observation that the \$n\$th element of the \$n\$th list is correct.

The program subsets the \$n\$th list, deduplicates and sorts it and outputs its \$n\$th element.

Commented version:
         ƒ           -- recursive function f(f(f(f(...)))) :
                 d23 --  | take list; the initial value is [2,3]
             ×  *    --  | multiply by itself (outer product)  
              o→     --  | add 1 to each number
         GS+         --  | join the right list and the result with a hook and scanl
       !¹            -- take N-th sublist
     Ou              -- deduplicate and sort
    !                -- output its N-th number
\$\endgroup\$
1
\$\begingroup\$

JavaScript (V8), 59 bytes

for(a=[,,1,i=1];;)a[++i]&&a.map((_,j)=>a[i*j+1]=1,print(i))

Try it online!

Slow because it iterates ~2^n items to get first n

or 64B fast

\$\endgroup\$

Your Answer

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.