g | x | w | all
Bytes Lang Time Link
029YASEPL240213T163311Zmadeforl
456jBasher2250909T151213Zmadeforl
043PowerShell Core250523T203453ZJulian
016TIBASIC TI83 Plus250523T161802Zmadeforl
022Labyrinth240213T095830ZBubbler
018Arturo230630T184604Zchunes
004Thunno 2230630T160605ZThe Thon
005Vyxal220424T023813ZnaffetS
011Pip211231T194718ZDLosc
043Python 3180122T100830ZManish K
036Python 2180122T103235ZNeil
061Ruby180219T084337ZNetherwi
030JavaScript ES7180122T104344ZShaggy
013Jelly180206T145225Zsporkl
044JavaScript Node.js180206T082603ZShieru A
009J180122T124358ZGalen Iv
006Japt180122T094640ZShaggy
004Jelly180122T095359Zuser2027
064Scala180126T104417Z6infinit
005Jelly180126T000014Zellie
049Dart180125T134836Zlrn
061clojure180125T070111Zuser8420
020Octave180124T084028ZTom Carp
030Ruby180122T095356ZAsone Tu
046C180123T114541ZToby Spe
005Pyt180124T021206Zmudkip20
033Python 2180123T224557Zxnor
nanPerl 5180123T162440ZXcali
021R180122T132103ZGiuseppe
nan180123T010812ZBrad Gil
014Actually180123T002334Zuser4594
036Python 2180122T100915ZRod
005MATL180122T093258ZLuis Men
061Clean180122T210534ZΟurous
026Ruby180122T193031ZMegaTom
014Julia 0.6180122T134454ZLukeS
036BrainFlak180122T163956ZMegaTom
034Haskell180122T095519Zoktupol
052C180122T165927ZAsone Tu
027Ruby180122T153248ZEric Dum
064SNOBOL4 CSNOBOL4180122T141230ZGiuseppe
052Java 8180122T100339ZKevin Cr
059C180122T122923ZMinersca
007APL Dyalog180122T104428ZUriel
040brainfuck180122T111851ZJo King
033Haskell180122T111934ZCristian
223><>180122T130108ZTeal pel
067Red180122T113022ZGalen Iv
241Perl v5.10 n180122T104814ZUnihedro
052C180122T122233ZSteadybo
00405AB1E180122T094204ZEmigna
031AWK a=0180122T115844ZUnihedro
011APL Dyalog Unicode180122T113937ZAdá
nanRuby n180122T100506ZUnihedro
007MATL180122T110909ZStewie G
052Haskell180122T105824Ztotallyh
053Python 3180122T103239ZAsone Tu
007Husk180122T095501ZMartin E
028Retina180122T093930ZMartin E

YASEPL, 39 29 bytes

=n=a'(`1!b$2^n-*/3+.9(<!+}2,a

explanation:

