Author Topic: OEIS golfing!  (Read 1648 times)

0 Members and 1 Guest are viewing this topic.

_iPhoenix_

• Custom title
• Super User
• Join Date: Mar 2017
• Location: Location
• Posts: 674
• Post Rating Ratio: +16/-1
• ███████ ▼ ♪ Best music ♫
• Gender:
OEIS golfing!
« on: June 16, 2018, 07:00:48 pm »
Inspired by this, and highly inspired by the Programming Puzzles and Code Golf (PPCG) stack exchange. If there are any doubts with the rules, default to their rules.

You must take a sequence in the OEIS (it must be known that it has infinite terms), and write a program taking a input, n, (somehow. If your language doesn't support taking input, editing the source code to insert a decimal number is allowed) and outputting, in base 10 plain text the first n terms of that sequence. You must start at the first term in the sequence. This can be via a file, a function return, etc

No hard-coding solutions, you should use an algorithm.
No asking for the sequence from the user.
The program should be your own work. No stealing programs from other users.
(Use good judgement)
Programming languages are defined by their implementation, so the programming language must have a working compiler/interpreter by the time of this post.
Feel free to help others with their code golf. Be sure to explain what you changed and why it works!

If you are showing off your code golf, format your post like this:
(click to show/hide)

I'll go first.

JavaScript, A025480, 70 bytes
Code: [Select]
`f=r=>{for(s=[],i=0;i<r;i++)s[2*i]=i,s[2*i+1]=s[i];return s.length=r,s}`Defines a function, f, that returns an array of integers. Call it from the console with f(n), with n being your input.

inb4
JavaScript, A000012, 21 bytes
Code: [Select]
`f=r=>Array(r).fill(1)`Defines a function, f, that returns an array of integers. Call it from the console with f(n), with n being your input.
« Last Edit: June 17, 2018, 01:17:19 am by _iPhoenix_ »

• Calculators owned: Two TI-84+ CE's

"walruses are better than tuxedo chickens, all hail the great " ~ me
Evolution of my avatar:

Juju

• aka Yuki Kagayaki aka J̵̭͕͇ù̞̭̝̯̦j̴̭̙̗͖͡ù͏͓̲̕
• CodeWalrus Staff
• Super User
• Server Maintenance
• Moderator
• Forum Maintenance
• Original 5
• CodeWalrus Supporter
• Join Date: Nov 2014
• Location: Inside a walrus
• Posts: 3290
• Post Rating Ratio: +39/-2
• Couch potato
• Gender:
• WalriiPoints: 99999
Re: OEIS golfing!
« Reply #1 on: June 16, 2018, 08:18:55 pm »
Sounds fun. Let's golf every sequence!

JavaScript, A005132, 80 bytes
Code: [Select]
`f=x=>{for(q=[0],s=i=0;i<x;)q[++i]=s+=i*(s-i<=0||!!~q.indexOf(s-i))*2-i;return q}`Defines a function, f, that returns an array of integers. Call it from the console with f(n), with n being your input.

SVG, A005132, 271 267 bytes
Code: [Select]
`<svg xmlns="http://www.w3.org/2000/svg"><path id="p" stroke="#000" fill="none"/><script>/*<![CDATA[*/q=[];a="M2,325";for(s=i=0;i<65;)c=s-++i<=0||!!~q.indexOf(s-i),q[i]=s+=j=i*c*2-i,a+=`a1,1 0 0,\${i%2^c} \${j}0,0`;document.all[1].setAttribute("d",a)//]]></script></svg>`Interesting graphic of the above sequence (first 65 steps)

JavaScript (ES6), A001477, 25 bytes
Code: [Select]
`f=x=>[...Array(x).keys()]`Defines a function, f, that returns an array of integers. Call it from the console with f(n), with n being your input.
« Last Edit: June 16, 2018, 10:03:52 pm by Juju »
• Consoles, mobile devices and vintage computers owned: A lot
YUKI-CHAAAANNNN
In the beginning there was walrii. In the end there will be walrii. All hail our supreme leader --Snektron

if you wanna throw money at me and/or CodeWalrus monthly it's here

_iPhoenix_

• Custom title
• Super User
• Join Date: Mar 2017
• Location: Location
• Posts: 674
• Post Rating Ratio: +16/-1
• ███████ ▼ ♪ Best music ♫
• Gender:
Re: OEIS golfing!
« Reply #2 on: June 16, 2018, 09:18:12 pm »
JavaScript, A174375, 59 bytes
Code: [Select]
`f=n=>{for(i=0,q=[];i<n;i++) q.push((j=i*i)-(j^i));return q}`Defines a function, f, that returns an array of integers. Call it from the console with f(n), with n being your input.

This one is pretty cool, despite its apparent simplicity. If you graph f(n) against n, it approximates a Sierpinski gasket when the maximum n is a power of two.
• Calculators owned: Two TI-84+ CE's

"walruses are better than tuxedo chickens, all hail the great " ~ me
Evolution of my avatar:

Juju

• aka Yuki Kagayaki aka J̵̭͕͇ù̞̭̝̯̦j̴̭̙̗͖͡ù͏͓̲̕
• CodeWalrus Staff
• Super User
• Server Maintenance
• Moderator
• Forum Maintenance
• Original 5
• CodeWalrus Supporter
• Join Date: Nov 2014
• Location: Inside a walrus
• Posts: 3290
• Post Rating Ratio: +39/-2
• Couch potato
• Gender:
• WalriiPoints: 99999
Re: OEIS golfing!
« Reply #3 on: June 16, 2018, 11:01:52 pm »
JavaScript, A000007, 30 bytes
Code: [Select]
`f=x=>[1,...Array(x-1).fill(0)]`Defines a function, f, that returns an array of integers. Call it from the console with f(n), with n being your input.

JavaScript, A076337, 8 bytes
Code: [Select]
`[509203]`Returns the entire sequence with terms proven so far. (This one's kinda stupid, but yeah. It only have one term so far.)

JavaScript, A000079, 37 bytes
Code: [Select]
`f=x=>Array.from(Array(x),(a,i)=>2**i)`Defines a function, f, that returns an array of integers. Call it from the console with f(n), with n being your input.
« Last Edit: June 16, 2018, 11:07:35 pm by Juju »
• Consoles, mobile devices and vintage computers owned: A lot
YUKI-CHAAAANNNN
In the beginning there was walrii. In the end there will be walrii. All hail our supreme leader --Snektron

if you wanna throw money at me and/or CodeWalrus monthly it's here

_iPhoenix_

• Custom title
• Super User
• Join Date: Mar 2017
• Location: Location
• Posts: 674
• Post Rating Ratio: +16/-1
• ███████ ▼ ♪ Best music ♫
• Gender:
Re: OEIS golfing!
« Reply #4 on: June 17, 2018, 01:11:38 am »
Python 3, A005131, 53 bytes
Code: [Select]
`f=lambda n:[2*~-i//3if 1==i%3else 1for i in range(n)]`Defines a function, f, that returns an array of integers. Call with f(n), with n being your input.

(We don't need to stick to JS )
• Calculators owned: Two TI-84+ CE's

"walruses are better than tuxedo chickens, all hail the great " ~ me
Evolution of my avatar:

Juju

• aka Yuki Kagayaki aka J̵̭͕͇ù̞̭̝̯̦j̴̭̙̗͖͡ù͏͓̲̕
• CodeWalrus Staff
• Super User
• Server Maintenance
• Moderator
• Forum Maintenance
• Original 5
• CodeWalrus Supporter
• Join Date: Nov 2014
• Location: Inside a walrus
• Posts: 3290
• Post Rating Ratio: +39/-2
• Couch potato
• Gender:
• WalriiPoints: 99999
Re: OEIS golfing!
« Reply #5 on: July 09, 2018, 09:00:32 pm »
Bash/Ruby, xkcd2016, 160 bytes
Code: [Select]
`curl http://oeis.org/stripped.gz|gzcat|sed 1,4d|ruby -F, -e'a=(0..29999).to_a;while gets;a-=(s=\$_.split).slice(1,s.length-2).map{|x|x.to_i};end;p a.slice(0,20)'`Returns the first 20 integers which do not appear in the example terms of another OEIS sequence. (Warning: it downloads the entire OEIS database which is a 22MB gzipped file, might want to download it first and cat it instead if you're going to run it more than once.)

At the time of writing this post, the first 50 uninteresting numbers are:

Code: [Select]
`[17843, 20067, 20227, 20990, 22589, 22595, 22978, 23174, 23543, 23615, 23735, 23876, 23986, 24085, 24140, 24159, 24555, 24628, 25061, 25266, 25330, 25611, 25743, 25965, 26301, 26517, 26534, 26673, 26708, 26755, 27266, 27292, 27357, 27619, 27663, 27765, 27815, 27988, 28166, 28255, 28311, 28330, 28353, 28427, 28454, 28466, 28473, 28515, 28599, 28835]`
(It doesn't qualify either because it's not really an OEIS sequence, require pulling up the entire OEIS database to calculate the sequence and might change at any time when someone submits a new sequence to the OEIS, but I still find it very interesting.)
« Last Edit: July 09, 2018, 09:27:23 pm by Juju »
• Consoles, mobile devices and vintage computers owned: A lot