templeos-info/public/Wb/Demo/MagicPairs.HC.HTML

407 lines
31 KiB
HTML
Executable File

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="TempleOS V5.03">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/style/templeos.css">
<script src="/script/templeos.js"></script>
<style type="text/css">
.cF0{color:#000000;background-color:#ffffff;}
.cF1{color:#0000aa;background-color:#ffffff;}
.cF2{color:#00aa00;background-color:#ffffff;}
.cF3{color:#00aaaa;background-color:#ffffff;}
.cF4{color:#aa0000;background-color:#ffffff;}
.cF5{color:#aa00aa;background-color:#ffffff;}
.cF6{color:#aa5500;background-color:#ffffff;}
.cF7{color:#aaaaaa;background-color:#ffffff;}
.cF8{color:#555555;background-color:#ffffff;}
.cF9{color:#5555ff;background-color:#ffffff;}
.cFA{color:#55ff55;background-color:#ffffff;}
.cFB{color:#55ffff;background-color:#ffffff;}
.cFC{color:#ff5555;background-color:#ffffff;}
.cFD{color:#ff55ff;background-color:#ffffff;}
.cFE{color:#ffff55;background-color:#ffffff;}
.cFF{color:#ffffff;background-color:#ffffff;}
</style>
</head>
<body>
<pre id="content">
<a name="l1"></a><span class=cF2>/*The magic pairs problem:</span><span class=cF0>
<a name="l2"></a>
<a name="l3"></a></span><span class=cF2>Let SumFact(n) be the sum of factors</span><span class=cF0>
<a name="l4"></a></span><span class=cF2>of n.</span><span class=cF0>
<a name="l5"></a>
<a name="l6"></a></span><span class=cF2>Find all n1,n2 in a range such that</span><span class=cF0>
<a name="l7"></a>
<a name="l8"></a></span><span class=cF2>SumFact(n1)-n1-1==n2 and</span><span class=cF0>
<a name="l9"></a></span><span class=cF2>SumFact(n2)-n2-1==n1</span><span class=cF0>
<a name="l10"></a>
<a name="l11"></a></span><span class=cF2>-----------------------------------------------------</span><span class=cF0>
<a name="l12"></a></span><span class=cF2>To find SumFact(k), start with prime factorization:</span><span class=cF0>
<a name="l13"></a>
<a name="l14"></a></span><span class=cF2>k=(p1^n1)(p2^n2) ... (pN^nN)</span><span class=cF0>
<a name="l15"></a>
<a name="l16"></a></span><span class=cF2>THEN,</span><span class=cF0>
<a name="l17"></a>
<a name="l18"></a></span><span class=cF2>SumFact(k)=(1+p1+p1^2...p1^n1)*(1+p2+p2^2...p2^n2)*</span><span class=cF0>
<a name="l19"></a></span><span class=cF2>(1+pN+pN^2...pN^nN)</span><span class=cF0>
<a name="l20"></a>
<a name="l21"></a></span><span class=cF2>PROOF:</span><span class=cF0>
<a name="l22"></a>
<a name="l23"></a></span><span class=cF2>Do a couple examples -- it's obvious:</span><span class=cF0>
<a name="l24"></a>
<a name="l25"></a></span><span class=cF2>48=2^4*3</span><span class=cF0>
<a name="l26"></a>
<a name="l27"></a></span><span class=cF2>SumFact(48)=(1+2+4+8+16)*(1+3)=1+2+4+8+16+3+6+12+24+48</span><span class=cF0>
<a name="l28"></a>
<a name="l29"></a></span><span class=cF2>75=3*5^2</span><span class=cF0>
<a name="l30"></a>
<a name="l31"></a></span><span class=cF2>SumFact(75)=(1+3)*(1+5+25) =1+5+25+3+15+75</span><span class=cF0>
<a name="l32"></a>
<a name="l33"></a></span><span class=cF2>Corollary:</span><span class=cF0>
<a name="l34"></a>
<a name="l35"></a></span><span class=cF2>SumFact(k)=SumFact(p1^n1)*SumFact(p2^n2)*...*SumFact(pN^nN)</span><span class=cF0>
<a name="l36"></a>
<a name="l37"></a></span><span class=cF2>*/</span><span class=cF0>
<a name="l38"></a>
<a name="l39"></a></span><span class=cF2>//Primes are needed to sqrt(N). Therefore, we can use U32.</span><span class=cF0>
<a name="l40"></a></span><span class=cF1>class</span><span class=cF0> PowPrime
<a name="l41"></a>{
<a name="l42"></a> </span><span class=cF9>I64</span><span class=cF0> n;
<a name="l43"></a> </span><span class=cF9>I64</span><span class=cF0> sumfact; </span><span class=cF2>//Sumfacts for powers of primes are needed beyond sqrt(N)</span><span class=cF0>
<a name="l44"></a>};
<a name="l45"></a>
<a name="l46"></a></span><span class=cF1>class</span><span class=cF0> Prime
<a name="l47"></a>{
<a name="l48"></a> </span><span class=cF9>U32</span><span class=cF0> prime,pow_cnt;
<a name="l49"></a> PowPrime *pp;
<a name="l50"></a>};
<a name="l51"></a>
<a name="l52"></a></span><span class=cF9>I64</span><span class=cF0> *PrimesNew(</span><span class=cF9>I64</span><span class=cF0> N,</span><span class=cF9>I64</span><span class=cF0> *_sqrt_primes,</span><span class=cF9>I64</span><span class=cF0> *_cbrt_primes)
<a name="l53"></a>{
<a name="l54"></a> </span><span class=cF9>I64</span><span class=cF0> i,j,sqrt=</span><span class=cF5>Ceil</span><span class=cF0>(</span><span class=cF5>Sqrt</span><span class=cF7>(</span><span class=cF0>N</span><span class=cF7>)</span><span class=cF0>),cbrt=</span><span class=cF5>Ceil</span><span class=cF0>(N`</span><span class=cF7>(</span><span class=cF0>1/3.0</span><span class=cF7>)</span><span class=cF0>),sqrt_sqrt=</span><span class=cF5>Ceil</span><span class=cF0>(</span><span class=cF5>Sqrt</span><span class=cF7>(</span><span class=cF0>sqrt</span><span class=cF7>)</span><span class=cF0>),
<a name="l55"></a> sqrt_primes=0,cbrt_primes=0;
<a name="l56"></a> </span><span class=cF1>U8</span><span class=cF0> *s=</span><span class=cF5>CAlloc</span><span class=cF0>(</span><span class=cF7>(</span><span class=cF0>sqrt+1+7</span><span class=cF7>)</span><span class=cF0>/8);
<a name="l57"></a> Prime *primes,*p;
<a name="l58"></a>
<a name="l59"></a> </span><span class=cF1>for</span><span class=cF0> (i=2;i&lt;=sqrt_sqrt;i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l60"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>s,i</span><span class=cF7>)</span><span class=cF0>) {
<a name="l61"></a> j=i*2;
<a name="l62"></a> </span><span class=cF1>while</span><span class=cF0> (j&lt;=sqrt) </span><span class=cF7>{</span><span class=cF0>
<a name="l63"></a> </span><span class=cF5>Bts</span><span class=cF0>(s,j);
<a name="l64"></a> j+=i;
<a name="l65"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l66"></a> }
<a name="l67"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l68"></a> </span><span class=cF1>for</span><span class=cF0> (i=2;i&lt;=sqrt;i++)
<a name="l69"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>s,i</span><span class=cF7>)</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l70"></a> sqrt_primes++; </span><span class=cF2>//Count primes</span><span class=cF0>
<a name="l71"></a> </span><span class=cF1>if</span><span class=cF0> (i&lt;=cbrt)
<a name="l72"></a> cbrt_primes++;
<a name="l73"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l74"></a>
<a name="l75"></a> p=primes=</span><span class=cF5>CAlloc</span><span class=cF0>(sqrt_primes*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>Prime</span><span class=cF7>)</span><span class=cF0>);
<a name="l76"></a> </span><span class=cF1>for</span><span class=cF0> (i=2;i&lt;=sqrt;i++)
<a name="l77"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>s,i</span><span class=cF7>)</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l78"></a> p-&gt;prime=i;
<a name="l79"></a> p++;
<a name="l80"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l81"></a> </span><span class=cF5>Free</span><span class=cF0>(s);
<a name="l82"></a>
<a name="l83"></a> *_sqrt_primes=sqrt_primes;
<a name="l84"></a> *_cbrt_primes=cbrt_primes;
<a name="l85"></a> </span><span class=cF1>return</span><span class=cF0> primes;
<a name="l86"></a>}
<a name="l87"></a>
<a name="l88"></a>PowPrime *PowPrimesNew(</span><span class=cF9>I64</span><span class=cF0> N,</span><span class=cF9>I64</span><span class=cF0> sqrt_primes,Prime *primes,</span><span class=cF9>I64</span><span class=cF0> *_num_powprimes)
<a name="l89"></a>{
<a name="l90"></a> </span><span class=cF9>I64</span><span class=cF0> i,j,k,sf,num_powprimes=0;
<a name="l91"></a> Prime *p;
<a name="l92"></a> PowPrime *powprimes,*pp;
<a name="l93"></a>
<a name="l94"></a> p=primes;
<a name="l95"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;sqrt_primes;i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l96"></a> num_powprimes+=</span><span class=cF5>Floor</span><span class=cF0>(</span><span class=cF5>Ln</span><span class=cF7>(</span><span class=cF0>N</span><span class=cF7>)</span><span class=cF0>/</span><span class=cF5>Ln</span><span class=cF7>(</span><span class=cF0>p-&gt;prime</span><span class=cF7>)</span><span class=cF0>);
<a name="l97"></a> p++;
<a name="l98"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l99"></a>
<a name="l100"></a> p=primes;
<a name="l101"></a> pp=powprimes=</span><span class=cF5>MAlloc</span><span class=cF0>(num_powprimes*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>PowPrime</span><span class=cF7>)</span><span class=cF0>);
<a name="l102"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;sqrt_primes;i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l103"></a> p-&gt;pp=pp;
<a name="l104"></a> j=p-&gt;prime;
<a name="l105"></a> k=1;
<a name="l106"></a> sf=1;
<a name="l107"></a> </span><span class=cF1>while</span><span class=cF0> (j&lt;N) {
<a name="l108"></a> sf+=j;
<a name="l109"></a> pp-&gt;n=j;
<a name="l110"></a> pp-&gt;sumfact=sf;
<a name="l111"></a> j*=p-&gt;prime;
<a name="l112"></a> pp++;
<a name="l113"></a> p-&gt;pow_cnt++;
<a name="l114"></a> }
<a name="l115"></a> p++;
<a name="l116"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l117"></a> *_num_powprimes=num_powprimes;
<a name="l118"></a> </span><span class=cF1>return</span><span class=cF0> powprimes;
<a name="l119"></a>}
<a name="l120"></a>
<a name="l121"></a></span><span class=cF9>I64</span><span class=cF0> SumFact(</span><span class=cF9>I64</span><span class=cF0> n,</span><span class=cF9>I64</span><span class=cF0> sqrt_primes,Prime *p)
<a name="l122"></a>{
<a name="l123"></a> </span><span class=cF9>I64</span><span class=cF0> i,k,sf=1;
<a name="l124"></a> PowPrime *pp;
<a name="l125"></a> </span><span class=cF1>if</span><span class=cF0> (n&lt;2)
<a name="l126"></a> </span><span class=cF1>return</span><span class=cF0> 1;
<a name="l127"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;sqrt_primes;i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l128"></a> k=0;
<a name="l129"></a> </span><span class=cF1>while</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>n%p-&gt;prime</span><span class=cF7>)</span><span class=cF0>) {
<a name="l130"></a> n/=p-&gt;prime;
<a name="l131"></a> k++;
<a name="l132"></a> }
<a name="l133"></a> </span><span class=cF1>if</span><span class=cF0> (k) {
<a name="l134"></a> pp=p-&gt;pp+(k-1);
<a name="l135"></a> sf*=pp-&gt;sumfact;
<a name="l136"></a> </span><span class=cF1>if</span><span class=cF0> (n==1)
<a name="l137"></a> </span><span class=cF1>return</span><span class=cF0> sf;
<a name="l138"></a> }
<a name="l139"></a> p++;
<a name="l140"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l141"></a> </span><span class=cF1>return</span><span class=cF0> sf*(1+n); </span><span class=cF2>//Prime</span><span class=cF0>
<a name="l142"></a>}
<a name="l143"></a>
<a name="l144"></a></span><span class=cF1>Bool</span><span class=cF0> TestSumFact(</span><span class=cF9>I64</span><span class=cF0> n,</span><span class=cF9>I64</span><span class=cF0> target_sf,</span><span class=cF9>I64</span><span class=cF0> sqrt_primes,</span><span class=cF9>I64</span><span class=cF0> cbrt_primes,Prime *p)
<a name="l145"></a>{
<a name="l146"></a> </span><span class=cF9>I64</span><span class=cF0> i=0,k,b,x1,x2;
<a name="l147"></a> PowPrime *pp;
<a name="l148"></a> </span><span class=cF1>F64</span><span class=cF0> disc;
<a name="l149"></a> </span><span class=cF1>if</span><span class=cF0> (n&lt;2)
<a name="l150"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l151"></a> </span><span class=cF1>while</span><span class=cF0> (i++&lt;cbrt_primes) </span><span class=cF7>{</span><span class=cF0>
<a name="l152"></a> k=0;
<a name="l153"></a> </span><span class=cF1>while</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>n%p-&gt;prime</span><span class=cF7>)</span><span class=cF0>) {
<a name="l154"></a> n/=p-&gt;prime;
<a name="l155"></a> k++;
<a name="l156"></a> }
<a name="l157"></a> </span><span class=cF1>if</span><span class=cF0> (k) {
<a name="l158"></a> pp=p-&gt;pp+(k-1);
<a name="l159"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>ModU64</span><span class=cF7>(</span><span class=cF0>&amp;target_sf,pp-&gt;sumfact</span><span class=cF7>)</span><span class=cF0>)
<a name="l160"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l161"></a> </span><span class=cF1>if</span><span class=cF0> (n==1) </span><span class=cF7>{</span><span class=cF0>
<a name="l162"></a> </span><span class=cF1>if</span><span class=cF0> (target_sf==1)
<a name="l163"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l164"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l165"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l166"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l167"></a> }
<a name="l168"></a> p++;
<a name="l169"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l170"></a></span><span class=cF2>/* At this point we have three possible cases to test</span><span class=cF0>
<a name="l171"></a></span><span class=cF2>1)n==p1</span><span class=cF0> </span><span class=cF2>-&gt;sf==(1+p1)</span><span class=cF0> </span><span class=cF2> ?</span><span class=cF0>
<a name="l172"></a></span><span class=cF2>2)n==p1*p1</span><span class=cF0> </span><span class=cF2>-&gt;sf==(1+p1+p1^2) ?</span><span class=cF0>
<a name="l173"></a></span><span class=cF2>3)n==p1*p2</span><span class=cF0> </span><span class=cF2>-&gt;sf==(p1+1)*(p2+1) ?</span><span class=cF0>
<a name="l174"></a>
<a name="l175"></a></span><span class=cF2>*/</span><span class=cF0>
<a name="l176"></a> </span><span class=cF1>if</span><span class=cF0> (1+n==target_sf) </span><span class=cF7>{</span><span class=cF0>
<a name="l177"></a> </span><span class=cF1>while</span><span class=cF0> (i++&lt;sqrt_primes) {
<a name="l178"></a> k=0;
<a name="l179"></a> </span><span class=cF1>while</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>n%p-&gt;prime</span><span class=cF7>)</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l180"></a> n/=p-&gt;prime;
<a name="l181"></a> k++;
<a name="l182"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l183"></a> </span><span class=cF1>if</span><span class=cF0> (k) </span><span class=cF7>{</span><span class=cF0>
<a name="l184"></a> pp=p-&gt;pp+(k-1);
<a name="l185"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>ModU64</span><span class=cF7>(</span><span class=cF0>&amp;target_sf,pp-&gt;sumfact</span><span class=cF7>)</span><span class=cF0>)
<a name="l186"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l187"></a> </span><span class=cF1>if</span><span class=cF0> (n==1) {
<a name="l188"></a> </span><span class=cF1>if</span><span class=cF0> (target_sf==1)
<a name="l189"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l190"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l191"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l192"></a> }
<a name="l193"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l194"></a> p++;
<a name="l195"></a> }
<a name="l196"></a> </span><span class=cF1>if</span><span class=cF0> (1+n==target_sf)
<a name="l197"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l198"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l199"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l200"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l201"></a>
<a name="l202"></a> k=</span><span class=cF5>Sqrt</span><span class=cF0>(n);
<a name="l203"></a> </span><span class=cF1>if</span><span class=cF0> (k*k==n) </span><span class=cF7>{</span><span class=cF0>
<a name="l204"></a> </span><span class=cF1>if</span><span class=cF0> (1+k+n==target_sf)
<a name="l205"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l206"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l207"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l208"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l209"></a></span><span class=cF2>// n==p1*p2 -&gt; sf==(p1+1)*(p2+1) ? where p1!=1 &amp;&amp; p2!=1</span><span class=cF0>
<a name="l210"></a> </span><span class=cF2>// if p1==1 || p2==1, it is FALSE because we checked a single prime above.</span><span class=cF0>
<a name="l211"></a>
<a name="l212"></a> </span><span class=cF2>// sf==(p1+1)*(n/p1+1)</span><span class=cF0>
<a name="l213"></a> </span><span class=cF2>// sf==n+p1+n/p1+1</span><span class=cF0>
<a name="l214"></a> </span><span class=cF2>// sf*p1==n*p1+p1^2+n+p1</span><span class=cF0>
<a name="l215"></a> </span><span class=cF2>// p1^2+(n+1-sf)*p1+n=0</span><span class=cF0>
<a name="l216"></a> </span><span class=cF2>// x=(-b+/-sqrt(b^2-4ac))/2a</span><span class=cF0>
<a name="l217"></a> </span><span class=cF2>// a=1</span><span class=cF0>
<a name="l218"></a> </span><span class=cF2>// x=(-b+/-sqrt(b^2-4c))/2</span><span class=cF0>
<a name="l219"></a> </span><span class=cF2>// b=n+1-sf;c=n</span><span class=cF0>
<a name="l220"></a> b=n+1-target_sf;
<a name="l221"></a></span><span class=cF2>// x=(-b+/-sqrt(b^2-4n))/2</span><span class=cF0>
<a name="l222"></a> disc=b*b-4*n;
<a name="l223"></a> </span><span class=cF1>if</span><span class=cF0> (disc&lt;0)
<a name="l224"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l225"></a> x1=(-b-</span><span class=cF5>Sqrt</span><span class=cF7>(</span><span class=cF0>disc</span><span class=cF7>)</span><span class=cF0>)/2;
<a name="l226"></a> </span><span class=cF1>if</span><span class=cF0> (x1&lt;=1)
<a name="l227"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l228"></a> x2=n/x1;
<a name="l229"></a> </span><span class=cF1>if</span><span class=cF0> (x2&gt;1 &amp;&amp; x1*x2==n)
<a name="l230"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l231"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l232"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l233"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l234"></a>}
<a name="l235"></a>
<a name="l236"></a></span><span class=cF1>U0</span><span class=cF0> PutFactors(</span><span class=cF9>I64</span><span class=cF0> n) </span><span class=cF2>//For debugging</span><span class=cF0>
<a name="l237"></a>{
<a name="l238"></a> </span><span class=cF9>I64</span><span class=cF0> i,k,sqrt=</span><span class=cF5>Ceil</span><span class=cF0>(</span><span class=cF5>Sqrt</span><span class=cF7>(</span><span class=cF0>n</span><span class=cF7>)</span><span class=cF0>);
<a name="l239"></a> </span><span class=cF1>for</span><span class=cF0> (i=2;i&lt;=sqrt;i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l240"></a> k=0;
<a name="l241"></a> </span><span class=cF1>while</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>n%i</span><span class=cF7>)</span><span class=cF0>) {
<a name="l242"></a> k++;
<a name="l243"></a> n/=i;
<a name="l244"></a> }
<a name="l245"></a> </span><span class=cF1>if</span><span class=cF0> (k) {
<a name="l246"></a> </span><span class=cF6>&quot;%d&quot;</span><span class=cF0>,i;
<a name="l247"></a> </span><span class=cF1>if</span><span class=cF0> (k&gt;1)
<a name="l248"></a> </span><span class=cF6>&quot;^%d&quot;</span><span class=cF0>,k;
<a name="l249"></a> </span><span class=cF6>''</span><span class=cF0> </span><span class=cF3>CH_SPACE</span><span class=cF0>;
<a name="l250"></a> }
<a name="l251"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l252"></a> </span><span class=cF1>if</span><span class=cF0> (n!=1)
<a name="l253"></a> </span><span class=cF6>&quot;%d &quot;</span><span class=cF0>,n;
<a name="l254"></a>}
<a name="l255"></a>
<a name="l256"></a></span><span class=cF1>class</span><span class=cF0> RangeJob
<a name="l257"></a>{
<a name="l258"></a> </span><span class=cF9>CDoc</span><span class=cF0> *doc;
<a name="l259"></a> </span><span class=cF9>I64</span><span class=cF0> num,lo,hi,N,sqrt_primes,cbrt_primes;
<a name="l260"></a> Prime *primes;
<a name="l261"></a> </span><span class=cF9>CJob</span><span class=cF0> *cmd;
<a name="l262"></a>} rj[</span><span class=cFB>mp_cnt</span><span class=cF0>];
<a name="l263"></a>
<a name="l264"></a></span><span class=cF9>I64</span><span class=cF0> TestCoreSubRange(RangeJob *r)
<a name="l265"></a>{
<a name="l266"></a> </span><span class=cF9>I64</span><span class=cF0> i,j,m,n,n2,sf,res=0,range=r-&gt;hi-r-&gt;lo,
<a name="l267"></a> *sumfacts=</span><span class=cF5>MAlloc</span><span class=cF0>(range*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>I64</span><span class=cF7>)</span><span class=cF0>),
<a name="l268"></a> *residue =</span><span class=cF5>MAlloc</span><span class=cF0>(range*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>I64</span><span class=cF7>)</span><span class=cF0>);
<a name="l269"></a> </span><span class=cF9>U16</span><span class=cF0> *pow_cnt =</span><span class=cF5>MAlloc</span><span class=cF0>(range*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>U16</span><span class=cF7>)</span><span class=cF0>);
<a name="l270"></a> Prime *p=r-&gt;primes;
<a name="l271"></a> PowPrime *pp;
<a name="l272"></a> </span><span class=cF5>MemSetI64</span><span class=cF0>(sumfacts,1,range);
<a name="l273"></a> </span><span class=cF1>for</span><span class=cF0> (n=r-&gt;lo;n&lt;r-&gt;hi;n++)
<a name="l274"></a> residue[n-r-&gt;lo]=n;
<a name="l275"></a> </span><span class=cF1>for</span><span class=cF0> (j=0;j&lt;r-&gt;sqrt_primes;j++) </span><span class=cF7>{</span><span class=cF0>
<a name="l276"></a> </span><span class=cF5>MemSet</span><span class=cF0>(pow_cnt,0,range*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>U16</span><span class=cF7>)</span><span class=cF0>);
<a name="l277"></a> m=1;
<a name="l278"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;p-&gt;pow_cnt;i++) {
<a name="l279"></a> m*=p-&gt;prime;
<a name="l280"></a> n=m-r-&gt;lo%m;
<a name="l281"></a> </span><span class=cF1>while</span><span class=cF0> (n&lt;range) </span><span class=cF7>{</span><span class=cF0>
<a name="l282"></a> pow_cnt[n]++;
<a name="l283"></a> n+=m;
<a name="l284"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l285"></a> }
<a name="l286"></a> </span><span class=cF1>for</span><span class=cF0> (n=0;n&lt;range;n++)
<a name="l287"></a> </span><span class=cF1>if</span><span class=cF0> (i=pow_cnt[n]) {
<a name="l288"></a> pp=&amp;p-&gt;pp[i-1];
<a name="l289"></a> sumfacts[n]*=pp-&gt;sumfact;
<a name="l290"></a> residue [n]/=pp-&gt;n;
<a name="l291"></a> }
<a name="l292"></a> p++;
<a name="l293"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l294"></a>
<a name="l295"></a> </span><span class=cF1>for</span><span class=cF0> (n=0;n&lt;range;n++)
<a name="l296"></a> </span><span class=cF1>if</span><span class=cF0> (residue[n]!=1)
<a name="l297"></a> sumfacts[n]*=1+residue[n];
<a name="l298"></a>
<a name="l299"></a> </span><span class=cF1>for</span><span class=cF0> (n=r-&gt;lo;n&lt;r-&gt;hi;n++) </span><span class=cF7>{</span><span class=cF0>
<a name="l300"></a> sf=sumfacts[n-r-&gt;lo];
<a name="l301"></a> n2=sf-n-1;
<a name="l302"></a> </span><span class=cF1>if</span><span class=cF0> (n&lt;n2&lt;r-&gt;N) {
<a name="l303"></a> </span><span class=cF1>if</span><span class=cF0> (r-&gt;lo&lt;=n2&lt;r-&gt;hi &amp;&amp; sumfacts[n2-r-&gt;lo]-n2-1==n ||
<a name="l304"></a> TestSumFact</span><span class=cF7>(</span><span class=cF0>n2,sf,r-&gt;sqrt_primes,r-&gt;cbrt_primes,r-&gt;primes</span><span class=cF7>)</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l305"></a> </span><span class=cF5>DocPrint</span><span class=cF0>(r-&gt;doc,</span><span class=cF6>&quot;%u:%u\n&quot;</span><span class=cF0>,n,sf-n-1);
<a name="l306"></a> res++;
<a name="l307"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l308"></a> }
<a name="l309"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l310"></a> </span><span class=cF5>Free</span><span class=cF0>(pow_cnt);
<a name="l311"></a> </span><span class=cF5>Free</span><span class=cF0>(residue);
<a name="l312"></a> </span><span class=cF5>Free</span><span class=cF0>(sumfacts);
<a name="l313"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l314"></a>}
<a name="l315"></a>
<a name="l316"></a>#</span><span class=cF1>define</span><span class=cF0> CORE_SUB_RANGE 0x1000
<a name="l317"></a>
<a name="l318"></a></span><span class=cF9>I64</span><span class=cF0> TestCoreRange(RangeJob *r)
<a name="l319"></a>{
<a name="l320"></a> </span><span class=cF9>I64</span><span class=cF0> i,n,res=0;
<a name="l321"></a> RangeJob rj;
<a name="l322"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(&amp;rj,r,</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>RangeJob</span><span class=cF7>)</span><span class=cF0>);
<a name="l323"></a> </span><span class=cF1>for</span><span class=cF0> (i=r-&gt;lo;i&lt;r-&gt;hi;i+=CORE_SUB_RANGE) </span><span class=cF7>{</span><span class=cF0>
<a name="l324"></a> rj.lo=i;
<a name="l325"></a> rj.hi=i+CORE_SUB_RANGE;
<a name="l326"></a> </span><span class=cF1>if</span><span class=cF0> (rj.hi&gt;r-&gt;hi)
<a name="l327"></a> rj.hi=r-&gt;hi;
<a name="l328"></a> res+=TestCoreSubRange(&amp;rj);
<a name="l329"></a>
<a name="l330"></a> n=rj.hi-rj.lo;
<a name="l331"></a> </span><span class=cF1>lock</span><span class=cF0> {</span><span class=cFB>progress1</span><span class=cF0>+=n;}
<a name="l332"></a>
<a name="l333"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l334"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l335"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l336"></a>}
<a name="l337"></a>
<a name="l338"></a></span><span class=cF9>I64</span><span class=cF0> MagicPairs(</span><span class=cF9>I64</span><span class=cF0> N)
<a name="l339"></a>{
<a name="l340"></a> </span><span class=cF1>F64</span><span class=cF0> t0=</span><span class=cF5>tS</span><span class=cF0>;
<a name="l341"></a> </span><span class=cF9>I64</span><span class=cF0> res=0;
<a name="l342"></a> </span><span class=cF9>I64</span><span class=cF0> sqrt_primes,cbrt_primes,num_powprimes,
<a name="l343"></a> i,k,n=(N-1)/</span><span class=cFB>mp_cnt</span><span class=cF0>+1;
<a name="l344"></a> Prime *primes=PrimesNew(N,&amp;sqrt_primes,&amp;cbrt_primes);
<a name="l345"></a> PowPrime *powprimes=PowPrimesNew(N,sqrt_primes,primes,&amp;num_powprimes);
<a name="l346"></a>
<a name="l347"></a> </span><span class=cF6>&quot;N:%u SqrtPrimes:%u CbrtPrimes:%u PowersOfPrimes:%u\n&quot;</span><span class=cF0>,
<a name="l348"></a> N,sqrt_primes,cbrt_primes,num_powprimes;
<a name="l349"></a> </span><span class=cFB>progress1</span><span class=cF0>=0;
<a name="l350"></a> *</span><span class=cFB>progress1_desc</span><span class=cF0>=0;
<a name="l351"></a> </span><span class=cFB>progress1_max</span><span class=cF0>=N;
<a name="l352"></a> k=2;
<a name="l353"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;</span><span class=cFB>mp_cnt</span><span class=cF0>;i++) </span><span class=cF7>{</span><span class=cF0>
<a name="l354"></a> rj[i].doc=</span><span class=cF5>DocPut</span><span class=cF0>;
<a name="l355"></a> rj[i].num=i;
<a name="l356"></a> rj[i].lo=k;
<a name="l357"></a> k+=n;
<a name="l358"></a> </span><span class=cF1>if</span><span class=cF0> (k&gt;N) k=N;
<a name="l359"></a> rj[i].hi=k;
<a name="l360"></a> rj[i].N=N;
<a name="l361"></a> rj[i].sqrt_primes=sqrt_primes;
<a name="l362"></a> rj[i].cbrt_primes=cbrt_primes;
<a name="l363"></a> rj[i].primes=primes;
<a name="l364"></a> rj[i].cmd=</span><span class=cF5>JobQue</span><span class=cF0>(&amp;TestCoreRange,&amp;rj[i],</span><span class=cFB>mp_cnt</span><span class=cF0>-1-i,0);
<a name="l365"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l366"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;</span><span class=cFB>mp_cnt</span><span class=cF0>;i++)
<a name="l367"></a> res+=</span><span class=cF5>JobResGet</span><span class=cF0>(rj[i].cmd);
<a name="l368"></a> </span><span class=cF5>Free</span><span class=cF0>(powprimes);
<a name="l369"></a> </span><span class=cF5>Free</span><span class=cF0>(primes);
<a name="l370"></a> </span><span class=cF6>&quot;Found:%u Time:%9.4f\n&quot;</span><span class=cF0>,res,</span><span class=cF5>tS</span><span class=cF0>-t0;
<a name="l371"></a> </span><span class=cFB>progress1</span><span class=cF0>=</span><span class=cFB>progress1_max</span><span class=cF0>=0;
<a name="l372"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l373"></a>}
<a name="l374"></a>
<a name="l375"></a>MagicPairs(1000000);
</span></pre></body>
</html>