Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>Polyglot: 5 languages, all using bignums</h1> <p>So, I wrote a polyglot which works in the three languages I often write in, as well as one from my other answer to this question and one I just learned today. It's a standalone program, which reads a single line containing a nonnegative integer and prints a single line containing its factorial. Bignums are used in all languages, so the maximum computable factorial depends only on your computer's resources.</p> <ul> <li><b>Perl</b>: uses built-in bignum package. Run with <code>perl FILENAME</code>.</li> <li><b>Haskell</b>: uses built-in bignums. Run with <code>runhugs FILENAME</code> or your favorite compiler's equivalent.</li> <li><b>C++</b>: requires GMP for bignum support. To compile with g++, use <code>g++ -lgmpxx -lgmp -x c++ FILENAME</code> to link against the right libraries. After compiling, run <code>./a.out</code>. Or use your favorite compiler's equivalent.</li> <li><b>brainf*ck</b>: I wrote some bignum support in <a href="https://stackoverflow.com/questions/23930/factorial-algorithms-in-different-languages/432010#432010">this post</a>. Using <a href="http://aminet.net/package.php?package=dev/lang/brainfuck-2.lha" rel="nofollow noreferrer">Muller's classic distribution</a>, compile with <code>bf &lt; FILENAME &gt; EXECUTABLE</code>. Make the output executable and run it. Or use your favorite distribution.</li> <li><b>Whitespace</b>: uses built-in bignum support. Run with <code>wspace FILENAME</code>.</li> </ul> <p><i>Edit:</i> added Whitespace as a fifth language. Incidentally, do <em>not</em> wrap the code with <code>&lt;code&gt;</code> tags; it breaks the Whitespace. Also, the code looks much nicer in fixed-width.</p> <pre>char&#32;//#&#32;b=0+0{-&#32;|0*/;&#32;#&gt;&gt;&gt;&gt;,----------[&gt;&gt;&gt;&gt;,-------- #define&#09;a/*#--]&gt;&gt;&gt;&gt;++&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;[&gt;++++++[&lt;------&gt;-]&lt;-&lt;&lt;&lt; #Perl&#09;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&#09;&#32;&lt;&gt;&#32;&lt;&gt;&#32;&lt;&lt;]&gt;&gt;&gt;&gt;[[&gt;&gt;+&lt;&lt;-]&gt;&gt;[&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gt;&gt; #define&#09;print&#32;std::cout&#09;&lt;&lt;&#32;//&#32;&gt;&#09;&lt;+&lt;-]&gt;[&lt;&lt;+&gt;+&gt;-]&lt;&lt;[&gt;&gt;&gt; #define&#09;z&#32;std::cin&gt;&gt;//&lt;&lt;&#32;+&lt;&lt;&lt;-]&gt;&gt;&gt;[&lt;&lt;&lt;+&gt;&gt;+&gt;-]&lt;-&gt;+++++ #define&#32;c/*++++[-&lt;[-[&gt;&gt;&gt;&gt;+&lt;&lt;&lt;&lt;-]]&gt;&gt;&gt;&gt;[&lt;&lt;&lt;&lt;+&gt;&gt;&gt;&gt;-]&lt;&lt;*/ #define&#09;abs&#32;int&#32;$n&#32;//&gt;&lt;&#09;&lt;]&lt;[&gt;&gt;+&lt;&lt;&lt;&lt;[-]&gt;&gt;[&lt;&lt;+&gt;&gt;-]]&gt;&gt;]&lt; #define&#09;uc&#32;mpz_class&#32;fact(int&#09;$n){/*&lt;&lt;&lt;[&lt;&lt;&lt;&lt;]&lt;&lt;&lt;[&lt;&lt; use&#32;bignum;sub#&lt;&lt;]&gt;&gt;&gt;&gt;-]&gt;&gt;&gt;&gt;]&gt;&gt;&gt;[&gt;[-]&gt;&gt;&gt;]&lt;&lt;&lt;&lt;[&gt;&gt;+&lt;&lt;-] z{$_[0+0]=readline(*STDIN);}sub&#32;fact{my($n)=shift;#&gt;&gt; #[&lt;&lt;+&gt;+&gt;-]&lt;-&gt;+&lt;[&gt;-&lt;[-]]&gt;[-&lt;&lt;-&lt;&lt;&lt;&lt;[&gt;&gt;+&lt;&lt;-]&gt;&gt;[&lt;&lt;+&gt;+&gt;+*/ uc;if($n==0){return&#32;1;}return&#32;$n*fact($n-1);&#09;}//;# eval{abs;z($n);print&#32;fact($n);print("\n")/*2;};#-]&lt;-&gt; '+&lt;[&gt;-&lt;[-]]&gt;]&lt;&lt;[&lt;&lt;&lt;&lt;]&lt;&lt;&lt;&lt;-[&gt;&gt;+&lt;&lt;-]&gt;&gt;[&lt;&lt;+&gt;+&gt;-]+&lt;[&gt;-+++ -}--&#09;&lt;[-]]&gt;[-&lt;&lt;++++++++++&lt;&lt;&lt;&lt;-[&gt;&gt;+&lt;&lt;-]&gt;&gt;[&lt;&lt;+&gt;+&gt;-++ fact&#32;0&#09;=&#32;1&#32;--&#32;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&#09;&gt;&#32;&lt;&gt;&lt;&gt;&lt;&#09;]+&lt;[&gt;-&lt;[-]]&gt;]&lt;&lt;[&lt;&lt;+&#32;+ fact&#09;n=n*fact(n-1){-&lt;&lt;]&gt;&gt;&gt;&gt;[[&gt;&gt;+&lt;&lt;-]&gt;&gt;[&lt;&lt;+&gt;+++&gt;+-} main=do{n&lt;-readLn;print(fact&#32;n)}--&#32;+&gt;-]&lt;-&gt;+&lt;[&gt;&gt;&gt;&gt;+&lt;&lt;+ {-x&lt;-&lt;[-]]&gt;[-]&gt;&gt;]&gt;]&gt;&gt;&gt;[&gt;&gt;&gt;&gt;]&lt;&lt;&lt;&lt;[&gt;+++++++[&lt;+++++++&gt;-] &lt;--.&lt;&lt;&lt;&lt;]+written+by+++A+Rex+++2009+.';#+++x-}--x*/;} </pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload