[Question]
  [
Your task is pretty simple, calculate the n-th element of [A190810](https://oeis.org/A190810).
Elements of A190810 are calculated according to these rules:
1. The first element is 1
2. The sequence is increasing
3. If `x` occurs in the sequence, then `2x+1` and `3x-1` also do
You can use 1-based or 0-based indexing, but if you use 0-based indexing, please say it in the answer.
Test cases
```
a(1) = 1
a(2) = 2
a(3) = 3
a(4) = 5
a(5) = 7
a(10) = 17
a(20) = 50
a(30) = 95
a(55) = 255
```
Since this is code-golf, the shortest answer in bytes wins!
  
[Answer]
# Python 2, ~~88~~ ~~83~~ 72 bytes
You may want to read the programs in this answer in reverse order...
Slower and shorter still, thanks to Dennis:
```
L=1,;exec'L+=2*L[0]+1,3*L[0]-1;L=sorted(set(L))[1:];'*input()
print L[0]
```
[**Try it online**](http://ideone.com/wPYat6)
---
This doesn't run as fast, but is shorter (**83 bytes**.) By sorting and removing duplicates each iteration, as well as removing the first element, I remove the need for an index into the list. The result is simply the first element after `n` iterations.
I may have out-golfed Dennis. :D
```
L=[1]
n=input()
while n:L+=[2*L[0]+1,3*L[0]-1];n-=1;L=sorted(set(L))[1:]
print L[0]
```
[**Try it online**](http://ideone.com/FI4XPA)
---
This version below (**88 bytes**) runs really fast, finding the 500000th element in about two seconds.
It's pretty simple. Compute elements of the list until there are three times more elements than `n`, since every element added may add at most 2 more unique elements. Then remove duplicates, sort, and print the `n`th element (zero-indexed.)
```
L=[1]
i=0
n=input()
while len(L)<3*n:L+=[2*L[i]+1,3*L[i]-1];i+=1
print sorted(set(L))[n]
```
[**Try it online**](http://ideone.com/DdAPnZ)
[Answer]
# [Jelly](http://github.com/DennisMitchell/jelly), 16 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
×3’;Ḥ‘$;
1Ç¡ṢQ³ị
```
*Very* inefficient. [Try it online!](http://jelly.tryitonline.net/#code=w5cz4oCZO-G4pOKAmCQ7CjHDh8Kh4bmiUcKz4buL&input=&args=MTA)
### How it works
```
1Ç¡ṢQ³ị   Main link. Argument: n (integer)
1         Set the return value to 1.
 Ç¡       Execute the helper link n times.
   Ṣ      Sort the resulting array.
    Q     Unique; deduplicate the sorted array.
     ³ị   Retrieve its n-th element.
×3’;Ḥ‘$;  Helper link. Argument: A (array)
×3        Multiply all elements of A by 3.
  ’       Decrement the resulting products.
      $   Combine the two links to the left into a monadic chain.
    Ḥ     Unhalve; multiply all elements of A by 2.
     ‘    Increment the resulting products.
   ;      Concatenate 3A-1 and 2A+1.
       ;  Concatenate the result with A.
```
[Answer]
# Python 2, 59 bytes
```
t={1}
exec'm=min(t);t=t-{m}|{2*m+1,3*m-1};'*input()
print m
```
Based on [@mbomb007's Python answer](https://codegolf.stackexchange.com/a/85414). Test it on [Ideone](http://ideone.com/9Vi8Mq).
[Answer]
## Haskell, ~~76~~ ~~73~~ 69 bytes
```
a#b=mod a b<1&&t(div a b)
t x=x<2||(x-1)#2||(x+1)#3
(filter t[1..]!!)
```
Uses a 0-based index. Usage example: `(filter t[1..]!!) 54` -> `255`.
Instead of building the list by repeatedly inserting `2x+1` and `3x-1` as seen in most other answers, I go through all integers and check if they can reduced to `1` by repeatedly applying `(x-1) / 2` or `(x+1) / 3` if divisible.
[Answer]
# Haskell, ~~77~~ 74 bytes
```
import Data.List
i=insert
f(x:y)=x:f(i(2*x+1)$i(3*x-1)y)
a=(!!)(nub$f[1])
```
This provides a function `a` for the n-th entry. It's zero indexed. Alternatively, `a=nub$f[1]` will create the whole list (lazily).
It's a list-variant of Reinhard Zumkeller's `Set` code.
[Answer]
# Python 2, ~~88~~ 84 bytes
```
g=lambda k:g(k%2*k/2)|g(k%3/2*-~k/3)if k>1else k
f=lambda n,k=1:n and-~f(n-g(k),k+1)
```
Test it on [Ideone](http://ideone.com/6tPaoZ).
[Answer]
# Pyth, ~~20~~ 19 bytes
```
hutS{+G,hyhGt*3hGQ0
```
[Try it online.](http://pyth.herokuapp.com/?code=hutS%7B%2BG%2ChyhGt%2a3hGQ0&input=54&debug=0) [Test suite.](http://pyth.herokuapp.com/?code=hutS%7B%2BG%2ChyhGt%2a3hGQ0&test_suite=1&test_suite_input=0%0A1%0A2%0A3%0A4%0A9%0A19%0A29%0A54&debug=0)
Uses zero-based indexing.
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 45 bytes
```
:1-I,?:?*:1ydo:Im.
1.|:1-:1&I(:3*:1-.;I*:1+.)
```
Computes `N = 1000` in about 6 seconds on my machine.
This is 1-indexed, e.g.
```
run_from_file('code.brachylog',1000,Z).
Z = 13961 .
```
### Explanation
* Main predicate:
```
:1-I,               I = Input - 1
     ?:?*           Square the Input
         :1y        Find the first Input*Input valid outputs of predicate 1
            do      Remove duplicates and order
              :Im.  Output is the Ith element
```
* Predicate 1:
```
1.                  Input = Output = 1
|                   Or
:1-:1&I             I is the output of predicate 1 called with Input - 1 as input
       (            
         :3*:1-.      Output is 3*I-1
       ;            Or
         I*:1+.       Output is 2*I+1
       )
```
You may note that we don't pass any input to predicate 1 when we call `y - Yield`. Because of constraint propagation, it will find the right input once reaching the `1.` clause which will propagate the correct input values.
[Answer]
## 05AB1E, ~~18~~ 17 bytes
Uses [CP-1252](http://www.cp1252.com) encoding.
```
$FÐ·>s3*<)˜Ù}ï{¹è
```
**Explanation**
```
$                  # initialize with 1
 F          }      # input number of times do
  Ð                # triplicate current list/number
   ·>              # double one copy and add 1
     s3*<          # multiply one copy by 3 and subtract 1
         )˜Ù       # combine the 3 lists to 1 list and remove duplicates
             ï{    # convert list to int and sort
               ¹è  # take the element from the list at index input
```
[Try it online for small numbers](http://05ab1e.tryitonline.net/#code=JEbDkMK3PnMzKjwpy5zDmX3Dr3vCucOo&input=OQ)
Very slow.  
Uses 0-based indexing.
[Answer]
## JavaScript (ES6), 63 bytes
```
f=(n,a=[1],i=0)=>a[i++]?--n?f(n,a,a[i*2]=a[i*3-2]=1):i:f(n,a,i)
```
Probably gives up quickly due to the recursion.
[Answer]
# MATL, ~~19, 18~~ 17 bytes
```
1w:"tEQy3*qvSu]G)
```
This is an *extremely* inefficient algorithm. The online interpreter runs out of memory for inputs greater than 13.
*One byte saved, thanks to Luis Mendo!*
[Try it online!](http://matl.tryitonline.net/#code=MXc6InRFUXkzKnF2U3VdRyk&input=MTA)
This version is longer, but more efficient (21 bytes)
```
1`tEQy3*qvSutnG3*<]G)
```
[Try it online](http://matl.tryitonline.net/#code=MWB0RVF5MypxdlN1dG5HMyo8XUcp&input=MjA)
Explanation:
The logical way to do it, is to add elements to the array until it is long enough to grab the i'th element. That's how the efficient one works. The *golfy* (and inefficient) way to do it, is to just increase the array size *i* times.
So first, we define the start array: `1`. Then we swap the top two elements, so that input is on top. `w`. Now, we loop through the input with `:"`. So *i* times:
```
t             %Duplicate our starting (or current) array.
 EQ           %Double it and increment
   y          %Push our starting array again
    3*q       %Multiply by 3 and decrement
       v      %Concatenate these two arrays and the starting array
        Su    %Sort them and remove all duplicate elements.
```
Now, we have a *gigantic* array of the sequence. (Way more than is needed to calculate) So we stop looping, `]`, and grab the i'th number from this array with `G)` (1-indexed)
[Answer]
## Retina, 57
```
^.+
$*¶¶1
¶¶(1(1*))
¶1$1$1¶$2$1$1
O`
}`(¶1+)\1\b
$1
G2`
1
```
[Try it online!](http://retina.tryitonline.net/#code=Xi4rCiQqwrbCtjEKwrbCtigxKDEqKSkKwrYxJDEkMcK2JDIkMSQxCk9gCn1gKMK2MSspXDFcYgokMQpHMmAKMQ&input=OQ)
0-indexed. Follows the frequently used algorithm: remove the minimum value from the current set, call it `x`, and add `2x+1` and `3x-1` to the set a number of times equal to the input, then the leading number is the result. The "set" in Retina is just a list that is repeatedly sorted and made to contain only unique elements. There are some sneaky bits added to the algorithm for golf, which I will explain once I've had some more time.
Big thanks to Martin for golfing off around 20 bytes!
[Answer]
## Clojure, 114 108 bytes
```
#(loop[a(sorted-set 1)n 1](let[x(first a)](if(= n %)x(recur(conj(disj a x)(+(* 2 x)1)(-(* 3 x)1))(inc n)))))
```
I wouldn't be surprised if this could be golfed/reduced by a significant amount but `set`'s not supporting nth really hurt my train of thought.
[Try the online](http://ideone.com/yCxLlV)
Version with spaces:
```
#(loop [a (sorted-set 1)
        n 1]
  (let [x (first a)]
    (if (= n %)
      x
      (recur (conj (disj a x) (+ (* 2 x) 1) (- (* 3 x) 1)) (inc n))
      )))
```
[Answer]
# C++, 102 bytes
```
[](int i){int t;map<int,int>k;for(k[1];i--;k.erase(t))t=k.begin()->first,k[t*2+1],k[t*3-1];return t;};
```
This lambda function requires `#include <map>` and `using std::map`.
The `map` here is just a collection of keys; their values are ignored. I use `map` in order to benefit from the terse code for insertion:
```
k[1]; // inserts the key 1 into the map
```
Thanks to the sorted order of `map`, the smallest element is extracted by `k.begin()->first`.
[Answer]
## Actually, 27 bytes
```
╗1#╜`;;2*1+)3*1@-#++╔S`n╜@E
```
[Try it online!](http://actually.tryitonline.net/#code=4pWXMSPilZxgOzsyKjErKTMqMUAtIysr4pWUU2Bu4pWcQEU&input=NA)
This program uses 0-based indexing. The approach is very brute-force, so don't expect it to work in the online interpreter for larger inputs.
Explanation:
```
╗1#╜`;;2*1+)3*1@-#++╔S`n╜@E
╗                            save input (n) in register 0
 1#                          push [1]
   ╜                         push n
    `;;2*1+)3*1@-#++╔S`n     do the following n times:
     ;;                        make two copies of the list
       2*1+                    apply 2x+1 to each element in one copy
           )3*1@-              and 3x-1 to each element in the other copy
                 #             workaround for a weird list bug
                  ++           append those two lists to the original list
                    ╔S         uniquify and sort
                        ╜@E  get the nth element (0-indexed)
