Hot answers tagged

16

05AB1E, 14 bytes ∞v®yÒgm=Ox<.±, Try it online! My first non-trivial 05AB1E answer! Happy for suggestions to improve it. The code prints two interleaved sign sequences, both related to the Pólya conjecture. In 1919, George Pólya conjectured that the majority (no less than half) of positive integers up to any finite limit \$\ge2\$ have an odd number of ...


13

Jelly, 7 6 bytes Rb9ḌİS Try it online! -1 byte thanks to @Razetime Takes input 1-indexed as an argument (footer on TIO converts to 0-indexed like in test cases) Returns the nth partial sum How it Works Let's look at the denominators in base 10: \$[1, 2, 3, 4, 5, 6, 7, 8, 10,11,12,13,...]\$ (base 10) Since these consist of the nine base-9 digits, we can ...


7

Wolfram Language (Mathematica) primesUpTo[n_] := Select[Range[n], PrimeQ]; sumsOfTwoPrimes[n_] := Union @@ Outer[Plus, primesUpTo[n], primesUpTo[n]]; GoldbachConjectureHoldsFor[n_] := MemberQ[sumsOfTwoPrimes[n], n]; BooleanToSign[TrueOrFalse_] := 2 Boole[TrueOrFalse] - 1; Do[Print[ BooleanToSign@GoldbachConjectureHoldsFor[2n] ], {n, ∞}] Try it online! ...


7

Python 3, 91 bytes s=b's=%r;q=s%%s\nwhile[print(c%%2*2-1)for c in s%%s]:0' while[print(c%2*2-1)for c in s%s]:0 Try it online! Quine, which outputs 1 or -1 according to the last bit of each byte of the quine, repeating infinitely. Starts 1 1 -1 1 1 1 1 -1 1 1 Previously 107 bytes. I know it's not code-golf, but I couldn't resist golfing it a bit. This does ...


7

Python 3, 79 bytes lambda s:eval('{*range%s}'%s.translate({91:40,40:'(1+',93:'+1)',85:',*range'})) Try it online! -6 bytes thanks to ovs -3 bytes thanks to dingledooper


7

Kakoune, 62 keystrokes <a-l>"ndd<a-l>S "hddQxy<a-p><a-p>_S a*<c-r>h+<a-;> <backspace><esc><a-h>|bc k<a-i>ndd<a-j>Q:ex<tab> <c-r>nq K<a-x>d%<a-s><a-h>w<a-l>d<a-j> (note the trailing spaces on line 1 and 2) Assumes the input in the following ...


7

Jelly, 15 14 bytes :;Ɱæl/€ṀɗƬŒṗL’ Try it online! Explanation :;Ɱæl/€ṀɗƬŒṗL’ Main monadic link : Divide the input by itself to get 1 Ƭ Repeat until reaching fixed point, collecting intermediate results ɗ ( ;Ɱ Œṗ Prepend to each integer partition of the input € For each / ...


7

Haskell, 72 bytes (1%) k%n|k?n==k=0|1>0=1+k?n%n k?0=k k?n=maximum[lcm x$k?(n-x)|x<-[1..n]] Try it online! The helper function k?n compute a the Landau function g(n) except k is also in the list of numbers. Then, k%n counts how many iterations of replacing x with x?n starting with k we need to reach a fixed point.


6

C (gcc), 38 bytes int f() { return (rand() & 2) - 1; } Try it online! This could be golfed into 21 bytes (f(n){n=(rand()&2)-1;}) but this question is not tagged as code-golf. In C, random without seeds behave deterministically which actually fit the requirement of this question. I don't know how the sequence is generated. But it does generate a ...


6

JavaScript (ES7),  86 83  82 bytes Saved 3 bytes thanks to @xnor Saved 1 byte thanks to @VarunVejalla An approximation of Thomsen's approximation. (a,b,c)=>(519*((a*b)**(p=1.535)+c**p*(b**p+a**p))/(31+27*a*b*c/(a+b+c)**3))**(1/p) Try it online!


6

Wolfram Language (Mathematica), 16 bytes LinearRecurrence Try it online! Given the OP's background, I suppose this isn't too much of a surprise.


6

Haskell, 54 bytes (flip take.).g g(x:r)k=x:g(r++[sum(zipWith(*)k$x:r)])k Try it online! The function g takes the seed and the kernel as input and builds the sequence as an infinite list. The first line is an anonymous function taking the seed, the kernel and the length as input, passing the former two arguments to g and truncating the infinite sequence to ...


6

05AB1E, 27 21 15 bytes 1.ΓIÅœs䪀.¿à}g -6 bytes by porting @xigoi's Jelly answer, so make sure to upvote him! Try it online or verify all test cases. Original 27 21 bytes approach: 1[DIL.¿QD–#IÅœs䪀.¿à -6 bytes thanks to @ovs. Try it online or verify all test cases. Explanation: 1 # Push 1 .Γ # Loop until it no longer changes, ...


5

APL (Dyalog Extended), 31 bytes (⎕D,⎕A)⊃⍨⊃⎕⌽¯1⌽,⍉⎕(⊣⊤⍳⍤*~*-2⍨)⎕ Try it online! A full program that takes numbers from stdin in the order of k, b, n. n being 1-based cost me 3 bytes ¯1⌽. How it works The main mathematical trick is well explained in Neil's answer. The gist is that the repeating part of the base-b representation consists of length-k base-b ...


5

Ruby, 52 39 36 bytes -13 bytes thanks to Dingus! -3 bytes thanks to Sisyphus x=0;"#$."[?9]||p(x+=1r/$.)while$.+=1 Try it online! Prints all values.


5

