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

Using two languages of your choice, write the smallest “mutual quine” you can.

That is, write a program P in language A that outputs the source code for a program Q in language B, such that the output of program Q is identical to the source code for P.

Empty files don't count, nor do "read the source file and print it"-style programs.

Edit: Answers with P=Q no longer count.

share|improve this question
Btw, this should preclude cases where the sources match. For example 1 is a quine in several languages. So you could say its a J program that prints a Golfscript program that prints a...... and so on. – cthom06 May 16 '11 at 20:49
you can adopt the rule of "Try to avoid or rather don't use 1 byte submissions like this one ,since it spoils all fun." from… – Ming-Tang May 16 '11 at 22:40
A Brainfuck + Befunge solution should be posted. – pyon Jul 15 '11 at 0:13

11 Answers 11

up vote 18 down vote accepted

Golfscript and R and J and....

Before someone else does

share|improve this answer
Well, no, not HQ9+. – J B May 16 '11 at 22:00
@J B H9+ is different. It prints any character that's not an h 9 or + – cthom06 May 17 '11 at 10:20
This answer definitely gets the "best abuse of the rules" award! – dan04 May 18 '11 at 0:31
Works with Ti-Basic 83 and 84 – Timtech Dec 11 '13 at 23:18
Does not work in H9+. H9+ ignores all chars that are not H, 9, or + – Justin Feb 23 '14 at 18:57

Python and Ruby, 39 characters

This Python snippet

s='puts %%q{s=%r;print s%%s}';print s%s

generates this Ruby snippet

puts %q{s='puts %%q{s=%r;print s%%s}';print s%s}

which then generates the inital Python snippet again:

$ diff -s <(ruby <(python
Files and /dev/fd/63 are identical

Note that this is similar to J B's answer.

share|improve this answer

C and Perl, 73

This C:

main(s){printf(s="print q<main(s){printf(s=%c%s%c,34,s,34);}>",34,s,34);}

...outputs the following Perl:

print q<main(s){printf(s="print q<main(s){printf(s=%c%s%c,34,s,34);}>",34,s,34);}>

...that outputs the C back.

share|improve this answer

><> (Fish) and Python - 26 characters

"00gr00g:a9*2+$' tnirp'>o<

Generates this Python

print "\"00gr00g:a9*2+$' tnirp'>o<"
share|improve this answer
You have an issue with the ><> code because " and ' enable string parsing, so the entire program is just pushed onto the stack and never displayed. – Kevin Brown May 16 '11 at 22:44
@Bass5098 That's precisely how the whole thing works. The first " enables string parsing. It reads.the whole program and then wraps back around to.the first " and runs the program with itself on the stack – cthom06 May 17 '11 at 10:22
Fixed the issue in my interpreter. Your ><> code outputs print \00gr00g:a9*2+$' tnirp'>o< after timing out. I'm pretty sure this is because g copies the actual cell contents, but o outputs the ASCII version of the character, and " isnt a valid ASCII character code. – Kevin Brown May 17 '11 at 22:38
@Bass5098 " is ASCII 34. Your interpreter must be off. – cthom06 May 18 '11 at 0:11

C and C++, 123 chars

This C (compilable with gcc v4.3.4):

#include <stdio.h>
main(){char *c="#include <stdio.h>%cmain(){char *c=%c%s%c;printf(c,10,34,c,34);}";printf(c,10,34,c,34);}

outputs this (identical) C++ (compilable with g++ 4.3.4, one warning):

#include <stdio.h>
main(){char *c="#include <stdio.h>%cmain(){char *c=%c%s%c;printf(c,10,34,c,34);}";printf(c,10,34,c,34);}

This is within the rules as posted. :P And, like Ventero's, this is based on J B's answer.

share|improve this answer
+1 How unimaginative and yet clever :P – mellamokb May 16 '11 at 20:22
OK, so it was within the rules as posted until the edit saying the sources can't be the same. A minor change could fix that, but I'm disinclined to encourage such late edits. – Matthew Read Jun 8 '11 at 21:22

Python + Piet one-liners: 417 chars + 7391 7107 executing codels

This Python script produces a 7393 x 2 image; the rightmost 2x2 block of which is a "sentinel" which terminates the program; so I'm not counting those; and the second row is otherwise white. I can probably golf the piet code down further by using addition/subtraction/multiplication chains instead of the naive binary algorithm... but I don't want to give away a solution to a future puzzle.

I'm not going to post the image here, because of its ridiculous dimensions. If you want to see it, run the python code, and pipe the output to a .ppm file. Then, convert the .ppm to a .gif, and run the output at Rapapaing. (alternately, use a non-web Piet interpreter that groks .ppm)

A='P=lambda A:reduce(lambda(D,H,B),P:(D[P/3:]+D[:P/3],H[P%3*2:]+H[:P%3*2],B+"".join("%i "%H[(D[0]/P)%2]for P in[1,2,4])),map(" A !        @    B".find,A),([1,3,2,6,4,5],[0,192,192,255,0,255],"P3 %i 2 255 "%(len(A)+2)))[2]+"255 "*4+"0 0 "+"255 "*len(A)*3+"255 0 0 "*2;B=lambda D:["@!%s","@@!%s!"][D%2]%B(D/2)if 1<D else"";print P("".join("A%sB"%B(ord(D))for D in"A=%s;exec A[:-13]"%`A`)+" ");exec A[:-13]';exec A[:-13]

edit: golfed the piet a bit by reducing Hamming weight of variable names.

less golfed pre-quine:

This is a previous version, before I realized I could make it a one-liner. It's marginally easier to understand. The function P translates a special instruction set into Piet; and the function p takes an integer and produces a sequence of instructions to create that integer on the stack. I'm only using the instructions =,+,:,|, so this could probably be made more efficient... but I kinda like having a fullblown Piet compiler (of sorts) in the source.

s="""def P(s):
 l=len(s)+1;R="P3 %i 2 255 "%(l+2);C=[1,3,2,6,4,5];V=[0,192,192,255,0,255]
 for x in map("=|^+-*/%~>.,:@$?#!".find,"="+s):
  for i in [1,2,4]:R+="%i "%V[(C[0]//i)%2]
 return R+"255 "*4+"0 0 "+"255 "*l*3+"255 0 0 "*2
p=lambda x:[":+%s","::+%s+"][x%2]%p(x/2)if x/2 else""
print P("".join("|%s!"%k(ord(c))for c in "s="+`s`+";exec s[:-13]"))
exec s[:-13]"""
exec s[:-13]
share|improve this answer
Piet Creator is a much nicer (and far less buggy) Piet IDE. Written by a user of this site, by the way :) – Joey Jul 17 '11 at 22:02
I've used Piet Creator before; it's a great tool. Rapapaing is nice for people who are lazy, like me, and don't want to bother. FWIW, I'd use Piet Creator if you ran it through, and put that up on your page. – boothby Jul 17 '11 at 23:52
I couldn't even get the Qt version to compile here ;) – Joey Jul 18 '11 at 7:33
;) Thanks for trying, anyway. I wrote you a fuzz tester:… – boothby Jul 20 '11 at 21:38
Well, you could poke Casey in chat and point him to it ;-) – Joey Jul 20 '11 at 21:40