```
[Answer]
# Ruby, 70 bytes
```
->n{a=*1
n.times{a<<a.map{|i|([2*i+1,3*i-1]-a).min||1.0/0}.min}
a[-2]}
```
## Explanation
```
->n{
    # Magical, golfy way of initializing an array. Equivalent to a = [1].
    a=*1
    n.times{
        # Generate the next element in the sequence, by...
        a<<
            # ... finding the minimal term that will appear at some point.
            a.map{|i|
                ([2*i+1,3*i-1]-a).min||1.0/0
            }.min
    }
    # We generated n+1 elements, so we'll take the *second* to last one.
    a[-2]
}
```
[Answer]
## CJam (25 bytes)
```
ri1a1${{_2*)1$3*(}%_&}*$=
```
[Online demo](http://cjam.aditsu.net/#code=ri1a1%24%7B%7B_2*)1%243*(%7D%25_%26%7D*%24%3D&input=10). Note that this uses zero-based indexing.
This uses a similar approach to most: apply the transforms `n` times and then sort and extract the `n`th item. As a nod to efficiency the deduplication is applied inside the loop and the sorting is applied outside the loop.
[Answer]
## [Retina](https://github.com/m-ender/retina), 48 bytes
```
.+
$*
+1`^(((!*)!(!|\3)(?=\3!1))*!)1|\b
!$1
-2`.
```
[Try it online!](http://retina.tryitonline.net/#code=LisKJCoKKzFgXigoKCEqKSEoIXxcMykoPz1cMyExKSkqISkxfFxiCiEkMQotMmAu&input=MTA)
Inspired by [nimi's answer](https://codegolf.stackexchange.com/a/85432/8478) I thought I'd try a different approach for Retina, making use of the regex engine's backtracking to figure out if any given (unary) number is in the sequence or not. It turns out this can be determined with a 27 byte regex, but making use of it costs a few more, but it still ends up shorter than the generative approach.
Here's an alternative 48-byte solution:
```
.+
$*
{`1\b
1!
}T`1``1((!*)!(!|\2)(?=!\2$))*!$
!
```
And using unary I/O we can do 42 bytes, but I'm trying to avoid that and the other Retina answer uses decimal as well:
```
1\b
1!
}T`1``1((!*)!(!|\2)(?=!\2$))*!$
!
1
```
[Answer]
# Perl, ~~173~~ 132 bytes +1 for -n = 133
```
sub c{my$a=pop;return($a==1||($a%2&&c(($a-1)/2))?1:$a%3!=2?0:$a%3==2?c(($a+1)/3):1)}while($#b<$_){$i++;@b=(@b,$i)if c$i}say$b[$_-1];
```
Ungolfed:
```
my @array = ();
my $n = <>;
sub chk {
    my $a = shift;
    return 1 if ($a == 1);
    if ($a % 2 == 0) {
        if ($a % 3 != 2) {
            return 0;
        } else {
            return chk(($a + 1) / 3);
        }
    } else {
        if (chk(($a - 1) / 2) == 0) {
            if ($a % 3 != 2) {
                return 0;
            } else {
                return chk(($a + 1) / 3);
            }
        } else {
            return 1
        }
    }
}
my $i = 1;
while ($#array < $n-1) {
    push(@array,$i) if (chk($i) == 1);
    $i++;
}
print $array[$n];
```
I can probably do better if I thought more about it, but this is what I came up with after just a few minutes. My first time golfing, so this was pretty fun!
Thanks to @Dada and @TùxCräftîñg (and a bunch of minor byte-optimizations) for -40 bytes
[Answer]
# [Factor](https://factorcode.org/), 73 bytes
```
[ { } 1 rot [ { 2 3 } n*v { 1 -1 } v+ union natural-sort unclip ] times ]
```
[Try it online!](https://tio.run/##NU7LjsIwELv3K8x1EYjyWB77ASsuXBCnFYcoGmhEO8lOJpUQ6rd3U2Avlu3xyL4Yq17603F/@N7hRsJUozFaPWEqhq8UX7ylIfoWl8RWneeIIKR6D@JYEek3Edv8EUkzeFHHV3wV@8MO0YpRWxXFCEpR4TgkjcUDM5SYY4ElVvjEGhtsUc7Q9T94oMtH8YqBD6EO/NFmXmJSZtGOkTjvABtNYurJ0JktW7uAM9Q1ecy5b0z4r/VJn71T2JqM9H8 "Factor – Try It Online")
Found a hidden golfing gem `unclip` which, given an array, returns `rest first` on the stack. It is especially helpful that `first` is on top of the stack. Using the `unclip`-ed state as the loop invariant, I could save a byte from `swap` vs. `rot` and six bytes from saving `first` at the end.
```
[                          ! anonymous lambda
  { } 1 rot [ ... ] times  ! setup stack as ( rest min ) and call n times...
    { 2 3 } n*v            !   ( rest {2*min,3*min} )
    { 1 -1 } v+            !   ( rest {2*min+1,3*min-1} )
    union natural-sort     !   set union of rest and next two numbers, sorted
    unclip                 !   extract first item (= next min)
]
```
[Answer]
# J, 31 bytes
```
{1(]]/:~@~.@,3&*,&:<:2*>:)^:[~]
```
Uses zero-based indexing. Very memory-inefficient.
## Explanation
```
{1(]]/:~@~.@,3&*,&:<:2*>:)^:[~]  Input: n
                              ]  Identity function, gets n
 1                               The constant 1
  (                      )^:[~   Repeat n times with an initial array a = [1]
                       >:          Increment each in a
                     2*            Multiply by 2 to get 2a+2
             3&*                   Multiply each in a by 3 to get 3a
                 &:<:              Decrement both x and y to get 2a+1 and 3a-1
                ,                  Join them
    ]                              Identity function, gets a
            ,                      Join a with 2a+1 and 3a-1
         ~.@                       Take the distinct values
     /:~@                          Sort up
   ]                               Return the sorted list
{                                Select the value from the list at index n and return it
```
[Answer]
# Octave, 68 bytes
```
function r=a(n)s=1;for(i=1:n)r=s(i);s=union(s,[r*2+1 r*3-1]);end;end
```
[Answer]
# JavaScript (ES6), 58
```
n=>(a=>{for(;n;)a[++i]?a[i-~i]=a[3*i-1]=--n:0})([i=0,1])|i
```
*Less golfed*
```
n=>{
  a=[];
  a[1] = 1;
  for(i = 0; n;)
  {
    ++i
    if (a[i])
    {
      a[2*i+1] = 1;
      a[3*i-1] = 1;
      --n;
    }
  }
  return i
}
```
*Test*
About time and memory: element 500000 in ~20 sec and 300MB used by my FireFox 64 bit alpha
```
F=
n=>(a=>{for(;n;)a[++i]?a[i-~i]=a[3*i-1]=--n:0})([i=0,1])|i
function test() {
  var n=+I.value, t0=+new Date
  O.textContent = F(n)
  console.log((+new Date-t0)/1000,'sec')
}  
test()
```
```
#I { width:5em}
```
```
<input id=I type=number value=10 oninput="test()"> 
<span id=O></span>
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), 17 bytes
```
!OuΞ¡ṁ§eo←*3o→D;1
```
[Try it online!](https://tio.run/##yygtzv7/X9G/9Ny8Qwsf7mw8tDw1/1HbBC1jIDnJxdrw////RgYA "Husk – Try It Online")
There should be a shorter way using set difference.
This answer creates all the possibilities, merges them and deduplicates.
[Answer]
# [Perl 5](https://www.perl.org/) `-p`, 82 bytes
```
$;{1}++;map@;{$_*2+1,$_*3-1}=1,keys%;while$_*3>keys%;;$_=(sort{$a-$b}keys%;)[$_-1]
```
[Try it online!](https://tio.run/##JYlRCgIhFEX/3zoMKsfwaa8fmWgDrSBCJhAaspRxIELcemYMXDicc6ObPNXKTMbCuXkO8WQys1vFsWvQAkuP3cN90sq876N3/3hc3DDbr1OY5swGwW5lqZsLswKvtSIo0LAHApSgJGgJ1IZACkgDtYeADt8Q5zG8UhVn2kmUVUT/Aw "Perl 5 – Try It Online")
[Answer]
# TI-Basic, 74 bytes
```
Input N
{1→A
For(I,1,N
1+2ʟA(I
If min(Ans≠ʟA
augment(ʟA,{Ans→A
3ʟA(I)-1
If min(Ans≠ʟA
augment(ʟA,{Ans→A
End
SortA(ʟA
ʟA(N
```
Uses 1-based indexing. Output is stored in `Ans` and is displayed.
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 37 bytes
```
Nest[Union[#,2#+1,3#-1]&,{1},#][[#]]&
```
[Try it online!](https://tio.run/##DcgxCoAwDADArwQDXUzB6qyTs7g4hQxFFDO0Be1WfHv1xgs@X0fwWXdfTxjrcjyZt6gpMlKPraMBrRNDxb2Ewowips6J11tjZiVowE7QEJysIgTlr757pX4 "Wolfram Language (Mathematica) – Try It Online")
]