New answers tagged

1

Pyth, 30 bytes Fdr2Kt*2QVr2hQI!%J*Nt^QdK/JK.q Try it online! Uses a similar approach to my Python 2 answer. Note: I also used the upper bound of 2n-2 on d that @Bubbler mentioned in a comment. (Q) Implicitly initialize Q to be the input Kt*2Q Initialize K to be 2Q-1 Fdr2K For d in range(2, 2Q-1): Vr2hQ - For N in range(2, Q+1): ...


0

Bash + bc, 103 bytes for((m=1;2*m!=p;));do((m++));t=$(bc<<<"obase=$1;$m");p=$(bc<<<"ibase=$1;${t: -1}${t::-1}");done;echo $m Try it online!


1

Python 2, 78 bytes Simple direct solution. Increments x and d until we obtain the appropriate answer. n=input() j=x=d=2;k=2*n-1 while j%k:j=x*n**d-x;x=[2,x+1][x<n];d+=x<3 print j/k Try it online!


0

Charcoal, 41 bytes Nθ≔⊖⊗θηI÷⌊ΦE×θη∨‹ι⊗θ∨‹﹪ιθ²×﹪ιθ⊖Xθ÷ιθ¬﹪ιηη Try it online! Link is to verbose version of code. Uses the OEIS quote plus the additional information provided by @Bubbler in a comment whereby d < 2n-1. Explanation: Nθ Input n. ≔⊖⊗θη Calculate 2n-1. E×θη Loop nd + x from 0 to n(2n-1). ∨‹ι⊗θ Ensure d > 1 by returning 1 if it is ...


3

05AB1E, 15 11 bytes ∞.ΔxsIвÁIβQ -4 thanks to @Grimmy Try it online! explanation: ∞ get all positive numbers .Δ find the first number for which: xs the number doubled and itself (e.g. 64, 32) Iв convert to base input (e.g. [1, 0, 1, 2]) Á rotate right (e.g. [2, 1, 0, 1]) Iβ convert back from base input to a number (e.g. 64) ...


1

Japt, 11 bytes _ѶZsU'é}a1 Try it _ѶZsU'é}a1 :Implicit input of integer U _ :Function taking an integer Z as an argument Ñ : Multiply by 2 ¶ : Check for equality with ZsU : Convert Z to a string in base U 'é : Rotate right (string is converted back to decimal afterwards) ...


3

JavaScript (ES7),  90 88 86  85 bytes A recursive port of the Maple function. n=>(d=1,x=n,g=(a=2**++d-1,b=q=n+~-n)=>b?g(b,a%b):(x=q/a)<n?(x+!~-x)*(n**d-1)/q:g())() Try it online! (\$a(3)\$ to \$a(9)\$) Or, for +5 bytes, a BigInt version: n=>(d=1n,x=n,g=(a=2n**++d-1n,b=q=n+~-n)=>b?g(b,a%b):(x=q/a)<n?(~-x?x:2n)*~-(n**d)/q:g())()...


3

Python 2, 77 76 70 bytes f=lambda n,m=1,i=1:i<m and f(n,m,i*n)or(m+m%n*i)/n-m*2and f(n,m+1)or m Try it online! Input: base n Output: The smallest integer m satisfies the requirement. Saved 6 bytes thanks to @Bubbler! This is a brute-force search that starts at m = 1 and works its way up. Will run out of recursion limit if the actual solution is too ...


0

cQuents, 10 bytes D9*_+$)[0] Try it online! Just another port of Grimmy's answer. $ # Index _+ # +1 9* # *9 D )[0] # First digit


0

cQuents, 10 bytes K\rJ$)))^2 Try it online! First time using cQuents. Explanation $ # Take the index. J ) # Convert to (default) base 2. \r ) # Reverse it. K ) # Convert from (default) base 2 to base 10. ^2 # And square it.


0

C (gcc), 38 bytes r;f(n){for(r=n%2;n/=2;r+=r+n%2);r*=r;} Reverse all the bits, square the result. Pretty standard. -1 byte thanks to ceilingcat! Try it online!


2

MathGolf, 4 bytes àxå² Try it online. Explanation: à # Convert the (implicit) input-integer to a binary-string x # Reverse this string å # Convert it from a binary-string back to an integer ² # Take the square of this integer # (after which the entire stack joined together is output implicitly as result)


2

05AB1E, 4 bytes bRCn Try it online! Explanation Implicit input b Convert to binary R Reverse C Convert from binary n Square Implicit output


0

