9
\$\begingroup\$

Your task is to write some code that outputs an OEIS sequence, and contains the name of the sequence in the code (A______). Easy enough right? Well here's the catch, your code must also output a second separate sequence when the name of the sequence in the code is changed to the name of the second sequence.

Input Output

Your code can be a function or complete program that takes n via a standard input method and outputs the nth term of the sequence as indexed by the provided index on the OEIS page.

You must support all values provided in the OEIS b files for that sequence, any number not in the b files need not be supported.

Scoring

This is . Your score will be the number of bytes in your code, with less bytes being better.

Example

Here's an example in Haskell that works for A000217 and A000290.

f x|last"A000217"=='0'=x^2|1>0=sum[1..x]

Try it online!

\$\endgroup\$
8
  • \$\begingroup\$ To clarify: Your code should work for two sequences where putting the name of the sequence in that part of the code will output the number of that sequence? \$\endgroup\$
    – hyperneutrino
    Commented Aug 2, 2017 at 16:27
  • \$\begingroup\$ @HyperNeutrino Yes. When the name of the sequence is substituted in it should change the function of the program to be the second sequence. \$\endgroup\$
    – Wheat Wizard
    Commented Aug 2, 2017 at 16:28
  • 1
    \$\begingroup\$ Do the sequences in the code have to have leading zeros. \$\endgroup\$ Commented Aug 2, 2017 at 16:31
  • \$\begingroup\$ @ppperry Yes it should. \$\endgroup\$
    – Wheat Wizard
    Commented Aug 2, 2017 at 16:31
  • 1
    \$\begingroup\$ Is the A required? \$\endgroup\$
    – Okx
    Commented Aug 2, 2017 at 16:33

22 Answers 22

8
\$\begingroup\$

JavaScript (ES6), 16 15 bytes

n=>4&~0xA000004

Works with A000004 (all 0s) and A010709 (all 4s).

Previous 17-byte solution works with A010850 to A010859 inclusive:

n=>~-0xA010850%36

Previous 25-byte solution works with A010850 to A010871 inclusive:

