Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

A quine is a computer program which takes no input and produces a copy of its own source code as its only output. There are many trivial quines in Mathematica:

In[1]:= "Hello world"
Out[1]= "Hello world"

In[2]:= 3.14
Out[2]= 3.14

In[3]:= f[x]
Out[3]= f[x]

where f and x are undefined symbols; and some more

In[4]:= Hold[N[\[Pi]]]
Out[4]= Hold[N[\[Pi]]]

These are all trivial. I was thinking that perhaps a more interesting challenge for Mathematica was a multiquine. This is a program A that outputs another program B, distinct from A, such that when B is exectued, the output is A. Multiple levels of depth are also allowed: Thus one might have a program that when executed outputs a distinct program that when executed outputs another program distinct from the first two ... that when executed outputs the original program.

There are also multiquines that output a distinct program in a different language, such that when this program is executed, the output is the original program.

QUESTION (Though more of a challenge): Can you come up with a multiquine for Mathematica?

share|improve this question

5 Answers 5

Self-rotating code:

(Interpretation[Rotate[R, 2 π #/5], #0[Mod[#, 5] + 1]] &)[0]

It produces R which rotates by 2 π/5 after each evaluation

enter image description here

Full input form of this symbol is

Interpretation[Rotate[R, (2*Pi)/5], 
 (Interpretation[Rotate[R, 2*Pi*(#1/5)], #0[Mod[#1, 5] + 1]] & )[
  Mod[1, 5] + 1]]

Don't drink and derive! :)

enter image description here

You can copy-pasting result of one self-rotating code to another. Full code is here.

share|improve this answer

I think this is the most straightforward way to translate the canonical quine of "Print the following twice, the second time in quotes" into Mathematica.

share|improve this answer

I have written several quines in Mathematica that you might appreciate.

Here is one solution:

quine[x_String] :=   Print[x, ";", FromCharacterCode[10] , "quine[", InputForm[x],    "]"];
quine["quine[x_String]:=Print[x,\";\",FromCharacterCode[10],\"\ quine[\",InputForm[x]],\"]\""]

And here is a much more fun one, which is a little hard to display in StackExchange,

bitmap quine

where that tiny text is a bitmap that reads

bitmap detail

The TextRecognize[] command doesn't do well with non-dictionary words, which means it typically does poorly with Mathematica code. I found that it recognized a bitmap of "quine" as "guine" in every font I tested, so I just renamed the function accordingly.

These are not multiquines, but aficionados should like them nonetheless.

share|improve this answer

Something like this would work?

Module[{$guard = True},
     f[i_List] /; $guard := 
  Block[{$guard = False, par = RotateRight[i]}, g[par]];
     g[i_List] /; $guard := 
  Block[{$guard = False, par = RotateRight[i]}, f[par]]



g[{9, 10, 1, 2, 3, 4, 5, 6, 7, 8}]

f[{7, 8, 9, 10, 1, 2, 3, 4, 5, 6}]


Another option

#2[#0[! #, #2]] &[!True, Defer]
share|improve this answer
I think that prior definitions should be avoided, so your first example is not what I had in mind. The second example is more like it. But I think you should change True to !False. Then you truly have a 2-level multiquine. –  becko May 28 '12 at 18:53
@becko, if what you had in mind is code that returns some cell that if evaluated as is, returns some other etc etc in a loop, without prior definitions, it probably can only be done by messing with the parsing stage, as I do here with Defer. The kernel will always return evaluated expressions I think unless you use the trick/bug on my first example, which isn't what you were looking for –  Rojo May 28 '12 at 20:52
@becko, thanks, edited to !True –  Rojo May 29 '12 at 1:04
I'm pretty sure #0 is cheating. It's like fopen("program.c", "r") in C. –  nikie Jan 7 at 21:15

Here is a 24 character nontrivial solution:

Print[ToString[#0][]] & []
share|improve this answer
Sorry, that's not a Quine. However adding a space after the & makes it one. –  celtschk May 28 '12 at 8:22
Thanks, just did. –  M.R. May 28 '12 at 16:42
It doesn't work as I'd expect it to. When I execute Print[ToString[#0][]] & [], the output is "Print[ToString[#0][]] & "[], which doesn't equal the original expression. Then if you execute "Print[ToString[#0][]] & "[], you get "Print[ToString[#0][]] & "[]. Therefore the program "Print[ToString[#0][]] & "[] is a quine. However, Print[ToString[#0][]] & [] is neither a quine nor a multiquine. –  becko May 28 '12 at 18:05
@IstvánZachar Good point. I think that the output's InputForm should equal the original quine source code (or the output of the output ... for a multiquine), but I am open to suggestions. As far as I know a established definition of what a quine is in the particular case of Mathematica doesn't exist, and as your comment exemplifies, there are subtleties that distinguish Mathematica from other languages. Part of the knowledge I expect to gain from this question is a definition of Mathematica quine that makes the problem non-trivial and interesting. –  becko May 28 '12 at 18:50
Well, start the kernel (without the front end), enter the code (which consists of a series of characters, of course) and look at the produced output (which is again a series of characters, sent to standard output). Or alternatively, you could also enter the code in the Front end, but then it would have to produce an identical cell (i.e. the same box structure). The internal form of the expression doesn't matter (except as far as it affects the output). –  celtschk May 29 '12 at 8:23

Your Answer


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.