Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is the robbers' thread. The cops' thread goes here.

In the cops thread, the task was to write a program/function that takes a positive (or non-negative) integer and outputs/returns another number (not necessarily integer). The robbers task is to unscramble the code the cops used to produce this output.

The cracked code doesn't have to be identical, as long as it has the same length and any revealed characters are in the correct positions. The language must also be the same (version numbers can be different). The output must of course be identical.

No-ops can be used in robber's solution.

The winner of the robbers thread will be the user who has cracked the most submissions by May 7th 2016. If there's a tie, the user who has cracked submissions with the longest combined code will win.

The submission should be formatted like this:

Language, nn characters (including link to answer), Cop's username

Code:

function a(n)
    if n<2 then
        return n
    else
        return a(n-1) + a(n-2)
    end
end

Output

a(0) returns 0
a(3) returns 2

Optional explanation and comments.

share|improve this question
    
These rules here are different from the cops thred, where it says: However, any proposed source code that produces the same set of output also counts as valid, as long as it is also found in OEIS. – flawr yesterday
    
What happens if the examples match multiple OEIS series ? This Just happened with Adnan and me – FliiFe 9 hours ago

31 Answers 31

MATL, 5 bytes, Luis Mendo

H5-*|

This code calculates abs((2-5)*input) which is just a(n)=3*n for positive numbers, which is http://oeis.org/A008585

share|improve this answer
    
Well done! My original code was 35B*s :-) – Luis Mendo yesterday

Hexagony, 7 bytes, Adnan, A005843

