<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title><![CDATA[Godefroy de Compreignac]]></title>
	<description><![CDATA[Flux RSS des articles]]></description>
	<pubDate>Mon, 10 Nov 2008 18:40:30 +0100</pubDate>
	<link>http://www.skreo.net</link>
	<language>fr</language>
	<generator>http://www.eklablog.com</generator>
	
	<item>
		<title><![CDATA[Interpréteur de BrainFuck]]></title>
		<link>http://www.skreo.net/article-2905-21372-interpreteur-de-brainfuck.html</link>
		<dc:creator>Skreo</dc:creator>
		<description><![CDATA[Dans ma lutte perp&eacute;tuelle contre le travail utile, je viens de gaspiller 2 heures &agrave; coder un interpr&eacute;teur de BrainFuck en PHP.
 Pour ceux qui ne connaissent pas le BrainFuck : http://fr.wikipedia.org/wiki/Brainfuck 
 Vous allez me dire, &quot;Hey mais &ccedil;a existe...]]></description>
		<content:encoded><![CDATA[Dans ma lutte perp&eacute;tuelle contre le travail utile, je viens de gaspiller 2 heures &agrave; coder un interpr&eacute;teur de BrainFuck en PHP.<br />   Pour ceux qui ne connaissent pas le BrainFuck : <a href="http://fr.wikipedia.org/wiki/Brainfuck">http://fr.wikipedia.org/wiki/Brainfuck</a> <br />  Vous allez me dire, <em>&quot;Hey mais &ccedil;a existe d&eacute;j&agrave; t&#39;es trop nul !&quot;</em>, ou&eacute; c&#39;est vrai, mais celui-ci est beaucoup plus rapide que les autres car il compile le BrainFuck en PHP et ex&eacute;cute le PHP obtenu avec <em>eval</em>. Et pour les ptits emmerdeurs qui me diront que la fonction <em>eval</em> n&#39;est pas s&eacute;curitaire, je leur r&eacute;pondrai que oui mais non parce que l&agrave; on g&egrave;re que des cha&icirc;nes de caract&egrave;res, celui qui arrive &agrave; me hacker &ccedil;a je lui offre un pikachu en peluche.<br />   Je pr&eacute;viens, c&#39;est totalement inutile, mais &ccedil;a peut &ecirc;tre int&eacute;ressant (&ccedil;a peut...).<br />   <br />   Voici la seule fonction de l&#39;interpr&eacute;teur :<br /> <div class="code"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp;<br />
&nbsp;<span style="color: #000000; font-weight: bold;">function</span> brainfuck<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$code</span>, <span style="color: #0000ff;">$input</span>=<span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp;    <span style="color: #0000ff;">$code</span> = <a href="http://www.php.net/preg_replace"><span style="color: #000066;">preg_replace</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'#[^<span style="color: #000099; font-weight: bold;">\[</span><span style="color: #000099; font-weight: bold;">\]</span><span style="color: #000099; font-weight: bold;">\+</span><span style="color: #000099; font-weight: bold;">\-</span><span style="color: #000099; font-weight: bold;">\.</span>,&lt;&gt;]#'</span>, <span style="color: #ff0000;">''</span>, <span style="color: #0000ff;">$code</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// On supprime tous les caractères non reconnus par le BF</span><br />
&nbsp;    <span style="color: #0000ff;">$code</span> = <a href="http://www.php.net/str_replace"><span style="color: #000066;">str_replace</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">']'</span>, <span style="color: #ff0000;">'}'</span>, <span style="color: #0000ff;">$code</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp;    <span style="color: #0000ff;">$code</span> = <a href="http://www.php.net/str_replace"><span style="color: #000066;">str_replace</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'['</span>, <span style="color: #ff0000;">'while($t[$p]!=&quot;<span style="color: #000099; font-weight: bold;">\x</span>00&quot;){'</span>, <span style="color: #0000ff;">$code</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp;    <span style="color: #0000ff;">$code</span> = <a href="http://www.php.net/preg_replace"><span style="color: #000066;">preg_replace</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'#([<span style="color: #000099; font-weight: bold;">\+</span>]+)#e'</span>, <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\'</span>$t[$p] = chr(ord($t[$p])+<span style="color: #000099; font-weight: bold;">\'</span>.strlen(&quot;<span style="color: #000099; font-weight: bold;">\\</span>1&quot;).<span style="color: #000099; font-weight: bold;">\'</span>);<span style="color: #000099; font-weight: bold;">\'</span>'</span>, <span style="color: #0000ff;">$code</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp;    <span style="color: #0000ff;">$code</span> = <a href="http://www.php.net/preg_replace"><span style="color: #000066;">preg_replace</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'#([<span style="color: #000099; font-weight: bold;">\-</span>]+)#e'</span>, <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\'</span>$t[$p] = chr(ord($t[$p])-<span style="color: #000099; font-weight: bold;">\'</span>.strlen(&quot;<span style="color: #000099; font-weight: bold;">\\</span>1&quot;).<span style="color: #000099; font-weight: bold;">\'</span>);<span style="color: #000099; font-weight: bold;">\'</span>'</span>, <span style="color: #0000ff;">$code</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp;    <span style="color: #0000ff;">$code</span> = <a href="http://www.php.net/preg_replace"><span style="color: #000066;">preg_replace</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'#([&gt;]+)#e'</span>, <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\'</span>$p += <span style="color: #000099; font-weight: bold;">\'</span>.strlen(&quot;<span style="color: #000099; font-weight: bold;">\\</span>1&quot;).<span style="color: #000099; font-weight: bold;">\'</span>;<span style="color: #000099; font-weight: bold;">\'</span>'</span>, <span style="color: #0000ff;">$code</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp;    <span style="color: #0000ff;">$code</span> = <a href="http://www.php.net/preg_replace"><span style="color: #000066;">preg_replace</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'#([&lt;]+)#e'</span>, <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\'</span>$p -= <span style="color: #000099; font-weight: bold;">\'</span>.strlen(&quot;<span style="color: #000099; font-weight: bold;">\\</span>1&quot;).<span style="color: #000099; font-weight: bold;">\'</span>;<span style="color: #000099; font-weight: bold;">\'</span>'</span>, <span style="color: #0000ff;">$code</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp;    <span style="color: #0000ff;">$code</span> = <a href="http://www.php.net/str_replace"><span style="color: #000066;">str_replace</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'.'</span>, <span style="color: #ff0000;">'$o .= $t[$p];'</span>, <span style="color: #0000ff;">$code</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp;    <span style="color: #0000ff;">$code</span> = <a href="http://www.php.net/str_replace"><span style="color: #000066;">str_replace</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">','</span>, <span style="color: #ff0000;">'$t[$p] = $i[$pi++];'</span>, <span style="color: #0000ff;">$code</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp;    <span style="color: #0000ff;">$code</span> = <span style="color: #ff0000;">'$p = 0;'</span>.                                             <span style="color: #808080; font-style: italic;">// Pointeur</span><br />
&nbsp;            <span style="color: #ff0000;">'$t = str_pad(<span style="color: #000099; font-weight: bold;">\'</span><span style="color: #000099; font-weight: bold;">\'</span>, 30000, chr(0));'</span>.                <span style="color: #808080; font-style: italic;">// Tableau d'octets</span><br />
&nbsp;            <span style="color: #ff0000;">'$i = <span style="color: #000099; font-weight: bold;">\'</span>'</span>.<a href="http://www.php.net/str_replace"><span style="color: #000066;">str_replace</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\'</span>'</span>, <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\'</span>'</span>, <span style="color: #0000ff;">$input</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\'</span>;'</span>.    <span style="color: #808080; font-style: italic;">// Input</span><br />
&nbsp;            <span style="color: #ff0000;">'$pi = 0;'</span>.                                            <span style="color: #808080; font-style: italic;">// Pointeur de l'input</span><br />
&nbsp;            <span style="color: #ff0000;">'$o = <span style="color: #000099; font-weight: bold;">\'</span><span style="color: #000099; font-weight: bold;">\'</span>;'</span>.                                        <span style="color: #808080; font-style: italic;">// Output</span><br />
&nbsp;            <span style="color: #0000ff;">$code</span>.<br />
&nbsp;            <span style="color: #ff0000;">'return $o;'</span>;<br />
&nbsp;    <br />
&nbsp;    <span style="color: #0000ff;">$o</span> = @<a href="http://www.php.net/eval"><span style="color: #000066;">eval</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$code</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp;    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$o</span> ? <span style="color: #0000ff;">$o</span> : <span style="color: #ff0000;">''</span>;<br />
&nbsp;<span style="color: #66cc66;">&#125;</span><br />
&nbsp;<span style="color: #000000; font-weight: bold;">?&gt;</span><div style="display:none;">[code=php]<br /> &lt;?php<br />   <br />   function brainfuck($code, $input=&#39;&#39;){<br />   &nbsp;&nbsp;&nbsp; $code = preg_replace(&#39;#[^\[\]\+\-\.,&lt;&gt;]#&#39;, &#39;&#39;, $code); // On supprime tous les caract&egrave;res non reconnus par le BF<br />   &nbsp;&nbsp;&nbsp; $code = str_replace(&#39;]&#39;, &#39;}&#39;, $code);<br />   &nbsp;&nbsp;&nbsp; $code = str_replace(&#39;[&#39;, &#39;while($t[$p]!=&quot;\x00&quot;){&#39;, $code);<br />   &nbsp;&nbsp;&nbsp; $code = preg_replace(&#39;#([\+]+)#e&#39;, &#39;\&#39;$t[$p] = chr(ord($t[$p])+\&#39;.strlen(&quot;\\1&quot;).\&#39;);\&#39;&#39;, $code);<br />   &nbsp;&nbsp;&nbsp; $code = preg_replace(&#39;#([\-]+)#e&#39;, &#39;\&#39;$t[$p] = chr(ord($t[$p])-\&#39;.strlen(&quot;\\1&quot;).\&#39;);\&#39;&#39;, $code);<br />   &nbsp;&nbsp;&nbsp; $code = preg_replace(&#39;#([&gt;]+)#e&#39;, &#39;\&#39;$p += \&#39;.strlen(&quot;\\1&quot;).\&#39;;\&#39;&#39;, $code);<br />   &nbsp;&nbsp;&nbsp; $code = preg_replace(&#39;#([&lt;]+)#e&#39;, &#39;\&#39;$p -= \&#39;.strlen(&quot;\\1&quot;).\&#39;;\&#39;&#39;, $code);<br />   &nbsp;&nbsp;&nbsp; $code = str_replace(&#39;.&#39;, &#39;$o .= $t[$p];&#39;, $code);<br />   &nbsp;&nbsp;&nbsp; $code = str_replace(&#39;,&#39;, &#39;$t[$p] = $i[$pi++];&#39;, $code);<br />   &nbsp;&nbsp;&nbsp; $code = &#39;$p = 0;&#39;. &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // Pointeur<br />   &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &#39;$t = str_pad(\&#39;\&#39;, 30000, chr(0));&#39;.&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // Tableau d&#39;octets<br />   &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &#39;$i = \&#39;&#39;.str_replace(&#39;\&#39;&#39;, &#39;\\\&#39;&#39;, $input).&#39;\&#39;;&#39;.&nbsp;&nbsp;&nbsp; // Input<br />   &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &#39;$pi = 0;&#39;.&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // Pointeur de l&#39;input<br />   &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &#39;$o = \&#39;\&#39;;&#39;.&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; // Output<br />   &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $code.<br />   &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &#39;return $o;&#39;;<br />   &nbsp;&nbsp;&nbsp; <br />   &nbsp;&nbsp;&nbsp; $o = @eval($code);<br />   &nbsp;&nbsp;&nbsp; return $o ? $o : &#39;&#39;;<br />   }<br />   ?&gt;<br /> [/code]</div></div>  <br />   <br />   Et voici un exemple d&#39;utilisation :<br /> <div class="code"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp;<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> brainfuck<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'-[&gt;+&gt;++&gt;++&lt;&lt;&lt;-----]+++[&gt;++++++&lt;-]&gt;.&gt;+++++.+.&gt;-----.&lt;&lt;---.&gt;.+++.--------.'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp;<span style="color: #000000; font-weight: bold;">?&gt;</span><div style="display:none;">[code=php]<br />&lt;?php<br />   echo brainfuck(&#39;-[&gt;+&gt;++&gt;++&lt;&lt;&lt;-----]+++[&gt;++++++&lt;-]&gt;.&gt;+++++.+.&gt;-----.&lt;&lt;---.&gt;.+++.--------.&#39;);<br />   ?&gt;<br /> [/code]</div></div>  <br />   <br />   D&icirc;tes-moi le r&eacute;sultat que &ccedil;a vous donne :p (Pas toi divarvel !)<br />   Vous pouvez aussi tester la fonction avec des codes en BF que vous trouverez sur internet, &agrave; priori tout marche ;-)<br />  Si vous avez trop la flemme de recopier ce code dans votre &eacute;diteur (bande de fain&eacute;ants !), voici un fichier php avec le code de la fonction et des exemples :<br />  <a href="http://data0.murties.com/skreo/perso/perso/brainfuck.php.txt">http://data0.murties.com/skreo/perso/perso/brainfuck.php.txt</a> <br />   ]]></content:encoded>
		<pubDate>Sun, 08 Jul 2007 02:09:14 +0200</pubDate>
		<guid isPermaLink="true">http://www.skreo.net/article-2905-21372-interpreteur-de-brainfuck.html</guid>
	</item>
	<item>
		<title><![CDATA[Calcul de la racine carrée]]></title>
		<link>http://www.skreo.net/article-2905-8276-calcul-de-la-racine-carree.html</link>
		<dc:creator>Skreo</dc:creator>
		<description><![CDATA[Je me posais depuis un certain temps la question toute bête mais interessante : comment calculer efficacement la racine carrée d'un nombre ?
Je parts de x² = a
Il y a quelques mois j'avais fait un petit programme un peu brutal et très lent sur ma TI-89 qui essayait plein de possibilités...]]></description>
		<content:encoded><![CDATA[Je me posais depuis un certain temps la question toute bête mais interessante : comment calculer efficacement la racine carrée d'un nombre ?<br />Je parts de x² = a<br />Il y a quelques mois j'avais fait un petit programme un peu brutal et très lent sur ma TI-89 qui essayait plein de possibilités pour trouver x en vérifiant à chaque fois en comparant x² et a, jusqu'à obtenir un écart maximal prédéfini. Mais c'était pas du tout satisfaisant...<br />J'ai alors cherché un peu sur internet, et j'ai trouvé plusieurs méthodes, mais la plupart étaient faites pour être utilisées à l'écrit, et elles étaient donc inexploitable dans un programme. Et j'ai trouvé la méthode d'Héron d'Alexandrie (1<sup>er</sup> siècle après J-C) qui consiste en fait à créer une fonction dont le minimum sur R<sup>+</sup> est en x = &#8730;a et y = &#8730;a. On cherche donc à atteindre le minimum de cette fonction pour avoir la valeur la plus approchée possible de &#8730;a<br /><img src="http://data0.murties.com/skreo/perso/images/maths/courbe_sqrt9.jpg" alt="" align="left" hspace="10" vspace="5" />A partir de l'équation x² = a, on obtient :<br />2x² = x² + a<br />2x = x + a/x<br />x = (x + a/x)/2<br /><br />Ce qui donne une fonction f(x) = (x + a/x)/2<br />A gauche, on voit la fonction f(x) = (x + 9/x)/2 pour trouver la racine carré de 9<br />On voit bien que le minimum de f(x) sur R<sup>+</sup> est en 3, qui est bien ce que l'on cherche ^^<br />Mais comment trouver ce minimum ? En calculant la dérivée f '(x) de f(x) et en cherchant la solution de f '(x) = 0 (la dérivée étant égale au coefficient directeur de la tangente en un point, elle est égale à 0 lorsque la tangente est horizontale, et donc lorsque f(x) atteint son minimum)<br />Mais non ! car si on fait ça, le résultat est.... &#8730;a ! On ne va tout de même pas tourner en rond ^^<br />Non en fait cette fonction permet à partir de n'importe quel nombre de départ d'obtenir un nombre plus proche de &#8730;a que ce nombre de départ. Donc en répétant le calcul 3 ou 4 fois on obtient déjà une très bonne approximation.<p>Prenons par exemple a = 671, et la racine carré de la puissance de 10 de ce nombre comme nombre de départ (facile à mettre en oeuvre dans un programme).<br />On a dans ce cas comme nombre de départ : x = &#8730;100 = 10<br />f(10) = (10 + 671 / 10) / 2 = 39,05<br />f(39,05) = (39,05 + 671 / 39,05) / 2 = 28,1025641025<br />f(28,1025641025) = (28,1025641025 + 671 / 28,1025641025) / 2 = 25,9896944600<br />f(25,9896944600) = (25,9896944600 + 671 / 25,9896944600) / 2 = 25,9038100697<br />f(25,9038100697) = (25,9038100697 + 671 / 25,9038100697) / 2 = 25,9036676943<br />f(25,9036676943) = (25,9036676943 + 671 / 25,9036676943) / 2 = 25,9036676940<br /><br />La valeur réelle de la racine carré de 671 donnée par un ordinateur est 25,9036676940<br />Au bout de 3 calculs, on obtient déjà un résultat convenable : 25,98<br />Et au bout de 6 calculs, on trouve la racine carré de 671 avec une précision de 10 chiffres après la virgule !<br />Evidemment, plus le nombre de départ est proche de la racine carré, et le nombre dont on veut trouver la racine carré est petit, moins il y a de calculs à faire ^^</p>]]></content:encoded>
		<pubDate>Sat, 11 Mar 2006 00:44:30 +0100</pubDate>
		<guid isPermaLink="true">http://www.skreo.net/article-2905-8276-calcul-de-la-racine-carree.html</guid>
	</item>
</channel>
</rss><!--mdp=-->