n=>"A010850".slice(5)-39
\$\endgroup\$
7
  • \$\begingroup\$ This is just showing off (I don't know OEIS well enough to see how clever this is :P) \$\endgroup\$ Commented Aug 2, 2017 at 16:30
  • \$\begingroup\$ Strange -- two answers started at 25 bytes and were both golfed to 17 bytes in a minute \$\endgroup\$ Commented Aug 2, 2017 at 16:33
  • \$\begingroup\$ @ppperry Heh, but I could port your answer and it would be only 15 bytes... \$\endgroup\$
    – Neil
    Commented Aug 2, 2017 at 16:36
  • \$\begingroup\$ JS tied with Jelly?! Very nicely done \$\endgroup\$
    – Shaggy
    Commented Aug 2, 2017 at 17:03
  • \$\begingroup\$ @Shaggy Nope; a new shorter jelly answer was posted. \$\endgroup\$ Commented Aug 2, 2017 at 21:44
6
\$\begingroup\$

C#, 28 bytes

n=>n*n*("A000290"[6]<49?1:n)

Works with A000290 (Squares) and A000578 (Cubes).

Try it online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Nice job using a non-constant sequence. \$\endgroup\$ Commented Aug 2, 2017 at 18:55
3
\$\begingroup\$

Haskell, 28 bytes

f x|"A000012"!!6<'3'=1|1<2=x

The second sequence is A007953. Try it online!

\$\endgroup\$
3
\$\begingroup\$

cQuents, 16 bytes

=A000007//5#|A:0

Try it online!, A000007, 1,0,0,0,0...

=A000004//5#|A:0

Try it online!, A000004, 0,0,0,0,0...

Explanation

                    Implicit input A
=A000007            First item in the sequence equals A * 7
        //5                                                 intdiv 5 = 1
           #|A      n equals A
              :     Mode : (sequence): output nth item (1-based)
               0    Rest of the sequence is 0
                    Implicit input A
=A000004            First item in the sequence equals A * 4
        //5                                                 intdiv 5 = 0
           #|A      n equals A
              :     Mode : (sequence): output nth item (1-based)
               0    Rest of the sequence is 0

Thanks to Conor O'Brien for 4//5 = 0 and 7//5 = 1.

If the spec was more flexible, it would be O7A$ and O4A$.

\$\endgroup\$
2
\$\begingroup\$

Cubix, 28 bytes

I:uU**...L..A000578@O\s<s(;?

returns the perfect cubes, a(n)=n^3.

Try it online!

On the other hand,

I:uU**...L..A068601@O\s<s(;?

returns the perfect cubes minus one, a(n)=n^3-1.

\$\endgroup\$
2
\$\begingroup\$

dc, 13 bytes

Edit: Apparently OEIS lists the powers from 0th to 30th - I just did a search on these sequences and it turns out the original 13 byte solution is the golfiest. But I found another solution for just 1 byte more that works for 9 sequences.

Solution for A000012 (constant 1's sequence):

?A000012 4%^p

Try it online!

Solution for A001477 (non-negative integers):

?A001477 4%^p

Try it online!

Solution for A000290 (perfect squares sequence):

?A000290 4%^p

Try it online!

Ungolfed/Explanation

These solutions make use of the fact that dc interprets A as 10, so A001477 becomes the value 10001477. Further it exploits that the sequences are n^0, n^1 and n^2 which coincides with 10000012 % 4 == 0, 10001477 % 4 == 1 and 10000290 % 4 == 2.

So these sequences are xyz(n) = n ^ (xyz % 4).

Command          Description          Example (3) 
?              # Push the input       [3]
 A000290       # Push sequence name   [3,10000290]
         4%    # Top %= 4             [3,2]
           ^   # Pop x,y & push y^x   [9]
            p  # Print the top        [9]

14 byte solution for 9 sequences

The idea is still the same, this time we need to do a % 97, to get the right power - it works for sequences A010801, A010802, A010803, A010804, A010805, A010806, A010807, A010808 and A010809 (these are the sequences n^13,...,n^21).

Here's the first one:

?A010801 97%^p

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ +1 for supporting more than two sequences! \$\endgroup\$
    – Neil
    Commented Aug 3, 2017 at 8:58
1
\$\begingroup\$

Python 2, 25 17 bytes

print'A000012'[5]

Works for A000004 and A000012. (input is ignored because the sequences are all constant terms).

\$\endgroup\$
0
1
\$\begingroup\$

Befunge 98, 10 bytes

#A000012$q

Also works for A000004. Output by exit code.

\$\endgroup\$
1
\$\begingroup\$

Jelly, 17 bytes

“A000578”OS%⁵ạ6*@

Try it online!

“A000578”OS%⁵ạ6*@  Main link
“A000578”          String
         O         Codepoints
          S        Sum (364 for A000290, 373 for A000578)
           %⁵      Modulo 10 (4 for A000290, 3 for A000578)
             ạ6    Absolute Difference with 6 (2 for A000290, 3 for A000578)
               *@  [left argument] ** [result of last link (right argument)]

Also works with A000290

\$\endgroup\$
1
  • \$\begingroup\$ Good job using non-constant sequences. \$\endgroup\$ Commented Aug 2, 2017 at 19:00
1
\$\begingroup\$

PowerShell, 23 bytes

+(0xA000012-eq160mb+18)

Try it online!

Uses A000012 (the all ones sequence) and A000004 (the all zeros sequence).

Leverages several neat tricks. We use 0x as the hexadecimal operator onto the sequence which gives us 167772178. That's compared to see if its -equal to 160mb+18 using the mb operator (160mb is 167772160). That Boolean result is then cast as an int with + to output the proper 1 or 0. Note that any sequence in the code other than A000012 will result in 0 being output.

\$\endgroup\$
1
\$\begingroup\$

Neim, 10 9 bytes

A000012ᛄ>

Explanation:

A            Push 42
 000012      Push 4
 or
 A007395     Push 7395
        ᛄ     Modulo 2
         >    Increment

A000012 (all ones) and A007395 (all twos)

A function that takes the input on the top of the stack and leaves the output on the top of the stack.

Try it online!

\$\endgroup\$
0
0
\$\begingroup\$

Gaia, 9 bytes

A000012₉/

Works with A000012 and A000004.

Try A000012!

Try A000004!

Explanation

A          Undefined (ignored)
 000012    Push 12
       ₉   Push 9
        /  Integer division, results in 1


A          Undefined (ignored)
 000004    Push 4
       ₉   Push 9
        /  Integer division, results in 0
\$\endgroup\$
2
  • \$\begingroup\$ Why does everyone keep posting A00004 and A00012? \$\endgroup\$ Commented Aug 2, 2017 at 16:39
  • \$\begingroup\$ @ppperry A000004 is the zero sequence and A000012 is the ones sequence. Simply floor divide the numbers by 9 and output the result forever. \$\endgroup\$ Commented Aug 2, 2017 at 20:47
0
\$\begingroup\$

PowerShell, 12 bytes

'A000012'[5]

Try it online!

Works for A000012 (the all ones sequence) and A000004 (the all zeros sequence).

Port of ppperry's Python answer.

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 9 bytes

A000004¨θ

Try it online!

Works for A000004 and A000012.

\$\endgroup\$
0
\$\begingroup\$

Actually, 10 bytes

A000004X.⌂

Try it online!

Works for A000004 and A000012.

\$\endgroup\$
0
\$\begingroup\$

Jelly, 10 bytes

6ị“A000004

Try it online!

Works for A000004 and A000012.

\$\endgroup\$
0
\$\begingroup\$

Pyth, 11 bytes

@"A000004"5

Try it here.

Supports A000004 and A000012.

\$\endgroup\$
0
\$\begingroup\$

Husk, 20 bytes

This one returns a bit more interesting sequences, again solutions are 1-indexed.

This works for A000040 (the prime numbers):

!!i→"A000040"e:0İfİp

Try it online!

And this one for A000045 (the Fibonacci numbers):

!!i→"A000045"e:0İfİp

Try it online!

Explanation

This makes use of the fact that the last digit of the sequence names have a different parity:

                      -- implicit input N
             e        -- construct a list with:
              :0İf    --   list of Fibonacci numbers (prepend 0)
                  İp  --   list of the prime numbers
  i→"Axxxxx?"         -- get the last character and convert to number,
 !                    -- use it as modular index (0 -> primes, 5 -> Fibonacci)
!                     -- get the value at the Nth index
\$\endgroup\$
0
\$\begingroup\$

AHK, 40 bytes

a:=SubStr("A000004",6)//9
Loop
Send %a%,

Output: 0,0,0,0,0,0,0,0,0,0,0,0,...

a:=SubStr("A000012",6)//9
Loop
Send %a%,

Output: 1,1,1,1,1,1,1,1,1,1,1,1,...

This might not be the shortest code but I bet it's the shortest sequence pair we can find. A000004 is the zero sequence and A000012 is the ones sequence. Simply floor divide the numbers by 9 and output the result forever.

\$\endgroup\$
0
\$\begingroup\$

QBIC, 28 bytes

p=!_s@A000035`,-1|!?:%2+5-p

This switches between the sequences A000034 (1, 2, 1, 2, 1 ...) and A000035 (0, 1, 0, 1, 0, 1 ...)

Explanation

p=                  Set p to 
  !            !    A numeric representation of
   _s         |     a substring of
     @A000035`      our sequence code (either A0035 or A0034)
     ,-1            taking just one character from the right.
?:%2                PRINT <n> MOD 2 (gives us a either 0 or 1)
    +5-p            Plus 1 for seq A24 (5-4), or plus 0 for A35
\$\endgroup\$
0
\$\begingroup\$

Husk, 16 bytes

Both solutions are 1-indexed.

This works for A000351 (powers of 5):

!¡*i!6"A000351"1

Try it online!

And this one for A000007 (powers of 0):

!¡*i!6"A000007"1

Try it online!

Explanation

It makes use that the names A000351, A000007 contain the right digit D at the position 6, such that the sequence is D^0,D^1,D^2,...:

                  -- implicit input N
   i!6"AxxxxDx"   -- get the right digit D and convert to number,
 ¡*            1  -- iterate (D*) infinitely beginning with 1,
!                 -- extract the value at Nth position
\$\endgroup\$
0
\$\begingroup\$

MATL, 14 bytes

A000027 The positive integers

A000027 3\Gw-&

Try it online!

A001477 The nonnegative integers

A001477 3\Gw-&

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.