=n=a'(`1!b$2^n-*/3+.9(<!+}2,a                 packed
=n                                            declare increment 
  =a'(                                        get user input and make it integer
      `1               ! }2,a                 while n < a...
        !b$2^n-*/3                                b = 2*(2^n-1)/3
                  +.9(                            b = ceil(b)
                      <                           print b
                       !+                         add one to n and check loop

jBasher2, 456 bytes

create n with type number
create a with type number
create l with type number
create i with type number
create s with type number
ask for input
set that to a
set 1 to n
while n < a
set 1 to l
set 1 to i
while i < n
multiply l by 2
set that to l
add 1 by i
set that to i
endwhile
subtract l by 1
multiply that by 2
divide that by 3
set that to l
parse l as int
set that to s
if s < l
add 1 by s
set that to s
endif
output s
add n by 1
set that to n
endwhile

PowerShell Core, 43 bytes

for($n=0;$i++-le"$args";$n+=$n+!($n%2)){$n}

Try it online!

TI-BASIC (TI-83 Plus), 16 bytes

Input N
int(seq(2^K/3,K,1,N

1-indexed.

Labyrinth, 22 bytes

1?+:_
 }: 3
 " !/
@({\

Try it online!

Uses the "power of 2 divided by 3" formula.

Intro: 1?
1      Push 1 [1]
?      Input  [1 n]

Loop:  }:+:_3/!\{(  [2^i n-i] -> [2^(i+1) n-(i+1)]
}      Backup top   [2^i | n-i]
:+     Double       [2^(i+1) | n-i]
:      Dup          [2^(i+1) 2^(i+1) | n-i]
_3/    Divide by 3  [2^(i+1) a_(i+1) | n-i]
!\     Print with newline [2^(i+1) | n-i]
{(     Pull and decrement [2^(i+1) n-(i+1)]

Exit:  @ (Halt)

Arturo, 18 bytes

$=>[map&=>[/2^&3]]

Try it!

Port of The Thonnu's Thunno 2 answer.

Thunno 2, 4 bytes

RO3÷

Attempt This Online!

Port of Neil's Python answer.

Explanation

RO3÷  # Implicit input       ->  5
R     # Push [1..input]      ->  [1,2,3,4,5]
 O    # Two power of each    ->  [2,4,8,16,32]
  3÷  # Integer divide by 3  ->  [0,1,2,5,10]
      # Implicit output

Vyxal, 5 bytes

ʁEd3ḭ

Try it Online!

Port of Neil's Python answer.

How?

ʁEd3ḭ
ʁ     # Exclusive zero range of (implicit) input
 E    # Square, implicit vectorization
  d   # Double, implicit vectorization
   3ḭ # Floor divide by three, implicit vectorization again

Pip, 11 bytes

Lai+:i+!%Pi

Attempt This Online!

Explanation

We can compute this sequence without using binary via the following recurrence relation:

\$ a_{n+1} = \left\{ \begin{array}{ll} 2 a_n + 1 & \text{ if } a_n \text{ even,} \\ 2 a_n & \text{ if } a_n \text{ odd.} \end{array} \right. \$

Lai+:i+!%Pi
             i (preset to 0) represents the current item in the sequence
 a           First command-line input
L            Loop that many times:
         Pi    Print i
        %      Take i mod 2
       !       Logically negate (1 -> 0 and 0 -> 1)
     i+        Plus current value of i
  i+:          Add that whole expression to i in-place to get next value in sequence

Python 3, 68 61 54 48 43 bytes

c=lambda x,r=0:x and[r]+c(x-1,2*r+~r%2)or[]  

Thanks to user202729 for helping save 19 bytes and ovs for helping save 6 bytes.

Try It Online

Python 2, 36 bytes

lambda n:[2**i*2/3for i in range(n)]

Try it online! Explanation: The binary representation of \$\frac{2}3\$ is 0.101010101... so it simply remains to multiply it by an appropriate power of 2 and take the integer portion.

Ruby, 72 68 61 bytes

->n{a=[0]*n;n.times{|i|i.times{|j|a[i]|=1<<j if i%2!=j%2}};a}

Explained:

def f(n)
  a = [0] * n
  n.times do |i|
    i.times do |j|
      if i.even? != j.even?
        a[i] |= (1 << j)
      end
    end
  end
  a
end

This approach uses n'th bit installation using x | (1 << n). We start from the last bit and proceeding to the first, setting each 2'nd, alternating ones and zeros 'even?' check tells where to start.

Try Now!

I am new in both code golf and Ruby, so any comments will be appreciated!

JavaScript (ES7), 39 35 31 30 bytes

1-indexed with output in reverse order.

f=n=>n?[2**n/3|0,...f(--n)]:[]

Try it

o.innerText=(
f=n=>n?[2**n/3|0,...f(--n)]:[]
)(i.value=8);oninput=_=>o.innerText=f(+i.value)
<input id=i type=number><pre id=o>


35 byte version, without recursion

n=>[...Array(n)].map(_=>2**n--/3|0)

o.innerText=(f=
n=>[...Array(n)].map(_=>2**n--/3|0)
)(i.value=8);oninput=_=>o.innerText=f(+i.value)
<input id=i type=number><pre id=o>

Jelly, 13 bytes

Rṁ@⁾10VDḄ
ḶÇ€

Try it online!

JavaScript (Node.js), 44 bytes

In ascending order. Simple recursion. 1-indexed.

f=(n,i=0,a=[])=>n?f(n-1,~i&1+i*2,[...a,i]):a

Try it online!

JavaScript (Node.js), 43 41 38 35 bytes

... or return as string. Still in ascending order. 0-indexed.

f=(n,i=0)=>n?i+[,f(n-1,~i&1+i*2)]:i

Try it online!

JavaScript (Node.js), 40 bytes

In ascending order. 2**n/3 trick. 1-indexed.

n=>Array(n).fill(i=0).map(_=>2**++i/3|0)

Try it online!

J, 9 bytes

[:#.\2|i.

How it works?

i. - list 0..n-1

2| - the list items mod 2

\ - all prefixes

#. - to decimal

[: - caps the fork (as I have even number (4) of verbs)

Try it online!

Japt, 10 9 7 6 bytes

All derived independently from other solutions.

1-indexed.

õ!²mz3

Try it


Explanation

õ        :[1,input]
 !²      :Raise 2 to the power of each
   m     :Map
    z3   :Floor divide by 3

Try it


7 byte version

õ_ou ì2

Try it

õ            :[1,input]
 _           :Pass each through a function
   o         :[0,current element)
    u        :Modulo 2 on above
      ì2     :Convert above from base-2 array to base-10

9 byte version

õ_îA¤w)n2

Try it

õ            :[1,input]
 _           :Pass each through a function
   A         :10
    ¤        :Convert to binary
     w       :Reverse
  î          :Repeat the above until it's length equals the current element
      )      :Close nested methods
       n2    :Convert from binary to base-10

Jelly, ... 4 bytes

Thanks miles for -1 byte!

ḶḂḄƤ

Try it online!

Explanation:

owered range, or Unength. Get [0, 1, 2, 3, ..., n-1]
 Ḃ    it. Get the last bit of each number. [0, 1, 0, 1, ...]
   Ƥ  for each Ƥrefixes [0], [0, 1], [0, 1, 0], [0, 1, 0, 1], ...
  Ḅ   convert it from inary to integer.

Jelly, 4 bytes

Jonathan Allan's version.

Ḷ€ḂḄ

Try it online!

owered range, or Unength.
 €    Apply for each. Automatically convert the number n
      to the range [1,2,..,n]. Get [[0],[0,1],[0,1,2],..].
  Ḃ   it. Get the last bit from each number.
      Current value: [[0],[0,1],[0,1,0],..]
   Ḅ  Convert each list from inary to integer.

A version based on Neil's 2/3 trick gives 5 bytes, see revision history.

Scala, 64 bytes

val f=(n:Int)=>Stream from 1 map(i=>(1<<i)/3)take n mkString " "

1-indexed. A call to f(7) for example would return 0 1 2 5 10 21 42.

Jelly, 5 bytes

R2*:3

Try it online!

Took Emigna's strategy and ported it to Jelly.

Dart, 49 bytes

f(n,{a:0})=>new List.generate(n,(x)=>a=a<<1|x&1);

Use as

main() {
 print(f(31));
}

See DartPad

clojure, 61 bytes

(fn f[n r](if(> n 0)(cons r(f(- n 1)(+ r r 1(-(mod r 2)))))))

Usage:

user> (f 10 0)
(0 1 2 5 10 21 42 85 170 341)

Octave, 20 bytes

@(x)fix(2.^(1:x)./3)

Try it online!

Using @Neils Python method (+1 to him) saves a heck of a lot of bytes.


Previous answer (independent creation):

Octave, 49 40 bytes

@(n)arrayfun(@(x)sum(2.^(x-1:-2:0)),0:n)

Try it online!

Basically for each value x in 0:n where n is the input (0-indexed), we take a range of x-1:-2:0, and raise 2 to the power of each element in the range. The range results in alternating powers of 2, starting with an empty array [] for 0, then [],[1] for 0:1, then [],[1],[1 4] for 0:2, and so on.

If we then sum each of the produced alternating powers of two, we end up with the required sequence. This only works because in Octave the sum of an empty array is 0, so we can produce the first number 0 by producing no powers of two.

The resulting array, which contains all numbers in the pattern up to and including n is then returned.

Ruby 42 41 43 41 37 35 31 33 30 bytes

-2 bytes thanks to Unihedron

-3 bytes thanks to G B

->x{a=0;x.times{a-=~a+p(a)%2}}

Try it online!

C, 47 46 bytes

a;f(n){for(a=0;n--;a+=a-~a%2)printf("%d ",a);}

The accumulator a begins with zero. At each step, we double it (a+=a) and add one if the previous least-significant bit was zero (!(a%2), or equivalently, -(~a)%2).

Test program

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    while (*++argv) {
        f(atoi(*argv));
        puts("");
    }
}

Results

$ ./153783 1 2 3 4 5 6
0 
0 1 
0 1 2 
0 1 2 5 
0 1 2 5 10 
0 1 2 5 10 21 

Pyt, 5 bytes

1←ř«₃

Explanation:

1        Pushes 1
 ←       Gets input
  ř      Pushes [1,2,...,input]
   «     Bit-shift 1 to the left by each element in the array
    ₃    Python 2-style division by 3 (2^k/3)

Try it online!

Python 2, 33 bytes

s=2
exec"print s/3;s*=2;"*input()

Try it online!


Python 2, 34 bytes

f=lambda n:n*[f]and[2**n/3]+f(n-1)

Try it online!

Returns in reverse order.

Perl 5, 44 + 2 (-pa) = 46 bytes

$\+=(length sprintf'%b',$_)/$F[0]while$_--}{

Try it online!

R, 21 bytes

cat(2^(1:scan())%/%3)

Try it online!

Based on the same algorithm as many here. 1-indexed.

R, 37 bytes

for(i in 0:scan())cat(F<-2*F+i%%2,"")

Try it online!

0-indexed. Doubling and adding n mod 2 at each iteration yields the correct result. F is initialized to zero.

Perl 6,  35 30 27 25  20 bytes

{[\~](0,+!*...*)[^$_]».&{:2(~$_)}}

Try it (35)

{(0,{$_*2+|($+^=1)}…*)[^$_]}

Try it (30)

{(⅓X*(2,4,8…2**$_))».Int}

Try it (30)

{(⅔,* *2…*)[^$_]».Int}

Try it (27)

{((2 X**1..$_)X/3)».Int}

Try it (25)

{(2 X**1..$_)Xdiv 3}

Try it (20)

Expanded:

{
 (
  2                  # 2
    X**              # cross to the power of
       1..$_         # Range from 1 to the input (inclusive)
            )

             Xdiv    # cross using integer divide
                  3  # by 3
}

Actually, 14 bytes

r⌠;"10"*H2@¿⌡M

Try it online!

Explanation:

r⌠;"10"*H2@¿⌡M
r               range(0, input)
 ⌠;"10"*H2@¿⌡M  map (for n in range):
   "10"*          repeat "10" n times
  ;     H         first n characters
         2@¿      interpret as binary integer

Python 2, 45 37 36 bytes

-3 bytes thanks to user202729
-1 byte thanks to mathmandan

s=0
exec"print s;s+=s+~s%2;"*input()

Try it online!

MATL, 5 bytes

:WI/k

Based on Neil's answer.

Explanation

:       % Implicit input, n. Push range [1 2 ... n]
W       % 2 raised to that, element-wise. Gives [2 4 ...2^n] 
I       % Push 3
/       % Divide, element-wise
k       % Round down, element-wise. Implicit display

Try it online!


MATL, 9 bytes

:q"@:oXBs

Try it online!

Explanation

:       % Implicit input n. Range [1 2 ... n]
q       % Subtract 1, element-wise: gives [0 1 ... n-1]
"       % For each k in [0 1 ... n-1]
  @     %   Push k
  :     %   Range [1 2 ... k]
  o     %   Modulo 2, element-wise: gives [1 0 1 ...]
  XB    %   Convert from binary to decimal
  s     %   Sum. This is needed for k=0, to transform the empty array into 0
        % Implicit end. Implicit display

Clean, 61 bytes

import StdEnv
$i=[sum[2^(n-p)\\p<-[1..n]|isOdd p]\\n<-[0..i]]

Try it online!

Ruby, 26 bytes

->n{(1..n).map{|i|2**i/3}}

Try it online!

Beats all the older ruby answers.

Explanation

1/3 in binary looks like 0.01010101..., so If you multiply it by powers of two, you get:

n| 2^n/3
-+---------
1|0.1010101...
2|01.010101...
3|010.10101...
4|0101.0101...
5|01010.101...
6|010101.01...

But Ruby floors the numbers on int division, giving me the sequence I need.

Julia 0.6, 15 14 bytes

!n=2.^(1:n)÷3

Try it online!

Using the 2/3 method. ÷ does integer division in Julia and . is element-wise function application.

-1 Byte thanks to Dennis.

Brain-Flak, 36 bytes

{([()]{}<((({}<>)<>){}([{}]()))>)}<>

Try it online!

Explanation:

The next number in the sequence is obtained by n*2+1 or n*2+0.

{([()]{}< Loop input times
  (
   (({}<>)<>){} Copy n to other stack; n*2
   ([{}]())  i = 1-i
  ) push n*2 + i
>)} End loop
<> Output other stack

Haskell, 47 40 53 49 44 40 34 bytes

-4 bytes thanks to user202729
-6 bytes thanks to Laikoni

(`take`l)
l=0:[2*a+1-a`mod`2|a<-l]

Try it online!

C 52 bytes

i,a;f(n){for(;i++<n;){printf("%d ",a);a=a*2+1-a%2;}}

Try it online!

Without error messages (61 bytes):

int i,a;void f(n){for(;i++<n;){printf("%d ",a);a=a*2+1-a%2;}}

Try it online!

Ruby, 27 bytes

->n{n.times{|i|p 2**i*2/3}}

Try it online!

It's just a Ruby port of this awesome Python answer.

SNOBOL4 (CSNOBOL4), 64 bytes

	N =INPUT
I	X =LT(X,N) X + 1	:F(END)
	OUTPUT =2 ^ X / 3	:(I)
END

Try it online!

1-indexed. Uses the 2^i/3 method.

Java 8, 115 81 80 52 bytes

n->{for(int i=2;n-->0;i*=2)System.out.println(i/3);}

Port of @Neil's Python 2 answer.
1-indexed and outputted directly, each value on a separated line.

Explanation:

Try it online.

n->{                           // Method with integer parameter and no return-type
  for(int i=2;                 //  Start integer `i` at 2
      n-->0;                   //  Loop `n` times:
      i*=2)                    //    Multiply `i` by 2 after every iteration
    System.out.println(i/3);}  //   Print `i` integer-divided by 3 and a new-line

Old 80 bytes answer:

n->{String t="",r=t;for(Long i=0L;i<n;)r+=i.parseLong(t+=i++%2,2)+" ";return r;}

1-indexed input and space-delimited String output

Explanation:

Try it online.

n->{                             // Method with integer parameter and String return-type
  String t="",r=t;               //  Temp and result-Strings, both starting empty
  for(Long i=0L;i<n;)            //  Loop from 0 to `n` (exclusive)
    r+=                          //   Append the result-String with:
       i.parseLong(        ,2);  //    Binary to integer conversion
                   t+=           //     append the temp-String with:
                      i  %2      //      current index `i` modulo-2
                       ++        //      and increase `i` by one afterwards
       +" ";                     //    + a space
  return r;}                     //  Return the result-String

C, 81 55 59 bytes

1 indexed.

i,j;f(c){for(i=j=0;i<c;)printf("%d ",i++&1?j+=j+1:(j+=j));}

Full program, less golfed:

i;j;main(c,v)char**v;{c=atoi(*++v);for(;i<c;i++)printf("%d ",i&1?j+=j+1:(j+=j));}

Try it online!

EDIT 2: I was under the assumption that functions didn't need to be reusable now that I think of it, it makes perfect sense that they would have to be reusable :P

EDIT: I was under the misconception that I had to include the entire program in the answer, turns out I only needed the function that does it. That's nice.

I'm decently sure I can shave off a few bytes here and there. I've already employed a few tricks. A large chunk of the program is dedicated to getting the argument and turning it into an int. This is my first code golf. If I'm doing anything wrong tell me :P

APL (Dyalog), 7 bytes

⌊3÷⍨2*⍳

Try it online!


APL (Dyalog), 11 bytes

(2⊥2|⍳)¨1+⍳

Try it online!

Uses ⎕IO←0.

brainfuck, 40 bytes

,[>.>>[>]<[.->[>]+[<]+<]+<[[-<+>]>-<]<-]

Try it online!

0-indexed. Input as char code, output as unary with null bytes separating series of char code 1s. Assumes 8-bit cells unless you want to input over 255. Assumes negative cells, though this could be fixed at the expense of several bytes.

Previously, 50 bytes

,[[<]>->>[<-<->>>>-<]<[->>++<<]>>+[-<<+>>]<<.<<+>]

Try it online!

Inputs as char code, outputs as char code. 1-indexed. Probably could be golfed a little.

@Unihedron points out I forgot to specify that this needs infinite sized cells, otherwise it tops out at the 8th number.

Haskell, 33 bytes

l=1:0:l
($scanl((+).(2*))0l).take

Try it online!

><>, 22 + 3 (-v flag) bytes

0:nao::1+2%++$1-:?!;$!

Try it online!

Explanation

The stack gets initialized with the loop counter.

0:nao                  : Push 0 to the stack, duplicate and print with a new line.
                         [7] -> [7, 0]
     ::1+              : Duplicate the stack top twice more then add 1 to it.
                         [7, 0] -> [7, 0, 0, 1]
         2%++          : Mod the stack top by 2 then add all values on the stack bar the loop counter.
                         [7, 0, 0, 1] -> [7, 1]
             $1-:?!;$! : Swap the loop counter to the top, minus 1 from it and check if zero, if zero stop the program else continue.

Red, 71 67 bytes

f: func[n][d: 0 loop n - 1[print d d: d * 2 + either odd? d[0][1]]]

1-indexed

Try it online!

And here's the Red impementation of Neil's 2/3 trick:

Red, 51 bytes

f: func[n][repeat i n[print to-integer 2 ** i / 3]]

Try it online!

Perl v5.10 -n, 24+1 bytes

-3 bytes thanks to Nahuel Fouilleul!

say$v=$v*2|$|--while$_--

Try it online!

Same logic as my Ruby version, but shorter because perl is more concise. For some odd reason, print wouldn't do a seperator (dammit!), so I had to use say from v5.10; in order for this to run, I'm not sure how to score this, so I'm leaving it out for now?...

Explanation

say    # Like shouting, but milder.
  $v = $v*2 | $|-- # Next element is this element times 2 bitwise-OR
                   # with alternating 0 1 0 1..., so 0b0, 0b1, 0b10, 0b101...
                   # $. is OUTPUT_AUTOFLUSH, which is initially 0 and
                   #   setting all non-zero values seem to be treated as 1
  while $_-- # do for [input] times

C, 52 bytes

i,k;f(n){for(i=k=0;i<n;k=++i%2+2*k)printf("%d ",k);}

1-indexed

Try it online!

05AB1E, 4 bytes

2 bytes saved using Neil's 2/3 trick

Lo3÷

Try it online!

Explanation

L      # push range [1 ... input]
 o     # raise 2 to the power of each
  3÷   # integer division of each by 3

05AB1E, 6 bytes

TRI∍ηC

Try it online!

Explanation

T        # push 10
 R       # reverse it
  I∍     # extend to the lenght of the input
    η    # compute prefixes
     C   # convert each from base-2 to base-10

AWK a=0, 31 bytes

{for(;$1--;a=a*2+1-a%2)print a}

Try it online!

Uses the formula shamelessly stolen from this other Ruby answer.

While not having a=0 would work (awk treats "empty" as 0), the first element of 0 won't get printed and instead be an empty line, which while I would argue is a valid output probably won't pass, so there's a=0 which can be inserted as command line argument.

APL (Dyalog Unicode), 11 bytesSBCS

Assumes ⎕IO (Index Origin) to be 0, which is default on many systems. Anonymous tacit prefix function. 1-indexed.

(2⊥⍴∘1 0)¨⍳

Try it online!

ɩndices 0…n−1

( apply the following tacit function to each

⍴∘1 0 cyclically reshape the list [1,0] to that length

2⊥ convert from base-2 (binary) to normal number

Ruby -n, 32 30+1 bytes

Since we have exactly 1 line of input, $. is godly convenient!

EDIT: I'm amazed that I managed to outgolf myself, but it seems using -n which counts as 1 (by rule 2 in default special conditions, since Ruby can be run with ruby -e 'full program' (thus -n is 1) all instances of gets which is only used once can be golfed down 1 char this way; I believe this is a milestone for ruby, please speak up if you disagree with this train of thought before I repeatedly reuse it in the future)

v=0
?1.upto($_){p v=v*2|$.^=1}

Try it online!

Explanation

# while gets(); -- assumed by -n
v=0            # First element of the sequence
?1.upto($_){   # Do from "1" to "$LAST_READ_LINE" aka: Repeat [input] times
  p            # print expression
  v=v*2|$.^=1  # Next element is current element times two
               # bitwise-or 0 or 1 alternating
               # $. = lines of input read so far = 1 (initially)
}
# end           -- assumed by -n

MATL, 7 bytes

:&+oRXB

Try it online!

Explanation:

         % Implicitly grab input, n
:        % Range: 1 2 ... n

 &+      % Add the range to itself, transposed
         % 2 3 4 5 ...
         % 3 4 5 6 ...
         % 4 5 6 7 ...
         % 5 6 7 8 ...

   o     % Parity (or modulus 2)
         % 0 1 0 1 ...
         % 1 0 1 0 ...
         % 0 1 0 1 ...
         % 1 0 1 0 ...

    R    % Upper triangular matrix:
         % 0 1 0 1
         % 0 0 1 0
         % 0 0 0 1
         % 0 0 0 0

    XB   % Convert rows to decimal:
         % [5, 2, 1, 0]
         % Implicitly output

The output would be 0, 1, 2, 5 ... if P was added to the end (flip), making it 8 bytes.

Haskell, 52 bytes

(`take`map a[0..])
a 0=0
a 1=1
a n=a(n-1)+2*a(n-2)+1

Try it online!

Python 3 53 bytes

lambda n:[int(('0'+'10'*i)[:i+1],2)for i in range(n)]

Try it online

Husk, 7 bytes

mḋḣ↑Θݬ

Try it online!

1-based, so input n gives the first n results.

Explanation

     ݬ   The infinite list [1, 0, 1, 0, 1, ...]
    Θ     Prepend a zero.
   ↑      Take the first n elements.
  ḣ       Get the prefixes of that list.
mḋ        Interpret each prefix as base 2.

Retina, 28 bytes

)K`0
"$+"+¶<`.+
$.(*__2*$-1*

Try it online!

0-based, so input n gives the first n+1 results.

Explanation

Uses the recursion from OEIS:

a(n) = a(n-1) + 2*a(n-2) + 1

Let's go through the program:

)K`0

