New answers tagged

0

Python 3.8 (pre-release), 126 bytes Neither short nor pretty, but possibly a new method! from decimal import* def f(n):l=n//4+1;p=10**l;getcontext().prec=n*l-l+1;return int(str(Decimal(p**2)/Decimal(p**2-p-1))[-l:]) Uses, for example: 1/(1000000-1000-1) = 0.000 001 001 002 003 005 008 013 021 034 055 089 ... l can be any upper bound for the number of ...


1

Jelly (fork), 15 bytes ŒṗŒ!€ẎQ½+¥/€Æ²S Try it online! This will work for some inputs on TIO, but will fail for others due to floating point errors. However, my fork has symbolic math support, meaning that it basically never uses floats. How it works This is a brute force attempt. Times out on TIO for \$n \ge 10\$. Uses a similar approach to Kevin's answer, ...


1

JavaScript (Node.js), 60 50 bytes -10 bytes thanks to @user (p=x=>process.stdout.write(...x))`L`;while(1)p`ol` Try it online!


2

FALSE, 13 bytes 'L,[1]["ol"]# Try it online! Explanation 'L, // outputs "L" character [1] // pushes lambda which evaluates to 1 onto the stack ["ol"] // pushes lambda which prints "ol" onto the stack # // Executes lambda on top of stack while the lambda below it // does not evaluate to zero Note: This is my ...


0

GForth 30 bytes : A ." L" 0 1 do ." ol" loop ;


0

Pip, 15 bytes W P1L UiP2*Uv%t Prints indefinitely. Try it here! Or, here's a 16-byte equivalent in Pip Classic: Try it online! Explanation W While this expression is truthy-- P1 print 1 (evaluates to 1, which is truthy) --loop: Ui Increment i (initially 0) L and loop that many ...


1

Flipbit, 24 bytes <<^>>>^>^>>.^<^<^<^<<,^] Try it online! or, using a bit less odd behavior: ^[>>>^>^>>.^<^<^<^<<,^<] Try it online!


3

Flipbit, 35 30 28 bytes Thanks to Bubbler for -5 by shortening the loop Thanks to ovs for -2 by being big smort ^>>>^>^>>.<<<<<^>>>[>^>^.<<] Try it online! Prints L, gets the tape set up for l, then makes use of the fact that o and l differ by only their two least significant bits to create a short loop ...


1

Stax, 7 bytes é╟φQRl: Run and debug it


1

05AB1E, 8 bytes Same approach as my APL answer. $>и32SβÆ Try it online! $ # push 1 and input n > # increment n и # a list of n+1 1's 32S # push list [3, 2] β # for each of those numbers, convert the list of 1's from that base Æ # reduce by reduction The first three bytes could be >Å1 instead: Try ...


0

Lua, 31 bytes u=io.write u"L"::x::u"ol"goto x Try it online!


0

COMET II ("CASLシミュレータ (CASL II 対応)" by Daytime), 28 bytes. The leftmost column shows an address, and one word is two bytes. 0000: 7001 0000 7002 0000 0004: 1210 0012 1220 0014 0008: F000 0002 7120 7110 000C: 1210 006C 1110 0012 0010: 6400 0000 004C 006F 0014: 0002 The simulator seems to output an LF automatically every time OUT is executed, and it ...


1

><> and Gol><>, 10 bytes "volL :>o$ Try it online! (><>) Try it online! (Gol><>) How it works "volL Push charcodes of v, o, l, L in that order (L is at the top) "v End string mode > and move to the second row o Pop and print one char (initially L) : o$ Infinite loop: Swap, ...


4

Factor + math.extras, 18 bytes [ 3 + 3 stirling ] Ignoring leading zeroes. Zero-indexed. stirling is bugged in the build TIO uses. It was fixed about three years ago, so here's a picture of running it in the listener with a more recent build.


3

Vyxal sr, 6 bytes ƛ23fe¯ Try it Online! A port of xigoi's Jelly answer Explained ƛ23fe¯ ƛ # over the range [1, input] (call each item n) 23f # the list [2, 3] e # ^ ** n ¯ # deltas of ^ # the s flag auto-sums the result ```


4

Rattle, 24 bytes |s>-s=3e~s<=2e~sg1-~+/R1 Try it Online! This is a port of xnor's Python answer into Rattle using this formula: $$ f(n) = \frac{1}{2}(3^{n-1}-2^n+1)$$ Explanation | takes the user's input s saves the user's input (n) to memory slot 0 > move pointer right - ...


