20
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – flawr Apr 9 '16 at 18:52
  • \$\begingroup\$ What happens if the examples match multiple OEIS series ? This Just happened with Adnan and me \$\endgroup\$ – FliiFe Apr 10 '16 at 20:06
  • \$\begingroup\$ @FliiFe Under the current rules, any code which matches the cop's code and outputs an OEIS sequence whose values coincide with the cop's examples is a valid crack. \$\endgroup\$ – Mego Apr 11 '16 at 20:22
  • \$\begingroup\$ Has this finished? Is there a winner? \$\endgroup\$ – Andrew Savinykh May 16 '16 at 2:38

57 Answers 57

7
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Well done! My original code was 35B*s :-) \$\endgroup\$ – Luis Mendo Apr 9 '16 at 20:35
5
\$\begingroup\$

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.
\$\endgroup\$
  • \$\begingroup\$ The exact same with what I had! :) \$\endgroup\$ – Leaky Nun Apr 9 '16 at 10:00
  • \$\begingroup\$ @KennyLau I think the solution is unique up to swapping the roles of B and C. \$\endgroup\$ – Martin Ender Apr 9 '16 at 10:01
4
\$\begingroup\$

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.
\$\endgroup\$
  • \$\begingroup\$ Nice job! You understand the code more than I did XD \$\endgroup\$ – Conor O'Brien Apr 9 '16 at 14:39
4
\$\begingroup\$

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)]
\$\endgroup\$
4
\$\begingroup\$

JavaScript, 10 bytes, user81655, A033999

I think I got it. Yeah. This one was really hard. I like the submission because it relies heavily on precedences.


It's the sequence A033999:

a(n) = (-1)^n.

Source

t=>~t.z**t

Explanation

If you split this code according to the JavaScript operator precedences you get:

  1. . (precedence 18) gets evaluated first and t.z will return undefined.
  2. ~ (precedence 15) tries to cast undefined, resulting in 0, and returns -1 after bitwise not.
  3. ** (precedence 14) will return -1 ^ t, where t is odd or even, resulting in -1 or 1.

Demo

console.log(
    (t=>~t.z**t)(0),
    (t=>~t.z**t)(1),
);

Try before buy


I will award a 100 rep bounty on this cool Cop submission.

\$\endgroup\$
  • 1
    \$\begingroup\$ You are correct, congratulations! :) \$\endgroup\$ – user81655 Apr 14 '16 at 8:32
  • \$\begingroup\$ I consider myself well-versed in javascript, but I have no idea how this works. \$\endgroup\$ – Conor O'Brien Apr 14 '16 at 14:04
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ I've added an explanation. Hopefully it explains it well enough. \$\endgroup\$ – insertusernamehere Apr 14 '16 at 14:21
  • \$\begingroup\$ That's why the brute-force didn't find it. I used a transpiler with wrong op precedence >_< \$\endgroup\$ – Conor O'Brien Apr 14 '16 at 14:39
3
\$\begingroup\$

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());
      }  }
\$\endgroup\$
  • \$\begingroup\$ Nice! I was trying this, but I couldn't figure out how to get the , to stop breaking things. \$\endgroup\$ – Nic Hartley Apr 9 '16 at 6:09
  • \$\begingroup\$ My trick was to do ,$ to produce a 1, which gave me an excuse to put the really confusing , operator in my program. \$\endgroup\$ – PhiNotPi Apr 9 '16 at 6:13
  • \$\begingroup\$ I was stuck at the } for tooooo long :( \$\endgroup\$ – Leaky Nun Apr 9 '16 at 6:14
3
\$\begingroup\$

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.

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

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
\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ That is most definitely not the identity function. It's alert the input. There are actual identity functions :P \$\endgroup\$ – Conor O'Brien Apr 10 '16 at 1:23
3
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ Very different from the original code, but still, congrats! \$\endgroup\$ – Daniel M. Apr 10 '16 at 2:20
3
\$\begingroup\$

Ruby, 38 bytes, histocrat, A008592

->o{(s="+o++o"*5).sum==03333&&eval(s)}

Could be different from the intended solution as I found this by hand.

\$\endgroup\$
  • \$\begingroup\$ Nicely done! Intended solution was similar: "+f+=f"*5. \$\endgroup\$ – histocrat Apr 12 '16 at 13:37
3
\$\begingroup\$

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) XOR (2 × a(n - 1))

Luckily, I remembered this one :)

\$\endgroup\$
  • 1
    \$\begingroup\$ And it is the exact same one, indeed :D \$\endgroup\$ – Paul Picard Apr 9 '16 at 13:06
3
\$\begingroup\$

S.I.L.O.S, betseg, A001844

readIO
a=i
a+1
i*2
i*a
i+1
printInt i

Try it online!

