<?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>Elementary arithmetic &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/elementary-arithmetic/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Computer Algorithms: Adding Large Integers</title>
		<link>/2013/01/07/computer-algorithms-adding-large-integers/</link>
		<comments>/2013/01/07/computer-algorithms-adding-large-integers/#comments</comments>
		<pubDate>Mon, 07 Jan 2013 15:16:47 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[Abstract algebra]]></category>
		<category><![CDATA[Addition]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Arbitrary-precision arithmetic]]></category>
		<category><![CDATA[Binary numeral system]]></category>
		<category><![CDATA[Computer arithmetic]]></category>
		<category><![CDATA[Elementary arithmetic]]></category>
		<category><![CDATA[Elementary number theory]]></category>
		<category><![CDATA[faster algorithm]]></category>
		<category><![CDATA[Integer]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Number]]></category>
		<category><![CDATA[Radix sort]]></category>

		<guid isPermaLink="false">/?p=3525</guid>
		<description><![CDATA[Introduction We know how to add two integers using a perfectly simple and useful algorithm learned from school or even earlier. This is perhaps one of the very first techniques we learn in mathematics. However we need to answer few questions. First of all do computers use the same technique, since they use binary representation &#8230; <a href="/2013/01/07/computer-algorithms-adding-large-integers/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Adding Large Integers</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/12/24/computer-algorithms-sorting-in-linear-time/" rel="bookmark" title="Computer Algorithms: Sorting in Linear Time">Computer Algorithms: Sorting in Linear Time </a></li>
<li><a href="/2010/06/25/friday-algorithms-sorting-a-set-of-integers-far-quicker-than-quicksort/" rel="bookmark" title="Friday Algorithms: Sorting a Set of Integers &#8211; Far Quicker than Quicksort!">Friday Algorithms: Sorting a Set of Integers &#8211; Far Quicker than Quicksort! </a></li>
<li><a href="/2012/05/15/computer-algorithms-karatsuba-fast-multiplication/" rel="bookmark" title="Computer Algorithms: Karatsuba Fast Multiplication">Computer Algorithms: Karatsuba Fast Multiplication </a></li>
<li><a href="/2013/01/02/computer-algorithms-bucket-sort/" rel="bookmark" title="Computer Algorithms: Bucket Sort">Computer Algorithms: Bucket Sort </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>We know how to add two integers using a perfectly simple and useful algorithm learned from school or even earlier. This is perhaps one of the very first techniques we learn in mathematics. However we need to answer few questions. First of all do computers use the same technique, since they use binary representation of numbers? Is there a faster algorithm used by computers? What about boundaries and large integers?</p>
<h2>Overview</h2>
<p>Let’s start by explaining how we humans add two numbers. An important fact is that by adding two single-digit numbers we get at most two digit number. This can be proven by simply realizing that 9+9 = 18. This fact lays down in the way we add integers. Here’s how.</p>
<p><img src="https://docs.google.com/drawings/pub?id=11pxzTffU-mVas5OYWiFg2sdSUAXa-QvBD1pSewNao3A&amp;w=620&amp;h=399"></p>
<p>We just line-up the integers on their right-most digit and we start adding them in a column. In case we got a sum greater than 9 (let’s say 14) we keep only the right-most digit (the 4) and the 1 is added to the next sum.</p>
<p>Thus we get to the simple fact that by adding two n-digit integers we can have either an n-digit integer or a n+1 digit integer. As an example we see that by adding 53 + 35 (two 2-digit integers) we get 88, which is again 2-digit integer, but 53 + 54 result in 107, which is 3 digit integer. </p>
<p>That fact is practically true, as I mentioned above, for each pair of n-digit integers.</p>
<h3>What about binary numbers?</h3>
<p>In fact binaries can be added by using the exact same algorithm. At the example below we add two integers represented as binary numbers.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1E376ILBpXg5fUU8JbxiKwIcSa8DxSDtaIjqVsCJCjWk&amp;w=620&amp;h=399"></p>
<p>As a matter of fact this algorithm is absolutely wonderful, because it works not only on decimals and binaries but in any base B.</p>
<p>Of course computers tend to perform better when adding integers that “fit” the machine word. However as we can see later this isn’t always the case and sometimes we need to add larger numbers that exceed the type boundaries.</p>
<h3>What about big integers?</h3>
<p>Since we know how to add “small” integers, it couldn’t be so hard to apply the same algorithm on big integers. The only problem is that the addition will be slower and sometimes (done by humans) can be error prone. </p>
<p>So practically the algorithm is the same, but we can’t just put a 1 billion integer into a standard computer type INT, right? That means that the tricky part here is the way we represent integers in our application. A common solution is to store the “big” integer into an array, thus each digit will be a separate array item. Then the operation of addition will be simple enough to be applied.</p>
<h2>Complexity</h2>
<p>When we talk about an algorithm that is so well known by every human being (or almost every) a common question is “is there anything faster” or “do computers use a different algorithm”. The answer may be surprising to someone, but unfortunately that is the fastest (optimal) algorithm for number addition. </p>
<p>Practically there’s nothing to optimize here. We just read the two n-digit numbers (O(n)), we apply “simple” addition to each digit and we carry over the 1 from the sums greater than 9 to the next &#8220;simple&#8221; addition. We don’t have loops or any complex operation in order to search for an optimization niche.</p>
<h2>Application</h2>
<p>It’s strange how often this algorithm is asked on coding interviews. Perhaps the catch is whether the interviewed person will start to look for a faster approach?! Thus is cool to know that this algorithm is optimal.</p>
<p>Sometimes we may ask ourselves why we humans use decimals. It’s considered because we have 10 fingers on our hands and this is perhaps true.</p>
<p>An interesting fact though, is that the Mayas (who barely predicted the end of the world a couple of weeks ago) used a system of a base 20. That is logical, since we have not 10, but total of 20 fingers considering our legs.</p>
<p>Finally, this algorithm may seem to easy to be explained but it lays down in more complex algorithms.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/12/24/computer-algorithms-sorting-in-linear-time/" rel="bookmark" title="Computer Algorithms: Sorting in Linear Time">Computer Algorithms: Sorting in Linear Time </a></li>
<li><a href="/2010/06/25/friday-algorithms-sorting-a-set-of-integers-far-quicker-than-quicksort/" rel="bookmark" title="Friday Algorithms: Sorting a Set of Integers &#8211; Far Quicker than Quicksort!">Friday Algorithms: Sorting a Set of Integers &#8211; Far Quicker than Quicksort! </a></li>
<li><a href="/2012/05/15/computer-algorithms-karatsuba-fast-multiplication/" rel="bookmark" title="Computer Algorithms: Karatsuba Fast Multiplication">Computer Algorithms: Karatsuba Fast Multiplication </a></li>
<li><a href="/2013/01/02/computer-algorithms-bucket-sort/" rel="bookmark" title="Computer Algorithms: Bucket Sort">Computer Algorithms: Bucket Sort </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2013/01/07/computer-algorithms-adding-large-integers/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to Setup Different Error Messages for Each Zend Form Element Validator</title>
		<link>/2011/11/23/how-to-setup-different-error-messages-for-each-zend-form-element-validator/</link>
		<comments>/2011/11/23/how-to-setup-different-error-messages-for-each-zend-form-element-validator/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 08:25:07 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[0]]></category>
		<category><![CDATA[Elementary arithmetic]]></category>
		<category><![CDATA[Error message]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[measurement]]></category>
		<category><![CDATA[Nothing]]></category>
		<category><![CDATA[Numbers]]></category>
		<category><![CDATA[Pi]]></category>

		<guid isPermaLink="false">/?p=2469</guid>
		<description><![CDATA[Anyone who has worked with that has come across this problem. I&#8217;d like to show different error message on each validator attached to a Zend_Form_Element. Let&#8217;s say we validate an text input field. We want it to contain only digits, but also we&#8217;d like to display different messages when the field is empty and when &#8230; <a href="/2011/11/23/how-to-setup-different-error-messages-for-each-zend-form-element-validator/" class="more-link">Continue reading <span class="screen-reader-text">How to Setup Different Error Messages for Each Zend Form Element Validator</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/07/07/default-error-handling-in-zend-framework/" rel="bookmark" title="Default Error Handling in Zend Framework">Default Error Handling in Zend Framework </a></li>
<li><a href="/2010/06/04/one-form-multiple-db-records/" rel="bookmark" title="One Form &#8211; Multiple DB Records">One Form &#8211; Multiple DB Records </a></li>
<li><a href="/2010/04/09/secure-forms-with-zend-framework/" rel="bookmark" title="Secure Forms with Zend Framework">Secure Forms with Zend Framework </a></li>
<li><a href="/2010/07/22/zend_validate_db_recordexists-in-zend-framework-1-10/" rel="bookmark" title="Zend_Validate_Db_RecordExists in Zend Framework 1.10+">Zend_Validate_Db_RecordExists in Zend Framework 1.10+ </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Anyone who has worked with that has come across this problem. I&#8217;d like to show different error message on each validator attached to a <a href="http://framework.zend.com/manual/1.11/en/zend.form.standardElements.html" title="Zend Framework: Standard Form Elements" target="_blank">Zend_Form_Element</a>. Let&#8217;s say we validate an text input field. We want it to contain only digits, but also we&#8217;d like to display different messages when the field is empty and when the user has entered something that is different from digits. </p>
<p>It can be done by attaching to the form element two validators: <a href="http://framework.zend.com/manual/en/zend.validate.set.html#zend.validate.set.digits" title="Zend Framework: Standard Validation Classes, Zend_Validate_Digits" target="_blank">Zend_Validate_Digits</a> and <a href="http://framework.zend.com/manual/en/zend.validate.set.html#zend.validate.set.notempty" title="Zend Framework: Standard Validation Classes, Zend_Validate_NotEmpty" target="_blank">Zend_Validate_NotEmpty</a>, but first let&#8217;s see how to change the default &#8220;Value is required and can&#8217;t be empty&#8221; error message of a form field.</p>
<pre lang="PHP">
$element = $form->createElement('text', 'phone');
$element->setLabel('Please, enter your phone number:')
	->setRequired(true)
	->addValidator('Digits');
$form->addElement($element);
</pre>
<p>Here we validate the field with Zend_Validate_Digits and we have set it to be required. Thus everything containing characters, i.e. &#8220;my123name&#8221; or &#8220;007bond&#8221;, will be false, while &#8220;1234&#8221; will be true.</p>
<p><figure id="attachment_2485" style="width: 620px" class="wp-caption alignnone"><a href="/category/zend-framework-on-stoimen-com/"><img src="/wp-content/uploads/2011/11/zend-framework-logo.png" alt="Zend Framework" title="zend-framework-logo" width="620" height="169" class="size-full wp-image-2485" /></a><figcaption class="wp-caption-text">To show different error messages you&#039;ve to attach them per validator and not per form element!</figcaption></figure><br />
 <span id="more-2469"></span><br />
First of all this field is set to be required with the line ->setRequired(true), so we cannot submit the form if the input is empty and we&#8217;ll receive the default error message &#8220;Value is required and can&#8217;t be empty&#8221;. The question is how to change this default message, because as you know sometimes you&#8217;d like to say something different to your users or you&#8217;d like to display error messages on a language different from English. Here&#8217;s how.</p>
<pre lang="PHP">
$element = $form->createElement('text', 'phone');
$element->setLabel('Please, enter your phone number:')
	->setRequired(true)
	->addValidator('Digits')
	->addErrorMessage('Please, type your phone here!');
$form->addElement($element);
</pre>
<p>Now the error message is changed from &#8220;Value is required and can&#8217;t be empty&#8221; to &#8220;Please, type your phone here!&#8221;. </p>
<p>The problem is that when you add more than one validator to a form field you can still show one message regardless of the validator that has failed.</p>
<pre lang="PHP">
$element = $form->createElement('text', 'phone');
$element->setLabel('Please, enter your phone number:')
	->setRequired(true)
	->addValidator('NotEmpty', true)
	->addValidator('Digits', true)
	->addErrorMessage('Please, type your phone here!');
$form->addElement($element);
</pre>
<p>In this case whenever the field is empty or it contains something different from digits, the message shown to the user will be &#8220;Please, type your phone here!&#8221;. The question is can we show different error messages on every validator. This should look something like &#8220;The field cannot be empty!&#8221; when the field is empty and &#8220;Please, enter only digits!&#8221; when the user has entered something into the field, but it doesn&#8217;t contain only digits.</p>
<h2>The Solution</h2>
<p>Actually we have to attach error messages <em><strong>per validator</strong></em>, and not on a form element. Here&#8217;s how it can be done.</p>
<pre lang="PHP">
$notEmpty = new Zend_Validate_NotEmpty();
$notEmpty->setMessage('The field cannot be empty!');

$digits = new Zend_Validate_Digits();
$digits->setMessage('Please, enter only digits');

$element = $form->createElement('text', 'phone');
$element->setLabel('Please, enter your phone:')
	->setRequired(true)
	->addValidator($notEmpty, true)
	->addValidator($digits, true);
$form->addElement($element);
</pre>
<p>Note that we set to &#8220;true&#8221; the second parameter of addValidator. This is important because this way we break the validator&#8217;s chain and when the validation fails on NotEmpty the framework stops the validation of that field against the other validators.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/07/07/default-error-handling-in-zend-framework/" rel="bookmark" title="Default Error Handling in Zend Framework">Default Error Handling in Zend Framework </a></li>
<li><a href="/2010/06/04/one-form-multiple-db-records/" rel="bookmark" title="One Form &#8211; Multiple DB Records">One Form &#8211; Multiple DB Records </a></li>
<li><a href="/2010/04/09/secure-forms-with-zend-framework/" rel="bookmark" title="Secure Forms with Zend Framework">Secure Forms with Zend Framework </a></li>
<li><a href="/2010/07/22/zend_validate_db_recordexists-in-zend-framework-1-10/" rel="bookmark" title="Zend_Validate_Db_RecordExists in Zend Framework 1.10+">Zend_Validate_Db_RecordExists in Zend Framework 1.10+ </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/11/23/how-to-setup-different-error-messages-for-each-zend-form-element-validator/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
