Science Fair Projects Ideas - Quine

All Science Fair Projects

      

Science Fair Project Encyclopedia for Schools!

  Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary     

Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

Quine

This is an article about a kind of computer program. You may be looking for information on the philosopher W. V. Quine.


In computing, a quine is a program (a form of metaprogram) that produces its complete source code as its only output. For amusement, hackers sometimes attempt to develop the shortest possible quine in any given programming language.

Note that simply opening the source file of the program and printing the contents is considered cheating.

Quines are named after W. V. Quine, who made an extensive study of indirect self-reference: he coined, among others, the paradox-producing expression, "yields falsehood when appended to its own quotation."

Contents

Sample quines in C

#include<stdio.h>
char*i="\\#include<stdio.h>",n='\n',q='"',*p=
"%s%cchar*i=%c%c%s%c,n='%cn',q='%c',*p=%c%c%s%c,*m=%c%c%s%c%c;%s%c",*m=
"int main(){return!printf(p,i+1,n,q,*i,i,q,*i,q,n,q,p,q,n,q,m,q,n,m,n);}"
;int main(){return!printf(p,i+1,n,q,*i,i,q,*i,q,n,q,p,q,n,q,m,q,n,m,n);}

Another one (this should be one single line, and assumes the compiler runs on an ASCII-Machine):

