<?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>AquilaX's development blog &#187; Math</title>
	<atom:link href="http://dev.horemag.net/category/math/feed/" rel="self" type="application/rss+xml" />
	<link>http://dev.horemag.net</link>
	<description>code and so on...</description>
	<lastBuildDate>Thu, 02 Feb 2012 08:29:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Perlin noise generator in PHP</title>
		<link>http://dev.horemag.net/2008/03/02/perlin-noise-generator-in-php/</link>
		<comments>http://dev.horemag.net/2008/03/02/perlin-noise-generator-in-php/#comments</comments>
		<pubDate>Sun, 02 Mar 2008 10:56:17 +0000</pubDate>
		<dc:creator>AquilaX</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[perlin noise]]></category>
		<category><![CDATA[SVG]]></category>
		<category><![CDATA[terrain]]></category>

		<guid isPermaLink="false">http://dev.horemag.net/2008/03/02/perlin-noise-generator-in-php/</guid>
		<description><![CDATA[Generating Perlin noise requires heavy calculation and PHP (in my opinion) is not the best language for this task but still it can be useful.
The algorithm is an adaptation form this pseudo code.
The class seems to work but I'm not if it works 100% correctly. The tricky part was to deal with the types because [...]]]></description>
			<content:encoded><![CDATA[<p><img src='http://dev.horemag.net/wp-content/uploads/noise.gif' alt='Perlin noise' style="float:left; margin-right:5px;"/>Generating <a href="http://en.wikipedia.org/wiki/Perlin_noise" onclick="javascript:urchinTracker('/outbound/article/http://en.wikipedia.org/wiki/Perlin_noise');">Perlin noise</a> requires heavy calculation and PHP (in my opinion) is not the best language for this task but still it can be useful.<br />
The algorithm is an adaptation form <a href="http://freespace.virgin.net/hugo.elias/models/m_perlin.htm" onclick="javascript:urchinTracker('/outbound/article/http://freespace.virgin.net/hugo.elias/models/m_perlin.htm');">this</a> pseudo code.<br />
The class seems to work but I'm not if it works 100% correctly. The tricky part was to deal with the types because PHP tends to convert large integers to float, and the algorithm uses bitwise operators on some stages. </p>
<p>This class is just for testing purposes it consumes a lot of CPU cycles and I hardly recommend to avoid using it in live environment. Try to keep the cycles low as well as the octaves number close to 1. </p>
<p>Sample SVG result: <a class="html" href='http://dev.horemag.net/wp-content/uploads/noise.svg' title='noise.svg'>noise.svg</a>.<br />
Example usage (generate SVG map of terrain): </p>
<pre class="php">&nbsp;
  <span style="color: #b1b100;">require</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;class.perlin.php&quot;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #0000ff;">$per</span> = <span style="color: #000000; font-weight: bold;">new</span> Perlin;
&nbsp;
  <span style="color: #0000ff;">$xl</span> = <span style="color: #cc66cc;">100</span>;
  <span style="color: #0000ff;">$yl</span> = <span style="color: #cc66cc;">40</span>;
&nbsp;
  <a href="http://www.php.net/header" onclick="javascript:urchinTracker('/outbound/article/http://www.php.net/header');"><span style="color: #000066;">header</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Content-type: image/svg+xml&quot;</span><span style="color: #66cc66;">&#41;</span>;
  <a href="http://www.php.net/echo" onclick="javascript:urchinTracker('/outbound/article/http://www.php.net/echo');"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;?xml version=&quot;1.0&quot; standalone=&quot;no&quot;?&gt;'</span>;
  <a href="http://www.php.net/echo" onclick="javascript:urchinTracker('/outbound/article/http://www.php.net/echo');"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;!DOCTYPE svg PUBLIC &quot;-//W3C//DTD SVG 1.1//EN&quot;
  &quot;http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd&quot;&gt;'</span>;
  <a href="http://www.php.net/echo" onclick="javascript:urchinTracker('/outbound/article/http://www.php.net/echo');"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;svg width=&quot;400&quot; height=&quot;400&quot;
  xmlns=&quot;http://www.w3.org/2000/svg&quot; version=&quot;1.1&quot;&gt;'</span>;
  <span style="color: #0000ff;">$size</span> = <span style="color: #cc66cc;">3</span>;
  <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$x</span> = <span style="color: #cc66cc;">0</span>; <span style="color: #0000ff;">$x</span> &lt; <span style="color: #0000ff;">$xl</span>; <span style="color: #0000ff;">$x</span>++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$y</span> = <span style="color: #cc66cc;">0</span>; <span style="color: #0000ff;">$y</span> &lt; <span style="color: #0000ff;">$yl</span>; <span style="color: #0000ff;">$y</span>++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
      <span style="color: #0000ff;">$n</span> = <span style="color: #0000ff;">$per</span>-&gt;<span style="color: #006600;">perlinNoise2d</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$x</span>, <span style="color: #0000ff;">$y</span><span style="color: #66cc66;">&#41;</span>*<span style="color: #cc66cc;">255</span>;
      <span style="color: #0000ff;">$c</span> = <span style="color: #ff0000;">'#986121'</span>;
      <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$n</span> &lt; <span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$c</span> = <span style="color: #ff0000;">'#0000ff'</span>;
      <span style="color: #66cc66;">&#125;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$n</span> &gt; <span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$c</span> = <span style="color: #ff0000;">'#FFFFFF'</span>;
      <span style="color: #66cc66;">&#125;</span>
      <a href="http://www.php.net/echo" onclick="javascript:urchinTracker('/outbound/article/http://www.php.net/echo');"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;&lt;rect x=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>.<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$x</span>*<span style="color: #0000ff;">$size</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span> y=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>.<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$y</span>*<span style="color: #0000ff;">$size</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>
            width=<span style="color: #000099; font-weight: bold;">\&quot;</span>$size<span style="color: #000099; font-weight: bold;">\&quot;</span> height=<span style="color: #000099; font-weight: bold;">\&quot;</span>$size<span style="color: #000099; font-weight: bold;">\&quot;</span> fill=<span style="color: #000099; font-weight: bold;">\&quot;</span>$c<span style="color: #000099; font-weight: bold;">\&quot;</span>/&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
  <a href="http://www.php.net/echo" onclick="javascript:urchinTracker('/outbound/article/http://www.php.net/echo');"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">'&lt;/svg&gt;'</span>;
&nbsp;</pre>
<p>Download: <a class="zip" href='http://dev.horemag.net/wp-content/uploads/classperlinphp.zip' title='classperlinphp.zip'>classperlinphp.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://dev.horemag.net/2008/03/02/perlin-noise-generator-in-php/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Point in triangle</title>
		<link>http://dev.horemag.net/2007/11/10/point-in-triangle/</link>
		<comments>http://dev.horemag.net/2007/11/10/point-in-triangle/#comments</comments>
		<pubDate>Sat, 10 Nov 2007 19:55:03 +0000</pubDate>
		<dc:creator>AquilaX</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[point]]></category>
		<category><![CDATA[triangle]]></category>

		<guid isPermaLink="false">http://dev.horemag.net/2007/11/10/point-in-triangle/</guid>
		<description><![CDATA[A function in PHP to check if a point is in triangle. 
&#160;
&#160;
  function fAB&#40;$t, $x, $y&#41; &#123;
    return &#40;$y - $t&#91;'y1'&#93;&#41;*&#40;$t&#91;'x2'&#93; - $t&#91;'x1'&#93;&#41;
      -&#40;$x - $t&#91;'x1'&#93;&#41;*&#40;$t&#91;'y2'&#93; - $t&#91;'y1'&#93;&#41;;
  &#125;
&#160;
  function fBC&#40;$t, $x, $y&#41; &#123;
    return &#40;$y-$t&#91;'y2'&#93;&#41;*&#40;$t&#91;'x3'&#93;-$t&#91;'x2'&#93;&#41;
     [...]]]></description>
			<content:encoded><![CDATA[<p>A function in <a href="http://php.net" onclick="javascript:urchinTracker('/outbound/article/http://php.net');">PHP</a> to check if a point is in triangle. </p>
<pre class="php">&nbsp;
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> fAB<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$t</span>, <span style="color: #0000ff;">$x</span>, <span style="color: #0000ff;">$y</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$y</span> - <span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'y1'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>*<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'x2'</span><span style="color: #66cc66;">&#93;</span> - <span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'x1'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
      -<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$x</span> - <span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'x1'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>*<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'y2'</span><span style="color: #66cc66;">&#93;</span> - <span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'y1'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> fBC<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$t</span>, <span style="color: #0000ff;">$x</span>, <span style="color: #0000ff;">$y</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$y</span>-<span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'y2'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>*<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'x3'</span><span style="color: #66cc66;">&#93;</span>-<span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'x2'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
      -<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$x</span>-<span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'x2'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>*<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'y3'</span><span style="color: #66cc66;">&#93;</span>-<span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'y2'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> fCA<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$t</span>, <span style="color: #0000ff;">$x</span>, <span style="color: #0000ff;">$y</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$y</span>-<span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'y3'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>*<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'x1'</span><span style="color: #66cc66;">&#93;</span>-<span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'x3'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
      -<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$x</span>-<span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'x3'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>*<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'y1'</span><span style="color: #66cc66;">&#93;</span>-<span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'y3'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> CheckPointInside<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$t</span>, <span style="color: #0000ff;">$x</span>, <span style="color: #0000ff;">$y</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>fAB<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$t</span>, <span style="color: #0000ff;">$x</span>, <span style="color: #0000ff;">$y</span><span style="color: #66cc66;">&#41;</span>*fBC<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$t</span>, <span style="color: #0000ff;">$x</span>, <span style="color: #0000ff;">$y</span><span style="color: #66cc66;">&#41;</span>&gt;<span style="color: #cc66cc;">0</span>
      &amp;&amp; fBC<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$t</span>, <span style="color: #0000ff;">$x</span>, <span style="color: #0000ff;">$y</span><span style="color: #66cc66;">&#41;</span>*fCA<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$t</span>, <span style="color: #0000ff;">$x</span>, <span style="color: #0000ff;">$y</span><span style="color: #66cc66;">&#41;</span>&gt;<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
    <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Function <strong>CheckPointInside</strong> expects three parameters: <strong>$t</strong> an associative array of triangle's coordinates written as x1,y1, x2, y2, x3, y3 and <strong>$x</strong>, <strong>$y</strong>, the point's coordinates.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.horemag.net/2007/11/10/point-in-triangle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pythagorean triple in PHP</title>
		<link>http://dev.horemag.net/2007/11/07/pythagorean-triple-in-php/</link>
		<comments>http://dev.horemag.net/2007/11/07/pythagorean-triple-in-php/#comments</comments>
		<pubDate>Wed, 07 Nov 2007 05:13:45 +0000</pubDate>
		<dc:creator>AquilaX</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://dev.horemag.net/2007/11/07/pythagorean-triple-in-php/</guid>
		<description><![CDATA[Pythagorean triple is every integer a, b and c for which a2 + b2 = c2 or in other words the a, b, and c are the sides of a right triangle. To generate triples in PHP, you can use the following function:
&#160;
  function CalcTriple&#40;$n&#41;&#123;
    $res = array&#40;&#41;;
    [...]]]></description>
			<content:encoded><![CDATA[<p>Pythagorean triple is every integer a, b and c for which a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup> or in other words the a, b, and c are the sides of a right triangle. To generate triples in PHP, you can use the following function:</p>
<pre class="php">&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> CalcTriple<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$n</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$res</span> = <a href="http://www.php.net/array" onclick="javascript:urchinTracker('/outbound/article/http://www.php.net/array');"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$res</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>*<span style="color: #0000ff;">$n</span><span style="color: #66cc66;">&#41;</span><span style="color: #cc66cc;">+1</span>;
    <span style="color: #0000ff;">$res</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'b'</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>*<span style="color: #0000ff;">$n</span><span style="color: #66cc66;">&#41;</span>*<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$n</span><span style="color: #cc66cc;">+1</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$res</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'c'</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>*<span style="color: #0000ff;">$n</span><span style="color: #66cc66;">&#41;</span>*<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$n</span><span style="color: #cc66cc;">+1</span><span style="color: #66cc66;">&#41;</span><span style="color: #cc66cc;">+1</span>;
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$res</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p><strong>$n</strong> is an Integer and the returned array contains the triple.<br />
Unfortunately this function <strong>WON'T</strong> generate <strong>ALL</strong> triples. It just generates <strong>valid triples</strong>.<br />
Seen on <a href="http://en.wikipedia.org/wiki/Pythagorean_triple#Other_formulas_for_generating_triples" onclick="javascript:urchinTracker('/outbound/article/http://en.wikipedia.org/wiki/Pythagorean_triple#Other_formulas_for_generating_triples');">Wikipedia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.horemag.net/2007/11/07/pythagorean-triple-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Prime numbers in PHP</title>
		<link>http://dev.horemag.net/2007/11/04/prime-numbers-in-php/</link>
		<comments>http://dev.horemag.net/2007/11/04/prime-numbers-in-php/#comments</comments>
		<pubDate>Sat, 03 Nov 2007 23:18:47 +0000</pubDate>
		<dc:creator>AquilaX</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[prime numbers]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://dev.horemag.net/2007/11/04/prime-numbers-in-php/</guid>
		<description><![CDATA[This is quite fast (but unfortunately memory consuming) algorithm for generating all prime numbers up no n:
&#160;
&#160;
  $n = 100; //this is the upper limit
&#160;
  for &#40;$i = 2; $i &#60;=$n; $i++&#41;&#123;
    $primes&#91;&#93; = $i;
  &#125;
&#160;
  foreach &#40;$primes as $num&#41;&#123;
    if &#40;$num != 1&#41;&#123;
  [...]]]></description>
			<content:encoded><![CDATA[<p>This is quite fast (but unfortunately memory consuming) algorithm for generating all <a href="http://en.wikipedia.org/wiki/Prime_number" onclick="javascript:urchinTracker('/outbound/article/http://en.wikipedia.org/wiki/Prime_number');">prime numbers</a> up no <strong>n</strong>:</p>
<pre class="php">&nbsp;
&nbsp;
  <span style="color: #0000ff;">$n</span> = <span style="color: #cc66cc;">100</span>; <span style="color: #808080; font-style: italic;">//this is the upper limit</span>
&nbsp;
  <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$i</span> = <span style="color: #cc66cc;">2</span>; <span style="color: #0000ff;">$i</span> &lt;=<span style="color: #0000ff;">$n</span>; <span style="color: #0000ff;">$i</span>++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$primes</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$i</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$primes</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$num</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$num</span> != <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
      <span style="color: #0000ff;">$step</span> = <span style="color: #0000ff;">$num</span>;
      <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$i</span> = <span style="color: #cc66cc;">2</span>*<span style="color: #0000ff;">$num</span><span style="color: #cc66cc;">-2</span>; <span style="color: #0000ff;">$i</span> &lt;= <a href="http://www.php.net/count" onclick="javascript:urchinTracker('/outbound/article/http://www.php.net/count');"><span style="color: #000066;">count</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$primes</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #0000ff;">$i</span>= <span style="color: #0000ff;">$i</span> + <span style="color: #0000ff;">$step</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$primes</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span> != <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
          <span style="color: #0000ff;">$primes</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">1</span>;
        <span style="color: #66cc66;">&#125;</span>
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">$primes</span> = <a href="http://www.php.net/array_unique" onclick="javascript:urchinTracker('/outbound/article/http://www.php.net/array_unique');"><span style="color: #000066;">array_unique</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$primes</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #0000ff;">$primes</span> = <a href="http://www.php.net/array_values" onclick="javascript:urchinTracker('/outbound/article/http://www.php.net/array_values');"><span style="color: #000066;">array_values</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$primes</span><span style="color: #66cc66;">&#41;</span>;
  <a href="http://www.php.net/sort" onclick="javascript:urchinTracker('/outbound/article/http://www.php.net/sort');"><span style="color: #000066;">sort</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$primes</span><span style="color: #66cc66;">&#41;</span>;
  <a href="http://www.php.net/unset" onclick="javascript:urchinTracker('/outbound/article/http://www.php.net/unset');"><span style="color: #000066;">unset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$primes</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>I found this on Wikipedia <a href="http://en.wikipedia.org/wiki/Prime_number#Pseudocode_for_programs_to_find_primes" onclick="javascript:urchinTracker('/outbound/article/http://en.wikipedia.org/wiki/Prime_number#Pseudocode_for_programs_to_find_primes');">here</a>. It needs optimization (for example count($primes) doesn't need to be called on every iteration) but helped me a lot for solving some <a href="http://projecteuler.net" onclick="javascript:urchinTracker('/outbound/article/http://projecteuler.net');">Project Euler</a> problems.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.horemag.net/2007/11/04/prime-numbers-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