5

Scala, 132 bytes n=>(for{i<-1 to n-1 j<-i+1 to n-1 p<-1.to(n).permutations}yield{Set(p.slice(0,i),p.slice(i,j),p.slice(j,n))map(_.toSet)}).toSet.size Try it online! Is it short? No. Is it efficient? No. Is it clever? No. Why did I make it? I...don't know. I'll try golfing it later, if possible.


3

APL (Dyalog Unicode), 14 bytes -/3 2⊥¨∘⊂1,⍴∘1 Try it online! A function implementing the sum formula using base conversion: $$ a(n) = \sum_{k=0}^n \left( 3^k-2^k \right) = \sum_{k=0}^n 3^k - \sum_{k=0}^n 2^k = (\underbrace{1 \cdots 1}_{n+1})_3 - (\underbrace{1 \cdots 1}_{n+1})_2 $$ ⍴∘1 ⍝ a vector of n 1's 1, ⍝ prepend an ...


8

Jelly, 7 bytes cþæ*2FS Try it online! Now that xigoi has outgolfed me, I thought I'd share my answer. This outputs the \$n\$ term of the sequence without leading zeroes. How it works We generate the matrix $$\left[\begin{matrix} \binom 1 1 & \binom 2 1 & \cdots & \binom n 1 \\ \binom 1 2 & \binom 2 2 & \cdots & \binom n 2 \\ \vdots &...


2

