<p>Fun question! As a base I will use this Haskell quine I found on Wikipedia:</p>

<pre><code>main=putStr(p++show(p))where p="main=putStr(p++show(p))where p="
</code></pre>

<p>You can make it print out two copies of itself by replacing the occurrences of p++show(p) with p++show(p)++p++show(p). If you see why, the pattern to achieve variable repetition should be clear.</p>

<p>I will be using the following function that calculates the nth iterate of f on x:</p>

<pre><code>iterateN n f x = (iterate f x) !! n
</code></pre>

<p>I'll assume it's available as a library function. You can easily embed its definition directly in the quine, but that would clutter the presentation without good reason. Now the rest is simple:</p>

<pre><code>main=putStr(iterateN 42(++(p++show(p)))[])
  where p="main=putStr(iterateN 42(++(p++show(p)))[])where p="
</code></pre>

<p>The line break was inserted to aid readability; remove it if you want exact self-replication.</p>

CC BY-SA 2.5
2010-10-01T04:35:16.817Z
Very nicely done!
arnab
CC BY-SA 2.5
2010-10-01T04:20:33.587Z
2010-10-01T04:58:05.630Z
2010-10-01T04:58:05.630Z
Per Vognsen
<p>You can also proof the existence of such programs without giving an example.<br>
Let $(\Phi_i)$ be the list of all partial computable functions. Clearly there is a partial computable function $\varphi(k)$ which prints the input $k$ n times. So there is an Index $e$ with $\varphi(k) = \Phi_e(k)$. Using the smn-theorem we see that there is a computable function $f$ with $\Phi_{f(k)} = \Phi_e(k) = \varphi(k)$ for all $k$. Now we can apply the recursion theorem and get an $s$ with $\Phi_{f(s)} = \Phi_s$. So $\Phi_s$ is a program which outputs $s$ n times.</p>

CC BY-SA 2.5
2010-10-01T10:24:13.837Z
2010-10-01T10:24:13.837Z
aurevoirlesenfants
<p>Here is another one, based on the printf-version on wikipedia:</p>

<pre><code>main() { int i=5; char *s="main() { int i=5; char *s=%c%s%c; while (i--)
  printf(s,34,s,34); }"; while (i--) printf(s,34,s,34); }`
</code></pre>

<p>Though it is short, it is actually not so nice, as it lacks the inclusion for printf, as well as the counter has to be specified twice.
A slightly longer version cures both issues:</p>

<pre><code>#include &lt;stdio.h&gt;
#define k 5
main() { int i=k; char *s="#include &lt;stdio.h&gt; %c#define k %d%cmain() { int i=k;
  char *s=%c%s%c; while (i--) printf(s,10,k,10,34,s,34); }";
  while (i--) printf(s,10,k,10,34,s,34); }
</code></pre>

CC BY-SA 2.5
2010-10-01T09:46:34.337Z
2010-10-01T09:46:34.337Z
Mike B.
<p>A <a href="http://en.wikipedia.org/wiki/Quine_%28computing%29" rel="nofollow">Quine</a> is a computer program which produces a copy of its own source code as its only output. Is there any Quine program that could print itself out n times, with n specified some way in the program?</p>