Pyth, 13 bytes A(Z1)#HA(H+HG Try it online! Explanation A(Z1)#HA(H+HG (Z1) : The tuple (0, 1) A : Assign the first value of the tuple to G and the second to H # : Loop until error statement H : Print H (H+HG : The tuple (H, H + G) A : Assign the first value of the tuple to G ...


2

05AB1E, 48 47 bytes g≠iā<DδmUεXøINǝ}Xšεā<sUœε©2.ÆíÆ.±Xε®Nèè}«P}O}ć÷ Sometimes 05AB1E's lack of almost all matrix builtins is pretty annoying.. ;) Inspired by @Arnauld's JavaScript answer. Try it online or verify almost all test cases (removed the last two largest ones, since they time out on TIO). Explanation: First handle the edge case of a ...


1

APL (Dyalog Unicode), 10 bytesSBCS ⊢⌹∘.*⍨∘⍳∘≢ Try it online! A port of Graham's APL+WIN solution into a modern APL, which happens to work exactly the same (and have the same byte count) as my own J solution. How it works ⊢⌹∘.*⍨∘⍳∘≢ ⍝ Input: V, result of a polynomial evaluated at 0..m-1 ⍳∘≢ ⍝ Generate 0..m-1 ∘.*⍨∘ ⍝ Self outer product by * ...


1

Haskell, 77 bytes h%(a:t)=h-a:a%t h%_=[h] f(h:t)=h:foldr(%)[](f$zipWith((/).(-h+))t[1..]) f e=e Try it online!


0

Jelly, 14 bytes J’*þ`æ*-⁸æ×ær0 A monadic Link accepting a list of integers which yields a list of the exponents (floats and/or integers) with the lowest degree on the left of the same length as the input (with trailing zeros if need be). Try it online! How? J’*þ`æ*-⁸æ×ær0 - Link: list of integers, V J - range of length (V) ’ - ...


3

J, 10 bytes %.^/~@i.@# Try it online! Obligatory J answer on a matrix-related challenge. Takes input as a vector of extended integers (otherwise the answer may have small floating-point errors), and gives the polynomial's coefficients in lowest-first order, possibly with some extra zeroes at the end. How it works %.^/~@i.@# NB. Input: a vector V of ...


2

Charcoal, 68 62 bytes ≔⟦¹⟧ηFLθ«⊞υ⁰≔÷⁻§θιΣEυ×κXιλ∨ΠEι⊕κ¹ζUMυ⁺κ×ζ§ηλ⊞η⁰≔Eη⁻§η⊖λ×κιη»Iυ Try it online! Link is to verbose version of previous version of code that excludes trailing zeros, but apparently it isn't necessary to do that, thus saving 6 bytes. Outputs the terms in power order i.e. the constant term is printed first. Explanation: ≔⟦¹⟧η Start by ...


1

MATL, 12 bytes n:qGyz3$ZQYo The result is given with higher-order coefficients first, and may contain leading zeros. Try it online! Or verify all test cases Explanation Consider input [-3, -1, 5, 15, 29] as an example. n:q % Implicit input. Number of elements. Range. Subtract 1, element-wise % STACK: [0, 1, 2, 3, 4] G % Push input again ...


1

SageMath, 63 48 bytes lambda v:QQ[x].lagrange_polynomial(enumerate(v)) Try it online! Outputs the polynomial as $$a_k n^k + \cdots + a_3 n^3 + a_2 n^2 + a_1 n + a_0 $$


2

Python 3 + Numpy, 69 bytes lambda x:polyfit(range(len(x)),x,len(x)-1).round() from numpy import* Try it online! May have leading zeros.


5

R, 55 52 bytes -3 bytes thanks to Giuseppe. round(solve(outer(n<-seq(a=u<-scan())-1,n,"^"))%*%u) Try it online! Outputs \$(a_0, a_1,\ldots,)\$ with possible trailing zeros. Let \$u\$ be the output sequence, and \$X\$ be the \$m\times m\$ matrix such that \$X_{i,j}=i^j\$ (0-indexed), i.e. \$ X=\begin{pmatrix} 1&0&0&\ldots&0\\ 1&...


2

