<?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>Ticklish Techs &#187; algorithm</title>
	<atom:link href="http://www.ticklishtechs.net/tag/algorithm/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ticklishtechs.net</link>
	<description>a mostly .NET but also some other cool techs blog</description>
	<lastBuildDate>Fri, 23 Jul 2010 06:07:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Hunting high and low</title>
		<link>http://www.ticklishtechs.net/2009/07/14/hunting-high-and-low/</link>
		<comments>http://www.ticklishtechs.net/2009/07/14/hunting-high-and-low/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 11:48:31 +0000</pubDate>
		<dc:creator>Wolfram Bernhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[maths]]></category>

		<guid isPermaLink="false">http://www.ticklishtechs.net/2009/07/14/hunting-high-and-low/</guid>
		<description><![CDATA[Here is a nice little challenge, Benjamin came up with some months ago: &#8220;From a list of numbers find the smallest and the largest one &#8211; but do not use more than 1,5 comparisons per number.&#8221;
Dear reader: Try it!
The solution is simple, straight forward and very nice. And I didn&#8217;t find it.
I found something else. [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a nice little challenge, Benjamin came up with some months ago: &#8220;From a list of numbers find the smallest and the largest one &#8211; but do not use more than 1,5 comparisons per number.&#8221;</p>
<p>Dear reader: Try it!</p>
<p>The solution is simple, straight forward and very nice. And I didn&#8217;t find it.</p>
<p>I found something else. My approach was to look at the largest-number-so-far (max) and the smallest-number-so-far (min) as the boundaries of a region. A number outside this region must be larger than max or smaller than min and causes that boundary to change.</p>
<p>To check if a number (a) was outside that region a little computation came to my mind:</p>
<pre class="code">check = (max - a)*(min - a);</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>You would expect n to be smaller than max, so (max-n) should be positive.</p>
<p>You would expect n to be larger than min, so (n-min) should be positive, too.</p>
<p>Two positive numbers multiplied result in a positive number again.</p>
<p>So, if n was outside the boundaries, <font size="2" face="Courier New">check</font> would become negative. I only had to check which boundary was exceeded and that&#8217;s it:</p>
<pre class="code"><span style="color: blue">if (</span>check &lt; 0)
<span style="color: blue">    if </span>(a &gt; max)
        max = a;
    <span style="color: blue">else
        </span>min = a;</pre>
<p>Using this approach the number of comparisons&nbsp; converges to 1 when the length of the list grows. So I found a solution that is way better than 1,5 comparisons per number, didn&#8217;t I?</p>
<h3>Boooooh</h3>
<p>No, I didn&#8217;t. I cheated. In fact&nbsp; <font size="2" face="Courier New">(max-a)</font> and <font size="2" face="Courier New">(min-a)</font> are comparisons, they just don&#8217;t use &gt; or &lt;.</p>
<p>(Actually it&#8217;s the other way around: To compute &lt; or &gt; most processors do a subtraction and compare the result to 0.)</p>
<p>So &#8211; if you count the subtractions as well you get 3+ comparisons per number&#8230;</p>
<p>The intended solution to the challenge (and the code you should provide in your exam) is:</p>
<pre class="code">a = list[count++];
b = list[count++];

<span style="color: blue">if </span>(a&lt;b)
{
    <span style="color: blue">if </span>(a&lt;min) min = a;
    <span style="color: blue">if </span>(b&gt;max) max = b;
}
<span style="color: blue">else
</span>{
    <span style="color: blue">if </span>(b &lt; min) min = b;
    <span style="color: blue">if </span>(a &gt; max) max = a;
}
</pre>
<h3>&nbsp;</h3>
<h3>So &#8211; what&#8217;s the point of this post?</h3>
<p>The point is: My silly approach can be faster than the standard solution. Depending on the type and range of the numbers in the list calculating and probing <font size="2" face="Courier New">check</font> needs less time than the comparisons in the standard solution.</p>
<p>It may not be much, but sometimes small advantages matter.&nbsp; For example: For an integer-list of length 10^8 with values from -10.000 to 10.000 my approach is 0.02 seconds faster (on my current laptop). And has less code.</p>
<p>So if you feel like using it &#8211; I won&#8217;t charge you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ticklishtechs.net/2009/07/14/hunting-high-and-low/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Permutations and the number 9 (proof)</title>
		<link>http://www.ticklishtechs.net/2009/02/27/permutations-and-the-number-9-proof/</link>
		<comments>http://www.ticklishtechs.net/2009/02/27/permutations-and-the-number-9-proof/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 16:47:06 +0000</pubDate>
		<dc:creator>Wolfram Bernhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[maths]]></category>

		<guid isPermaLink="false">http://www.ticklishtechs.net/2009/02/10/permutations-and-the-number-9-proof/</guid>
		<description><![CDATA[Ten days ago I promised a proof why the differences of any permutation of the same digits is always a multiple of 9. Here it is
Let&#8217;s say x and y are digits and &#8216;xy&#8217; is not &#8216;x*y&#8217;, but the number that consist of the digits x and y.
xy &#8211; yx = multiple of 9.
Is it [...]]]></description>
			<content:encoded><![CDATA[<p>Ten days ago I promised a proof why the <a href="http://www.ticklishtechs.net/2009/01/21/permutations-and-the-number-9/">differences of any permutation of the same digits is always a multiple of 9</a>. Here it is</p>
<p>Let&#8217;s say x and y are digits and &#8216;xy&#8217; is not &#8216;x*y&#8217;, but the number that consist of the digits x and y.</p>
<p>xy &#8211; yx = multiple of 9.</p>
<p>Is it always true? Yes, it is:</p>
<p>(10x + 1y) &#8211; (10y + 1x) = multiple of 9?    <br />9x &#8211; 9y = multiple of 9?     <br />9(x-y) = multiple of 9? Yes, for sure.</p>
<p>I calculated the numeric values of <strong>xy</strong> and <strong>yx</strong> by &quot;10 times the higher position + 1 time the lower position&quot;, just like we all do every day in our beloved decimal-system. More formally we describe the factor for the position (1, 10, 100&#8230;) by</p>
<p>10^position, (position is zero-based, counting from right to left, of course)</p>
<p>Now, when we change the position of a single digit within a number we change its numeric value from</p>
<p>x*10^(old position) to x*10^(new position).</p>
<p>We can neglect x here, because it&#8217;s a factor that occurs in both values, so we can get rid of it by division.    <br />The remaining part for building the difference is</p>
<p>10^p1 &#8211; 10^p2, a formula that always produces multiples of 9: 10-1, 1000-100, 10-1000000.</p>
<h3>Conclusion</h3>
<p>Since the difference of a number and one of it&#8217;s permutations is a sum of (10^p1-10^p2)-parts, which are all multiples of 9, the total also can be divided by 9.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ticklishtechs.net/2009/02/27/permutations-and-the-number-9-proof/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Permutations and the number 9</title>
		<link>http://www.ticklishtechs.net/2009/02/17/permutations-and-the-number-9/</link>
		<comments>http://www.ticklishtechs.net/2009/02/17/permutations-and-the-number-9/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 16:43:14 +0000</pubDate>
		<dc:creator>Wolfram Bernhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[maths]]></category>

		<guid isPermaLink="false">http://www.ticklishtechs.net/2009/01/21/permutations-and-the-number-9/</guid>
		<description><![CDATA[Lately I stumbled over permutations and noticed a funny fact: When you take any integer, produce a permutation and subtract one form the other, you always get a multiple of 9. 
It works with a any length. Some examples: 
Length 2    23 &#8211; 32 = -9&#160; 84 &#8211; 48 = 36  [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I stumbled over permutations and noticed a funny fact: When you take any integer, produce a permutation and subtract one form the other, you always get a multiple of 9. </p>
<p>It works with a any length. Some examples: </p>
<p>Length 2    <br />23 &#8211; 32 = -9&#160; <br />84 &#8211; 48 = 36     <br />60 &#8211; 06 = 54 </p>
<p>Length 3    <br />123 &#8211; 132 = -9     <br />and so on&#8230; </p>
<p>It still works for changing the front position:    <br />123 &#8211; 213 = -90 </p>
<p>In fact it works for changing any position:    <br />123 &#8211; 321 = -198 (-22*9) </p>
<p>This leads to the conclusion I stated in the beginning. The difference of any permutation of the same number is a multiple of 9. Feel free to check it with you favorite numbers.</p>
<p>It&#8217;s not a great mathematical invention (if it is, please let me know), but I didn&#8217;t know it, didn&#8217;t expect it und find it kind of fascinating. I asked a couple of friends and no one was aware of this. </p>
<p>I still try to find a way to use these fact, maybe for spell-check-like for numbers or a fast calculation of permutations, but so far it seems just useless <img src='http://www.ticklishtechs.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  </p>
<p>Instead I can provide a proof for numbers of any length and I&#8217;ll post it here in ten days from today. Meanwhile I want you to try it yourself. The proof is really, really simple, almost trivial and it won&#8217;t cost you more than five minutes to understand it all. </p>
<p>Please email or comment your solution!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ticklishtechs.net/2009/02/17/permutations-and-the-number-9/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Asymmetric encryption / public key encryption / RSA</title>
		<link>http://www.ticklishtechs.net/2008/06/17/asymmetric-encryption-public-key-encryption-rsa/</link>
		<comments>http://www.ticklishtechs.net/2008/06/17/asymmetric-encryption-public-key-encryption-rsa/#comments</comments>
		<pubDate>Tue, 17 Jun 2008 07:07:05 +0000</pubDate>
		<dc:creator>Wolfram Bernhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithm]]></category>

		<guid isPermaLink="false">http://www.ticklishtechs.net/2008/06/17/asymmetric-encryption-public-key-encryption-rsa/</guid>
		<description><![CDATA[It took me a lot of time to find a neat explanation of asymmetric encryption. Many sides say &#8220;a message is encrypted using a public key and it can only be decrypted with a corresponding private key.&#8221; Okay, fine, but how does it work in detail? Finally I found the RSA-entry in wikipedia. It&#8217;s almost [...]]]></description>
			<content:encoded><![CDATA[<p>It took me a lot of time to find a neat explanation of asymmetric encryption. Many sides say &#8220;a message is encrypted using a public key and it can only be decrypted with a corresponding private key.&#8221; Okay, fine, but how does it work in detail? Finally I found <a href="http://en.wikipedia.org/wiki/RSA">the RSA-entry in wikipedia</a>. It&#8217;s almost what I was looking, but there are still lots of links to mathematical definitions and calculations you can&#8217;t do with your pocket calculator. So I decided to write a little compilation of the presented algorithm and to apply it using numbers you can handle. Also small number help to clarify things &#8211; but keep in mind that all the numbers are much much much greater in serious encryption. </p>
<h3>How do we get our private-and-public-key-pair?</h3>
<ol>
<li>Choose two distinct (large) random <a href="http://en.wikipedia.org/wiki/Prime_number">prime numbers</a>&nbsp;<em>p</em> and q<i><br />p = 7,&nbsp;&nbsp; q = 3.<br /></i> </li>
<li>Compute n = p*q. This n is used as the <a href="http://en.wikipedia.org/wiki/Modular_arithmetic">modulus</a> for both the public and private keys <br />n = 7*3 = 21. </li>
<li>Compute the <a href="http://en.wikipedia.org/wiki/Totient">totient</a>: phi(n) = (p-1)(q-1) <br />phi(21) = (7-1) * (3-1) = 12. </li>
<li>Choose an integer <em>e</em> such that 1&lt;e&lt;phi(n) and that e and phi(n) share no factors other than 1 (i.e. <em>e</em> and phi(n) are <a href="http://en.wikipedia.org/wiki/Coprime">coprime.</a>). <i>e</i> is released as the public key exponent. <br />In order to chose an e I will factorize my phi(12) first: 12 = 2*2*3.<br />I chose e=5. e does not necessarily have to be prime but it makes it easier to avoid sharing a factor. </li>
<li>Compute an integer <i>d</i> to satisfy the <a href="http://en.wikipedia.org/wiki/Modular_arithmetic#The_congruence_relation">congruence relation</a>&nbsp;<strong>d*e mod phi(n) = 1</strong>; <i>d</i> is kept as the private key exponent. <br />&#8220;congruence relation&#8221; sounds more complicated than it actually is: Take two different numbers and apply the same modulo-operation to them (like mod 3). If the result is the same for both you may call your integers <b>congruent</b> <b>modulo</b> <em>5.&nbsp; </em>Like 11 and 16 are congruent modulo 5, since <strong>11 mod 5 = 1</strong> and <strong>16 mod 5 = 1</strong>. <br />So we are looking for a integer d that fits into<br />d * 5&nbsp; mod 12 = 1. <br />I needed some tries here and wrote some lines to find the lowest d = 5. With one d found you can find all but just adding 12 <img src='http://www.ticklishtechs.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> . I didn&#8217;t want to take 5 as the private key exponent, since having the same exponent for encryption and decryption is&#8230; well&#8230; stupid. So I&#8217;ll choose 17.<br />17 * 5 mod 12 = 1.<br />85 mod 12 = 1.&nbsp; Correct!</li>
</ol>
<p>Do we have our private and our public key now? Yes, we have. Note that a key is not one single number but a pair of two numbers. Both are needed in the processes of encryption und decryption. One is the exponent and the other the modulus. Just a second and you&#8217;ll see why.</p>
<p>public key: e =5 (exponent) / n = 21 (modulus).</p>
<p>private key: d = 17 (exponent) / n = 21 (modulus).&nbsp; </p>
<h3>Let&#8217;s encrypt something</h3>
<p>We have given our public key to anyone we know. And we kept our private key hidden somewhere under the bed. Now a beautiful and clever girl named Alice wants to send me a message. Fortunately the letters of her message can be represented as a stream of bits and we interpret this stream as a number. So her message is 10. It is important that her message is lower than our modulus, we&#8217;ll later see why. Let&#8217;s call her original message <strong>m</strong> and the encrypted message <strong>c</strong>.</p>
<p>m=10 </p>
<p>The encryption it a simple formula: <strong>c = m^e mod n</strong>. </p>
<p>c = 10^5 mod 21</p>
<p>c = 100000 mod 21</p>
<p>c = 19.</p>
<p>So Alice hands me a little note saying &#8220;19&#8243;. 19? What the hack is that supposed to mean? </p>
<h3>Now the magic happens</h3>
<p>I rush back home to find my private key and apply it to &#8220;19&#8243;</p>
<p>The formula for the decryption look very similar: <strong>m = c^d mod n</strong>. </p>
<p>m = 19^17 mod 21.</p>
<p>m = 5480386857784802185939 mod 21. (Try it using calc.exe)</p>
<p>m = 10!</p>
<p>&nbsp;</p>
<p>Wow! Alice says &#8220;10&#8243; to us. Isn&#8217;t 10 the international code for &#8220;I would like to date you?&#8221; I think so. </p>
<p>&nbsp;</p>
<h3>Signing </h3>
<p>To encrypt with the public key means you can decrypt only with the private key. The converse is also true &#8211; to encrypt with the private key means you can decrypt only with the public key. Try it!</p>
<p>How can utilize this? We can use it to guarantee that a message is from a specific sender.</p>
<p>I have been dating Alice for some time now and we are used to leave messages to each other at a hidden place. Unfortunately another girl &#8211; Eve &#8211; is very jealous and has spied our secret mailbox. One week ago she found a message from Alice to me. She couldn&#8217;t read it, but she threw it away and replaced it by a mean offense against me. She encrypted it using my public key (which is public <img src='http://www.ticklishtechs.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  ) and I thought it was written by Alice. The message started a little fight but luckily we found about Eve&#8217;s intention and started signing our messages. Now we transfer messages like this:</p>
<ol>
<li>Alice writes me a message <strong>m</strong>.  </li>
<li>She encrypts <strong>m</strong> using my public key, the result is <strong>c</strong>.  </li>
<li>Now she encrypts <strong>c</strong> using her own private key, the result is <strong>cs</strong> (<strong>c</strong> signed)  </li>
<li>She leaves me the message  </li>
<li>I decrypt <strong>cs</strong> using her public key, getting <strong>c</strong>. (Eve can do this as well &#8211; but that&#8217;s it. She can&#8217;t read <strong>c</strong> and she can&#8217;t leave me a fake <strong>cs</strong> because she doesn&#8217;t have Alice&#8217;s private key.  </li>
<li>I decrypt <strong>c</strong> using my private key and get back <strong>m</strong> &#8211; bingo!</li>
</ol>
<p>&nbsp;</p>
<h3>Padding</h3>
<p>Breaking this code (meaning &#8220;decrypt without having the private key&#8221;) is always a lot of work and hard trying. But is becomes much easier, when you know that <strong>m</strong> is short. That&#8217;s because you only have check that little fraction of configurations where <strong>m = c^d mod n </strong>leads to small <strong>m</strong>s. To avoid this small messages are artificially made longer until they are close to <strong>n</strong>. That process is called <a href="http://en.wikipedia.org/wiki/Padding_%28cryptography%29">padding</a> and even padding can be a tricky task.</p>
<h3>Last question</h3>
<p>We know what happens to short messages. But what happens to long messages? It is obvious that messages &#8211; seen as a number &#8211; can&#8217;t be greater than <strong>n-1,</strong> since the <strong>mod n</strong> &#8211; part of the decryption formula will never return values &gt;<strong>n-1</strong>. I guess the message is split up in parts, but couldn&#8217;t find a satisfying answer. If I do, I&#8217;ll let you know. </p>
<p>&nbsp;</p>
<p>Take care,&nbsp; <br />Bob </p>
]]></content:encoded>
			<wfw:commentRss>http://www.ticklishtechs.net/2008/06/17/asymmetric-encryption-public-key-encryption-rsa/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
