Explore a Klarner-Rado sequence

6

1

One of the Klarner-Rado sequences is defined as follows:

  • the first term is \$1\$
  • for all subsequent terms, the following rule applies: if \$x\$ is present, so are \$2x+1\$ and \$3x+1\$
  • the sequence is strictly increasing

This is A002977.

The first few terms are:

1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...

(\$3\$ is present because it's \$2\times1+1\$, \$4\$ is present because it's \$3\times1+1\$, \$7\$ is present because it's \$2\times3+1\$, etc.)

Your task

Given \$n\$, you must return the \$n\$th element of the sequence. You may use either 0-based or 1-based indexing. (Please specify your choice in your answer.)

0-indexed example:

input = 10
output = 22

Let's see who can get less bytes...

Also featured on codewars

maryrio7

Posted 2019-10-11T11:33:57.123

Reputation: 73

Question was closed 2019-10-11T20:10:48.087

Why is this tagged [javascript]? Is this restricted to it? How is this related to array manipulation and linear algebra? Does "ordered with <" mean "ascending"? – my pronoun is monicareinstate – 2019-10-11T11:46:30.113

Apart from the arbitrary language restriction there isn't any problem. – user202729 – 2019-10-11T11:59:45.473

https://codegolf.meta.stackexchange.com/a/8058/69850 -- you can "look at the challenge as a separate competition in each language". – user202729 – 2019-10-11T12:02:38.607

@Arnauld and what's with that? I solved the problem but I want to see community's inventions – maryrio7 – 2019-10-11T12:29:50.220

5@user89702 It's not yours, so you can't post it, especially without any attribution. – my pronoun is monicareinstate – 2019-10-11T12:30:22.203

Why so strict? Is just a challenge, if you don't want to participate just ignore this post, let people have fun – maryrio7 – 2019-10-11T12:31:36.980

2

However, I do believe this is a rather interesting challenge that would be worth rephrasing properly for Code Golf. There's nothing wrong on posting a challenge about this sequence (which is A002977, btw), but you can't copy from an external site without any authorization or credits.

– Arnauld – 2019-10-11T12:38:45.437

I want to agree with @user89702. Two of my top posts are not my challenge, but they get upvotes. – HighlyRadioactive – 2019-10-11T12:42:56.993

1I've rephrased your post so that it better fits our standard and is not a copy of codewars anymore. Feel free to edit further if needed. – Arnauld – 2019-10-11T13:36:56.840

closely related – nimi – 2019-10-11T14:09:17.957

Currently this needs to mention that this sequence is in ascending order. Without it the only term that we can know the location of is the first one. – Post Rock Garf Hunter – 2019-10-11T18:30:29.127

I upvoted & closed as a duplicate (even though slightly different). This could get reopened if people think the offset difference is enough to make answers not trivially portable. – Jonathan Allan – 2019-10-11T20:17:52.920

How do we define duplicate? This question: If x occurs in the sequence, then 2x+1 and 3x+1 also exists. Claimed duplicate:If x occurs in the sequence, then 2x+1 and 3x-1 also exists. Are the two definitions sufficiently close to count as a duplicate. Rules Committee???? – Graham – 2019-10-11T20:50:51.187

I made the comment above as it is exceedingly frustrating to spend the time to answer a question only to find some considerable time later it is claimed to be a duplicate. Personally I do not have the time or inclination to validate every question before answering it. – Graham – 2019-10-11T21:07:43.247

2@Graham, if you've taken the time to work up a solution then absolutely post it to the dupe target. If your solution cannot be trivially modified to fit the dupe target then you have a case to reopen this one and you should cast your vote accordingly. – Shaggy – 2019-10-12T00:38:36.177

Answers

3

05AB1E, 15 14 bytes

$FDx>DŠ+)˜ê}sè

Try it online!

Grimmy

Posted 2019-10-11T11:33:57.123

Reputation: 12 521

Times out for me on TIO for inputs of 14 and above – Graham – 2019-10-11T16:29:40.470

@Graham Well this is code golf, not fastest code, so that’s not an issue. – Grimmy – 2019-10-11T17:18:17.490