Wolfram Language (Mathematica), 27 bytes Shamelessly copying from the 2D solution by @J42161217: SurfaceArea[#~Ellipsoid~#]& Try it online!


5

Jelly, 23 bytes ṣ”Uµṙ1“,()“rḊṖ”yḟØ[V)FQ Try it online! Blegh -1 byte thanks to ovs How it works ṣ”Uµṙ1“,()“rḊṖ”yḟØ[V)FQ - Main link. Takes a string S on the left ṣ”U - Split S on "U" µ ) - Over each section R in the split S: ṙ1 - Rotate by once, shifting the first character to the end ...


5

C++ (gcc), 139 138 134 104 101 100 bytes Uses C++20 features which aren't enabled by default in GCC, but are standard. int f(auto&s,auto&o){int x,y;for(char c,d;s>>c>>x>>d>>y>>d;s>>d)for(y-=d<42;y>x-c%2;o.insert(y--));} Try it online! Uses streams because I wanted to simulate>>a>>merge>>...


5

Factor, 38 bytes [ [ 2dup v. prefix dup pop . ] times ] Try it online! Takes the inputs in the order of kernel seed(reversed) length, and prints out the desired sequence to STDOUT, one item per line. The seed must be of a sequence type that supports pop method, e.g. a vector. [ ! ( kernel seed length -- kernel seed' ) [ ! inner ...


5

Haskell, 69 bytes A small improvement to xnor's answer. (1%) k%n|k?n==k=0|1>0=1+k?n%n k?n=maximum$k:[lcm x$k?(n-x)|x<-[1..n]] Try it online! Compared to the original solution I have simplified the definition of k?n, which calculates one iteration of the Landau function. For \$n=0\$ the function returns \$k\$ and for positive \$n\$ it always returns ...


4

QuadR, 21 bytes ⍎⍵ \+F -F F (+-×) 1- Try it online! or a version that accepts newline-separated test cases. QuadR port of Razetime's APL answer, as it happens to be a set of ⎕R substitution followed by post-processing code. I moved ⍵~'F' part into the regex substitution 'F' → '', which saves a byte. It has no problem running multiple test cases separated ...


4

Jelly, 20 bytes ⁾-Cyœṣ⁾+Fj“+_×ɗ”ḟ”FV Try it online! To quote myself: Well, it's disgustingly hacked together, but it works :D Well, it's much more elegantly hacked together. Haha, beating QuadR! Jelly doesn't like strings. For a more elegant solution that exploits Jelly natural infix notation, see below. Takes input from ARGV. The Footer in the link takes ...


4

05AB1E, 9 5 bytes -4(!) bytes thanks to Command Master! Outputs one value 1-indexed L9BzO Try it online! or Try all cases! L # push the range [1..n] 9B # convert each number to base 9 # this yields the first n natural numbers that don't contain a 9 z # take reciprocal of each number O # sum the list


4

Ruby 2.7, 86 83 bytes Saved three bytes, thanks to Dingus! ->a{(519*a.zip(a.rotate).sum{(_1*_2)**1.535}/(31+27*eval(a*?*)/a.sum**3))**0.65147} Try it online! Expects an array of \$a,b,c\$. TIO uses an older version of Ruby, whereas in Ruby 2.7, we've numbered parameters, which saves three bytes.


4

05AB1E, 9 7 bytes Inputs are the seed, then the kernel and the number of integers last. Thanks to Kevin Cruijssen for -2 bytes! λ£³ā₆*O Try it online! or Try all cases! The challenge reads like it's made for the recursive list generation builtin λ ;) λ£ takes the seed and the number as input and generates the first terms of the sequence starting with the ...


4

APL (Dyalog Extended), 23 (SBCS) Full program. Prompts for seed (reversed of OP's), then required length, then kernel (OP's). ⎕(⌽⊣≢⍛↓(⊢,⍨⊣+.×≢⍛↑)⍣⎕)⎕ Try it online! ⎕(…)⎕ apply the following tacit function with kernel and seed as left and right arguments:  (…)⍣⎕ prompt for required length, and apply the following tacit function that many times:   ≢⍛↑ take ...


4

J, 28 bytes 0{"1(}.@],1#.]*1>@{[)^:(0{[) Try it online! There are a lot of ugly mechanics in this solution, even though the underlying "power of" verb ^: solves the problem nicely. The issue is that we have only two arguments in J, and have to get 3 pieces of information into the verb, so have to make one of the arguments do double duty, ...


4

Python 3, 72 67 64 bytes Takes as input \$k\$, \$s\$ and \$n\$, which are the kernel, seed, and number of integers to display, respectively. lambda k,s,n:s[exec("s+=sum(map(int.__mul__,s[::-1],k)),;"*n):n] Try it online! Python 3, 72 bytes f=lambda k,s,n:len(s)//n*s or f(k,s+[sum(map(int.__mul__,s[::-1],k))],n) Try it online!


4

ARM Thumb-2, strict ruleset, 34 bytes Machine code: 4563 d90d 4664 2700 000a 3c01 5705 f912 6b01 fb06 7705 d8f8 7017 3101 3b01 e7ef 4770 Overcommented assembly with C-callable wrapper (not part of score, just for testing with C) // Now with C-style comments because no ARM syntax highlighting .syntax unified .arch armv6t2 ....


4

JavaScript (ES6),  145  116 bytes n=>~(G=(a,b)=>b?G(b,a%b):a,P=(p,n,i=1)=>m=n?i>n||Math.max(P(p*i/G(p,i),n-i,i),P(p,n,i+1)):p,h=p=>P(p,n)-p&&h(m)-1)`` Try it online! Commented The helper function \$G\$ computes \$\gcd(a,b)\$. G = (a, b) => b ? G(b, a % b) : a The helper function \$P(p,n)\$ recursively computes all integer ...


Only top voted, non community-wiki answers of a minimum length are eligible