CC BY-SA 2.5
2010-10-01T04:01:05.847Z
Could you please provide more information about what is a Quine program? (Also please read the [FAQ](http://cstheory.stackexchange.com/faq) and [how to ask a good question](http://meta.cstheory.stackexchange.com/questions/300/how-to-ask-a-good-question) if you have not read yet.)
Kaveh
CC BY-SA 2.5
2010-10-01T04:44:57.480Z
@Kaveh: I re-added the logic tag. Quines originate in logic and the study of self-reference, self-application, etc, so it seem apropos.
Per Vognsen
CC BY-SA 2.5
2010-10-01T10:05:24.933Z
A great resource on quines: http://www.nyx.net/~gthompso/quine.htm
Sylvain Peyronnet
CC BY-SA 2.5
2010-10-01T12:22:13.087Z
Sorry, it is the first time I hear the phrase, I was used to self-copying/self-replicating/self-producing program and have not seen it being called a Quine program! Would like to know who has coined this name, wiki does not seem to give any clue. Anyway, I think recursion theory tag would be more appropriate than general logic tag.
Kaveh
CC BY-SA 2.5
2010-10-01T12:27:42.170Z
Btw, I don't think there is any need to write the code of such a program, the existence follows easily from [Kleene's fixed point theorem](http://en.wikipedia.org/wiki/Kleene%27s_recursion_theorem).
Kaveh
CC BY-SA 2.5
2010-10-01T15:12:45.267Z
@Kaveh: Willard V. O. Quine was a philosopher and logician who studied self-reference. See the great book _"Godel, Escher, Bach - An Eternal Golden Braid"_ by Douglas Hofstadter.
Giorgio Camerani
CC BY-SA 2.5
2010-10-01T15:45:39.447Z
@Walter Bishop: Thank you, I already know who Quine is and am familiar with his work to some extent. Pardon me, but are you saying the name "Quine program" is coined by *the great* book GEB? (My search on Google scholar for "Quine Program" -philosophy returns only 4 articles, one from 2007 and the rest from 2009/2010.)
Kaveh
CC BY-SA 2.5
2010-10-01T16:21:59.727Z
@Kaveh: Yes I suspect that the term _"Quine program"_ has been coined by Hofstadter in GEB (he certainly coined the verb _"to aritmoquine"_). But I'm not 100% sure. I heartily recommend everyone to read GEB (at least, everyone interested in Logic and/or Artificial Intelligence). IMHO, it is a masterpiece.
Giorgio Camerani
<computability><recreational>
Repeated Quine program
huyichen
# Repeated Quine program

(Asked by: username_0 on 2010-10-01)

A [Quine](http://en.wikipedia.org/wiki/Quine_%28computing%29) is a computer program which produces a copy of its own source code as its only output. Is there any Quine program that could print itself out n times, with n specified some way in the program?

**Tags:** computability, recreational

> **Comments:**
> **username_1**: Could you please provide more information about what is a Quine program? (Also please read the [FAQ](http://cstheory.stackexchange.com/faq) and [how to ask a good question](http://meta.cstheory.stackexchange.com/questions/300/how-to-ask-a-good-question) if you have not read yet.)  
> **username_2**: @username_1: I re-added the logic tag. Quines originate in logic and the study of self-reference, self-application, etc, so it seem apropos.  
> **username_3**: A great resource on quines: http://www.nyx.net/~gthompso/quine.htm  
> **username_1**: Sorry, it is the first time I hear the phrase, I was used to self-copying/self-replicating/self-producing program and have not seen it being called a Quine program! Would like to know who has coined this name, wiki does not seem to give any clue. Anyway, I think recursion theory tag would be more appropriate than general logic tag.  
> **username_1**: Btw, I don't think there is any need to write the code of such a program, the existence follows easily from [Kleene's fixed point theorem](http://en.wikipedia.org/wiki/Kleene%27s_recursion_theorem).  
> **username_4**: @username_1: Willard V. O. Quine was a philosopher and logician who studied self-reference. See the great book _"Godel, Escher, Bach - An Eternal Golden Braid"_ by Douglas Hofstadter.  
> **username_1**: @Walter Bishop: Thank you, I already know who Quine is and am familiar with his work to some extent. Pardon me, but are you saying the name "Quine program" is coined by *the great* book GEB? (My search on Google scholar for "Quine Program" -philosophy returns only 4 articles, one from 2007 and the rest from 2009/2010.)  
> **username_4**: @username_1: Yes I suspect that the term _"Quine program"_ has been coined by Hofstadter in GEB (he certainly coined the verb _"to aritmoquine"_). But I'm not 100% sure. I heartily recommend everyone to read GEB (at least, everyone interested in Logic and/or Artificial Intelligence). IMHO, it is a masterpiece.  

---

## 3 answers

### Answer by username_6 on 2010-10-01. Score: 10

You can also proof the existence of such programs without giving an example.  
Let $(\Phi\_i)$ be the list of all partial computable functions. Clearly there is a partial computable function $\varphi(k)$ which prints the input $k$ n times. So there is an Index $e$ with $\varphi(k) = \Phi\_e(k)$. Using the smn-theorem we see that there is a computable function $f$ with $\Phi\_{f(k)} = \Phi\_e(k) = \varphi(k)$ for all $k$. Now we can apply the recursion theorem and get an $s$ with $\Phi\_{f(s)} = \Phi\_s$. So $\Phi\_s$ is a program which outputs $s$ n times.


---

### Answer by username_2 on 2010-10-01. Score: 9

Fun question! As a base I will use this Haskell quine I found on Wikipedia:
main=putStr(p++show(p))where p="main=putStr(p++show(p))where p="

You can make it print out two copies of itself by replacing the occurrences of p++show(p) with p++show(p)++p++show(p). If you see why, the pattern to achieve variable repetition should be clear.
I will be using the following function that calculates the nth iterate of f on x:
iterateN n f x = (iterate f x) !! n

I'll assume it's available as a library function. You can easily embed its definition directly in the quine, but that would clutter the presentation without good reason. Now the rest is simple:
main=putStr(iterateN 42(++(p++show(p)))[])
  where p="main=putStr(iterateN 42(++(p++show(p)))[])where p="

The line break was inserted to aid readability; remove it if you want exact self-replication.

> **Comments:**
> **username_5**: Very nicely done!  

---

### Answer by username_7 on 2010-10-01. Score: 3

Here is another one, based on the printf-version on wikipedia:
main() { int i=5; char *s="main() { int i=5; char *s=%c%s%c; while (i--)
  printf(s,34,s,34); }"; while (i--) printf(s,34,s,34); }`

Though it is short, it is actually not so nice, as it lacks the inclusion for printf, as well as the counter has to be specified twice.
A slightly longer version cures both issues:
#include 
#define k 5
main() { int i=k; char *s="#include  %c#define k %d%cmain() { int i=k;
  char *s=%c%s%c; while (i--) printf(s,10,k,10,34,s,34); }";
  while (i--) printf(s,10,k,10,34,s,34); }


---