@Graham, for the purposes of code golf, we may assume infinite memory & time. – Shaggy – 2019-10-11T18:28:13.893

@Grimy Thanks to you and Shaggy for this information which I had not appreciated. All solutions I have offered in the past have worked efficiently within the capabilities of my machine. This opens up some wacky impractical solutions ;) – Graham – 2019-10-11T18:40:16.427

2

JavaScript (ES6),  53  50 bytes

1-indexed.

n=>(g=k=>n?g(g[k]|!k++?g[n--,k*2]=g[k*3]=k:k):k)``

Try it online!

Commented

Note: On the first iteration, we have k = [''], which is zero-ish but truthy. By doing !k++, we force k to be coerced to \$0\$ right away (and not just before it's incremented), which makes the test work as expected.

n => (                    // n = requested index
  g = k =>                // h is a recursive function taking k, starting at 0
    n ?                   // if n is not equal to 0:
      g(                  //   do a recursive call:
        g[k] |            //     if g[k] is defined
        !k++ ?            //     or k = 0 (increment k after the test):
          g[n--, k * 2] = //       decrement n; set g[k * 2]
          g[k * 3] = k    //       and g[k * 3] and pass k
        :                 //     else:
          k               //       just pass k
      )                   //   end of recursive call
    :                     // else:
      k                   //   stop recursion and return k
)``                       // initial call to g with k = [''] (zero-ish)

JavaScript (ES6), 65 bytes

I thought the '11'[k/x-2] trick was neat, but overall this initial approach is far too long.

0-indexed.

n=>(g=k=>a[n]||g(-~k,a.some(x=>'11'[k/x-2])&&a.push(k+1)))(a=[1])

Try it online!

Commented

n => (                  // n = requested index
  g = k =>              // g is a recursive function taking k (starting at 1)
    a[n] ||             // if a[n] is defined, return it and stop
    g(                  // otherwise, do a recursive call:
      -~k,              //   with k + 1
      a.some(x =>       //   if there exists some x in a[]
        '11'[k / x - 2] //   such that k / x is either 2 or 3 ...
      )                 //
      && a.push(k + 1)  //   ... then push k + 1 in a[]
    )                   // end of recursive call
)(a = [1])              // initial call to g with k = a = [1]

Arnauld

Posted 2019-10-11T11:33:57.123

Reputation: 111 334

1

R, 57 bytes

n=scan();for(i in 1:n)T=sort(unique(c(T,T%o%2:3+1)));T[n]

Try it online!

1-based index.

Giuseppe

Posted 2019-10-11T11:33:57.123

Reputation: 21 077

1

APL+WIN, 54 bytes

Index origin 1

Prompts for the index of required value. Given the response that I received from Grimy and Shaggy that I can assume infinite memory and time then it is trivial to modify this function to work with a series up to the limit of your machine by increasing the value 20 in the function below. This version is limited to index position 1901981

m←1⋄(⍎∊20⍴⊂'m←(0≠1,-2-/m)/m←m[⍋m←m,∊1+mר⊂2 3]⋄')⋄m[⎕]

For some reason this function will not run on Dyalog Classic in TIO which is what I usually use.

On my machine 1 => 1, 11 => 22 and 61 => 237 (checked on http://oeis.org)

Graham

Posted 2019-10-11T11:33:57.123

Reputation: 3 184

0

JavaScript (ES6), 99 bytes

x=>eval("a=[i=1],a.b=a.includes;while(i++,c=i-1,x)a.b(i)||!a.b(c/2)&&!a.b(c/3)||(a.push(i),x--);c")

Naruyoko

Posted 2019-10-11T11:33:57.123

Reputation: 459

1

I think this works for 96 bytes: Try it online!

– my pronoun is monicareinstate – 2019-10-11T13:14:05.017

Ah, type casting is fun. – Naruyoko – 2019-10-11T13:16:04.013

0

Python 3, 73 bytes

f=lambda n,i=0,k=[1]:(i==n)*k[i]or f(n,i+1,sorted(k+[2*k[i]+1,3*k[i]+1]))

Try it online!

Jitse

Posted 2019-10-11T11:33:57.123

Reputation: 3 566