| Bytes | Lang | Time | Link |
|---|---|---|---|
| 024 | TIBASIC TI83 Plus | 250516T133215Z | madeforl |
| 028 | C gcc | 250516T063000Z | CaydendW |
| 032 | AWK | 250515T163839Z | xrs |
| 021 | Juby | 250507T020048Z | Jordan |
| 008 | Japt | 250507T103828Z | Shaggy |
| 027 | Pari/GP | 151223T144657Z | alephalp |
| 028 | MIPS | 151223T170603Z | qwr |
| 005 | Stax | 180312T112501Z | Weijun Z |
| 087 | brainfuck | 180120T053911Z | Jo King |
| 023 | Perl 5 | 180120T000453Z | Xcali |
| 010 | Pyt | 180108T045356Z | mudkip20 |
| 004 | Samau | 151224T134023Z | alephalp |
| 015 | MATL | 151224T114644Z | Luis Men |
| 026 | Ruby | 151225T052207Z | Sherlock |
| 007 | Pyth | 151225T094503Z | Jakube |
| 025 | Ruby | 151225T080203Z | lynn |
| 004 | 05AB1E | 151223T142239Z | Adnan |
| 023 | JavaScript ES6 | 151223T144110Z | ETHprodu |
| 012 | Seriously | 151223T133623Z | quintopi |
| 045 | Matlab | 151223T185554Z | brainkz |
| 031 | APL | 151223T192119Z | Aaron Da |
| 033 | Python 2.7.6 | 151223T180353Z | MCS-Kaij |
| 044 | Haskell | 151223T182118Z | lynn |
| 026 | k4 | 151223T180514Z | Aaron Da |
| 037 | Pure Bash no external utilities | 151223T174016Z | Digital |
| 006 | Jelly | 151223T141634Z | Dennis |
| 026 | Ruby | 151223T143010Z | Level Ri |
| 010 | CJam | 151223T142457Z | Martin E |
| 024 | Mathematica | 151223T135824Z | alephalp |
| 070 | Matlab | 151223T135459Z | flawr |
TI-BASIC (TI-83 Plus), 34 25 24 bytes
Input N
seq(A,A,0,N
sum(2^Ansnot(not(fPart(N nCr Ans/2
uses this cool formula: \$\sum_{a=0}^{n}([\binom{n}{a}\mod2]2^a)\$ (apologies, i'm still learning LaTeX)
C (gcc), 28 bytes
k;f(n){k=n--?f(n)^2*f(n):1;}
Much original. So innovation. TIO pls don't IP ban for unleashing hell upon your servers.
This is mostly a direct port of ETHProductions's Javascript answer.
32 bytes
k;f(n){for(k=1;n--;k^=k*2);k=k;}
Port of the same person's non-recursive answer.
46 bytes
i,k;f(n){for(i=k=0;i<=n;)k=k*2|!(i++&~n);k=k;}
My original code and idea for solving this. Widely inefficient on bytes compared to the other 2 answers but I thought the code still looked cool.
AWK, 32 bytes
{for(x=1;$1--;)x=xor(x,2*x)}$0=x
AWK is a little different, you can't do x^=2*x as that raises it to the exponent, rather than doing xor.
Here's an earlier version that used some magic numbers to calculate the line digit by digit and then added each 2^x, x being the digit count:
{for(x=1;j++-62;)x+=!and(63-$1,j)*2^j}$0=x
J-uby, 21 bytes
:**&(:^%(:*&2))|~:^&1
Explanation
:** & (:^ % (:* & 2)) | ~:^ & 1
:^ % (:* & 2) # n^(2*n)
:** & ( ) | # ...applied input times
~:^ & 1 # ...with n starting at 1
Pari/GP, 27 bytes
n->lift(Mod(x+1,2)^n)%(x-2)
The function takes the \$n\$-th power of the polynomial \$x+1\$ in the ring \$\mathbb{F}_2[X]\$, lifts it to \$\mathbb{Z}[X]\$, and then evaluates it at \$x=2\$.
MIPS, 28 bytes
Input in $a0, output in $v0.
0x00400004 0x24020001 li $v0, 1
0x00400008 0x10800005 loop: beqz $a0, exit
0x0040000c 0x00024021 move $t0, $v0
0x00400010 0x00021040 sll $v0, $v0, 1
0x00400014 0x00481026 xor $v0, $v0, $t0
0x00400018 0x2084ffff addi $a0, $a0, -1
0x0040001c 0x08100002 j loop
Stax, 5 bytes
±s┤ε─
Port of the Jelly answer.
Uses ASCII representation to explain:
ODcH|^
O Put 1 under top of stack
D Repeat for times specified by input
cH|^ Xor the number with itself doubled
Implicit output
brainfuck, 87 bytes
,[>>[>]++<[[->+>+<<]>-->[-<<+>>]<[>+<-[>-<-]]>+<<<]>>>[[-<+>]>]<<[<]<-]>>[<[->++<]>>]<+
Assumes infinite sized cells (otherwise it can’t get past 7, which is conveniently 255). The Pascal’s triangle mod 2 method is actually much longer because of the costly mod 2 operation, while XOR is much easier to implement.
Pyt, 40 10 bytes
Đ0⇹Řć2%ǰ2Ĩ
Explanation:
Observing that the Binary Sierpinski Triangle is equivalent to Pascal's Triangle mod 2,
Implicit input
Đ Duplicate input
0⇹Ř Push [0,1,2,...,input]
ć2% Calculate the input-th row of Pascal's Triangle mod 2
ǰ Join elements of the array into a string
2Ĩ Interpret as a binary number
Implicit print
Samau, 4 bytes
Now Samau has built-in functions for XOR multiplication and XOR power.
▌3$ⁿ
Hex dump (Samau uses CP737 encoding):
dd 33 24 fc
Explanation:
▌ read a number
3 push 3
$ swap
ⁿ take the XOR power
MATL, 15 bytes
Similar to @flawr's answer:
i:1w"TToX+]2\XB
EDIT (May 20, 2016) Try it online! with X+ replaced by Y+ to conform to version 18.0.0 of the language.
Example
>> matl i:1w"TToX+]2\XB
> 5
51
Explanation
i % input
: % vector of values 1, 2, ... to previous input
1 % number literal
w % swap elements in stack
" % for
TTo % vector [1 1]
X+ % convolution
] % end
2\ % modulo 2
XB % convert from binary to decimal
Ruby, 31 26 bytes
EDIT: Changed to a different language entirely! All golfing suggestions welcome!
This program bitwise XORs the previous element of the sequence with twice itself, i.e. f(n) = f(n-1) ^ 2*f(n-1).
->n{v=1;n.times{v^=2*v};v}
Pyth, 7 bytes
uxyGGQ1
Try it online: Demonstration
Explanation:
u Q1 apply the following statement input-times to G=1:
xyGG update G with (2*G xor G)
Ruby, 25 bytes
->n{eval"n^=2*"*n+?1*n=1}
Shorter than the other answers on here so far. It constructs this string:
n^=2*n^=2*n^=2*n^=2*1
Then it sets n=1 (this actually happens while the string is being made) and evaluates the above string, returning the result.
05AB1E, 5 4 bytes
I proudly present to you, 05AB1E. Although it is very short, it is probably very bad at long challenges.
Thanks to ETHproductions for shaving off 1 byte :)
$Fx^
Explanation:
$ # Pushes 1 and input
F # Pops x, creates a for-loop in range(0, x)
x # Pops x, pushes x and 2x
^ # Bitwise XOR on the last two elements
# Implicit, ends the for-loop
# Implicit, nothing has printed so the last element is printed automatically
JavaScript (ES6), 23 bytes
f=x=>x?(y=f(x-1))^y*2:1
Based on the first formula on the OEIS page. If you don't mind the code taking almost forever to finish for an input of 30, we can shave off a byte:
f=x=>x?f(--x)^f(x)*2:1
Here's a non-recursive version, using a for loop in an eval: (32 bytes)
x=>eval("for(a=1;x--;a^=a*2);a")
Seriously, 12 bytes
2,╣`2@%`Mεj¿
Hex Dump:
322cb960324025604dee6aa8
Since Seriously does not include any means of doing a bitwise xor, this solution takes the challenge completely literally, directly computing the given row of the triangle. This method gives correct answers up to n=1029 (after which there is not enough memory to compute the given row of Pascal's triangle).
Explanation:
, get input
╣ push the nth row of pascal's triangle
`2@%`M take each element of the row mod 2
εj join all the binary digits into a string
2 ¿ interpret it as a base 2 number
Matlab, 45 bytes
Solution:
@(i)2.^[0:i]*diag(mod(fliplr(pascal(i+1)),2))
Test:
ans(10)
ans =
1285
Explanation:
pascal constructs Pascal triangle, but it starts from 1, so input should be i+1. fliplr flips array from left to right. mod(_,2) takes remainder after division by 2. diag extracts main diagonal.Multiplication using 2.^[0:i] converts vector to decimal
I'm glad, @flawr that I found Matlab competitor here :)
APL, 31 bytes
{({2⊥⊃~1 2=.{(64⍴2)⊤⍺×⍵}⍵}⍣⍵)1}
This is almost certainly awful code, but I'm a complete APL newb. I expect anyone with any skill could get rid of all the D-functions and shorten it considerably. The logic is more or less the same as my k4 answer -- multiply by 1 or 2, convert to bits with ⊤, XOR using not equals, convert back to a number with ⊥, wrap the whole thing in a function and ask for a specific number of iterations using ⍣. I have no idea why the result coming out of the inner product is enclosed, but ⊃ cleans that up.
Python 2.7.6, 38 33 bytes
Thanks to Dennis for shaving off a few bytes!
x=1
exec'x^=x*2;'*input()
print x
Haskell, 44 bytes
import Data.Bits
f n=iterate((2*)>>=xor)1!!n
In the ((->) r) monad, (f >>= g) x equals g (f x) x.
k4, 26 bytes
{x{2/:~(=). 0b\:'1 2*x}/1}
0b\: converts a number to a boolean vector (i.e. bitstring), XOR is implemented as "not equal", 2/: converts a bitstring back to a number by treating it as a polynomial to evaluate, and x f/y with x an integer is f applied to y first, and then to its successive outputs x times.
Sample run:
{x{2/:~(=). 0b\:'1 2*x}/1}'!5
1 3 5 15 17
Pure Bash (no external utilities), 37
Bash integers are 64-bit signed, so this works for inputs up to and including 62:
for((x=1;i++<$1;x^=x*2)){
:
}
echo $x
Jelly, 6 bytes
1Ḥ^$³¡
The binary version that works with this revision of the Jelly interpreter has the xxd dump
0000000: 31 a8 5e 24 8b 80 1.^$..
How it works
1Ḥ^$³¡ Input: n
1 Set the left argument to 1.
Ḥ Multiple the left argument by two.
^ Hook; XOR it with its initial value.
$ Create a monadic chain from the last two insructions.
³¡ Call the chain n times, updating the left argument after each call.
Ruby, 26 bytes
anonymous function with iteration.
->n{a=1;n.times{a^=a*2};a}
this recursive function is one byte shorter, but as it needs to be named to be able to refer to itself, it ends up one byte longer.
f=->n{n<1?1:(s=f[n-1])^s*2}
Mathematica, 40 24 bytes
Nest[BitXor[#,2#]&,1,#]&
Matlab, 77 70 bytes
This function calculates the n-th row of the Pascal triangle via repeated convolution with [1,1] (a.k.a. binomial expansion or repeated multiplication with a binomial), and calculates the number from that.
function r=c(n);k=1;for i=1:n;k=conv(k,[1,1]);end;r=2.^(0:n)*mod(k,2)'