\$\endgroup\$
  • 4
    \$\begingroup\$ goddamnit you ninjad me \$\endgroup\$ – Destructible Lemon Oct 20 '16 at 9:22
2
\$\begingroup\$

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

Code:

!K!8x

Output:

a(2) = 8
a(10) = 4738245926336
\$\endgroup\$
  • \$\begingroup\$ This was the exact same answer I had. I was about to post it. :( \$\endgroup\$ – Nic Hartley Apr 9 '16 at 1:34
  • \$\begingroup\$ Neither answer is the original, but they are functionally the same. \$\endgroup\$ – Conor O'Brien Apr 9 '16 at 1:34
  • \$\begingroup\$ @QPaysTaxes Sorry :( \$\endgroup\$ – Leaky Nun Apr 9 '16 at 1:34
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ +1 for </div>, an HTML closing tag that somehow appeared in Reng code. \$\endgroup\$ – user48538 Apr 17 '16 at 13:05
  • \$\begingroup\$ @zyabin101 Wrong place? \$\endgroup\$ – Leaky Nun Apr 17 '16 at 13:06
  • \$\begingroup\$ Nope. I just like finding hidden secrets in code. :-P \$\endgroup\$ – user48538 Apr 17 '16 at 13:12
  • \$\begingroup\$ Well this is in the cop's code, so... \$\endgroup\$ – Leaky Nun Apr 17 '16 at 13:13
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Nice job, that was the exact same solution :) \$\endgroup\$ – Adnan Apr 9 '16 at 7:52
2
\$\begingroup\$

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.)

\$\endgroup\$
  • 1
    \$\begingroup\$ Wow! Le builtins minuscules! +1 \$\endgroup\$ – Adnan Apr 9 '16 at 14:38
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Nic Hartley Apr 9 '16 at 14:40
  • \$\begingroup\$ @QPaysTaxes I guess I need to update the docs :P But seriously, just Ctrl+F the source code ;) \$\endgroup\$ – Conor O'Brien Apr 9 '16 at 14:40
  • \$\begingroup\$ I put my original code in my question if you wanna see it. In retrospect, I should have showed different characters :P \$\endgroup\$ – Nic Hartley Apr 9 '16 at 15:35
2
\$\begingroup\$

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.

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

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.

\$\endgroup\$
  • \$\begingroup\$ c is "cast to integer" \$\endgroup\$ – Conor O'Brien Apr 14 '16 at 14:05
2
\$\begingroup\$

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.

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

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!.

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

PHP, 18 bytes, insertusernamehere, A023443

Code:

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

Output:

a(0) = -1
a(1) = 0
\$\endgroup\$
  • \$\begingroup\$ Very nice approach. My source was slightly different: echo$argv[1]+-+!0;. :) \$\endgroup\$ – insertusernamehere Apr 10 '16 at 9:15
2
\$\begingroup\$

Octave (34 bytes) by Stewie Griffin

The sequence is A066911.

@(m)(mod(m,u=1:m  )&isprime(u))*u'
\$\endgroup\$
  • \$\begingroup\$ Nice =) For the record, I had u=0:m-1. The same sequence. \$\endgroup\$ – Stewie Griffin Apr 10 '16 at 19:52
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 10 bytes, George Gibson, A003215

Code:

Ds3*s1+*1+

Explanation:

Computes 3*n*(n+1)+1 which is the oeis sequence A003215.

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

Element, 10 bytes, PhiNotPi, A097547

2_4:/2@^^`

Try it online!

Output

a(3) = 6561
a(4) = 4294967296
\$\endgroup\$
1
\$\begingroup\$

Pyke, 6 bytes, muddyfish, A005563

0QhXts

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

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

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.

\$\endgroup\$
  • \$\begingroup\$ Gonna add one more restriction xd \$\endgroup\$ – Leaky Nun Apr 10 '16 at 9:45
1
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Oops, that's not the one. I updated my answer with another one \$\endgroup\$ – FliiFe Apr 10 '16 at 20:03
  • \$\begingroup\$ From the rules, this is valid. Congrats ! \$\endgroup\$ – FliiFe Apr 10 '16 at 20:08
  • \$\begingroup\$ I guess I can tell you the sequence now, It's A007770 (0-indexed) \$\endgroup\$ – FliiFe Apr 10 '16 at 20:31
  • \$\begingroup\$ @FliiFe Oh, I would never have guessed that :p \$\endgroup\$ – Adnan Apr 10 '16 at 20:32
  • \$\begingroup\$ Actually, if you know the sequence, it's easily spottable, but if you don't, it becomes really hard \$\endgroup\$ – FliiFe Apr 10 '16 at 20:39
1
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ Exactly what I had. Expected it to be easy, honestly. \$\endgroup\$ – CAD97 Apr 10 '16 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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