Reverse your code, reverse the OEIS

11

The task here is to write a program that takes an natural number, \$n\$, and produces the \$n\$th term of an OEIS sequence. That sequence should have an identifier in the form of A followed by 6 digits. Now when you take your source code and reverse the order of it's bytes to produce a new program, that program should also implement an OEIS sequence. The new sequence should be identified by an A followed by the same 6 digits as last time but in reverse order (including leading zeros).

Now to keep things from being trivial neither the OEIS identifier number nor your program can be palindromes. That is the sequences and programs should be different. You cannot choose a sequence for which it's reverse does not exist or is empty.

For each of your sequences you may choose to either use 0 or 1 indexing. They do not have to use the same indexing. Since some OEIS sequences have a limited domain you need only output the correct numbers for the domain of the sequence. Your required behavior is undefined outside of the domain (you can output 0, crash, order a pizza, etc.).

This is so answers will be scored in bytes with fewer bytes being better.

Post Rock Garf Hunter

Posted 2019-07-10T14:12:16.740

Reputation: 55 382

Do leading zeroes get included in the sequence number reversing? – pppery – 2019-07-11T00:15:27.633

1@pppery They have to be, since OEIS numbers have exactly 6 digits. (it also explicitly says so in the question) – Jo King – 2019-07-11T00:17:10.190

Can we take the input (index) as a string? – TFeld – 2019-07-11T07:50:17.527

Answers

10

05AB1E, 9 4 bytes (A000040 and A040000)

Ø=>≠

Try it online!

!enilno ti yrT

Explanation:

Ø          # nth prime
 =         # output
  >≠       # ignored

:noitanalpxE

≠          # boolean negation (0 if n == 1, 1 otherwise)
 >         # increment
  =        # output
   Ø       # ignored

Grimmy

Posted 2019-07-10T14:12:16.740

Reputation: 12 521

4

Perl 6, 55 bytes (A055642 and A246550)

+*.comb#}]1-_$[)4+_$^**X]_$^[)*..2,emirp-si&(perg(tros{

Try it online!

This is a anonymous Whatever lambda implementing the OEIS sequence A055642 (length of the decimal representation of \$n\$) 0-indexed.

{sort(grep(&is-prime,2..*)[^$_]X**^$_+4)[$_-1]}#bmoc.*+

Try it online!

The reverse is the sequence A246550 (the ordered list of \$x^e\$ where \$x\$ is prime and \$e \ge 4\$) 1-indexed.

Most of this challenge was just finding a good sequence with a not too complicated reverse.

Update: Using torcado's answer, this can be 19 bytes (A010851 and A158010)

{256*$_**2-$_}#{21}

Try it online!

Jo King

Posted 2019-07-10T14:12:16.740

Reputation: 38 234

2

\/\/>, 15 14 bytes (A010851 and A158010)

cn;n*-1*"Ā":j

effectively cn, output 12

j:"Ā"*1-*n;nc

effectively j:"Ā"*1-*n, n(256n-1)

thanks to a friend for finding incredibly simple sequences!

torcado

Posted 2019-07-10T14:12:16.740

Reputation: 550

1

Python 2, 59 bytes (A030000 and A000030)

f=lambda n,k=0:k if`n`in`2**k`else f(n,k+1)#]0[`n`:n adbmal

Try it online!

Defines a function f, returning the nth term of A030000 (smallest nonnegative number \$k\$ such that the decimal expansion of \$2^k\$ contains the string \$n\$), 0-indexed

lambda n:`n`[0]#)1+k,n(f esle`k**2`ni`n`fi k:0=k,n adbmal=f

Try it online!

An anonymous function returning the nth term of A000030 (Initial digit of \$n\$), 0-indexed


Shorter version, which takes strings as input (for both sequences), and both still 0-indexed:

Python 2, 56 bytes

f=lambda n,k=0:`k`*(n in`2**k`)or f(n,k+1)#]0[n:n adbmal

Try it online!

lambda n:n[0]#)1+k,n(f ro)`k**2`ni n(*`k`:0=k,n adbmal=f

Try it online!

TFeld

Posted 2019-07-10T14:12:16.740

Reputation: 19 246

1

Haskell, 47 bytes (A000010 and A010000)

Both sequences are relatively simple.

p n=sum[1|x<-[1..n],gcd x n<2]--2+n*n=n p;1=0 p

Try it online!

p n = the Euler totient function of n (A000010) (1-indexed)

Reversed:

p 0=1;p n=n*n+2--]2<n x dcg,]n..1[-<x|1[mus=n p

Try it online!

p n = 1 if n = 0, otherwise n^2 + 2

It would be interesting to see an answer which doesn't use comments...

Leo Tenenbaum

Posted 2019-07-10T14:12:16.740

Reputation: 2 655