Risky, 10 bytes +0?+1+_0+0__!2?-+_{0 Try it online! 1-indexed sequence without leading zeros. Uses the formula \$\sum_{k=0}^{n} 3^k - 2^k\$. Explanation + sum 0 range ? n + + 1 1 + copy-last _ 0 0 + + 0 0 _ map _ ! 3 2 ^ ? k - - + 2^ _ [k,n] { ...


4

Wolfram Language (Mathematica), 15 bytes #~StirlingS2~3& Try it online!


11

Jelly, 6 bytes 3Ḋ*)SI Try it online! -1 byte thanks to Jonathan Allan 1-indexed sequence without leading zeros. Uses the formula \$\sum_{k=1}^{n} 3^k - 2^k\$. Explanation 3Ḋ*)SI Main monadic link ) For each k in the range [1..n]: 3Ḋ Remove the first element from the range [1..3] * Raise each to the power of k S Sum (producing [sum(2^k)...


6

Jelly, 9 bytes Surely there is an eight or less out there... 1×Ɱ3$¡FQS A full program that accepts a non-negative integer from STDIN and prints the result using the 0-indexed, no leading zeros option. Try it online! How? 1×Ɱ3$¡FQS - Main Link: no arguments 1 - start with x=1 ¡ - repeat this (read from STDIN) times: $ - last two ...


4

C (gcc), 34 bytes f(n){n=n<3?0:5*f(n-1)-6*f(n-2)+1;} Try it online! Inputs \$0\$-based \$n\$. Returns \$S(n,3)\$. Uses the formula: \$a(n) = 5\cdot a(n-1) - 6\cdot a(n-2) + 1\$, for \$n > 2\$.


6

Jelly, 12 bytes 3ẹ@þṗ¥ẠƇṢ€QL Try it online! A monadic link that takes \$n\$ as its argument and returns \$a(n)\$. This is longer and less efficient than the answers based on the OEIS formulae, but should work for any value of \$k\$ by varying the first number in the link from 3. Also, removing the L at the end yields the actual sets. Explanation 3 ¥ ...


8

R, 23 bytes 3^(n=scan()+2)/2-2^n+.5 Try it online! Outputs without the leading zeros, 0-indexed. Uses formula from OEIS page: $$ a(n) = 9 \cdot 3^n/2 - 4 \cdot 2^n + 1/2 $$


7

Alchemist, 76 bytes _+0j->2c b+0j->3d 0j+0_+0b->j c+j+x->_+j d+j->x+b+j j+0c+0d->Out_x+Out_" "!b Try it online! Outputs nonzero values infinitely using the formula \$S(n, 3) = \sum_{k=1}^{n-2} (3^k - 2^k)\$. On TIO, this computes up to the 13th element (2375101) before timing out.


6

Jelly, 9 bytes Port of xnor's answer, made/fixed and golfed with hyper-neutrino's and caird coinheringaahing's help. ’3*_2*$‘H Try it online! This doesn't handle leading zeroes. ’3*_2*$‘H ’ n-1 3* 3^(n-1) _ From that, subtract 2*$ 2^n ‘ Increment that H Halve it


6

APL (Dyalog Unicode), 14 bytes Anonymous tacit prefix function using xnor's formula. 1-indexed. 2÷⍨1-2∘*-3*-∘1 Try it online!


6

Python 2, 24 bytes lambda n:3**n/6-~-2**n/2 Try it online! Formula from OEIS for \$n>0\$: $$ f(n) = \frac{1}{2}(3^{n-1}-2^n+1)$$


6

Hexagony, 5 bytes L;o>l Try it online! Hexagony golfing language confirmed For some reason I was looking at my own answer that prints "six" in 6 bytes and randomly thought "what if I remove @?", and exactly got this answer. 4 bytes is impossible because Lol; is already 4 bytes and it is impossible to alternate two chars and print both ...


0

HP 28S, 18 bytes 0 1 ROT FOR K K INV + NEXT


1

Perl 5 -n, A000290: the squares, safe. ;cdeghijklnopqrst Rules Input from stdin, as a string of decimal integer. Intended solution


0

Perl 5 -p, A005843, safe. .0123456789aelv A simple problem. You should solve this easily if you are familiar with the language well. Intended solution


0

Japt, 2 bytes Aõ Test it Aõ A :10 õ :Range [1,A]


0

J, 25 23 bytes {{(-:\:~"1)+/\y=/i.10}} -2 bytes thanks to Bubbler Note: No TIO for this version as it relies on newer J feater called direct definition {{...}} Working 24 byte TIO link Just a port of caird's great answer to make sure I understood it.


1

Jelly, 8 bytes ‘Ṭ€+\I’Ȧ Try it online! Essentially a port of my other answer, but with ‘ to handle zero-indexing


0

Vyxal rs, 5 bytes _ṡ?vc # main program _ # discard the initial number ṡ # inclusive range over implicit inputs ? # number to be checked vc # vectorized contains, sum the list I had to use -r because v vectorizes the first part of the stack otherwise (?) Try it Online!


1

Japt, 20 bytes @TwXµY *!ZøX ªX+YÑ}h Try it


1

Python 2, 65 bytes n=a=0 v=[] exec'a+=[n,-n][a+n in v];print a;n-=1;v+=a,n;'*input() Try it online! Avoids negative numbers by adding them to the list of already visited numbers.


0

Japt, 16 12 11 bytes 1-indexed È+T°*ZÔÅÎ}g Try it


2

Japt, 10 bytes Ò2nU²Ñ ¬ÄÄ Try it Ò2nU²Ñ ¬ÄÄ :Implicit input of integer U Ò :Negate the bitwise NOT of (i.e., floor and increment) 2n : Subtract 2 from U² : U squared Ñ : Times 2 ¬ : Square root ÄÄ :Add one twice


0

Vyxal, 13 bytes ƛ1⋎*›rṘ;fU?‹i Try it Online! Messy port


1

Wolfram Language (Mathematica), 20 bytes (14 characters) ⌊√(2#^2-2)⌋+3& Try it online! Shamelessly translating xnor's Python answer.


1

Java (JDK), 28 bytes L->L+=Math.sqrt(2*L*L-2)+3-L Try it online! Same as everyone, I guess, cheers to xnor! Same length as: L->(int)Math.sqrt(2*L*L-2)+3 Try it online!


2

Vyxal, 7 bytes *d⇩√3+⌊ Try it Online! Shameless port of xnor's answer * # Square d # Double ⇩ # Subtract 2 √ # Square root 3+ # Add 3 ⌊ # Floor


1

Vyxal s, 7 bytes ÷ṡƛS?hc Try it Online!


1

><>, 27 bytes :*2*2-0v :})?v1+>::*{ ;n+2< Try it online! Xnor's formula, but in a language with neither square root nor rounding operations. Instead, it does the equivalent thing of finding the least square number larger than \$2L^2 - 2\$


1

JavaScript (Node.js), 20 bytes n=>(2*n*n-2)**.5+3|0 Try it online! Shamelessly copies the idea from xnor's answer


22

Python 2, 27 bytes lambda L:(2*L*L-2)**.5//1+3 Try it online! A direct formula: $$ f(L) = \lfloor \sqrt{2 L^2-2}\rfloor + 3 $$ Derivation As noted by @Kirill L. and others, the optimal layout uses a near-diagonal line segment whose horizontal and vertical span are at least \$(h,h)\$ or \$(h,h+1)\$. We need the length-\$L\$ to cover at least this much ...


Top 50 recent answers are included