<?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 &#187; python</title>
	<atom:link href="http://jamie-wong.com/tag/python/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>Fri, 16 Jul 2010 15:57:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<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="p10116"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p101code16"><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="p10117"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p101code17"><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) &lt; '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>
    L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
    cmp(x, y) -> -1, 0, 1
</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="p10118"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p101code18"><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="p10119"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p101code19"><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="p10120"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p101code20"><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>
    map(function, sequence[, sequence, ...]) -> list
    
    Return a list of the results of applying the function to the items of
    the argument sequence(s).  If more than one sequence is given, the
    function is called with an argument list consisting of the corresponding
    item of each sequence, substituting None for missing values when not all
    sequences have the same length.  If the function is None, return a list of
    the items of the sequence (or a list of tuples if more than one sequence).
</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="p10121"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p101code21"><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="p10122"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p101code22"><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="p10123"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p101code23"><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="p10124"><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="p101code24"><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="p10125"><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="p101code25"><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>
    filter(function or None, sequence) -> list, tuple, or string
    
    Return those items of sequence for which function(item) is true.  If
    function is None, return the items that are true.  If sequence is a tuple
    or string, return the same type, else return a list.
</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="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>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="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: #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="p10128"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p101code28"><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>
    reduce(function, sequence[, initial]) -> value
    
    Apply a function of two arguments cumulatively to the items of a sequence,
    from left to right, so as to reduce the sequence to a single value.
    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
    ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
    of the sequence in the calculation, and serves as a default when the
    sequence is empty.
</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="p10129"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p101code29"><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="p10130"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p101code30"><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 [...]]]></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:
<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>.
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>
   1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
   2. Any live cell with more than three live neighbours dies, as if by overcrowding.
   3. Any live cell with two or three live neighbours lives on to the next generation.
   4. Any dead cell with exactly three live neighbours becomes a live cell.
</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>.
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>
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>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 [...]]]></description>
			<content:encoded><![CDATA[<p>Problem: <a href='https://www.spoj.pl/problems/PRIME1/'>Prime Generator</a></p>

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

<p><strong>Concept</strong>
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="p4741"><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="p47code41"><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>
<strong>C99</strong></p>


<div class="wp_codebox"><table style='width:99%;margin:0px;border:0;' ><tr id="p4742"><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="p47code42"><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="p4743"><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="p47code43"><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="p4744"><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="p47code44"><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="p4745"><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="p47code45"><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="p4746"><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="p47code46"><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="p4747"><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="p47code47"><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="p4748"><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="p47code48"><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="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
</pre></td><td class="code" id="p47code49"><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="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
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="p47code50"><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 [...]]]></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.
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>
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.
</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="p4161"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p41code61"><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="p4162"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p41code62"><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="p4163"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p41code63"><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="p4164"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p41code64"><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="p4165"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p41code65"><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="p4166"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p41code66"><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="p4167"><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="p41code67"><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="p4168"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p41code68"><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="p4169"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p41code69"><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="p4170"><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="p41code70"><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>
