<?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>Dreamshire &#187; Totient</title>
	<atom:link href="http://blog.dreamshire.com/tag/totient/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dreamshire.com</link>
	<description>Showing what's behind the curtain</description>
	<lastBuildDate>Wed, 01 Sep 2010 02:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Project Euler Problem 70 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/13/project-euler-problem-70-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/13/project-euler-problem-70-solution/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 22:57:46 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 70-79]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[solution]]></category>
		<category><![CDATA[Totient]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=652</guid>
		<description><![CDATA[Investigate values of n for which φ(n) is a permutation of n.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Euler&#8217;s Totient function, &phi;(<var>n</var>) [sometimes called the phi function], is used to determine the number of positive numbers less than or equal to <var>n</var> which are relatively prime to <var>n</var>. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, &phi;(9)=6.<br />The number 1 is considered to be relatively prime to every positive number, so &phi;(1)=1. </p>
<p>Interestingly, &phi;(87109)=79180, and it can be seen that 87109 is a permutation of 79180.</p>
<p>Find the value of <var>n</var>, 1 &lt;<var>n</var> &lt; 10<sup>7</sup>, for which &phi;(<var>n</var>) is a permutation of <var>n</var> and the ratio <var>n</var>/&phi;(<var>n</var>) produces a minimum.</p>
<h4><u>Analysis</u></h4>
<p>We can reduce our search significantly by selecting prime pairs (<em>p</em><sub>1</sub>,  <em>p</em><sub>2</sub> ) and calculate <em>n</em> as <em>n</em> = <em>p</em><sub>1</sub> x <em>p</em><sub>2</sub>.  This allows the totient to be calculated as  <nobr>&phi;(<var>n</var>) = (<em>p</em><sub>1</sub>-1)(<em>p</em><sub>2</sub>-1)</nobr> for <em>n</em>.  Now, just find the minimum ratio <em>n/&phi;(n) </em> for those <em>n</em> and &phi;(<var>n</var>)  that are permutations of one another.</p>
<p>The range of primes was selected by taking the square root of the upper bound, 10,000,000, which is about 3162 and taking &plusmn;25% for a range of primes from 2000 to 4000 (247 primes).</p>
<h4><u>Solution</u></h4>
<p>Runs < 3 seconds in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">@p</span> <span style="color: #339933;">=</span> <span style="color: #000066;">split</span> <span style="color: #009966; font-style: italic;">/s/</span><span style="color: #339933;">,</span><span style="color: #009999;">&lt;DATA&gt;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$min_q</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$min_n</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$i</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$max</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span>_000_000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$p1</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@p</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #0000ff;">$p2</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@p</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #339933;">++</span> <span style="color: #339933;">..</span> <span style="color: #0000ff;">$#p</span><span style="color: #009900;">&#93;</span><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: #0000ff;">$p1</span> <span style="color: #339933;">*</span> <span style="color: #0000ff;">$p2</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">last</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$n</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000ff;">$max</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$phi</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p1</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p2</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$q</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$n</span> <span style="color: #339933;">/</span> <span style="color: #0000ff;">$phi</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$min_q</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$min_n</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$q</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$n</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">if</span> permutation<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$phi</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$n</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #0000ff;">$min_q</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000ff;">$q</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#125;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE70 = $min_n&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> permutation <span style="color: #009900;">&#123;</span> <span style="color: #000066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #339933;">,</span> <span style="color: #000066;">sort</span> <span style="color: #000066;">split</span> <span style="color: #339933;">//,</span> <span style="color: #0000ff;">@_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">eq</span> <span style="color: #000066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #339933;">,</span> <span style="color: #000066;">sort</span> <span style="color: #000066;">split</span> <span style="color: #339933;">//,</span> <span style="color: #0000ff;">@_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">__DATA__</span>
<span style="color: #cc66cc;">2003</span> <span style="color: #cc66cc;">2011</span> <span style="color: #cc66cc;">2017</span> <span style="color: #cc66cc;">2027</span> <span style="color: #cc66cc;">2029</span> <span style="color: #cc66cc;">2039</span> <span style="color: #cc66cc;">2053</span> <span style="color: #cc66cc;">2063</span> <span style="color: #cc66cc;">2069</span> <span style="color: #cc66cc;">2081</span> <span style="color: #cc66cc;">2083</span> <span style="color: #cc66cc;">2087</span> <span style="color: #cc66cc;">2089</span> <span style="color: #339933;">...</span> <span style="color: #cc66cc;">3967</span> <span style="color: #cc66cc;">3989</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>The prime numbers from 2000 &#8211; 4000 were included at the end of the script and abbreviated here to save space.<br />
We tried the same method for <var>n</var>, 1 &lt;<var>n</var> &lt; 10<sup>9</sup>, using the square root 31623 and the 25% bounds of 23,700 through 39,500.  The result was 990326167 in about 50 seconds.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/13/project-euler-problem-70-solution/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 69 Solution</title>
		<link>http://blog.dreamshire.com/2009/04/04/project-euler-problem-69-solution/</link>
		<comments>http://blog.dreamshire.com/2009/04/04/project-euler-problem-69-solution/#comments</comments>
		<pubDate>Sat, 04 Apr 2009 08:52:52 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 60-69]]></category>
		<category><![CDATA[Prime Numbers]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Totient]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=376</guid>
		<description><![CDATA[Find the value of n ≤ 1,000,000 for which n/φ(n) is a maximum.]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>Euler&#8217;s Totient function, φ(n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, φ(9)=6.</p>
<table>
<tr>
<th>n
<th>Relatively Prime
<th>φ(n)
<th>n/φ(n)</tr>
<tr>
<td>2
<td>1
<td>1
<td>2 </tr>
<tr>
<td>3
<td>1,2
<td>2
<td>1.5 </tr>
<tr>
<td>4
<td>1,3
<td>2
<td>2 </tr>
<tr>
<td>5
<td>1,2,3,4
<td>4
<td>1.25 </tr>
<tr>
<td>6
<td>1,5
<td>2
<td>3 </tr>
<tr>
<td>7
<td>1,2,3,4,5,6
<td>6
<td>1.1666&#8230; </tr>
<tr>
<td>8
<td>1,3,5,7
<td>4
<td>2 </tr>
<tr>
<td>9
<td>1,2,4,5,7,8
<td>6
<td>1.5 </tr>
<tr>
<td>10
<td>1,3,7,9
<td>4
<td>2.5 </tr>
</table>
<p>It can be seen that n=6 produces a maximum n/φ(n) for n &le; 10.</p>
<p>Find the value of n &le; 1,000,000 for which n/φ(n) is a maximum.</p>
<h4><u>Analysis</u></h4>
<p>We&#8217;re trying to find the smallest φ(<em>n</em>) and the largest <em>n</em> in order to keep <em>n</em>/φ(n) at a maximum.  One method is to multiply as many consecutive primes as possible without exceeding the limit of 1,000,000.</p>
<h4><u>Solution</u></h4>
<p>Runs < 1 seconds in Python.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">primes = <span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">3</span>,<span style="color: #ff4500;">5</span>,<span style="color: #ff4500;">7</span>,<span style="color: #ff4500;">11</span>,<span style="color: #ff4500;">13</span>,<span style="color: #ff4500;">17</span>,<span style="color: #ff4500;">19</span>,<span style="color: #ff4500;">23</span>,<span style="color: #ff4500;">27</span>,<span style="color: #ff4500;">31</span>,<span style="color: #ff4500;">37</span>,<span style="color: #ff4500;">41</span><span style="color: black;">&#93;</span>
limit=<span style="color: #ff4500;">10</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">6</span>
<span style="color: #008000;">max</span> = <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">for</span> p <span style="color: #ff7700;font-weight:bold;">in</span> primes:
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">max</span> <span style="color: #66cc66;">*</span> p <span style="color: #66cc66;">&gt;</span> limit: <span style="color: #ff7700;font-weight:bold;">break</span>
  <span style="color: #008000;">max</span> <span style="color: #66cc66;">*</span>= p
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Answer to PE69 = &quot;</span>, <span style="color: #008000;">max</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>for 10<sup>7</sup>? 9,699,690 = 2 x 3 x 5 x 7 x 11 x 13 x 17 x 19, and following the same method for 10<sup>9</sup> would be 223,092,870</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/04/04/project-euler-problem-69-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 182 Solution</title>
		<link>http://blog.dreamshire.com/2009/03/30/project-euler-182-solution/</link>
		<comments>http://blog.dreamshire.com/2009/03/30/project-euler-182-solution/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 03:36:04 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Project Euler Solutions]]></category>
		<category><![CDATA[Solutions 150-199]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Totient]]></category>

		<guid isPermaLink="false">http://blog.dreamshire.com/?p=137</guid>
		<description><![CDATA[RSA encryption]]></description>
			<content:encoded><![CDATA[<h4><u>Problem Description</u></h4>
<p>The RSA encryption is based on the following procedure:</p>
<p>Generate two distinct primes <em>p</em> and <em>q</em>.<br />
Compute <em>n</em>=<em>pq</em> and φ=(<em>p</em>-1)(<em>q</em>-1).<br />
Find an integer <em>e</em>, 1<<em>e</em><φ, such that gcd(<em>e</em>,φ)=1.</p>
<p>A message in this system is a number in the interval [0,<em>n</em>-1].<br />
A text to be encrypted is then somehow converted to messages (numbers in the interval [0,<em>n</em>-1]).<br />
To encrypt the text, for each message, <em>m</em>, <em>c</em>=<em>m<sup>e</sup></em> mod <em>n</em> is calculated.</p>
<p>To decrypt the text, the following procedure is needed: calculate <em>d</em> such that <em>ed</em>=1 mod φ, then for each encrypted message, <em>c</em>, calculate <em>m</em>=<em>c<sup>d</sup></em> mod <em>n</em>.</p>
<p>There exist values of <em>e</em> and <em>m</em> such that <em>m<sup>e</sup></em> mod <em>n</em>=<em>m</em>.<br />
We call messages <em>m</em> for which <em>m<sup>e</sup></em> mod <em>n</em>=<em>m</em> unconcealed messages.</p>
<p>An issue when choosing <em>e</em> is that there should not be too many unconcealed messages.<br />
For instance, let <em>p</em>=19 and <em>q</em>=37.<br />
Then <em>n</em>=19*37=703 and φ=18*36=648.<br />
If we choose <em>e</em>=181, then, although gcd(181,648)=1 it turns out that all possible messages<br />
<em>m</em> (0&le;<em>m</em>&le;<em>n</em>-1) are unconcealed when calculating <em>m<sup>e</sup></em> mod <em>n</em>.<br />
For any valid choice of <em>e</em> there exist some unconcealed messages.<br />
It&#8217;s important that the number of unconcealed messages is at a minimum.</p>
<p>Choose <em>p</em>=1009 and <em>q</em>=3643.<br />
Find the sum of all values of <em>e</em>, 1&lt;<em>e</em>&lt;φ(1009,3643) and gcd(<em>e</em>,φ)=1, so that the number of unconcealed messages for this value of <em>e</em> is at a minimum.</p>
<h4><u>Analysis</u></h4>
<p>At the heart of this problem is the simple formula: [gcd(<em>e</em>-1,<em>p</em>-1)+1][gcd(<em>e</em>-1,<em>q</em>-1)+1] as outlined in many texts such as <em>Recent Advances in RCA Cryptography</em> in the chapter <em>Properties of the RSA cryptosystem</em>.  Specifically Lemma 5.1 on page 69 shows a nice proof of this equation.</p>
<p>So encoding this into a program is straightforward and simple.  Just sum all values of e where gcd(e,phi) = 1 and gcd(e-1,q-1) and gcd(e-1,p-1) are at a minimum (equal 2).</p>
<h4><u>Solution</u></h4>
<p>Runs in under 18 seconds in Perl.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$q</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$sum</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1009</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3643</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$phi</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$p</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$q</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</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: #b1b100;">my</span> <span style="color: #0000ff;">$e</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$e</span><span style="color: #339933;">&lt;</span><span style="color: #0000ff;">$phi</span><span style="color: #339933;">;</span> <span style="color: #0000ff;">$e</span><span style="color: #339933;">+=</span><span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #0000ff;">$sum</span> <span style="color: #339933;">+=</span> <span style="color: #0000ff;">$e</span> <span style="color: #b1b100;">if</span> gcd<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$e</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$phi</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&amp;&amp;</span> gcd<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$e</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$q</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">2</span> <span style="color: #339933;">&amp;&amp;</span> gcd<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$e</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$p</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</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: #000066;">print</span> <span style="color: #ff0000;">&quot;Answer to PE 182 = $sum&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<h4><u>Comments</u></h4>
<p>Runs in under 18 seconds.<br />
Christner discovered the every 4 rule. See the gcd subroutine <a href="http://blog.dreamshire.com/2009/03/26/94/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.dreamshire.com/2009/03/30/project-euler-182-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