This is a constant stage: it discards the input and sets the working string to 0, the initial value of the sequence. The ) wraps this stage in a group. That group itself does nothing, but almost every stage (including group stages) records its result in a log, and we'll need two copies of the 0 on that log for the program to work.

"$+"+¶<`.+
$.(*__2*$-1*

There's a bunch of configuration here: "$+"+ wraps the stage in a loop. The "$+" is treated as a substitution, and $+ refers to the program's input, i.e. n. This means that the loop is run n times.

Then ¶< wraps each iteration in an output stage, which prints the stage's input with a trailing linefeed (so the first iteration prints the zero, the second iteration prints the first iteration's result and so on).

The stage itself replaces the entire working string with the substitution on the last line. That one makes use of an implicit closing parenthesis and implicit arguments for the repetition operator *, so it's actually short for:

$.($&*__2*$-1*_)

The stuff inside the parentheses can be broken up into three parts:

Then $.(…) measures the length of the resulting string. In other words, we've computed a(n) = a(n-1) + 1 + 2*a(n-2) by going through unary (not really though: $.(…) is lazy and doesn't actually evaluate its content if it can determine the resulting length directly through arithmetic, so this is even quite efficient).

The result of the final loop iteration (the n+1th element of the sequence) is printed due to Retina's implicit output at the end of the program.