?{2'*!@

or

 ? {
2 ' *
 ! @

Try it online!

Simply doubles the input (and assumes positive input). The code is (for once) simply executed in reading order. The code uses three memory edges A, B, C with the memory pointer starting out as shown:

enter image description here

?    Read integer from STDIN into edge A.
{    Move memory pointer forwards to edge B.
2    Set edge B to 2.
'    Move memory pointers backwards to edge C.
*    Multiply edges A and B and store result in C.
!    Print result to STDOUT.
@    Terminate program.
share|improve this answer
    
The exact same with what I had! :) – Kenny Lau yesterday
    
@KennyLau I think the solution is unique up to swapping the roles of B and C. – Martin Büttner yesterday

J, 7 bytes, Cᴏɴᴏʀ O'Bʀɪᴇɴ

Code

2+*:@p:

Output

   f =: 2+*:@p:
   f 0
6
   f 2
27

Try it with J.js.

How it works

Sequence A061725 is defined as a(n) := pn² + 2, where pn is the (n + 1)th prime number.

2+*:@p:  Monadic verb. Argument: n

    @    Atop; combine the verbs to the right and to the left, applying one after
         the other.
     p:  Compute the (n+1)th prime number.
  *:     Square it.
2+       Add 2 to the result.
share|improve this answer
    
Nice job! You understand the code more than I did XD – Cᴏɴᴏʀ O'Bʀɪᴇɴ yesterday

Element, 7 bytes, PhiNotPi, A000042

_'[,1`}

Notes: I was misled by the } for soooooo long. So it also matches [.

Try it online!


How it works:

_'[,1`}
_        main_stack.push(input());
 '       control_stack.push(main_stack.pop());
  [      Object temp = control_stack.pop();
         for(int i=0;i<temp;i++){
   ,         Object a = main_stack.pop(); //is actually zero
             main_stack.push(a.toChars()[0]);
             main_stack.push(a);
    1        main_stack.push(1);
     `       System.out.println(main_stack.pop());
      }  }
share|improve this answer
    
Nice! I was trying this, but I couldn't figure out how to get the , to stop breaking things. – QPaysTaxes yesterday
    
My trick was to do ,$ to produce a 1, which gave me an excuse to put the really confusing , operator in my program. – PhiNotPi yesterday
    
@QPaysTaxes Thanks :) – Kenny Lau yesterday
    
I was stuck at the } for tooooo long :( – Kenny Lau yesterday

05AB1E, 4 bytes, Paul Picard, A001317

Code:

$Fx^

Try it online!

Explanation:

$      # Pushes 1 and input
 F     # Pops x, creates a for-loop in range(0, x)
  x    # Pops x, pushes x and 2x
   ^   # Bitwise XOR on the last two elements
       # Implicit, ends the for-loop
       # Implicit, nothing has printed so the last element is printed automatically

The sequence basically is a binary Sierpinski triangle:

f(0)=      1                    =1
f(1)=     1 1                   =3
f(2)=    1 0 1                  =5
f(3)=   1 1 1 1                 =15
f(4)=  1 0 0 0 1                =17

And translates to the formula a(n) = a(n - 1) ^ (2 × a(n - 1))

Luckily, I remembered this one :)

share|improve this answer
1  
And it is the exact same one, indeed :D – Paul Picard yesterday

05AB1E, 5 bytes, Adnan, A001788

Læ€OO

Try it online! This uses an alternative definition given on the page. Explanation:

Læ€OO
L     range;      [1..n]
 æ    powerset;   [[], [1], ..., [1..n]]
  €O  mapped sum; [0, 1, ..., T(n)]
    O sum;        [a(n)]
share|improve this answer

PHP, 41 bytes, insertusernamehere, A079978

echo/* does n%3=0 */$argv[1]%3<1?1:0    ;

Returns 1 if its argument is a multiple of 3, and 0 otherwise. Not much beyond that.

share|improve this answer

MATL, 9 bytes, beaker, A022844

Code (with a whitespace at the end):

3x2xYP*k 

Try it online!

Found the following three matches with a script I wrote:

Found match: A022844
info: "name": "Floor(n*Pi).",

Found match: A073934
info: "name": "Sum of terms in n-th row of triangle in A073932.",

Found match: A120068
info: "name": "Numbers n such that n-th prime + 1 is squarefree.",

I tried to do the first one, which is basically done with YP*k:

3x2x       # Push 3, delete it, push 2 and delete that too
    YP     # Push pi
      *    # Multiply by implicit input
       k   # Floor function
share|improve this answer

Jolf, 3 bytes, Easterly Irk, A001477

axx

Consists of a simple cat (ax) followed by a no-op. Not sure what the cop was going for here.

share|improve this answer
    
That is most definitely not the identity function. It's alert the input. There are actual identity functions :P – Cᴏɴᴏʀ O'Bʀɪᴇɴ yesterday

Java, 479 bytes, Daniel M., A000073

Code:

import java.util.*;
public class A{

    public static int i=0;
    public boolean b;

    static A a = new A();

    public static void main(String[] args){
        int input = Integer.parseInt(args[0]);

        LinkedList<Integer> l = new LinkedList<>();
        l.add(1);
        l.add(0);
        l.add(0);

        for(int ix = 0; ix<=input; ix++)if(ix>2){
            l.add(0,l//d
            .get(1)+l.peekFirst()+     l.get(2));
        }

        System.out.println(input<2?0:l.pop()
              +(A.i        +(/*( 5*/ 0 )));
    }
}

If you miss non-revealed characters, they are replaced with spaces.

share|improve this answer
1  
Very different from the original code, but still, congrats! – Daniel M. yesterday

Jolf, 5 characters, Cᴏɴᴏʀ O'Bʀɪᴇɴ, A033536

Code:

!K!8x

Output:

a(2) = 8
a(10) = 4738245926336
share|improve this answer
    
This was the exact same answer I had. I was about to post it. :( – QPaysTaxes 2 days ago
    
Neither answer is the original, but they are functionally the same. – Cᴏɴᴏʀ O'Bʀɪᴇɴ 2 days ago
    
@QPaysTaxes Sorry :( – Kenny Lau 2 days ago

05AB1E, 3 bytes, Adnan, A000292

LLO

Output

a(9) = 165
a(10) = 220

How it works

LLO Stack
L   [1,2,3,4,5,6,7,8,9]                         range
 L  [1,1,2,1,2,3,1,2,3,4,...,1,2,3,4,5,6,7,8,9] range of range
  O sum all of them

The mathematical equivalent is sum(sum(n)), where sum is summation.

share|improve this answer
    
Nice job, that was the exact same solution :) – Adnan yesterday

Jolf, 11 bytes, QPaysTaxes, A000005

aσ0xxdxxxxx

Simple enough: alert the σ0 (number of divisors of) x, then put useless stuff at the end.

Try it online! The test suite button's a bit broke, but still shows proper results.

(You could've golfed it down to two bytes! Just σ0 would've done nicely.)

share|improve this answer
1  
Wow! Le builtins minuscules! +1 – Adnan yesterday
1  
This is nothing like what I had, but it sure works. Mine was so long because you didn't have any mention of finding divisors in the docs. – QPaysTaxes yesterday
    
@QPaysTaxes I guess I need to update the docs :P But seriously, just Ctrl+F the source code ;) – Cᴏɴᴏʀ O'Bʀɪᴇɴ yesterday
    
I put my original code in my question if you wanna see it. In retrospect, I should have showed different characters :P – QPaysTaxes yesterday

Python 2, 87 bytes, Sp3000, A083054

n=input()
_=int(3**.5*n)-3*int(n/3**.5)########################################
print _

Not that hard, actually. Just searched for sequences that met the constraints until I found one that could be generated in the given space.

share|improve this answer

Jolf, 11 bytes, RikerW, A011551

Code:

c*mf^+91x~P

Explanation:

     +91     # add(9, 1) = 10
    ^   x    # 10 ** input
  mf         # floor function (no-op)
 *       ~P  # multiply by phi
c            # ¯\_(ツ)_/¯

Try it here.

share|improve this answer

JavaScript (ES6), 119 bytes, Cᴏɴᴏʀ O'Bʀɪᴇɴ, A178501

x=>(n="=>[[["|x|"##r(###f#n###;##")|n?Math.pow("#<1##].c####t.##pl##[####nc#"|10,"y([###(###(#]###)"|x-1|``):0|`#h####`

I'm sure the actual code generates a trickier sequence than this, but with just the two outputs, this OEIS sequence is simple and matches them.

Without all the ignored characters, the algorithm is just x=>x?Math.pow(10,x-1):0.

share|improve this answer

05AB1E, 5 bytes, Luis Mendo, A051696

Code:

Ðms!¿

Explanation:

Ð      # Triplicate input.
 m     # Power function, which calculates input ** input.
  s    # Swap two top elements of the stack.
   !   # Calculate the factorial of input.
    ¿  # Compute the greatest common divisor of the top two elements.

So, basically this calculates gcd(n!, nn), which is A051696.

Try it online!.

share|improve this answer

PHP, 18 bytes, insertusernamehere, A023443

Code:

echo$argv[1]+0+~0;

Output:

a(0) = -1
a(1) = 0
share|improve this answer
    
Very nice approach. My source was slightly different: echo$argv[1]+-+!0;. :) – insertusernamehere 20 hours ago

Element, 10 bytes, PhiNotPi, A097547

2_4:/2@^^`

Try it online!

Output

a(3) = 6561
a(4) = 4294967296
share|improve this answer

Reng v3.3, 36 bytes, Cᴏɴᴏʀ O'Bʀɪᴇɴ, A005449

iv:#+##->>)2%æ~¡#~
#>:3*1+*^##</div>

Output

a(1) = 2
a(3) = 15

Explanation

I completely ignored the prespecified commands, except the ) because I did not have enough space.

The actually useful commands are here:

iv      >>)2%æ~
 >:3*1+*^

Stretched to a straight line:

i:3*1+*)2%æ~

With explanation:

i:3*1+*)2%æ~ stack
i            [1]      takes input
 :           [1,1]    duplicates
  3          [1,1,3]  pushes 3
   *         [1,3]    multiplies
    1        [1,3,1]  pushes 1
     +       [1,4]    adds
      *      [4]      multiplies
       )     [4]      shifts (does nothing)
        2    [4,2]    pushes 2
         %   [2]      divides
          æ  []       prints
           ~ []       halts

The formula is a(n) = n(3n+1)/2.

share|improve this answer

Pyke, 6 bytes, muddyfish, A005563

0QhXts

Yay hacks! The 0Qh and s are no-ops. hXt just computes (n + 1) ^ 2 - 1.

share|improve this answer

J, 8 bytes, Kenny Lau, A057427

Code:

(-%- )\.

Output:

a(0) = 0
a(1..29) = 1

I don't think this is intended. And I don't know why J had this behavior. But it works.

share|improve this answer
    
Gonna add one more restriction xd – Kenny Lau 20 hours ago

Octave (34 bytes) by Stewie Griffin

The sequence is A066911.

@(m)(mod(m,u=1:m  )&isprime(u))*u'
share|improve this answer
    
Nice =) For the record, I had u=0:m-1. The same sequence. – Stewie Griffin 10 hours ago

Python, 108, CAD97, A005132

def a(n):
 if n == 0: return 0
 f=a(n-1)-n
 return f if f>0 and not f in(a(i)for i in range(n))else a(n-1)+n

Obfuscated code :

def a(n):
 ###n####0######n#0
 f=a#######
 return f #f#####a###### f ####a(##f###i#i###a####n##else a#######

Outputs:

>>> a(0)
0
>>> a(4)
2
>>> a(16)
8
>>> a(20)
42
share|improve this answer
    
Exactly what I had. Expected it to be easy, honestly. – CAD97 8 hours ago

C (71 bytes) by mIllIbyte

The sequence is floor(n/4).

x;f(_){x/=4;
    }
main(){
 scanf("%d",&x);
 f( 6);
 printf("%d", x);
}
share|improve this answer

Python 3 (58 bytes) by CAD97

The sequence is A062318. There were a lot of unnecessary characters in this one, so I commented some out.

a=lambda n: ~-( -~(n%2)
# if ###### else 
*3**(n//2))#)##)
share|improve this answer
    
The original was the slightly more literal a=lambda n:int(3**(n/2)-1 if n%2==0 else 2*3**(n/2-1/2)-1), but this works as well and is golfier. GG – CAD97 2 hours ago

Pyth, 70 bytes, FliiFe, A070650

Code (with obfuscated version below):

DhbI|qb"#"qb"#"R!1Iqb"#";=^Q6+""s ]%Q27  ;.qlY+Q1Ih+""Z##;.q)=Z+Z1;@YQ
DhbI|qb"#"qb"#"R!1Iqb"#"#####+""s####2###;##lY+Q1Ih+""Z#####)=Z+Z1;@YQ (obfuscated)

This basically does:

=^Q6%Q27

It calculates a(n) = n6 % 27, which is A070650. Explanation:

=^Q6       # Assign Q to Q ** 6
    %Q27   # Compute Q % 27
           # Implicit output

Try it here

share|improve this answer
    
Oops, that's not the one. I updated my answer with another one – FliiFe 10 hours ago
    
From the rules, this is valid. Congrats ! – FliiFe 9 hours ago
    
I guess I can tell you the sequence now, It's A007770 (0-indexed) – FliiFe 9 hours ago
    
@FliiFe Oh, I would never have guessed that :p – Adnan 9 hours ago
    
Actually, if you know the sequence, it's easily spottable, but if you don't, it becomes really hard – FliiFe 9 hours ago

J, 8 bytes, Kenny Lau, A057427

Code:

(>1-*)<.

Output:

a(0) = 0
a(i) = 1 for 0 < i < 30

The sequence is simply signum(n). The program computes floor(n) > 1 - signum(floor(n)), which is signum(n) for non-negative integers n.

share|improve this answer

JavaScript (ES7), 10 bytes, user81655, A033999

t=>~t##**#
t=>~t+t**t

Output

f(0) -> 1
f(1) -> -1
share|improve this answer
    
Are you sure? I get f(0) -> 0 for this solution. – user81655 5 hours ago

PHP, 137 bytes, insertusernamehere, A000959

Code:

for($s=range(1,303);$i<($t=count($s));$s=array_merge($s))for($j=($k=++$i==1?2:$s[$i-1])-1;$j<$t;$j+=$k )unset($s[$j]);echo$s[$argv[1]-1];

Output:

a(3)  =   7
a(7)  =  21
a(23) =  99
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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