Java to Python - 219


class Q{public static void main(String[]a){char q=34,c=39;String s="print%sclass Q{public static void main(String[]a){char q=34,c=39;String s=%s%s%s;System.out.printf(s,c,q,s,q,c);}}%s";System.out.printf(s,c,q,s,q,c);}}


print'class Q{public static void main(String[]a){char q=34,c=39;String s="print%sclass Q{public static void main(String[]a){char q=34,c=39;String s=%s%s%s;System.out.printf(s,c,q,s,q,c);}}%s";System.out.printf(s,c,q,s,q,c);}}'

Makes use of the fact that python allows ' for strings; this makes it much easier to write the java program's source in the python program.

share|improve this answer
+1 for doing the dirty work in the worst golfing language ever. :P – cjfaure Mar 10 '14 at 21:09

Ruby and Python (393 + 413 = 806 chars)

Slight change of this answer of mine. Could definitely be reduced since I just hard-coded the input without any optimisation.

Ruby (393 chars)

require 'json';s=%q[{"ruby":[" %q[","require 'json';s=##;j=JSON.load s;puts j[l='python'][1].sub('##',j[l][0].delete(' ')+s+j[l][2].delete(' '))"," ]"],"python":["' ''","import json,re;s=##;j=json.loads(s);l='ruby';print(re.sub('##',j[l][0].replace(' ','')+s+j[l][2].replace(' ',''),j[l][1],1))","' ''"]}];j=JSON.load s;puts j[l='python'][1].sub('##',j[l][0].delete(' ')+s+j[l][2].delete(' '))

Python (413 chars)

import json,re;s='''{"ruby":[" %q[","require 'json';s=##;j=JSON.load s;puts j[l='python'][1].sub('##',j[l][0].delete(' ')+s+j[l][2].delete(' '))"," ]"],"python":["' ''","import json,re;s=##;j=json.loads(s);l='ruby';print(re.sub('##',j[l][0].replace(' ','')+s+j[l][2].replace(' ',''),j[l][1],1))","' ''"]}''';j=json.loads(s);l='ruby';print(re.sub('##',j[l][0].replace(' ','')+s+j[l][2].replace(' ',''),j[l][1],1))
share|improve this answer

Python 2 to Befunge 98, 94 78

Again, making use of Python's two ways of making strings:


s='<@,kM%c%cs=%c%s%c;print s%%(39,34,39,s,39,34)%c';print s%(39,34,39,s,39,34)

Befunge 98:

<@,kM'"s='<@,kM%c%cs=%c%s%c;print s%%(39,34,39,s,39,34)%c';print s%(39,34,39,s,39,34)"

The python program formats the string to include itself and the characters for the single and double quote.

The Befunge program works like this:

  • <: move to the left, and wrap around. So now we execute the commands from right to left
  • ": make it so that every char we encounter is pushed to the stack until we encounter another ", so we push:

    )43,93,s,93,43,93(%s tnirp;'c%)43,93,s,93,43,93(%%s tnirp;c%s%c%=sc%c%Mk,@<'=s

    Which is actually just the python program, reversed because of the way Befunge prints (which is a pop+print loop)

  • 'M: push M to the stack. M is also the number 77, which is one less than the number of chars pushed to the stack by the "s.
  • k: pop the top value off the stack (M) and do the next operation that many times, plus one.
  • ,: pop the top value off the stack and print the char.
  • @: end the program.
share|improve this answer

Clipper and C (111 + 108 = 219 chars)

Clipper (111)


C (108)


This is a bit of a cop-out because:

  1. the "print" command in Clipper is really simple: ?'foo' :-)
  2. I based it on the "classic C" quine from, with some modification to squeeze in the ? and 's.
  3. I didn't #include <stdio.h>, so it gives a compiler warning
  4. The C code output by the Clipper version had to be prodded to remove a line break that had been introduced by an 80-character column limit in either my terminal or the Harbour printing routine or something.
share|improve this answer
Well, the BASIC PRINT command could be abbreviated ? as well :-) – Joey Jul 20 '11 at 21:41

Python and ~-~! - 77 and 81 = 158

This Python code:


outputs this ~-~! code:


Can definitely be improved a lot, and adopts a whole bunch from the other answers.

share|improve this answer

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.