Pari/GP, 38 bytes a->Vecrev(polinterpolate([0..#a-1],a)) Try it online!


4

Wolfram Language (Mathematica), 50 49 37 bytes Returns a polynomial. Mathematica is so awesome x+1 can be used as a variable in this context. Apart is a weird built-in that, quoting from the docs, seems to attempt to rewrite an expression as a sum of terms with minimal denominators, and also happens to expand polynomials (that are returned in a weird ...


8

JavaScript (ES7),  193 ... 154  145 bytes Saved 9 bytes thanks to @Bubbler Returns \$(a_0,a_1,...,a_k)\$ with some possible trailing zeros. v=>v.map((_,i)=>(g=(i,m=v.map((n,y)=>v.map((_,x)=>x==i?n:y**x)))=>+m||m.reduce((s,[v],i)=>v*g(0,m.map(([,...r])=>r).filter(_=>i--))-s,0))(i)/g()) Try it online! (removed the ...


4

APL+WIN, 16 bytes Index origin = 0 Prompts for input as a vector and outputs coefficients from a0 to an-1 where n is the length of the vector. The order of the polynomial can be obtained by summing the number of coefficients up to the last none zero coefficient: 0⍕n⌹m∘.*m←⍳⍴n←,⎕ Try it online! Courtesy of Dyalog Classic


0

R, 12 10 bytes Producing two different sequences with the correct limiting frequencies whether \$s=0\$ or \$s\ne 0\$: (!s):n%%10 Try it online!


0

W, 2 bytes (Port of the Charcoal answer.) How in the world can a 3-byter ever compress in the compressor‽ H≡ Uncompressed: !)[ Explanation ! % Logical negate the first input. ) % Increment the negated input. [ % Cyclic index the second input by that result.


1

MATLAB, 45 bytes v=input('');rng(fix(~v(1)));randi(10,v(2),1)-1 randi(N,n,1) generates an n-length sequence of uniform random integers in the range 1,...,N. Taking two digits of the resulting sequence of realizations at a time, the required distribution follows naturally, which can be checked using the example code below. N = 50000 ; [h,x] = hist(10*(...


1

dc, 15 13 bytes Saved 2 bytes thanks to Mitchell Spector!!! [A/]sa?0=aA%p Try it online! Input: \$n\$ and seed (\$s\$). Output: \$n^{\text{th}}\$ term zero-indexed. For \$s>0\$ the sequence is: \$0,1,2,\dots,9,\dots\$ For \$s=0\$ the sequence is: \$\underbrace{0,0,\dots,0}_{10},\underbrace{1,1,\dots,1}_{10},\underbrace{ 2,2,\dots,2}_{10},\dots,\...


1

Retina 0.8.2, 16 bytes ^(0,.+). $1 !`.$ Try it online! Link includes test cases. Takes input as s,n and outputs the second last digit of n if it has one and s is zero otherwise the last digit of n. The individual sequences are 10% of each digit but the combined sequence only approaches 1% after the 10th term. (I have a 16 byte answer in Retina 1 for which ...


3

Bash, 25 23 bytes Saved 2 bytes thanks to Mitchell Spector!!! echo $[$2/($1?10:1)%10] Try it online! Input: seed (\$s\$) and \$n\$. Output: \$n^{\text{th}}\$ term zero-indexed. For \$s=0\$ the sequence is: \$0,1,2,\dots,9,\dots\$ For \$s>0\$ the sequence is: \$\underbrace{0,0,\dots,0}_{10},\underbrace{1,1,\dots,1}_{10},\underbrace{ 2,2,\dots,2}_{...


2

JavaScript (Node.js), 15 bytes s=>n=>n[!s+1]|0 Try it online! If seed is 0 it will take the 3rd digit of the number, else it will take the 2nd digit of the number


0

perl -apple, 18 bytes $F[0]&&chop;$_%=10 Try it online! Prints the last digit of n for the special sequence (c = 0), otherwise, the penultimate digit of n. It doesn't quite work for single digit n's, but since we only have to tend towards equal distribution, it doesn't matter what it prints.


3

JavaScript (ES7), 19 bytes Takes input as (seed)(n) and returns the \$n\$-th term. The special seed is \$0\$. s=>n=>n/10**!s%10|0 Try it online!


2

Charcoal, 9 5 bytes §S⊕¬N Try it online! Link is to verbose version of code. Takes input as n, s and outputs the (1-indexed) third digit of n if s is zero otherwise the second digit of n (so somewhat similar to @ExpiredData's answer, although this was unintentional). The two sequences separately always output an exact 10% frequency after a power of 10 ...


5

C (gcc), 24 bytes f(s,n){s=(s?n/10:n)%10;} Try it online! Input: seed (\$s\$) and \$n\$. Output: \$n^{\text{th}}\$ term zero-indexed. For \$s=0\$ the sequence is: \$0,1,2,\dots,9,\dots\$ For \$s>0\$ the sequence is: \$\underbrace{0,0,\dots,0}_{10},\underbrace{1,1,\dots,1}_{10},\underbrace{ 2,2,\dots,2}_{10},\dots,\underbrace{9,9,\dots ,9}_{10},\dots\...


4

05AB1E,  4  3 bytes ΘÍè Try it online! Takes the seed then n and outputs the nth term, outputs different sequence for seed of 1 Explanation Θ - truthified (so 1 if input is 1, else 0) Í - subtract 2 (so -1 if input is 1, else -2) è - take this index of the nth term Since 05AB1E uses modular ...


4

Wolfram Language (Mathematica), 38 bytes Takes Input [n,seed] and outputs nth term Specific seed is 0 If[#2>0,#~Mod~10,Floor[#~Mod~100/10]]& Try it online!


9

Python 2, 23 22 bytes Thanks @xnor for figuring out a really cool way to convert the seed to -1 or -2, which saved 1 byte. lambda s,n:`n+9`[2/~s] Try it online! Input: The seed s (non-negative) and an index n (positive). Output: The element at the nth index (the sequence is one-indexed) If the seed is positive, the sequence is 0123456789 repeated ...


0

APL (Dyalog Unicode), 8 10 bytesSBCS ⊢↑∘∊⊣,/∘⍳⌈ Try it online! A dyadic train. Left arg = interval (n), right arg = number of terms (x). ⎕IO←1. How it works ⊢↑∘∊⊣,/∘⍳⌈ ⍝ Left: interval n, Right: terms x ∘⍳⌈ ⍝ Generate range [1..max(n,x)] ⍝ (Too short array will complain at N-wise reduce) ⊣,/ ⍝ Extract length-n intervals (...


0

Io, 50 bytes Port of Jonathan Allan's answer. method(n,x,Range 0 to(x-1)map(v,(v/n)floor+v%n+1)) Try it online!


0

APL (Dyalog Extended), 13 bytes {⍺↑2-2|⍸⍵}⍣≡⍨ Try it online! Basically a port of 13-byte J code at the bottom of algorithmshark's J answer ($1+2|I.)^:_~. Takes an integer n and gives first n terms. How it works {⍺↑2-2|⍸⍵}⍣≡⍨ ⍝ Input: n { } ⍝ At each step, ⍸⍵ ⍝ For each item a_i at index i, give a_i copies of i 2-2| ⍝ ...


0

dirt -v, 48 bytes "122,11"|.*`,(1',.*(1'2|2'1)|2',.*(1"22"|2"11")) Continuously outputs longer and longer Kolakoski sequences, with a , in the middle that moves right as the sequence is generated: 122,11 1221,12 12211,21 122112,122 1221121,221 12211212,2122 122112122,12211 1221121221,22112 12211212212,211211 122112122122,1121122 1221121221221,1211221 ...


0

APL (Dyalog Extended), 17 bytes {⍵⊃≢¨⊆⍨1+∊⊤¨⍳+⍨⍵} Try it online! Gives nth term, 1-indexed. How it works {⍵⊃≢¨⊆⍨1+∊⊤¨⍳+⍨⍵} { } ⍝ ⍵←n +⍨⍵ ⍝ Double of n ⍳ ⍝ 1 .. 2n, inclusive ∊⊤¨ ⍝ Convert each to binary and flatten 1+ ⍝ Add 1 ⊆⍨ ⍝ Partition self into non-increasing segments ...


1

W x, 7 bytes REALLY slow. The array is 1-indexed and it outputs all upto the input. (Glad that I tie with Husk BTW. Special bonus: it doesn't involve infinite lists!) ♫│x╤►U╟ Uncompressed: ^k2BLHkr Explanation ^ % 10 ^ input. Make sure that enough items are calculated. k % Find the length range of that. 2B % Convert every item to ...


0

Wolfram Language (Mathematica), 70 bytes Tr[1^Join@@Partition[Split[Join@@IntegerDigits[Range[2#],2]],2][[#]]]& Try it online!


0

Zpr'(h, 369 bytes s |> \ (g (foldr (op-> ++) () (map b |N))) (e ())|>o (e (S ()))|>z (e (S (S .n)))|>(e n) (h ())|>() (h (S ()))|>() (h (S (S .n)))|>(S (h n)) (b ())|>() (b (S .n))|>((b (h (S n))) ++ (' (e n) ())) (l (' z (' o .r)))|>1 (l (' z (' z .r)))|>(S (l (' z r))) (l (' o (' o .r)))|>(S (l (' o r))) (l (' o (' ...


Top 50 recent answers are included