<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Zero Wind - Jamie Wong</title>
	<atom:link href="http://jamie-wong.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jamie-wong.com</link>
	<description>Inside the mind of a Waterloo Software Engineering student</description>
	<lastBuildDate>Tue, 09 Mar 2010 17:46:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Learn Source Control with Git</title>
		<link>http://jamie-wong.com/2010/03/09/learn-source-control-with-git/</link>
		<comments>http://jamie-wong.com/2010/03/09/learn-source-control-with-git/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 17:46:33 +0000</pubDate>
		<dc:creator>Jamie Wong</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jamie-wong.com/?p=194</guid>
		<description><![CDATA[
One of the gaps among my tech skills upon entering university was source/revision control.
For those of you unfamiliar, revision control is a method of tracking and storing the changes made to files. This is particularly useful when keeping track of all the changes made to source code being worked on in a group. This allows [...]]]></description>
			<content:encoded><![CDATA[<div style='text-align:center'><img alt="" src="http://marklodato.github.com/visual-git-guide/basic-usage.svg.png" title="Git Visual Guide" class="alignnone" width="728" height="312" /></div>
<p>One of the gaps among my tech skills upon entering university was source/revision control.</p>
<p>For those of you unfamiliar, revision control is a method of tracking and storing the changes made to files. This is particularly useful when keeping track of all the changes made to source code being worked on in a group. This allows you to all work on the same set of files at once and merge together the changes later. </p>
<p>This doesn't mean it isn't useful for projects you're working on by yourself. If you've ever coded something up, then decided you have a better way of solving the problem, then finally realize your new solution doesn't work, you need to go back. Except the deleted code is one undo step beyond your history. Crap.</p>
<p>And no, allowing for more undo steps is not the solution to this problem. If you want to look at older versions across multiple files from weeks ago, undo won't help you. Revision control will.</p>
<p>After speaking with employers, it seems that the most commonly used source control system at the moment is git. I'd like to note that most of the people I interviewed with were small, fairly new, web or mobile based companies. Older companies may be using svn or possibly even cvs. Then there's the whole set of Microsoft source control systems such as Microsoft Visual Source Safe.</p>
<p>You can see a summary of source control options here: <a href="http://en.wikipedia.org/wiki/Comparison_of_revision_control_software">Comparison of revision control software @ Wikipedia</a></p>
<p>I'm posting about this now because I'm working on a collaborative project using git for the first time. Since this is a private project, I'm using <a href="http://www.projectlocker.com/">Project Locker</a> instead of <a href="http://github.com/">Github</a>. To be honest, I probably should have just set up my own private repository and I might look into that later.</p>
<p>You don't actually need to have a remote repository. You can use a git repository to control your source locally if you're the only one working on it. You might consider doing this for school projects so you don't accidentally overwrite your working code in an attempt to appease Marmoset (automated testing in CS at U Waterloo).</p>
<p>In order to learn how to use git, I can recommend two sources of information.</p>
<p>1. <a href="http://gitcasts.com">GitCasts</a>: these are screen casts, going through git and explaining things along the way. I'm in the middle of the fourth cast right now, so I can't say for sure these are all of high quality, but I'm seeing things I didn't know before, and that's enough for me.</p>
<p>2. <a href="http://marklodato.github.com/visual-git-guide/">Visual Git Guide</a>: Pictures are awesome, especially for those people of the tl;dr mindset. Or those attracted to colourful pictures. The picture at the top of the post is from the visual git guide. This is a fairly in depth explanation of some of the functionality of git and shows you what's actually happening behind the scenes.</p>
<p>For those of you more interested in learning Mercurial (Hg), <a href="http://se2014.com/~zmanji/">Zameer Manji</a> has recommended the following guide: <a href="http://hginit.com/index.html">Hg Init: a Mercurial tutorial by Joel Spolsky</a>.</p>
<p>I would recommend you go grab an account of GitHub to help yourself learning. If you're in need of something to fool around with, fork one of my projects: <a href="http://github.com/phleet">phleet @ github</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jamie-wong.com/2010/03/09/learn-source-control-with-git/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Jobmine Improved (Greasemonkey &amp; jQuery)</title>
		<link>http://jamie-wong.com/2010/02/05/jobmine-improved-greasemonkey-jquery/</link>
		<comments>http://jamie-wong.com/2010/02/05/jobmine-improved-greasemonkey-jquery/#comments</comments>
		<pubDate>Sat, 06 Feb 2010 05:41:43 +0000</pubDate>
		<dc:creator>Jamie Wong</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jamie-wong.com/?p=183</guid>
		<description><![CDATA[
I, like many (most) Waterloo Co-op students, am forced to use Jobmine and am extremely dissatisfied with its functionality. So I decided to kill three birds with one stone: improve Jobmine, learn Greasemonkey and learn jQuery all at the same time.
The result is, unsurprisingly, a Greasemonkey script written using jQuery that improves on some features [...]]]></description>
			<content:encoded><![CDATA[<div style='text-align: center'><img src="http://jamie-wong.com/wordpress/wp-content/uploads/2010/02/JobmineImproved.png" alt="JobmineImproved" title="JobmineImproved" width="482" height="263" class="alignnone size-full wp-image-184" /></div>
<p>I, like many (most) Waterloo Co-op students, am forced to use Jobmine and am extremely dissatisfied with its functionality. So I decided to kill three birds with one stone: improve Jobmine, learn Greasemonkey and learn jQuery all at the same time.</p>
<p>The result is, unsurprisingly, a Greasemonkey script written using jQuery that improves on some features of Jobmine.</p>
<p><strong>Features</strong></p>
<ul>
<li><strong>Table sorting</strong> - all major tables are now sortable (Interviews, Job Short List, Applications)</li>
<li><strong>Improved navigation</strong> - no more Student -> Use ridiculousness</li>
<li><strong>No more frames</strong> - you can refresh and it will stay on the same page!</li>
<li><strong>Colour highlighting for tables</strong> -  pictured above, you see the applications page with various statuses highlighted. Selected is green, not selected is red.</li>
<li><strong>No more spacers</strong> - the Jobmine page is riddled with spacer images just sitting there, stealing screen real estate</li>
</ul>
<p><strong>How to Install</strong><br />
You'll either need Firefox &#038; Greasemonkey, or a recent build of Chrome (Windows only?).<br />
You can get Greasemonkey here: <a href="https://addons.mozilla.org/en-US/firefox/addon/748">Greasemonkey @ addons.mozilla.org</a></p>
<p>Once you've done that, navigate to the script and click install.<br />
You can get the script here: <a href="http://userscripts.org/scripts/show/67574">Jobmine Upgrade @ userscripts.org</a></p>
<p>Now for the part where I explain the tech I used.</p>
<h1>Greasemonkey</h1>
<p>Greasemonkey is a tool for customizing the way a web page displays and interacts using javascript. More or less, it overlays javascript you write on top of pages you specify by URLs with wildcards (*). It doesn't overlay it directly, but wraps it in some way as to prevent it from messing things up in the global scope. It also seems to run once the page is done loading, not when the page head is loaded. There are plenty of tutorials out there for doing cool stuff with Greasemonkey, but I started here: <a href="http://diveintogreasemonkey.org/helloworld/index.html">Dive into Greasemonkey</a>. I know it says it's hideously outdated, but the metadata information it provides is still good enough. If you want more up to date information, go here: <a href="http://wiki.greasespot.net/Main_Page">GreaseSpot (Greasemonkey Wiki)</a>.</p>
<h1>jQuery</h1>
<p>jQuery is a javascript framework specifically designed for doing things involving the DOM tree absurdly quickly. Example: highlighting alternating rows of a table (zebra-striping).</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p1832"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code" id="p183code2"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Standard Javascript method:</span>
<span style="color: #003366; font-weight: bold;">var</span> tables <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;table&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> tables.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> rows <span style="color: #339933;">=</span> tables<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">tBodies</span><span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">rows</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> rows.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> rowColor<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">%</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            rowColor <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;#eef&quot;</span><span style="color: #339933;">;</span> 
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            rowColor <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;#fff&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> cells <span style="color: #339933;">=</span> rows<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">cells</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> k <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> k <span style="color: #339933;">&lt;</span> cells.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> k<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            cells<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> rowColor<span style="color: #339933;">;</span>
            cells<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">borderBottom</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;1px solid #ccc&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> 
&nbsp;
<span style="color: #006600; font-style: italic;">// jQuery way:</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;td&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;border-bottom&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;1px solid #ccc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tr:even &gt; td&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;background-color&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;#fff&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tr:odd &gt; td&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;background-color&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;#eef&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now before someone says it, I know usually you can set the background-color for the whole row, and the cells will inherit it. But since, for some crazy reason, each cell is assigned a background colour on Jobmine, each cell needs to be set individually. In any case, you can see that things are made substantially easier with jQuery. I figured out jQuery mostly just using the API and looking at other people's code, but this is a decent place to start: <a href="http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery">Getting Started with jQuery</a>.</p>
<p>For the table sorting functionality, I decided to use a jQuery plugin as opposed to write my own (I'd rather be able to distribute this sooner). You can read all about it here: <a href="http://tablesorter.com/docs/">jQuery Plugin: Tablesorter 2.0</a></p>
<p>What features do you want to see in this? By the way, the source is all available on the userscripts site, so feel free to tinker with it yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://jamie-wong.com/2010/02/05/jobmine-improved-greasemonkey-jquery/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tools: ideone, RegExr, jitouch 2, open, ditaa</title>
		<link>http://jamie-wong.com/2010/01/07/tools-ideone-regexr-jitouch-2-opengnome-open-ditaa/</link>
		<comments>http://jamie-wong.com/2010/01/07/tools-ideone-regexr-jitouch-2-opengnome-open-ditaa/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 17:06:42 +0000</pubDate>
		<dc:creator>Jamie Wong</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jamie-wong.com/?p=142</guid>
		<description><![CDATA[Through reading proggit and hearing about new technology from classmates, every once in a while, I build up a list of tools which I plan on checking out and see whether they're useful enough to add to my regular routine. The first four on this list fall into that category, and I may eventually find [...]]]></description>
			<content:encoded><![CDATA[<p>Through reading proggit and hearing about new technology from classmates, every once in a while, I build up a list of tools which I plan on checking out and see whether they're useful enough to add to my regular routine. The first four on this list fall into that category, and I may eventually find a use for the last.</p>
<h1>ideone</h1>
<p><img src="http://jamie-wong.com/wordpress/wp-content/uploads/2010/01/ideone.png" alt="ideone" title="ideone" width="300" height="55" style="float:left;margin-right:10px;"/><a href="http://www.ideone.com">ideone</a> is an in-browser, syntax-highlighted code editor complete with interpreters and compilers. Basically, if you've ever wanted to try out a language but really didn't feel like installing it on your system, this is the perfect place to start. The site even runs Brainf**k. </p>
<p>There's another site which accomplishes the same task, but less elegantly: <a href="http://codepad.org/">Codepad</a>. This site is so much less elegant that I wasn't originally planning on posting it, but decided to once I saw there was a <a href="http://www.vim.org/scripts/script.php?script_id=2298">Codepad vim plugin</a>. There's also emacs integrations.</p>
<h1>RegExr</h1>
<p><img src="http://jamie-wong.com/wordpress/wp-content/uploads/2010/01/regexr-300x111.png" alt="regexr" title="regexr" width="300" height="111" class="alignnone size-medium wp-image-148" style='float:left;margin-right:10px;'/> <a href="http://gskinner.com/RegExr/">Regexr</a> is an online tool, implemented in Adobe Flex, to test out regular expressions in real time. If you haven't learned about regular expressions yet, <a href="http://www.regular-expressions.info/">go learn</a> right now. They're just about the most powerful text matching, user verification and error correction tool in existence. They're also implemented in nearly all languages now in some form or another. Before I saw this site, I would test out all my regular expressions just using vim, but found it frustrating when the expressions needed to be changed to be compatible with php. So I'm likely going to start using RegExr instead.</p>
<h1>jitouch 2</h1>
<p><img src="http://jamie-wong.com/wordpress/wp-content/uploads/2010/01/Screen-shot-2010-01-07-at-11.09.43-AM.png" alt="Screen shot 2010-01-07 at 11.09.43 AM" title="Screen shot 2010-01-07 at 11.09.43 AM" width="311" height="101" class="alignnone size-full wp-image-162" style='float:left;margin-right:10px'/> <a href="http://www.jitouch.com/">Jitouch 2</a> is an application expanding on the multi-touch gestures available to MacBook Pro users who want to do more with just the touch pad. The two big things that this enables me to do that I love are opening links in new tabs using only taps on the keypad, and switching tabs using a gesture equivalent to ctrl-tab to switch tabs. I actually saw this reading <a href="http://blag.xkcd.com/">Randal Munroe's blag</a>.</p>
<h1>Mac OS X/Gnome open</h1>
<p><img src="http://jamie-wong.com/wordpress/wp-content/uploads/2010/01/Screen-shot-2010-01-07-at-11.16.08-AM-300x43.png" alt="Screen shot 2010-01-07 at 11.16.08 AM" title="Screen shot 2010-01-07 at 11.16.08 AM" width="300" height="43" class="alignnone size-medium wp-image-165" style='float:left;margin-right:10px'/>open &#038; gnome-open are terminal commands in Mac OS X and gnome respectively, but they do the same thing. Whenever you double click on a file in Finder or Nautilus, the operating system has a database of which extensions are opened by which applications. What's actually happening here is that it's called open. Examples:</p>
<pre>open "Office Space.avi"</pre>
<p>Open up Office Space in your default viewer.</p>
<pre>open http://www.jamie-wong.com</pre>
<p>Visit my website from the commandline, opening in your default browser</p>
<pre>open -a "Adobe Photoshop CS3"</pre>
<p>Launch Photoshop. Open -a opens files with an application. </p>
<h1>ditaa</h1>
<p><img src="http://jamie-wong.com/wordpress/wp-content/uploads/2010/01/Screen-shot-2010-01-07-at-11.44.21-AM-300x278.png" alt="Screen shot 2010-01-07 at 11.44.21 AM" title="Screen shot 2010-01-07 at 11.44.21 AM" width="300" height="278" class="alignnone size-medium wp-image-168" style="float:left;margin-right:10px;"/> <a href="http://ditaa.sourceforge.net/">ditaa</a> is a tool for converting ASCII art diagrams into graphical diagrams. This is pretty well illustrated in the picture to the left. I haven't actually found much of a use for this yet, but some of you might.</p>
]]></content:encoded>
			<wfw:commentRss>http://jamie-wong.com/2010/01/07/tools-ideone-regexr-jitouch-2-opengnome-open-ditaa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UWAngel-CLI</title>
		<link>http://jamie-wong.com/2009/12/27/uwangel-cli/</link>
		<comments>http://jamie-wong.com/2009/12/27/uwangel-cli/#comments</comments>
		<pubDate>Sun, 27 Dec 2009 07:10:03 +0000</pubDate>
		<dc:creator>Jamie Wong</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[stty]]></category>
		<category><![CDATA[UWAce]]></category>
		<category><![CDATA[UWAngel]]></category>

		<guid isPermaLink="false">http://jamie-wong.com/?p=131</guid>
		<description><![CDATA[
Near the end of the semester, I was getting kind of tired of navigating UW-ACE so frequently through my browsing and opening all the different tabs to grab all the files I wanted. While all the pretty graphics make for a decent user interface, it reduces the speed of the service. 
But aside from that, [...]]]></description>
			<content:encoded><![CDATA[<div style='text-align:center'><img src="http://jamie-wong.com/wordpress/wp-content/uploads/2009/12/Screen-shot-2009-12-27-at-1.39.50-AM.png" alt="Screen shot 2009-12-27 at 1.39.50 AM" title="Screen shot 2009-12-27 at 1.39.50 AM" width="480" height="244" class="alignnone size-full wp-image-132" /></div>
<p>Near the end of the semester, I was getting kind of tired of navigating UW-ACE so frequently through my browsing and opening all the different tabs to grab all the files I wanted. While all the pretty graphics make for a decent user interface, it reduces the speed of the service. </p>
<p>But aside from that, I like being able to do as much as I possibly can from the console.</p>
<p>So I made a Command Line Interface (CLI) for UW-ACE in php using cUrl. I built in on my Macbook Pro in Snow Leopard, but it should work just fine on any *nix machine, and possibly in Cygwin or other emulators. </p>
<p>As always, source is available on github: <a href="http://github.com/phleet/UWAngel-CLI">UWAngel-CLI @ Github</a>.</p>
<p>Since I always like to post snippets of code from my projects that may be universally useful, I'll do that here too.</p>
<p><strong>CLI Colour in PHP</strong><br />
cli_colours.php included in the UWAngel-CLI source is just a collection of constants which allow you to print out colours in your CLI scripts.</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p1315"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p131code5"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000088;">$COLOR_BLACK</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\033</span>[0;30m&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$COLOR_DARKGRAY</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\033</span>[1;30m&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$COLOR_BLUE</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\033</span>[0;34m&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$COLOR_LIGHTBLUE</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\033</span>[1;34m&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$COLOR_GREEN</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\033</span>[0;32m&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$COLOR_LIGHTGREEN</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\033</span>[1;32m&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$COLOR_CYAN</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\033</span>[0;36m&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$COLOR_LIGHTCYAN</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\033</span>[1;36m&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$COLOR_RED</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\033</span>[0;31m&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$COLOR_LIGHTRED</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\033</span>[1;31m&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$COLOR_PURPLE</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\033</span>[0;35m&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$COLOR_LIGHTPURPLE</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\033</span>[1;35m&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$COLOR_BROWN</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\033</span>[0;33m&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$COLOR_YELLOW</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\033</span>[1;33m&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$COLOR_LIGHTGRAY</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\033</span>[0;37m&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$COLOR_WHITE</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\033</span>[1;37m&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$COLOR_DEFAULT</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\033</span>[0;37m&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000cc; font-style: italic;">&lt;&lt;&lt; EOT
One fish, 
two fish, 
{$COLOR_RED}red{$COLOR_DEFAULT} fish, 
{$COLOR_BLUE}blue{$COLOR_DEFAULT} fish.
&nbsp;
EOT</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The only gripe I have about this is that COLOR_DEFAULT it's the same colour as I have on by default in iTerm. Anyone know the escape code to make it actually revert to what it was before instead of just making an assumption about what color is being used?</p>
<p><strong>Hide Commandline Input</strong><br />
One of the first things I looked up when I started this project today was how to hide user input from the command line. I sure as hell didn't want people typing their passwords for UWACE on screen and having it actually display.<br />
It turns out you can do this by temporarily telling your TTY to stop echoing what you type. The command for this is "stty -echo" and can be re-enabled using "stty echo". Below is how I implemented as part of the AngelAccess class to meet my needs.</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p1316"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p131code6"><pre class="php" style="font-family:monospace;"> <span style="color: #000000; font-weight: bold;">function</span> Prompt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$prompt_text</span><span style="color: #339933;">,</span><span style="color: #000088;">$hide</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$prompt_text</span><span style="color: #339933;">;</span>
     <span style="color: #000088;">$input</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$hide</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000088;">$input</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span>`stty <span style="color: #339933;">-</span><span style="color: #b1b100;">echo</span><span style="color: #339933;">;</span>head <span style="color: #339933;">-</span>n1<span style="color: #339933;">;</span>stty <span style="color: #b1b100;">echo</span>`<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000088;">$input</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/fgets"><span style="color: #990000;">fgets</span></a><span style="color: #009900;">&#40;</span>STDIN<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #b1b100;">return</span> <span style="color: #000088;">$input</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>As a complete side note, thanks to a boot-camped installation of Windows 7 (or at least I'm fairly sure that's the culprit,) my Macbook Pro is now stuck on Digital Out. This means I can't use the internal speakers on my computer under Mac OS X. Well... what I should say is that I can't use them without some annoying tricks. If I plug in my headphones, then tell my mac to use the input jack for audio input, then my internal speakers appear under the output options and let me use them. The speakers then work perfectly fine. They also work fine under Windows 7. A direct side effect of digital out being stuck on is a read light emanating from the audio jack. </p>
<p>The problem is apparently fairly common, unfortunately the only confirmed fixes for it are sending it back to Apple or <a href="http://forums.macrumors.com/showthread.php?t=239287">wiggling a toothpick around</a> in the audio jack. I had absolutely no luck with the toothpick, or precision screwdriver, or pen cartridge, or paintbrush handle. If anyone knows how to fix this problem, I would love to know. Otherwise, I'm just going to take it into the Apple store in Rideau some time this week and hope they can fix the problem. I really don't want to have to send my Mac in during my first week at Velocity.</p>
]]></content:encoded>
			<wfw:commentRss>http://jamie-wong.com/2009/12/27/uwangel-cli/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Manual Sorting</title>
		<link>http://jamie-wong.com/2009/12/12/manual-sorting/</link>
		<comments>http://jamie-wong.com/2009/12/12/manual-sorting/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 05:31:52 +0000</pubDate>
		<dc:creator>Jamie Wong</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[cards]]></category>
		<category><![CDATA[sorting]]></category>

		<guid isPermaLink="false">http://jamie-wong.com/?p=111</guid>
		<description><![CDATA[
The basis for this post is the following question: what is the fastest algorithm for sorting a deck of cards by hand?
TL:DR advisor - skip to the end of the post for a challenge.
In terms of algorithmic complexity, merge sort and quick sort are two of the fastest widely used sorting algorithms - both running [...]]]></description>
			<content:encoded><![CDATA[<div style='text-align:center'><img src="http://jamie-wong.com/wordpress/wp-content/uploads/2009/12/cards.jpg" alt="cards" title="cards" width="427" height="466" class="alignnone size-full wp-image-115" /></div>
<p>The basis for this post is the following question: <strong>what is the fastest algorithm for sorting a deck of cards by hand?</strong></p>
<p><em>TL:DR advisor - skip to the end of the post for a challenge.</em></p>
<p>In terms of algorithmic complexity, merge sort and quick sort are two of the fastest widely used sorting algorithms - both running at an average case of O(n log n). But if you've ever sorted a deck of cards, ordering primarily by suit and secondly by number, I doubt you used either of these algorithms. You might unknowingly be doing bucket sort, dividing the cards into 4 "buckets" - one for each suit, then sorting each suit using a different algorithm and joining all the suits together at the end. </p>
<p>If I had a deck of cards in my room right now, I would be inclined to take videos of me sorting them using various different algorithms and comparing the time required. I have a feeling that the fastest algorithm would involve drawing out a 4x13 grid on a big piece of paper with each cell labeled with the exact card that fits there, then running through the deck, placing all the cards on their grid cell and just picking them up in order at the end. Of course, this could be accomplished without the grid but requires a spacial sense which I simply do not possess. </p>
<p>As a followup, I have another question: <strong>what if the cards were labeled with a number system you've never seen before?</strong> Assume you have some visual reference allowing you to understand the system, but also assume that the number system is not intuitive. Does your approach change? Of course, the fastest approach here depends on what I'm really asking by "fastest". Given enough time and practice, you would be able to become familiar enough with the numeral system to use any approach you would with regular cards or regular decimal numbers. When I say "fastest", I'm asking how you would minimize the time between receiving the cards and the visual reference and the cards being sorted. In terms of computation, the introduction of this numeral system has drastically increased the time for a comparison or enumeration while not affecting the time for movement or swapping at all. The reverse would be to use the standard deck of cards but make them orders of magnitude larger - say 1 meter wide each. In this case a move or a swap is extremely costly, but a comparison is very cheap.</p>
<p>A more realistic scenario in which the cost of comparison is drastically increased comes about when the criteria used for sorting is not an absolute. Example: sort these pictures by aesthetic appeal. Even by yourself this may be a long process, as you second guess your original decision about the relative appeal of a picture. In the world of web 2.0 though, it's a much much longer process. Sorting by crowd-sourced opinion is the basis of many websites, such as bash.org and reddit. Most of these systems work by providing users with the ability or increase or decrease the item's value by small amounts. Is there some better way of ensuring that the articles which will be the most valuable to the readers will show up at the top? </p>
<p>Returning to the point of manual sorting, I have 2 more things to say.</p>
<p>The first is an idea - competitive sorting. I'm sure this sounds nerdy as hell, but I think I'd still find it fairly entertaining. Groups of people would be given sets of objects and told to sort by some criteria, the fastest group being the victor, with penalties dished out for people falsely claiming their list is sorted. The criteria could be size, weight, volume, buoyancy, color saturation, retail price, alcohol content, power consumption or even something as obscure as average salary of a worker for the company manufacturing the product. Given that I'm at Waterloo, I feel that organizing such a competition isn't all that unlikely. Anyone feel like coming up with a plan for making this actually happen?</p>
<p>The second is a challenge. I challenge everyone reading this to make a video of them sorting a deck of 52 cards (no jokers) as fast as they can, then post the video in the comment section. As soon as I actually get a deck of cards, I'll take part in this challenge myself.</p>
<p><strong>Rules.</strong></p>
<ol>
<li> The video must be all in one take</li>
<li> The deck must be shuffled thoroughly on screen, then fanned towards the camera to demonstrate the randomized order of the cards.</li>
<li> Once the cards have been fanned towards the camera, you can have a maximum of 10 seconds of review time looking through the cards before the sorting starts. All cards must stay in contact and in order during this review time.</li>
<li> The timer starts the second a card is separated from the rest of the deck.</li>
<li> The timer stops when the deck has been reassembled into a single sorted deck.</li>
<li> A deck is considered sorted if the cards are primarily ordered by suit in the order diamonds, clubs, hearts, spades, and secondarily ascending numerically.</li>
<li> You must fan the deck towards the camera after it is sorted.</li>
<li> You are allowed any setup you want before starting to sort (e.g. the grid I talked about above,) provided that the setup does not contain moving parts - no robots.</li>
<li> You must be the only one handling the cards - no team efforts.</li>
</ol>
<p>That's all. I fully expect to receive no responses to this challenge, but would love to see the things people come up with if someone does try it.</p>
<p>EDIT: Woohoo! Someone attempted!<br />
<a href='http://www.youtube.com/watch?v=iPDi0QfFvW8' >Timothy Armstrong Sorting</a></p>
<p>Also, the claimed record according to <a href="http://www.recordholders.org/en/records/cardsorting.html">this page</a> is 36.1 seconds. </p>
]]></content:encoded>
			<wfw:commentRss>http://jamie-wong.com/2009/12/12/manual-sorting/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Lambda Functions</title>
		<link>http://jamie-wong.com/2009/12/06/lambda-functions/</link>
		<comments>http://jamie-wong.com/2009/12/06/lambda-functions/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 02:49:27 +0000</pubDate>
		<dc:creator>Jamie Wong</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[reduce]]></category>
		<category><![CDATA[sort]]></category>

		<guid isPermaLink="false">http://jamie-wong.com/?p=101</guid>
		<description><![CDATA[One day, a friend of mine called me up asking for help with his CS assignment. It was about lambda functions in scheme. Lambda functions had always been something confusing both in theory and practice for me. Why do you need them? Before I explain what they are, I'll start with a simple probably justifying [...]]]></description>
			<content:encoded><![CDATA[<p>One day, a friend of mine called me up asking for help with his CS assignment. It was about lambda functions in scheme. Lambda functions had always been something confusing both in theory and practice for me. Why do you need them? Before I explain what they are, I'll start with a simple probably justifying <i>why</i> they are.</p>
<p>All code in this post is going to be in python, but most of it can be applied to other high level languages. I've only used lambda functions in python and scheme, but I'm sure the functionality exists in many, many other languages. </p>
<h2> Sorting </h2>
<p>Sorting in python is very straight forward. If I have a list of strings in python and want to sort them, I do this:</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p10122"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p101code22"><pre class="python" style="font-family:monospace;">a = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;banana&quot;</span>,<span style="color: #483d8b;">&quot;apple&quot;</span>,<span style="color: #483d8b;">&quot;pear&quot;</span>,<span style="color: #483d8b;">&quot;elephant&quot;</span>,<span style="color: #483d8b;">&quot;zebra&quot;</span>,<span style="color: #483d8b;">&quot;mango&quot;</span><span style="color: black;">&#93;</span>
a.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> a
<span style="color: #808080; font-style: italic;"># OUT: ['apple', 'banana', 'elephant', 'mango', 'pear', 'zebra']</span></pre></td></tr></table></div>

<p>But what happens when you try to sort a list of words containing capital letters?</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p10123"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p101code23"><pre class="python" style="font-family:monospace;">a = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;pear&quot;</span>,<span style="color: #483d8b;">&quot;Police&quot;</span>,<span style="color: #483d8b;">&quot;apple&quot;</span>,<span style="color: #483d8b;">&quot;Airplane&quot;</span>,<span style="color: #483d8b;">&quot;banana&quot;</span>,<span style="color: #483d8b;">&quot;Bear&quot;</span><span style="color: black;">&#93;</span>
a.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> a
<span style="color: #808080; font-style: italic;"># OUT: ['Airplane', 'Bear', 'Police', 'apple', 'banana', 'pear']</span></pre></td></tr></table></div>

<p>The sorting is done purely based on the byte values of the characters. Since 'P' (ASCII 80) < 'a' (ASCII 97), "Police" is placed before "apple" in the list of words. Normally when you're trying to sort a list of words, you want to do it alphabetically. So how do you fix this? You make a comparator function. Here's python's definition of the sort function.</p>
<blockquote><p>
    L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;<br />
    cmp(x, y) -> -1, 0, 1
</p></blockquote>
<p>For those of you who have used qsort in the standard C library, this isn't such an arcane concept. In python however, it is much easier (no void* madness).</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p10124"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p101code24"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> alphacmp<span style="color: black;">&#40;</span>x,y<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">cmp</span><span style="color: black;">&#40;</span>x.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,y.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
a = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;pear&quot;</span>,<span style="color: #483d8b;">&quot;Police&quot;</span>,<span style="color: #483d8b;">&quot;apple&quot;</span>,<span style="color: #483d8b;">&quot;Airplane&quot;</span>,<span style="color: #483d8b;">&quot;banana&quot;</span>,<span style="color: #483d8b;">&quot;Bear&quot;</span><span style="color: black;">&#93;</span>
a.<span style="color: black;">sort</span><span style="color: black;">&#40;</span>alphacmp<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> a
<span style="color: #808080; font-style: italic;"># OUT: ['Airplane', 'apple', 'banana', 'Bear', 'pear', 'Police']</span></pre></td></tr></table></div>

<p>Okay, great! We now have our list sorted in the intuitive way. The lower() function will ensure that the lowercase version of the strings are being compared, removing the issues of the byte comparisons. But this code seems a little longer than it could be. Normally, you wouldn't create a function for task you do only once if it was just as readable within the code body. But the sort function needs a comparator function. So how can you give sort a comparator function without having to actually declare a function? The answer is lambda functions.</p>
<h2> Lambda Functions </h2>
<p>Lambda functions are anonymous functions. What does that mean? They're functions without a declared name. Let's look at an example so you can see what I mean. In the above example, we can sort alphabetically without defining the alphacmp function like so:</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p10125"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p101code25"><pre class="python" style="font-family:monospace;">a = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;pear&quot;</span>,<span style="color: #483d8b;">&quot;Police&quot;</span>,<span style="color: #483d8b;">&quot;apple&quot;</span>,<span style="color: #483d8b;">&quot;Airplane&quot;</span>,<span style="color: #483d8b;">&quot;banana&quot;</span>,<span style="color: #483d8b;">&quot;Bear&quot;</span><span style="color: black;">&#93;</span>
a.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x,y: <span style="color: #008000;">cmp</span><span style="color: black;">&#40;</span>x.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,y.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> a
<span style="color: #808080; font-style: italic;"># OUT: ['Airplane', 'apple', 'banana', 'Bear', 'pear', 'Police']</span></pre></td></tr></table></div>

<p>So what does lambda x,y: cmp(x.lower(),y.lower()) mean? "lambda" says that the statements following define an anonymous function. "x,y:" defines the parameter list accepted by the anonymous function. "cmp(x.lower(),y.lower())" defines the return value for the lambda function. If you're still a little bit confused, read on for many many more examples. At some point, it will likely click and you'll see how valuable lambda really is.</p>
<p>Another sorting example: what if I want to sort by length of the strings instead of alphabetically? Easily done with lambda.</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p10126"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p101code26"><pre class="python" style="font-family:monospace;">a = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;pencil&quot;</span>,<span style="color: #483d8b;">&quot;pen&quot;</span>,<span style="color: #483d8b;">&quot;cap&quot;</span>,<span style="color: #483d8b;">&quot;zebra&quot;</span>,<span style="color: #483d8b;">&quot;Blizzard&quot;</span>,<span style="color: #483d8b;">&quot;0xB4DC0DE&quot;</span>,<span style="color: #483d8b;">&quot;!&quot;</span><span style="color: black;">&#93;</span>
a.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x,y: <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span> - <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>y<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> a
<span style="color: #808080; font-style: italic;"># OUT: ['!', 'pen', 'cap', 'zebra', 'pencil', 'Blizzard', '0xB4DC0DE']</span></pre></td></tr></table></div>

<p>The use of lambda functions makes the use of many higher level functions much nicer. I'll be outlining a few of them here.</p>
<h2> map </h2>
<blockquote><p>
    map(function, sequence[, sequence, ...]) -> list</p>
<p>    Return a list of the results of applying the function to the items of<br />
    the argument sequence(s).  If more than one sequence is given, the<br />
    function is called with an argument list consisting of the corresponding<br />
    item of each sequence, substituting None for missing values when not all<br />
    sequences have the same length.  If the function is None, return a list of<br />
    the items of the sequence (or a list of tuples if more than one sequence).
</p></blockquote>
<p>More or less, what map does is apply a function on every element of an array then return an array of the corresponding return values.</p>
<p>A very simple use for map would be making every word in a list upper case.</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p10127"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p101code27"><pre class="python" style="font-family:monospace;">a = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;abc&quot;</span>,<span style="color: #483d8b;">&quot;cattle&quot;</span>,<span style="color: #483d8b;">&quot;not even if there's a FIRE&quot;</span>,<span style="color: #483d8b;">&quot;Jeymi!?&quot;</span><span style="color: black;">&#93;</span>
b = <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x: x.<span style="color: black;">upper</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,a<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> b
<span style="color: #808080; font-style: italic;"># OUT: ['ABC', 'CATTLE', &quot;NOT EVEN IF THERE'S A FIRE&quot;, 'JEYMI!?']</span></pre></td></tr></table></div>

<p>A more useful example involves printing out grids. Say I have a matrix of numbers, represented as a list of lists in python. The default output formatting for this in python is extremely difficult to look at.</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p10128"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p101code28"><pre class="python" style="font-family:monospace;">a = <span style="color: black;">&#91;</span>
    <span style="color: black;">&#91;</span>1,212,-13<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span>41,5,614<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span>7,8,91<span style="color: black;">&#93;</span>
<span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> a
<span style="color: #808080; font-style: italic;"># OUT: [[1, 212, -13], [41, 5, 614], [7, 8, 91]]</span></pre></td></tr></table></div>

<p>What would be better is to output one row per line.</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p10129"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p101code29"><pre class="python" style="font-family:monospace;">a = <span style="color: black;">&#91;</span>
    <span style="color: black;">&#91;</span>1,212,-13<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span>41,5,614<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span>7,8,91<span style="color: black;">&#93;</span>
<span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x: <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>, a<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;">#OUT:</span>
<span style="color: #808080; font-style: italic;"># [1, 212, -13]</span>
<span style="color: #808080; font-style: italic;"># [41, 5, 614]</span>
<span style="color: #808080; font-style: italic;"># [7, 8, 91]</span></pre></td></tr></table></div>

<p><em>The join function concatenates a list of string, separating the elements with the separator character - in this case: "\n"</em></p>
<p>Much better! But what would be even better would be to print out the numbers in columns that line up nicely too.</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p10130"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p101code30"><pre class="python" style="font-family:monospace;">a = <span style="color: black;">&#91;</span>
    <span style="color: black;">&#91;</span>1,212,-13<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span>41,5,614<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span>7,8,91<span style="color: black;">&#93;</span>
<span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>
    <span style="color: #008000;">map</span><span style="color: black;">&#40;</span>
        <span style="color: #ff7700;font-weight:bold;">lambda</span> row:
            <span style="color: #483d8b;">&quot; &quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">map</span><span style="color: black;">&#40;</span>
                <span style="color: #ff7700;font-weight:bold;">lambda</span> y: <span style="color: #483d8b;">&quot;%4d&quot;</span> <span style="color: #66cc66;">%</span> y,
                row
            <span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,
        a
    <span style="color: black;">&#41;</span>
<span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;"># OUT:</span>
<span style="color: #808080; font-style: italic;">#   1  212  -13</span>
<span style="color: #808080; font-style: italic;">#  41    5  614</span>
<span style="color: #808080; font-style: italic;">#   7    8   91</span></pre></td></tr></table></div>

<p><em>"%4d" % y is applying the formatting string %4d to y, much the same way that printf in C and php do</em></p>
<p>Now, using a lambda function inside of a lambda function is less than readable, so I'll write out what this function does first without the maps and lambdas.</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p10131"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code" id="p101code31"><pre class="python" style="font-family:monospace;">a = <span style="color: black;">&#91;</span>
    <span style="color: black;">&#91;</span>1,212,-13<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span>41,5,614<span style="color: black;">&#93;</span>,
    <span style="color: black;">&#91;</span>7,8,91<span style="color: black;">&#93;</span>
<span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> rowformat<span style="color: black;">&#40;</span>row<span style="color: black;">&#41;</span>:
    formatted_cells = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> element <span style="color: #ff7700;font-weight:bold;">in</span> row:
        formatted_cells.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%4d&quot;</span> <span style="color: #66cc66;">%</span> element<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot; &quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>formatted_cells<span style="color: black;">&#41;</span>
&nbsp;
formatted_rows = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> row <span style="color: #ff7700;font-weight:bold;">in</span> a:
    formatted_rows.<span style="color: black;">append</span><span style="color: black;">&#40;</span>rowformat<span style="color: black;">&#40;</span>row<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>formatted_rows<span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;"># OUT:</span>
<span style="color: #808080; font-style: italic;">#   1  212  -13</span>
<span style="color: #808080; font-style: italic;">#  41    5  614</span>
<span style="color: #808080; font-style: italic;">#   7    8   91</span></pre></td></tr></table></div>

<p>In this case, "rowformat(row)" plays the exact same roles as "lambda row:".</p>
<h2> filter </h2>
<blockquote><p>
    filter(function or None, sequence) -> list, tuple, or string</p>
<p>    Return those items of sequence for which function(item) is true.  If<br />
    function is None, return the items that are true.  If sequence is a tuple<br />
    or string, return the same type, else return a list.
</p></blockquote>
<p>The filter function does just what it's name suggests: filters out items that don't meet certain requirements. Specifically, any element which does not evaluate to true when passed to the provided function will be discarded. Note that this is <em>not</em> in place - it returns the filtered list.</p>
<p>Example: remove all odd numbers from a list of numbers</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p10132"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p101code32"><pre class="python" style="font-family:monospace;">a = <span style="color: black;">&#91;</span>1,2,5,0,-15,100,1400,-1337,135<span style="color: black;">&#93;</span>
a = <span style="color: #008000;">filter</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x: x <span style="color: #66cc66;">%</span> 2 == 0, a<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> a
<span style="color: #808080; font-style: italic;">#OUT: [2, 0, 100, 1400]</span></pre></td></tr></table></div>

<p>Example: remove all vowels from a string</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p10133"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p101code33"><pre class="python" style="font-family:monospace;">a = <span style="color: #483d8b;">&quot;Hello there! My name is Jamie, not to be confused with Jeymi.&quot;</span>
a = <span style="color: #483d8b;">&quot;&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">filter</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x: <span style="color: #483d8b;">&quot;aeiou&quot;</span>.<span style="color: black;">find</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span> == -1, <span style="color: #008000;">list</span><span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> a
<span style="color: #808080; font-style: italic;"># OUT: Hll thr! My nm s Jm, nt t b cnfsd wth Jym.</span></pre></td></tr></table></div>

<p>Example: build a list of all prime numbers between 0 and 100 inclusive</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p10134"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p101code34"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># NOTE: This prime test is inefficient - used for readability)</span>
<span style="color: #ff7700;font-weight:bold;">def</span> isprime<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>x <span style="color: #66cc66;">&lt;</span> 2<span style="color: black;">&#41;</span>: 
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>2,x<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>x <span style="color: #66cc66;">%</span> i == 0<span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">filter</span><span style="color: black;">&#40;</span>isprime,<span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">101</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;"># OUT (placed on multiple lines for readability)</span>
<span style="color: #808080; font-style: italic;"># [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, </span>
<span style="color: #808080; font-style: italic;"># 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]</span></pre></td></tr></table></div>

<h2> reduce </h2>
<blockquote><p>
    reduce(function, sequence[, initial]) -> value</p>
<p>    Apply a function of two arguments cumulatively to the items of a sequence,<br />
    from left to right, so as to reduce the sequence to a single value.<br />
    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates<br />
    ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items<br />
    of the sequence in the calculation, and serves as a default when the<br />
    sequence is empty.
</p></blockquote>
<p>Example: Calculate 30 factorial without using local variables</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p10135"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p101code35"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Note that range(2,n) -&gt; [2,3,...,n-1]</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x,y: x<span style="color: #66cc66;">*</span>y, <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">31</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;"># OUT: 265252859812191058636308480000000</span></pre></td></tr></table></div>

<p>Example: Find the longest word in a list of words</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p10136"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p101code36"><pre class="python" style="font-family:monospace;">a = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;pencil&quot;</span>,<span style="color: #483d8b;">&quot;pen&quot;</span>,<span style="color: #483d8b;">&quot;cap&quot;</span>,<span style="color: #483d8b;">&quot;zebra&quot;</span>,<span style="color: #483d8b;">&quot;Blizzard&quot;</span>,<span style="color: #483d8b;">&quot;0xB4DC0DE&quot;</span>,<span style="color: #483d8b;">&quot;!&quot;</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x,y: y <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>y<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">else</span> x, a<span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;"># OUT: 0xB4DC0DE</span></pre></td></tr></table></div>

<p>And that's all for now. If you're in SE 2014 reading this and don't think you need to know how to use lambda functions - have fun with scheme.</p>
]]></content:encoded>
			<wfw:commentRss>http://jamie-wong.com/2009/12/06/lambda-functions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Game of Life</title>
		<link>http://jamie-wong.com/2009/12/01/game-of-life/</link>
		<comments>http://jamie-wong.com/2009/12/01/game-of-life/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 00:10:10 +0000</pubDate>
		<dc:creator>Jamie Wong</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[conway]]></category>
		<category><![CDATA[gif]]></category>
		<category><![CDATA[gifsicle]]></category>
		<category><![CDATA[life]]></category>
		<category><![CDATA[optparse]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://jamie-wong.com/?p=86</guid>
		<description><![CDATA[For my latest project, I'm implementing Conway's Game of Life in python into animated GIFs.
Before I even explain what Conway's Game of Life is, be amused by the below, generated animation:

As always, the code I'm using here is open source: Game Of Life @ Github.
In addition to the source code, there's also a few animation [...]]]></description>
			<content:encoded><![CDATA[<p>For my latest project, I'm implementing Conway's Game of Life in python into animated GIFs.</p>
<p>Before I even explain what Conway's Game of Life is, be amused by the below, generated animation:<br />
<img src="/images/queenbee.gif" alt="Queenbee"></p>
<p>As always, the code I'm using here is open source: <a href="http://www.github.com/phleet/GameOfLife/">Game Of Life @ Github</a>.<br />
In addition to the source code, there's also a few animation demos such as the one above.</p>
<p>Conway's Game of Life is a cellular automaton following very simple rules, as outlined in the Wikipedia article.It is a zero player game played on a 2 dimensional grid of squares, each holding either a state of dead or alive. The state of any cell is dependent on the state of the 8 cells neighbouring it in the previous generation. There are only 4 rules.</p>
<p>From the Wikipedia article <a href="http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Conway's Game of Life</a>:</p>
<blockquote><p>
   1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.<br />
   2. Any live cell with more than three live neighbours dies, as if by overcrowding.<br />
   3. Any live cell with two or three live neighbours lives on to the next generation.<br />
   4. Any dead cell with exactly three live neighbours becomes a live cell.
</p></blockquote>
<p>The colours you see in the above animation represent the alive status of each cell and also how many neighbours that cell has if it's alive.</p>
<p>This project is my first time making use of two utilities: optparse in python, and gifsicle. </p>
<p><b>optparse</b> is a python library designed to make the creation of command line application much simpler. Specifically it's targeted towards making application with many possible flags easy to maintain. lifeImage.py falls into this category. From the commandline, you can control the source of input, the colour scheme, the number of generations to output, the scaling of the image and various other things to produce the exact animation or image you want.</p>
<p>You can take a look at optparse here: <a href="http://docs.python.org/library/optparse.html">optparse @ docs.python.org</a>.<br />
The tutorial included there was enough for me to create this application.</p>
<p><b>gifsicle</b> is a command line application for the creation and modification of animated gifs. It can create gifs out of a sequence of images, convert an image into a sequence of images, or even modify replace a single frame of an animated gif with an external image. </p>
<p>You can see and download gifsicle here: <a href="http://www.lcdf.org/gifsicle/">Official Gifsicle Page</a></p>
<p>Why did I use gifsicle instead of the much more universal convert in ImageMagick? Simply put: gifsicle is faster. If someone would like to do a benchmark to (dis)prove this, I'd be happy to post the results, but from simple experimentation, it seemed obvious to me that gifsicle took less time to make the animation.</p>
<p><b>What's Next?</b><br />
Now that I have a working command line utility, my next goal is to make an AJAX powered web interface for the thing. This might explain why I have ProcMonitor in the github for Game of Life. The web interface was another key motivator behind using gifs for the output medium. People may want to use these things for avatar, and they're simply easier to share and move around than a java applet, or a flash swf, or some database stored simulation. It also helps that gifs are designed for palette based images, which works out nicely for optimizing the file size of these animations. The first 1000 generations of acorn is 2.5 MB as it is. </p>
<p>Another thing I want to do is make lifeImage.py read the .cells format from the <a href="http://www.bitstorm.org/gameoflife/lexicon/">Life Lexicon</a>. This would save me a lot of time having to code all the states myself. This will be a very straightforward process, as the .cells files are simply plaintext with 2 lines of header.</p>
<p>Suggestions/bug fixes for my implementation of Game of Life are welcomed.</p>
<p>I'm almost 100% sure that some combination of the command line flags of lifeImage.py don't work nicely together, and would like to know what they are.</p>
]]></content:encoded>
			<wfw:commentRss>http://jamie-wong.com/2009/12/01/game-of-life/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Omegle Voyeur &#8211; Multiple Connections</title>
		<link>http://jamie-wong.com/2009/11/14/omegle-voyeur-multiple-connections/</link>
		<comments>http://jamie-wong.com/2009/11/14/omegle-voyeur-multiple-connections/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 09:52:12 +0000</pubDate>
		<dc:creator>Jamie Wong</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[omegle]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://jamie-wong.com/me/?p=76</guid>
		<description><![CDATA[In case you haven't read the post about all my projects, here's a description of what Omegle Voyeur is:

Omegle is a website where you are connected to a stranger for a chat. It is dominated mostly by trolls whose primary purpose is to coerce you into a cyber session and then switch genders or to [...]]]></description>
			<content:encoded><![CDATA[<p>In case you haven't read the post about all my projects, here's a description of what Omegle Voyeur is:</p>
<blockquote><p>
Omegle is a website where you are connected to a stranger for a chat. It is dominated mostly by trolls whose primary purpose is to coerce you into a cyber session and then switch genders or to make you lose the game. Talking to these people is a rather tiresome endeavour, but seeing exactly what happens in these conversations is interesting. Omegle Voyeur is a way of watching a conversation which you aren't part of. What Voyeur does is form two simultaneous connections and then pass the input of one to the output of the other. This sets you up as a conversation proxy, allowing you to watch. Currently, this is exclusively a "sit and watch" program. Later I intend to add functionality to add more than 2 people into a conversation, automatically name the participants so it will be obvious that there are more than 2 people in the conversation, and allow the ability to interfere (mute participants/say things yourself) with a conversation. This concept was spawned during discussion (read: boredom) at CCC Stage 2, 2009.
</p></blockquote>
<p>In terms of technology, Omegle Voyeur is primarily one big Javascript Prototype class. <a href='http://www.prototypejs.org/'>Prototype</a> is a Javascript Framework which makes the creation and maintenance of classes, conversion of data into JSON for transfer, and sending AJAX requests much, much easier.</p>
<p>There's also a very small bit of code in php which is able to be so short because it uses the incredible program cUrl. <a href="http://curl.haxx.se/">cUrl</a> is a command line utility for grabbing data from websites using their URL. libcurl facilititates the use of curl in php without having to write your own wrapper.</p>
<p>Below is some php code I use to make curl even easier than it already is. simple_get($url) will return the HTTP GET result from the url specified. simple_post works similarly, but delivers data using the payload.</p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p7638"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code" id="p76code38"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #666666; font-style: italic;">// Simple cUrl</span>
<span style="color: #666666; font-style: italic;">// Simple get and post requests </span>
<span style="color: #000000; font-weight: bold;">function</span> simple_get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_init"><span style="color: #990000;">curl_init</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> 1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_exec"><span style="color: #990000;">curl_exec</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/curl_close"><span style="color: #990000;">curl_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> simple_post<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #000088;">$payload</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_init"><span style="color: #990000;">curl_init</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #000088;">$payload</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> 1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_exec"><span style="color: #990000;">curl_exec</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/curl_close"><span style="color: #990000;">curl_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>When I started working on this project today (well, I suppose that would be last night now... wonder if I'll see the sunrise) I figured it would be a good time to get used to using git, so I made a repository using github.<br />
So far I'm enjoying git. Everything seems to act pretty much the way you'd expect to, and I already had to do a revert once I realized my logic was wrong for the way I was structuring my code.</p>
<p>You can see the github for Omegle Voyeur here: <a href="http://github.com/phleet/Omegle-Voyeur">http://github.com/phleet/Omegle-Voyeur</a><br />
Feel free to design your own stuff with the all the code there - just be sure to link back here, or to the github page.</p>
<p>In any case, the thing the majority of the people reading this are probably interested in are the result.<br />
Things I've updated since last time are primarily aesthetic and behind the scenes, but I did add the ability to connect one person to more than one other person, and the connections don't have to be mutual. In the first 3 way conversation example, 1 can only speak to 2, 2 can only speak to 3 and 3 can only speak to 1. It leads to some rather confused people.</p>
<p><del datetime="2009-11-26T06:58:15+00:00">You can see the current running version here: <a href="/omegle/">Omegle Voyeur</a>.</del><br />
EDIT: It seems that after being posted on reddit, omegle has (manually?) IP blocked me. The source should still work, so feel free to try it out yourself.<br />
You can go grab <a href="http://www.apachefriends.org/en/xampp.html">XAMPP</a> to run it locally.<br />
For the time being, you can view it here: <a href="http://petersobot.com/omegle/">Omegle Voyeur</a></p>
<p>A quick note on how I figured out the Omegle communication protocol.<br />
The entire code governing the process is conveniently kept here: <a href="http://omegle.com/static/omegle.js?27">http://omegle.com/static/omegle.js?27</a><br />
Unless you enjoy reading 1000s of characters on a single line, you can use the <a href='http://jsbeautifier.org/'>JS Beautifier</a> to clean it up to a readable state.</p>
]]></content:encoded>
			<wfw:commentRss>http://jamie-wong.com/2009/11/14/omegle-voyeur-multiple-connections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SPOJ Problem 2: Prime Generator (PRIME1)</title>
		<link>http://jamie-wong.com/2009/11/12/spoj-problem-2-prime-generator/</link>
		<comments>http://jamie-wong.com/2009/11/12/spoj-problem-2-prime-generator/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 05:47:34 +0000</pubDate>
		<dc:creator>Jamie Wong</dc:creator>
				<category><![CDATA[SPOJ]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[c99]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[pascal]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[prime]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://jamie-wong.com/me/?p=47</guid>
		<description><![CDATA[Problem: Prime Generator

Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!

Concept
The idea behind every solution here (with some variation) is to generate all the prime numbers that could be factors of numbers up to the maximum endpoint 1 billion. That [...]]]></description>
			<content:encoded><![CDATA[<p>Problem: <a href='https://www.spoj.pl/problems/PRIME1/'>Prime Generator</a></p>
<blockquote><p>
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!
</p></blockquote>
<p><strong>Concept</strong><br />
The idea behind every solution here (with some variation) is to generate all the prime numbers that could be factors of numbers up to the maximum endpoint 1 billion. That square root happens to be around 32000. Using this array, do a bounded Sieve of Eratosthenes only in the range requested. In languages like php and python, it turns out that it's more efficient to build an associative array and check if the index is set than it is to generate a huge boolean array.</p>
<p><strong>Code</strong></p>
<p><strong>C++</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4749"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
</pre></td><td class="code" id="p47code49"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">/// PRIME1 - C++ (g++)</span>
<span style="color: #666666;">// AC Time: 2.52s</span>
<span style="color: #666666;">// NOTE: I am aware that the use of vector and set actually</span>
<span style="color: #666666;">//  makes this code run _slower_</span>
<span style="color: #666666;">// I used vector and set simply as a way of practicing STL</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;cmath&gt;</span>
<span style="color: #339900;">#include &lt;vector&gt;</span>
<span style="color: #339900;">#include &lt;set&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    vector<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> primes<span style="color: #008080;">;</span>
    primes.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">32000</span><span style="color: #008080;">;</span> i<span style="color: #000040;">+</span><span style="color: #000080;">=</span>2<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">bool</span> isprime <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">int</span> cap <span style="color: #000080;">=</span> <span style="color: #0000dd;">sqrt</span><span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
        vector<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> p<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>p <span style="color: #000080;">=</span> primes.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> p <span style="color: #000040;">!</span><span style="color: #000080;">=</span> primes.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> p<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>p <span style="color: #000080;">&gt;=</span> cap<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000040;">%</span> <span style="color: #000040;">*</span>p <span style="color: #000080;">==</span> 0<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                isprime <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
                <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>isprime<span style="color: #008000;">&#41;</span> primes.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">int</span> T,N,M<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> T<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> t <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> t <span style="color: #000080;">&lt;</span> T<span style="color: #008080;">;</span> t<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>t<span style="color: #008000;">&#41;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> M <span style="color: #000080;">&gt;&gt;</span> N<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>M <span style="color: #000080;">&lt;</span> 2<span style="color: #008000;">&#41;</span> M <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #0000ff;">int</span> cap <span style="color: #000080;">=</span> <span style="color: #0000dd;">sqrt</span><span style="color: #008000;">&#40;</span>N<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
        set<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> notprime<span style="color: #008080;">;</span>
        notprime.<span style="color: #007788;">clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        vector<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> p<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>p <span style="color: #000080;">=</span> primes.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> p <span style="color: #000040;">!</span><span style="color: #000080;">=</span> primes.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> p<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>p <span style="color: #000080;">&gt;=</span> cap<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">int</span> start<span style="color: #008080;">;</span>
&nbsp;
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>p <span style="color: #000080;">&gt;=</span> M<span style="color: #008000;">&#41;</span> start <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>p<span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span><span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">else</span> start <span style="color: #000080;">=</span> M <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>p <span style="color: #000040;">-</span> M <span style="color: #000040;">%</span> <span style="color: #000040;">*</span>p<span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> <span style="color: #000040;">*</span>p<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
            <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> j <span style="color: #000080;">=</span> start<span style="color: #008080;">;</span> j <span style="color: #000080;">&lt;=</span> N<span style="color: #008080;">;</span> j <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #000040;">*</span>p<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                notprime.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span>j<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> M<span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> N<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>notprime.<span style="color: #007788;">count</span><span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> 0<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> i <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-47"></span><br />
<strong>C99</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4750"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
</pre></td><td class="code" id="p47code50"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// PRIME1 - C99 (gcc)</span>
<span style="color: #666666; font-style: italic;">// AC Time: 0.07s</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;string.h&gt;</span>
<span style="color: #339933;">#include &lt;stdbool.h&gt;</span>
<span style="color: #339933;">#include &lt;math.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> primes<span style="color: #009900;">&#91;</span>4000<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> numprimes <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
    primes<span style="color: #009900;">&#91;</span>numprimes<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">32000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">+=</span>2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        bool isprime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #993333;">int</span> cap <span style="color: #339933;">=</span> sqrt<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> numprimes<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>primes<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;=</span> cap<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">%</span> primes<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                isprime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>isprime<span style="color: #009900;">&#41;</span> primes<span style="color: #009900;">&#91;</span>numprimes<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #993333;">int</span> T<span style="color: #339933;">,</span>N<span style="color: #339933;">,</span>M<span style="color: #339933;">;</span>
    scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,&amp;</span>T<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> t <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> t <span style="color: #339933;">&lt;</span> T<span style="color: #339933;">;</span> t<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#41;</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d %d&quot;</span><span style="color: #339933;">,&amp;</span>M<span style="color: #339933;">,&amp;</span>N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>M <span style="color: #339933;">&lt;</span> 2<span style="color: #009900;">&#41;</span> M <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #993333;">int</span> cap <span style="color: #339933;">=</span> sqrt<span style="color: #009900;">&#40;</span>N<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
&nbsp;
        bool isprime<span style="color: #009900;">&#91;</span>100001<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        memset<span style="color: #009900;">&#40;</span>isprime<span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>isprime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
&nbsp;
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> numprimes<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #993333;">int</span> p <span style="color: #339933;">=</span> primes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>p <span style="color: #339933;">&gt;=</span> cap<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #993333;">int</span> start<span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>p <span style="color: #339933;">&gt;=</span> M<span style="color: #009900;">&#41;</span> start <span style="color: #339933;">=</span> p<span style="color: #339933;">*</span><span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">else</span> start <span style="color: #339933;">=</span> M <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>p <span style="color: #339933;">-</span> M <span style="color: #339933;">%</span> p<span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> p<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> j <span style="color: #339933;">=</span> start<span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;=</span> N<span style="color: #339933;">;</span> j <span style="color: #339933;">+=</span> p<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                isprime<span style="color: #009900;">&#91;</span>j <span style="color: #339933;">-</span> M<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #993333;">int</span> start <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>M <span style="color: #339933;">%</span> 2<span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span>M<span style="color: #339933;">:</span>M<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>    
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>M <span style="color: #339933;">==</span> 2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;2<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> start<span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> N<span style="color: #339933;">;</span> i<span style="color: #339933;">+=</span>2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>isprime<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">-</span>M<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>PHP</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4751"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code" id="p47code51"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #666666; font-style: italic;">// PRIME1 - PHP</span>
<span style="color: #666666; font-style: italic;">// AC Time: 5.73</span>
&nbsp;
<span style="color: #000088;">$primes</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$numprimes</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">32000</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">+=</span>2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$isprime</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$cap</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/sqrt"><span style="color: #990000;">sqrt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$numprimes</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$cap</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">%</span> <span style="color: #000088;">$primes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$isprime</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$isprime</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$primes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$numprimes</span><span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<a href="http://www.php.net/fscanf"><span style="color: #990000;">fscanf</span></a><span style="color: #009900;">&#40;</span>STDIN<span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%d</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$T</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$t</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$t</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$T</span><span style="color: #339933;">;</span> <span style="color: #000088;">$t</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$t</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <a href="http://www.php.net/fscanf"><span style="color: #990000;">fscanf</span></a><span style="color: #009900;">&#40;</span>STDIN<span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%d</span> <span style="color: #009933; font-weight: bold;">%d</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$M</span><span style="color: #339933;">,</span><span style="color: #000088;">$N</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$M</span> <span style="color: #339933;">&lt;</span> 2<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$M</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$isprime</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$cap</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/sqrt"><span style="color: #990000;">sqrt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$N</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$numprimes</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$p</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$primes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$cap</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$M</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$p</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$M</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$M</span> <span style="color: #339933;">%</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$start</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$N</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$isprime</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$j</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$M</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #339933;">++</span><span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$M</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$N</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$isprime</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">-</span><span style="color: #000088;">$M</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$i</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Python</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4752"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
</pre></td><td class="code" id="p47code52"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># PRIME1 - Python</span>
<span style="color: #808080; font-style: italic;"># AC Time: 3.10s</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">math</span> <span style="color: #ff7700;font-weight:bold;">import</span> sqrt
primes = <span style="color: black;">&#91;</span>2<span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>3,32000,2<span style="color: black;">&#41;</span>:
    isprime = <span style="color: #008000;">True</span>
&nbsp;
    cap = sqrt<span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>+1
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> primes:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>j <span style="color: #66cc66;">&gt;</span>= cap<span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">break</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>i <span style="color: #66cc66;">%</span> j == 0<span style="color: black;">&#41;</span>:
            isprime = <span style="color: #008000;">False</span>
            <span style="color: #ff7700;font-weight:bold;">break</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>isprime<span style="color: black;">&#41;</span>:
        primes.<span style="color: black;">append</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>
&nbsp;
T = <span style="color: #008000;">input</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
output = <span style="color: #483d8b;">&quot;&quot;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> t <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>T<span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>t <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
        output += <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
&nbsp;
    M,N = <span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">' '</span><span style="color: black;">&#41;</span>
    M = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>M<span style="color: black;">&#41;</span>
    N = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>N<span style="color: black;">&#41;</span>
    cap = sqrt<span style="color: black;">&#40;</span>N<span style="color: black;">&#41;</span>+1
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>M <span style="color: #66cc66;">&lt;</span> 2<span style="color: black;">&#41;</span>:
        M = 2
&nbsp;
    isprime = <span style="color: black;">&#91;</span><span style="color: #008000;">True</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span>100001
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> primes:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>i <span style="color: #66cc66;">&gt;</span>= cap<span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">break</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>i <span style="color: #66cc66;">&gt;</span>= M<span style="color: black;">&#41;</span>:
            start = i<span style="color: #66cc66;">*</span>2
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            start = M + <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>i - M <span style="color: #66cc66;">%</span> i<span style="color: black;">&#41;</span><span style="color: #66cc66;">%</span>i<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># The two below, obscure lines create a continuous</span>
        <span style="color: #808080; font-style: italic;">#  block of false elements in order to set all</span>
        <span style="color: #808080; font-style: italic;">#  elements correspnding to numbers divisible by i</span>
        <span style="color: #808080; font-style: italic;">#  in isprime to be false</span>
        <span style="color: #808080; font-style: italic;"># In turns out that this runs substantially faster</span>
        <span style="color: #808080; font-style: italic;">#  than setting the elements individually using loops</span>
        falseblock = <span style="color: black;">&#91;</span><span style="color: #008000;">False</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">*</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>isprime<span style="color: black;">&#91;</span>start-M:N+1-M:i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
        isprime<span style="color: black;">&#91;</span>start-M:N+1-M:i<span style="color: black;">&#93;</span> = falseblock
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>M,N+1<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>isprime<span style="color: black;">&#91;</span>i-M<span style="color: black;">&#93;</span> == <span style="color: #008000;">True</span><span style="color: black;">&#41;</span>:
            output += <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> output<span style="color: black;">&#91;</span>:-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span></pre></td></tr></table></div>

<p><strong>Bash</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4753"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code" id="p47code53"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># PRIME1 Incomplete - BASH</span>
<span style="color: #666666; font-style: italic;"># This is not a complete solution</span>
<span style="color: #666666; font-style: italic;"># The speed of bash parsing makes getting an </span>
<span style="color: #666666; font-style: italic;">#  AC submission infeasible</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># The following code is a working prime generator</span>
<span style="color: #666666; font-style: italic;"># Giving enough time, it will output all prime </span>
<span style="color: #666666; font-style: italic;">#  numbers from 0 to 32000</span>
<span style="color: #666666; font-style: italic;"># ..which is the first step in the solution to PRIME1</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">let</span> PRIMES<span style="color: #7a0874; font-weight: bold;">&#91;</span>0<span style="color: #7a0874; font-weight: bold;">&#93;</span>=2
<span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">NUMPRIMES</span>=1
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>3..32000<span style="color: #7a0874; font-weight: bold;">&#125;</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">ISPRIME</span>=<span style="color: #000000;">1</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">#for j in {0..$NUMPRIMES}; do</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span> j = <span style="color: #000000;">0</span>;j<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #007800;">$NUMPRIMES</span>;j++ <span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">do</span>
        <span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">CURPRIME</span>=<span style="color: #800000;">${PRIMES[$j]}</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$CURPRIME</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #007800;">$CURPRIME</span> <span style="color: #660033;">-gt</span> <span style="color: #007800;">$i</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
            <span style="color: #7a0874; font-weight: bold;">break</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
        <span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">MOD</span>=$<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #007800;">$i</span> <span style="color: #000000; font-weight: bold;">%</span> <span style="color: #007800;">$CURPRIME</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$MOD</span> <span style="color: #660033;">-eq</span> 0 <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
            <span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">ISPRIME</span>=0
            <span style="color: #7a0874; font-weight: bold;">break</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$ISPRIME</span> <span style="color: #660033;">-eq</span> 1 <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span>
        <span style="color: #7a0874; font-weight: bold;">let</span> PRIMES<span style="color: #7a0874; font-weight: bold;">&#91;</span>NUMPRIMES<span style="color: #7a0874; font-weight: bold;">&#93;</span>=<span style="color: #007800;">$i</span>
        <span style="color: #7a0874; font-weight: bold;">let</span> <span style="color: #007800;">NUMPRIMES</span>=NUMPRIMES+1
    <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p><strong>Ruby</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4754"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
</pre></td><td class="code" id="p47code54"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># PRIME1 - Ruby</span>
<span style="color:#008000; font-style:italic;"># AC Time: 4.65s</span>
<span style="color:#008000; font-style:italic;"># </span>
<span style="color:#008000; font-style:italic;"># While I would have liked to use more</span>
<span style="color:#008000; font-style:italic;"># Ruby idioms such as for p in primes</span>
<span style="color:#008000; font-style:italic;"># or primes.each do |p| (used once),</span>
<span style="color:#008000; font-style:italic;"># the difference in runtime between</span>
<span style="color:#008000; font-style:italic;"># the use of loops like these and while</span>
<span style="color:#008000; font-style:italic;"># loops was non-negligible</span>
&nbsp;
primes = <span style="color:#006600; font-weight:bold;">&#91;</span>2<span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
3.<span style="color:#9900CC;">step</span><span style="color:#006600; font-weight:bold;">&#40;</span>32000,2<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
    isprime = <span style="color:#0000FF; font-weight:bold;">true</span>
    cap = <span style="color:#CC00FF; font-weight:bold;">Math</span>.<span style="color:#9900CC;">sqrt</span><span style="color:#006600; font-weight:bold;">&#40;</span>i<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> 1
&nbsp;
    primes.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#006600; font-weight:bold;">&gt;</span>= cap<span style="color:#006600; font-weight:bold;">&#41;</span>
            <span style="color:#9966CC; font-weight:bold;">break</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>i <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#CC0066; font-weight:bold;">p</span> == 0<span style="color:#006600; font-weight:bold;">&#41;</span>
            isprime = <span style="color:#0000FF; font-weight:bold;">false</span>
            <span style="color:#9966CC; font-weight:bold;">break</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> isprime
        primes <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> i
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
numprimes = primes.<span style="color:#9900CC;">length</span>
&nbsp;
T = <span style="color:#CC0066; font-weight:bold;">gets</span>.<span style="color:#9900CC;">to_i</span>
&nbsp;
&nbsp;
output = <span style="color:#996600;">&quot;&quot;</span>
t = 0
<span style="color:#9966CC; font-weight:bold;">while</span> t <span style="color:#006600; font-weight:bold;">&lt;</span> T
    <span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> t <span style="color:#006600; font-weight:bold;">&gt;</span> 0
    line = <span style="color:#CC0066; font-weight:bold;">gets</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot; &quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    m = line<span style="color:#006600; font-weight:bold;">&#91;</span>0<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_i</span>
    n = line<span style="color:#006600; font-weight:bold;">&#91;</span>1<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_i</span>
&nbsp;
    m = 2 <span style="color:#9966CC; font-weight:bold;">if</span> m <span style="color:#006600; font-weight:bold;">&lt;</span> 2
&nbsp;
    cap = <span style="color:#CC00FF; font-weight:bold;">Math</span>.<span style="color:#9900CC;">sqrt</span><span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> 1
&nbsp;
    notprime = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
    i = 0
    <span style="color:#9966CC; font-weight:bold;">while</span> i <span style="color:#006600; font-weight:bold;">&lt;</span> numprimes
        <span style="color:#CC0066; font-weight:bold;">p</span> = primes<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span>
        i<span style="color:#006600; font-weight:bold;">+</span>=1
        <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#006600; font-weight:bold;">&gt;</span>= cap<span style="color:#006600; font-weight:bold;">&#41;</span>
            <span style="color:#9966CC; font-weight:bold;">break</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#006600; font-weight:bold;">&gt;</span>= m<span style="color:#006600; font-weight:bold;">&#41;</span>
            start = <span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">*</span>2
        <span style="color:#9966CC; font-weight:bold;">else</span> 
            start = m <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#006600; font-weight:bold;">-</span> m <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">%</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        j = start
        <span style="color:#9966CC; font-weight:bold;">while</span> j <span style="color:#006600; font-weight:bold;">&lt;</span>= n
            notprime<span style="color:#006600; font-weight:bold;">&#91;</span>j<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
            j <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#CC0066; font-weight:bold;">p</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    i = m
    <span style="color:#9966CC; font-weight:bold;">while</span> <span style="color:#006600; font-weight:bold;">&#40;</span>i <span style="color:#006600; font-weight:bold;">&lt;</span>= n<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>notprime<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
            <span style="color:#CC0066; font-weight:bold;">print</span> i,<span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
        i<span style="color:#006600; font-weight:bold;">+</span>=1
    <span style="color:#9966CC; font-weight:bold;">end</span>
    t <span style="color:#006600; font-weight:bold;">+</span>= 1
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p><strong>Java</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4755"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
</pre></td><td class="code" id="p47code55"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// PRIME1 - Java</span>
<span style="color: #666666; font-style: italic;">// AC Time: 2.20</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.Math.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Scanner in <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Scanner<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">in</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> primes <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span>4000<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> numprimes <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
        primes<span style="color: #009900;">&#91;</span>numprimes<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">32000</span><span style="color: #339933;">;</span> i<span style="color: #339933;">+=</span>2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">boolean</span> isprime <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">double</span> cap <span style="color: #339933;">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">sqrt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1.0</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> numprimes<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">&gt;=</span> cap<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">%</span> primes<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    isprime <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                    <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isprime<span style="color: #009900;">&#41;</span> primes<span style="color: #009900;">&#91;</span>numprimes<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
        <span style="color: #000066; font-weight: bold;">int</span> T,N,M<span style="color: #339933;">;</span>
&nbsp;
        T <span style="color: #339933;">=</span> in.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> t <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> t <span style="color: #339933;">&lt;</span> T<span style="color: #339933;">;</span> t<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>t <span style="color: #339933;">&gt;</span> 0<span style="color: #009900;">&#41;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            M <span style="color: #339933;">=</span> in.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            N <span style="color: #339933;">=</span> in.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>M <span style="color: #339933;">&lt;</span> 2<span style="color: #009900;">&#41;</span> M <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">boolean</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> isprime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">boolean</span><span style="color: #009900;">&#91;</span>100001<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">100001</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                isprime<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> numprimes<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">int</span> p <span style="color: #339933;">=</span> primes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">int</span> start<span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>p <span style="color: #339933;">&gt;=</span> M<span style="color: #009900;">&#41;</span> start <span style="color: #339933;">=</span> p<span style="color: #339933;">*</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">else</span> start <span style="color: #339933;">=</span> M <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>p <span style="color: #339933;">-</span> M <span style="color: #339933;">%</span> p<span style="color: #009900;">&#41;</span><span style="color: #339933;">%</span>p<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> start<span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;=</span> N<span style="color: #339933;">;</span> j <span style="color: #339933;">+=</span> p<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    isprime<span style="color: #009900;">&#91;</span>j <span style="color: #339933;">-</span> M<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> M<span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isprime<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">-</span>M<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Perl</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4756"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
</pre></td><td class="code" id="p47code56"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># PRIME1 - Perl</span>
<span style="color: #666666; font-style: italic;"># AC Time: 4.28s</span>
<span style="color: #0000ff;">@primes</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">32000</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">+=</span>2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$isprime</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$cap</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/sqrt.html"><span style="color: #000066;">sqrt</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #0000ff;">$p</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@primes</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p</span> <span style="color: #339933;">&gt;=</span> <span style="color: #0000ff;">$cap</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
            <span style="color: #b1b100;">last</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span> <span style="color: #339933;">%</span> <span style="color: #0000ff;">$p</span> <span style="color: #339933;">==</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #0000ff;">$isprime</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">last</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$isprime</span> <span style="color: #339933;">==</span> 1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <a href="http://perldoc.perl.org/functions/push.html"><span style="color: #000066;">push</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@primes</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #0000ff;">$T</span> <span style="color: #339933;">=</span> <span style="color: #009999;">&lt;STDIN&gt;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$t</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$t</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000ff;">$T</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$t</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$t</span> <span style="color: #339933;">&gt;</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">$in</span> <span style="color: #339933;">=</span> <span style="color: #009999;">&lt;STDIN&gt;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">@line</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/split.html"><span style="color: #000066;">split</span></a><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/ /</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$in</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$M</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$line</span><span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$N</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$line</span><span style="color: #009900;">&#91;</span>1<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$M</span> <span style="color: #339933;">&lt;</span> 2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$M</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">$cap</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/sqrt.html"><span style="color: #000066;">sqrt</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$N</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #0000ff;">@isprime</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>1<span style="color: #009900;">&#41;</span> x 100001<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">foreach</span> <span style="color: #0000ff;">$p</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@primes</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p</span> <span style="color: #339933;">&gt;=</span> <span style="color: #0000ff;">$cap</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">last</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p</span> <span style="color: #339933;">&gt;=</span> <span style="color: #0000ff;">$M</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #0000ff;">$start</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$p</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #0000ff;">$start</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$M</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$M</span> <span style="color: #339933;">%</span> <span style="color: #0000ff;">$p</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #0000ff;">$p</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$j</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$start</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$j</span> <span style="color: #339933;">&lt;=</span> <span style="color: #0000ff;">$N</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$j</span> <span style="color: #339933;">+=</span> <span style="color: #0000ff;">$p</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #0000ff;">$isprime</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$j</span><span style="color: #339933;">-</span><span style="color: #0000ff;">$M</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$i</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$M</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">&lt;=</span> <span style="color: #0000ff;">$N</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$isprime</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">-</span><span style="color: #0000ff;">$M</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> 1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;$i<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>C#</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4757"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
</pre></td><td class="code" id="p47code57"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// PRIME1 - C# (gmcs)</span>
<span style="color: #008080; font-style: italic;">// AC Time: 1.50s</span>
&nbsp;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">class</span> PRIME1 <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
        <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> primes <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span>4000<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
        <span style="color: #FF0000;">int</span> numprimes <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
        primes<span style="color: #000000;">&#91;</span>numprimes<span style="color: #008000;">++</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;=</span> <span style="color: #FF0000;">32000</span><span style="color: #008000;">;</span> i<span style="color: #008000;">+=</span>2<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">bool</span> isprime <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
            <span style="color: #FF0000;">double</span> cap <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Sqrt</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">1.0</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;</span> numprimes<span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>j <span style="color: #008000;">&gt;=</span> cap<span style="color: #000000;">&#41;</span> break<span style="color: #008000;">;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>i <span style="color: #008000;">%</span> primes<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span> <span style="color: #008000;">==</span> 0<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                    isprime <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
                    break<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>isprime<span style="color: #000000;">&#41;</span> primes<span style="color: #000000;">&#91;</span>numprimes<span style="color: #008000;">++</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> i<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #FF0000;">int</span> T,N,M<span style="color: #008000;">;</span>
        T <span style="color: #008000;">=</span> <span style="color: #FF0000;">int</span>.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span>Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> t <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> t <span style="color: #008000;">&lt;</span> T<span style="color: #008000;">;</span> t<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>t <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> parts <span style="color: #008000;">=</span> Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">' '</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            M <span style="color: #008000;">=</span> <span style="color: #FF0000;">int</span>.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span>parts<span style="color: #000000;">&#91;</span>0<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            N <span style="color: #008000;">=</span> <span style="color: #FF0000;">int</span>.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span>parts<span style="color: #000000;">&#91;</span>1<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>M <span style="color: #008000;">&lt;</span> 2<span style="color: #000000;">&#41;</span> M <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">double</span> cap <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Sqrt</span><span style="color: #000000;">&#40;</span>N<span style="color: #000000;">&#41;</span><span style="color: #008000;">+</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #FF0000;">bool</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> isprime <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #FF0000;">bool</span><span style="color: #000000;">&#91;</span>100001<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">100001</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span> isprime<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> numprimes<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">int</span> p <span style="color: #008000;">=</span> primes<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>p <span style="color: #008000;">&gt;=</span> cap<span style="color: #000000;">&#41;</span> break<span style="color: #008000;">;</span>
                <span style="color: #FF0000;">int</span> start<span style="color: #008000;">;</span>
&nbsp;
&nbsp;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>p <span style="color: #008000;">&gt;=</span> M<span style="color: #000000;">&#41;</span> start <span style="color: #008000;">=</span> p<span style="color: #008000;">*</span><span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">else</span> start <span style="color: #008000;">=</span> M <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>p <span style="color: #008000;">-</span> M <span style="color: #008000;">%</span> p<span style="color: #000000;">&#41;</span><span style="color: #008000;">%</span>p<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> j <span style="color: #008000;">=</span> start<span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;=</span> N<span style="color: #008000;">;</span> j<span style="color: #008000;">+=</span> p<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                    isprime<span style="color: #000000;">&#91;</span>j <span style="color: #008000;">-</span> M<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> M<span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;=</span> N<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>isprime<span style="color: #000000;">&#91;</span>i<span style="color: #008000;">-</span>M<span style="color: #000000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span> Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>GNU Pascal</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4758"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
</pre></td><td class="code" id="p47code58"><pre class="pascal" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">{ 
    PRIME1 - GNU Pascal
    AC Time: 0.54
}</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">program</span> PRIME1;
<span style="color: #000000; font-weight: bold;">var</span>
    primes<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">array</span><span style="color: #009900;">&#91;</span>1..4000<span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">of</span> <span style="color: #000066; font-weight: bold;">integer</span>;
    numprimes<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">integer</span>;
    i<span style="color: #339933;">,</span>j<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">integer</span>;
    cap<span style="color: #339933;">:</span> double;
    isprime<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">boolean</span>;
    T<span style="color: #339933;">,</span>N<span style="color: #339933;">,</span>M<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">integer</span>;
    isp<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">array</span><span style="color: #009900;">&#91;</span>0..100001<span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">of</span> <span style="color: #000066; font-weight: bold;">boolean</span>;
    p<span style="color: #339933;">,</span>start<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">integer</span>;
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">begin</span>
    primes<span style="color: #009900;">&#91;</span>1<span style="color: #009900;">&#93;</span> <span style="color: #339933;">:=</span> <span style="color: #cc66cc;">2</span>;
    numprimes <span style="color: #339933;">:=</span> <span style="color: #cc66cc;">1</span>;
    <span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #339933;">:=</span> 3 <span style="color: #000000; font-weight: bold;">to</span> 32000 <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #000000; font-weight: bold;">begin</span>
        isprime <span style="color: #339933;">:=</span> <span style="color: #000000; font-weight: bold;">true</span>;
        cap <span style="color: #339933;">:=</span> <span style="color: #000066;">sqrt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>;
        <span style="color: #000000; font-weight: bold;">for</span> j <span style="color: #339933;">:=</span> 1 <span style="color: #000000; font-weight: bold;">to</span> numprimes <span style="color: #000000; font-weight: bold;">do</span>
        <span style="color: #000000; font-weight: bold;">begin</span>
            <span style="color: #000000; font-weight: bold;">if</span> primes<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> &gt;<span style="color: #339933;">=</span> cap <span style="color: #000000; font-weight: bold;">then</span> 
                    <span style="color: #000000; font-weight: bold;">break</span>;
&nbsp;
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #000000; font-weight: bold;">MOD</span> primes<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">then</span>
            <span style="color: #000000; font-weight: bold;">begin</span>
                isprime <span style="color: #339933;">:=</span> <span style="color: #000000; font-weight: bold;">false</span>;
                <span style="color: #000000; font-weight: bold;">break</span>;
            <span style="color: #000000; font-weight: bold;">end</span>
        <span style="color: #000000; font-weight: bold;">end</span>;
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> isprime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #000000; font-weight: bold;">then</span> 
        <span style="color: #000000; font-weight: bold;">begin</span>
            numprimes <span style="color: #339933;">:=</span> numprimes <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>;
            primes<span style="color: #009900;">&#91;</span>numprimes<span style="color: #009900;">&#93;</span> <span style="color: #339933;">:=</span> i
        <span style="color: #000000; font-weight: bold;">end</span>
    <span style="color: #000000; font-weight: bold;">end</span>;
&nbsp;
    <span style="color: #000066;">read</span><span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">for</span> t <span style="color: #339933;">:=</span> 1 <span style="color: #000000; font-weight: bold;">to</span> T <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #000000; font-weight: bold;">begin</span>
        <span style="color: #000000; font-weight: bold;">if</span> t &gt; 0 <span style="color: #000000; font-weight: bold;">then</span>
            <span style="color: #000066;">writeln</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #009900;">&#41;</span>;
&nbsp;
        <span style="color: #000066;">read</span><span style="color: #009900;">&#40;</span>M<span style="color: #009900;">&#41;</span>;
        <span style="color: #000066;">read</span><span style="color: #009900;">&#40;</span>N<span style="color: #009900;">&#41;</span>;
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> M &lt; 2 <span style="color: #000000; font-weight: bold;">then</span>
            M <span style="color: #339933;">:=</span> <span style="color: #cc66cc;">2</span>;
&nbsp;
        cap <span style="color: #339933;">:=</span> <span style="color: #000066;">sqrt</span><span style="color: #009900;">&#40;</span>N<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>;
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #339933;">:=</span> 0 <span style="color: #000000; font-weight: bold;">to</span> 100001 <span style="color: #000000; font-weight: bold;">do</span>
            isp<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">:=</span> <span style="color: #000000; font-weight: bold;">true</span>;
&nbsp;
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #339933;">:=</span> 1 <span style="color: #000000; font-weight: bold;">to</span> numprimes <span style="color: #000000; font-weight: bold;">do</span>
        <span style="color: #000000; font-weight: bold;">begin</span>
            p <span style="color: #339933;">:=</span> primes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>;            
            <span style="color: #000000; font-weight: bold;">if</span> p &gt;<span style="color: #339933;">=</span> cap <span style="color: #000000; font-weight: bold;">then</span> 
                <span style="color: #000000; font-weight: bold;">break</span>;
&nbsp;
            <span style="color: #000000; font-weight: bold;">if</span> p &gt;<span style="color: #339933;">=</span> M <span style="color: #000000; font-weight: bold;">then</span>
                start <span style="color: #339933;">:=</span> <span style="color: #009900;">&#40;</span>p <span style="color: #339933;">*</span> 2<span style="color: #009900;">&#41;</span>;
&nbsp;
            <span style="color: #000000; font-weight: bold;">if</span> p &lt; M <span style="color: #000000; font-weight: bold;">then</span>
                start <span style="color: #339933;">:=</span> M <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>p <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>M <span style="color: #000000; font-weight: bold;">MOD</span> p<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">MOD</span> p<span style="color: #009900;">&#41;</span>;
&nbsp;
            j <span style="color: #339933;">:=</span> start;
&nbsp;
            <span style="color: #000000; font-weight: bold;">while</span> j &lt;<span style="color: #339933;">=</span> N <span style="color: #000000; font-weight: bold;">do</span>
            <span style="color: #000000; font-weight: bold;">begin</span>
                isp<span style="color: #009900;">&#91;</span>j <span style="color: #339933;">-</span> M<span style="color: #009900;">&#93;</span> <span style="color: #339933;">:=</span> <span style="color: #000000; font-weight: bold;">false</span>;
                j <span style="color: #339933;">:=</span> j <span style="color: #339933;">+</span> p;
            <span style="color: #000000; font-weight: bold;">end</span>;
        <span style="color: #000000; font-weight: bold;">end</span>;
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #339933;">:=</span> M <span style="color: #000000; font-weight: bold;">to</span> N <span style="color: #000000; font-weight: bold;">do</span>
        <span style="color: #000000; font-weight: bold;">begin</span>
            <span style="color: #000000; font-weight: bold;">if</span> isp<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">-</span>M<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #000066;">writeln</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>;
        <span style="color: #000000; font-weight: bold;">end</span>;
    <span style="color: #000000; font-weight: bold;">end</span>;
<span style="color: #000000; font-weight: bold;">end</span>.</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://jamie-wong.com/2009/11/12/spoj-problem-2-prime-generator/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>SPOJ Problem 1</title>
		<link>http://jamie-wong.com/2009/11/08/spoj-problem-1/</link>
		<comments>http://jamie-wong.com/2009/11/08/spoj-problem-1/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 17:55:10 +0000</pubDate>
		<dc:creator>Jamie Wong</dc:creator>
				<category><![CDATA[SPOJ]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[c99]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[pascal]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[prime]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://jamie-wong.com/me/?p=41</guid>
		<description><![CDATA[When I saw that Thor had done solutions to the first few problems of Project Euler in many many languages, I thought "Hey! That's a good idea!" but didn't want to copy exactly.
So instead I'm doing solutions to some problems on SPOJ.
SPOJ is an online judge system full of algorithmic problems. This is one of [...]]]></description>
			<content:encoded><![CDATA[<p>When I saw that Thor had done solutions to the first few problems of Project Euler in many many languages, I thought "Hey! That's a good idea!" but didn't want to copy exactly.</p>
<p>So instead I'm doing solutions to some problems on SPOJ.<br />
SPOJ is an online judge system full of algorithmic problems. This is one of the things that <a href="http://www.topcoder.com/tc?module=MemberProfile&#038;cr=22664055&#038;tab=hs">Hanson Wang</a> did to get as good as he is. The solutions I'll be providing here are going to be very simple problems, so don't expect any magic. The beautiful thing about SPOJ is the sheer number of languages it will judge. I figured this was the perfect playground to make sure my code worked in all the languages I try.</p>
<p>Problem: <a href="http://www.spoj.pl/problems/TEST/"> Life, the Universe, and Everything</a></p>
<blockquote><p>
Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.
</p></blockquote>
<p>Solutions - In order of frequency that I use the language</p>
<p><strong>C++</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4169"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p41code69"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">//TEST AC - CPP (g++)</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> n<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span>1<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> n<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>n <span style="color: #000080;">==</span> 42<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><strong>C99</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4170"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p41code70"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//TEST AC - (gcc C99)</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> n<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,&amp;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">==</span> 42<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>php</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4171"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p41code71"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #666666; font-style: italic;">//TEST AC - PHP</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<a href="http://www.php.net/fscanf"><span style="color: #990000;">fscanf</span></a><span style="color: #009900;">&#40;</span>STDIN<span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%d</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$n</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span> <span style="color: #339933;">==</span> 42<span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Python</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4172"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p41code72"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#TEST AC - Python</span>
<span style="color: #ff7700;font-weight:bold;">while</span> 1:
	num = <span style="color: #008000;">input</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>num == 42<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">break</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> num</pre></td></tr></table></div>

<p><strong>Bash</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4173"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p41code73"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># TEST AC - BASH</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">true</span>; <span style="color: #000000; font-weight: bold;">do</span>
	<span style="color: #c20cb9; font-weight: bold;">read</span> n
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$n</span> <span style="color: #660033;">-eq</span> 42 <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
		<span style="color: #7a0874; font-weight: bold;">break</span>
	<span style="color: #000000; font-weight: bold;">fi</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$n</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p><strong>Ruby</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4174"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p41code74"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#TEST AC - Ruby</span>
<span style="color:#9966CC; font-weight:bold;">while</span> 1
	n = <span style="color:#CC0066; font-weight:bold;">gets</span>.<span style="color:#9900CC;">to_i</span>
	<span style="color:#9966CC; font-weight:bold;">if</span> n == 42
		<span style="color:#9966CC; font-weight:bold;">break</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
	<span style="color:#CC0066; font-weight:bold;">puts</span> n
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p><strong>Java</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4175"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p41code75"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//TEST AC - Java</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Scanner in <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Scanner<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">in</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">int</span> n <span style="color: #339933;">=</span> in.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">==</span> 42<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Perl</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4176"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p41code76"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#TEST AC - Perl</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>1<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #0000ff;">$n</span> <span style="color: #339933;">=</span> <span style="color: #009999;">&lt;STDIN&gt;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$n</span> <span style="color: #339933;">==</span> 42<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">last</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$n</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>C#</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4177"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p41code77"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">//TEST AC - C# (gmcs + Mono)</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">class</span> WelcomeCSS <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">while</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #FF0000;">int</span> n<span style="color: #008000;">;</span>
			n <span style="color: #008000;">=</span> <span style="color: #FF0000;">int</span>.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span>Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>n <span style="color: #008000;">==</span> 42<span style="color: #000000;">&#41;</span> break<span style="color: #008000;">;</span>
			Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>n<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>GNU Pascal</strong></p>

<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4178"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p41code78"><pre class="pascal" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">{TEST AC - GPC Pascal}</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">program</span> TEST;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> n<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">integer</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">begin</span>
	<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #000000; font-weight: bold;">do</span>
		<span style="color: #000000; font-weight: bold;">begin</span>
			<span style="color: #000066;">readln</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">if</span> n <span style="color: #339933;">=</span> 42 <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #000000; font-weight: bold;">begin</span>
				<span style="color: #000000; font-weight: bold;">break</span>;
			<span style="color: #000000; font-weight: bold;">end</span>;
			<span style="color: #000066;">writeln</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">end</span>;
<span style="color: #000000; font-weight: bold;">end</span>.</pre></td></tr></table></div>

<p>You can see Thor's Project Euler solutions here: <a href="http://www.thurn.ca/category/project-euler/">Derek Thurn's Website - Project Euler</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jamie-wong.com/2009/11/08/spoj-problem-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