extern printf(char*,...);main(){char*a="extern printf(char*,...);
main(){char*a=%c%s%c;printf(a,34,a,34,10);}%c";printf(a,34,a,34,10);}

or, even shorter (although this is not correct ISO C99):

main(){char*a="main(){char*a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);}

This one does not depend on ASCII and uses the C preprocessor for quoting and escaping:

#define T(a) main(){printf(a,#a);}
T("#define T(a) main(){printf(a,#a);}\nT(%s)\n")

It should be noted that a zero-length program is technically a quine, if it can be made to compile into an executable that does nothing (i.e. prints zero characters to stdout). This is possible in C by tweaking the Makefile a bit (because its standard behavior would be not to create an executable). This trick won the 'worst abuse of the rules' award in one of the earlier Obfuscated C Contests.

Sample quine in C++

(Note: linebreaks added for ease of reading)

#include <iostream>
int main(){const char c=',',dq='"',q[]="'",*s[]={"#include <iostream>",
"int main(){const char c=',',dq='","',q[]=",",*s[]={","};std::cout<<s[0]<<std::endl<<s[1]<<dq<<s[2]
<<dq<<q<<dq<<s[3]<<dq<<s[0]<<dq<<c<<dq<<s[1]<<dq<<c<<dq<<s[2]<<dq<<c<<dq<<s[3]<<dq<<c<<dq<<s[4]<<dq
<<s[4]<<std::endl;}"};std::cout<<s[0]<<std::endl<<s[1]<<dq<<s[2]<<dq<<q<<dq<<s[3]<<dq<<s[0]<<dq<<c
<<dq<<s[1]<<dq<<c<<dq<<s[2]<<dq<<c<<dq<<s[3]<<dq<<c<<dq<<s[4]<<dq<<s[4]<<std::endl;}

Sample quine in C#

(Note: linebreaks added for ease of reading)

using System;
namespace quine
{
  class Program
  {
    [STAThread]
    static void Main(string[] args)
    {
      string s = "using System;{0}namespace quine{0}{2}{0}{1}class Program{0}
{1}{2}{0}{1}{1}[STAThread]{0}{1}{1}static void Main(string[] args){0}{1}{1}{2}{0}{1}{1}{1}
string s = {4}{6}{4};{0}{1}{1}{1}Console.Write(s, Environment.NewLine, {4}{5}t{4}, {4}{2}
{4}, {4}{3}{4}, {4}{5}{4}{4}, {4}{5}{5}{4}, s);{0}{1}{1}{3}{0}{1}{3}{0}{3}";
      Console.Write(s, Environment.NewLine, "\t", "{", "}", "\"", "\\", s);
    }
  }
}

Sample quine in Scheme

   ((lambda (x)
           (list x (list (quote quote) x)))
       (quote
           (lambda (x)
               (list x (list (quote quote) x)))))

Sample quine in Common Lisp

   (funcall (lambda (x) 
              (append x (list (list 'quote x))))
            '(funcall (lambda (x) 
                         (append x (list (list 'quote x))))))

Sample quine in OCaml

(fun s -> Printf.printf "%s %S" s s) "(fun s -> Printf.printf \"%s %S\" s s)"

Sample quine in Python

a='a=%s;print a%%`a`';print a%`a`

Another:

b='\\';g='"';p='%';s="b='%s%s';g='%s';p='%s';s=%s%s%s;print s%s(b,b,g,p,g,s,g,p)";print s%(b,b,g,p,g,s,g,p)

And another one that shares the last 61 characters with the previous one (just to show that multiple assignments don't save typing):

b,g,p,s='\\','"','%',"b,g,p,s='%s%s','%s','%s',%s%s%s;print s%s(b,b,g,p,g,s,g,p)";print s%(b,b,g,p,g,s,g,p)

And yet another one, that is slightly more readable and more fun to read:

i = r'"i = r\'" + i + "\'\nprint " + i'
print "i = r\'" + i + "\'\nprint " + i

Sample quine in JavaScript

unescape(q="unescape(q=%220%22).replace(0,q)").replace(0,q)

Sample quine in Perl

$_=q{$_=q{Q};s/Q/$_/;print};s/Q/$_/;print

Another:

$_=q{print"\$_=q{$_};eval"};eval

And a shell/Perl combo:

perl -le '$n=q{perl -le a$n=q{$x};($_=$n)=~s/\141/\47/g;s/\$x/$n/;printa};($_=$n)=~s/\141/\47/g;s/\$x/$n/;print'

Sample quine in Ruby

puts <<2*2,2
puts <<2*2,2
2

Sample quine in BASIC

10 LIST

Also because that quine may be considered "cheating", because it directly accesses its own source, here is another quine:

10 C=": PRINT CHR(49)+CHR(48)+CHR(32)+CHR(67)+CHR(61)+CHR(34)+C+CHR(34)+C":
PRINT CHR(49)+CHR(48)+CHR(32)+CHR(67)+CHR(61)+CHR(34)+C+CHR(34)+C

Sample quine in Pascal

const a='const a=';b='begin write(a,#39,a,#39#59#98#61#39,b,#39#59#10,b) end.';
begin write(a,#39,a,#39#59#98#61#39,b,#39#59#10,b) end.

Remark 1. In the case of a DOS implementation of Pascal the screen output of the program can look somewhat misleading, and it would be appropriate for that case to replace both "#10" in the program by "#13#10" and to insert a CR before the LF at the end of the first line.

Remark 2. The above program can be made even shorter, since both occurrences of ") end." in it can be replaced by ")end." (although this is not good for the legibility). A further shortening can be achieved by skipping both "#10" and writing the program on a single line instead on two ones. After all these changes the program will look as follows (the fact that it continues to be a quine was checked at least in the cases of Turbo Pascal for DOS and Free Pascal for Linux):

const a='const a=';b='begin write(a,#39,a,#39#59#98#61#39,b,#39#59,b)end.';begin write(a,#39,a,#39#59#98#61#39,b,#39#59,b)end.

Another (Borland Pascal and Free Pascal):

const a='const a=;begin write(copy(a,1,8),#39,a,#39,copy(a,9,99)) end.';begin write(copy(a,1,8),#39,a,#39,copy(a,9,99)) end.

Another (Borland Pascal and Free Pascal):

const a:string='const a:string=;begin insert(#39+a+#39,a,16);write(a) end.';begin insert(#39+a+#39,a,16);write(a) end.

Sample quine in Brainfuck

(Note: This should be one continuous line of code, but line breaks have been added for ease of reading. "Ease" used very liberally here.)

 ->+>+++>>+>++>+>+++>>+>++>>>+>+>+>++>+>>>>+++>+>>++>+>+++>>++>++>>+>>+>++>++>
 +>>>>+++>+>>>>++>++>>>>+>>++>+>+++>>>++>>++++++>>+>>++>+>>>>+++>>+++++>>+>+++
 >>>++>>++>>+>>++>+>+++>>>++>>+++++++++++++>>+>>++>+>+++>+>+++>>>++>>++++>>+>>
 ++>+>>>>+++>>+++++>>>>++>>>>+>+>++>>+++>+>>>>+++>+>>>>+++>+>>>>+++>>++>++>+>+
 ++>+>++>++>>>>>>++>+>+++>>>>>+++>>>++>+>+++>+>+>++>>>>>>++>>>+>>>++>+>>>>+++>
 +>>>+>>++>+>++++++++++++++++++>>>>+>+>>>+>>++>+>+++>>>++>>++++++++>>+>>++>+>>
 >>+++>>++++++>>>+>++>>+++>+>+>++>+>+++>>>>>+++>>>+>+>>++>+>+++>>>++>>++++++++
 >>+>>++>+>>>>+++>>++++>>+>+++>>>>>>++>+>+++>>+>++>>>>+>+>++>+>>>>+++>>+++>>>+
 [[->>+<<]<+]+++++[->+++++++++<]>.[+]>>[<<+++++++[->+++++++++<]>-.------------
 ------->-[-<.<+>>]<[+]<+>>>]<<<[-[-[-[>>+<++++++[->+++++<]]>++++++++++++++<]>
 +++<]++++++[->+++++++<]>+<<<-[->>>++<<<]>[->>.<<]<<]

Sample quine in HQ9+

Q

Sample quine in DOS Batch

 @echo off
 %1 %2
 call %0 goto e %%
 call %0 goto e %%3 echo.%%4
 echo :f
 goto f
 :e
 echo.%4@echo off
 echo.%4%31 %32
 echo.%4call %30 goto e %3%3
 echo.%4call %30 goto e %3%33 echo.%3%34
 echo.%4echo :f
 echo.%4goto f
 echo.%4:e
 :f

Sample quine in PHP

 <?
 $a='chr(60).chr(63).chr(10).chr(36).chr(97).chr(61).chr(39).$a.chr(39).chr(59).chr(10)."echo $a;".chr(10).chr(63).chr(62)';
 echo chr(60).chr(63).chr(10).chr(36).chr(97).chr(61).chr(39).$a.chr(39).chr(59).chr(10)."echo $a;".chr(10).chr(63).chr(62);
 ?>
 
 <?
 $a='<?
 $a=2;
 echo str_replace(1+1,chr(39).$a.chr(39),$a);
 ?>';
 echo str_replace(1+1,chr(39).$a.chr(39),$a);
 ?>

Another:

<?php $c='echo \'<?php $c=\\\'\'.addslashes($c).\'\\\';eval($c) ?>\';';eval($c) ?>

Another: (Could be cheating...)

<? print file_get_contents(".".$PHP_SELF); ?>

Sample quine in PL1

(Note: This smallest possible PL/I quine will compile using the OS PL/I V2.3.0 compiler, but requires a left margin of 1 and the COMPILE option to override a rather significant number of severe errors, errors and warnings...)

  %dcl z%z='put edit';proc options(main;q=''''put list(m;do i=1,2;z(q)skip;do j=
  1to 78c=substr(m(i),j;if c=q z(c;z(c;end;z(q',';dcl(c,q)char,m(2)char(99)init(
  '%dcl z%z=''put edit'';proc options(main;q=''''''''put list(m;do i=1,2;z(q)skip;do j=',
  '1to 78c=substr(m(i),j;if c=q z(c;z(c;end;z(q'','';dcl(c,q)char,m(2)char(99)init(',

Sample quine in PostScript

 (dup == {dup cvx exec} pop 8 12 getinterval =)
 dup cvx exec

Sample quine in Visual FoxPro

 CLEAR
 SET TALK OFF
 SET TEXTMERGE ON
 \CLEAR
 \SET TALK OFF
 \SET TEXTMERGE ON

Sample quine in Dc

[91PP[dx]93PP]dx

Sample quine in Java

class Q{static void main(String[]a){char q=34;String t="class Q{static void main(String[]a){char q=34;String
t=;System.out.println(t.substring(0,55)+q+t+q+t.substring(55));}}";System.out.println(t.substring(0,55)+q+t+q+t.substring(55));}}

Sample quine in MATLAB

a='a=%c%s%c;a=sprintf(a,39,a,39);disp(a);';a=sprintf(a,39,a,39);disp(a);

External links

See also

11-30-2008 18:11:33
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice