<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Quiz: Primes in the BCL... part 2</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx</link><description>Some good comments on my BCL prime quiz &amp;#8230; but still one unanswered question (at the end)&amp;#8230; Thomas Woelfer quickly got that the place in the BCL for generating prime numbers is in Hashtable.cs and Eric Wilson does a good job explaining why&amp;#8230;</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Quiz: Primes in the BCL... part 2</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx#200403</link><pubDate>Thu, 29 Jul 2004 04:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200403</guid><dc:creator>S N</dc:creator><description>It should have been&lt;br&gt;&lt;br&gt;divisor &amp;lt;= (int)Math.Sqrt(candidate)  in the for loop condition.</description></item><item><title>re: Quiz: Primes in the BCL... part 2</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx#200450</link><pubDate>Thu, 29 Jul 2004 06:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200450</guid><dc:creator>Mike Dunn</dc:creator><description>If you pass in 9, the function will return true because divisor starts at 3, which is not less than sqrt(9), so the for loop never runs and the code drops to the return true line.</description></item><item><title>re: Quiz: Primes in the BCL... part 2</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx#200477</link><pubDate>Thu, 29 Jul 2004 07:23:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200477</guid><dc:creator>b. schwehn</dc:creator><description>same if you put in 1 which by definition isn't  prime. </description></item><item><title>re: Quiz: Primes in the BCL... part 2</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx#200481</link><pubDate>Thu, 29 Jul 2004 07:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200481</guid><dc:creator>b. schwehn</dc:creator><description>btw, using divisor &amp;lt;= (int)Math.Sqrt(candidate) still returns true for candidate = 1</description></item><item><title>re: Quiz: Primes in the BCL... part 2</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx#200506</link><pubDate>Thu, 29 Jul 2004 08:25:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200506</guid><dc:creator>damien morton</dc:creator><description> for (int divisor = 3; divisor &amp;lt; (int)Math.Sqrt (candidate); divisor+=2)&lt;br&gt;&lt;br&gt;should be:&lt;br&gt;sqrtcand = Math.Sqrt(candidate);&lt;br&gt;for (int divisor = 3; divisor &amp;lt; sqrtcand; divisor+=2)&lt;br&gt;&lt;br&gt;its a minor thing, but you dont want to be calling Math.Sqrt on every loop iteration.</description></item><item><title>re: Quiz: Primes in the BCL... part 2</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx#200517</link><pubDate>Thu, 29 Jul 2004 08:46:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200517</guid><dc:creator>Fons Sonnemans</dc:creator><description>What about:&lt;br&gt;&lt;br&gt;int sqrtcand = (int)Math.Sqrt(candidate); &lt;br&gt;for (int divisor = 3; divisor &amp;lt;= sqrtcand; divisor+=2) </description></item><item><title>re: Quiz: Primes in the BCL... part 2</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx#200530</link><pubDate>Thu, 29 Jul 2004 09:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200530</guid><dc:creator>Jonathan Perret</dc:creator><description>Indeed, every squared prime will be considered prime.&lt;br&gt;Funny how I had a slight doubt yesterday about that '&amp;lt;' but I didn't push into it as I was looking for something more subtle...&lt;br&gt;&lt;br&gt;Cheers,&lt;br&gt;--Jonathan&lt;br&gt;</description></item><item><title>re: Quiz: Primes in the BCL... part 2</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx#200547</link><pubDate>Thu, 29 Jul 2004 10:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200547</guid><dc:creator>b.schwehn</dc:creator><description>&amp;lt;quote&amp;gt;for (int divisor = 3; divisor &amp;lt; (int)Math.Sqrt (candidate); divisor+=2) &lt;br&gt;&lt;br&gt;should be: &lt;br&gt;sqrtcand = Math.Sqrt(candidate); &lt;br&gt;for (int divisor = 3; divisor &amp;lt; sqrtcand; divisor+=2)&amp;lt;/quote&amp;gt;&lt;br&gt;&lt;br&gt;I believe this is neccessary, the compiler knows  that candidate is constant and therefore sqrt(candidate) is constant as well and will do this optimisation itself</description></item><item><title>re: Quiz: Primes in the BCL... part 2</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx#200552</link><pubDate>Thu, 29 Jul 2004 10:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200552</guid><dc:creator>b.schwehn</dc:creator><description>Actually I was wrong, the nither the C# compilter nor the JIT compiler are able to optimize this themselves, presuably because they don't know whether Math.Sqrt has any intended Sideeffects besides the result. I wonder if there is a way to indicate this to the compiler..?&lt;br&gt;&lt;br&gt;&amp;lt;code&amp;gt;&lt;br&gt; if ((candidate &amp;amp; 1) != 0) {&lt;br&gt;00000000  push        ebp  &lt;br&gt;00000001  mov         ebp,esp &lt;br&gt;00000003  sub         esp,20h &lt;br&gt;00000006  push        edi  &lt;br&gt;00000007  push        esi  &lt;br&gt;00000008  push        ebx  &lt;br&gt;00000009  mov         dword ptr [ebp-4],ecx &lt;br&gt;0000000c  mov         esi,edx &lt;br&gt;0000000e  xor         ebx,ebx &lt;br&gt;00000010  xor         edi,edi &lt;br&gt;00000012  test        esi,1 &lt;br&gt;00000018  je          00000056 &lt;br&gt;						for (int divisor = 3; divisor &amp;lt; (int)Math.Sqrt (candidate); divisor+=2){&lt;br&gt;0000001a  mov         ebx,3 &lt;br&gt;0000001f  nop              &lt;br&gt;00000020  jmp         00000031 &lt;br&gt;                   if ((candidate % divisor) == 0)&lt;br&gt;00000022  mov         eax,esi &lt;br&gt;00000024  cdq              &lt;br&gt;00000025  idiv        eax,ebx &lt;br&gt;00000027  test        edx,edx &lt;br&gt;00000029  jne         0000002F &lt;br&gt;                    return false;&lt;br&gt;0000002b  xor         edi,edi &lt;br&gt;0000002d  jmp         00000063 &lt;br&gt;						for (int divisor = 3; divisor &amp;lt; (int)Math.Sqrt (candidate); divisor+=2){&lt;br&gt;0000002f  inc         ebx  &lt;br&gt;00000030  inc         ebx  &lt;br&gt;00000031  mov         dword ptr [ebp-14h],ebx &lt;br&gt;00000034  mov         dword ptr [ebp-20h],esi &lt;br&gt;00000037  fild        dword ptr [ebp-20h] &lt;br&gt;0000003a  fsqrt            &lt;br&gt;0000003c  fstp        qword ptr [ebp-1Ch] &lt;br&gt;0000003f  push        dword ptr [ebp-18h] &lt;br&gt;00000042  push        dword ptr [ebp-1Ch] &lt;br&gt;00000045  call        725CFA7C &lt;br&gt;0000004a  cmp         eax,dword ptr [ebp-14h] &lt;br&gt;0000004d  jg          00000022 &lt;br&gt;return true;&lt;br&gt;0000004f  mov         edi,1 &lt;br&gt;00000054  jmp         00000063 &lt;br&gt;return (candidate == 2);&lt;br&gt;00000056  cmp         esi,2 &lt;br&gt;00000059  sete        al   &lt;br&gt;0000005c  movzx       eax,al &lt;br&gt;0000005f  mov         edi,eax &lt;br&gt;00000061  jmp         00000063 &lt;br&gt;}&lt;br&gt;&amp;lt;/code&amp;gt;&lt;br&gt;&lt;br&gt;versus&lt;br&gt;&amp;lt;code&amp;gt;&lt;br&gt;		if ((candidate &amp;amp; 1) != 0) {&lt;br&gt;00000000  push        ebp  &lt;br&gt;00000001  mov         ebp,esp &lt;br&gt;00000003  sub         esp,20h &lt;br&gt;00000006  push        edi  &lt;br&gt;00000007  push        esi  &lt;br&gt;00000008  push        ebx  &lt;br&gt;00000009  mov         dword ptr [ebp-4],ecx &lt;br&gt;0000000c  mov         esi,edx &lt;br&gt;0000000e  mov         dword ptr [ebp-0Ch],0 &lt;br&gt;00000015  xor         ebx,ebx &lt;br&gt;00000017  xor         edi,edi &lt;br&gt;00000019  test        esi,1 &lt;br&gt;0000001f  je          0000005D &lt;br&gt;				int tt = (int)Math.Sqrt (candidate);&lt;br&gt;00000021  mov         dword ptr [ebp-20h],esi &lt;br&gt;00000024  fild        dword ptr [ebp-20h] &lt;br&gt;00000027  fsqrt            &lt;br&gt;00000029  fstp        qword ptr [ebp-1Ch] &lt;br&gt;0000002c  push        dword ptr [ebp-18h] &lt;br&gt;0000002f  push        dword ptr [ebp-1Ch] &lt;br&gt;00000032  call        725CF9FC &lt;br&gt;00000037  mov         dword ptr [ebp-0Ch],eax &lt;br&gt;				for (int divisor = 3; divisor &amp;lt;  tt;divisor+=2){&lt;br&gt;0000003a  mov         ebx,3 &lt;br&gt;0000003f  nop              &lt;br&gt;00000040  jmp         00000051 &lt;br&gt;					if ((candidate % divisor) == 0)&lt;br&gt;00000042  mov         eax,esi &lt;br&gt;00000044  cdq              &lt;br&gt;00000045  idiv        eax,ebx &lt;br&gt;00000047  test        edx,edx &lt;br&gt;00000049  jne         0000004F &lt;br&gt;						return false;&lt;br&gt;0000004b  xor         edi,edi &lt;br&gt;0000004d  jmp         0000006A &lt;br&gt;				for (int divisor = 3; divisor &amp;lt;  tt;divisor+=2){&lt;br&gt;0000004f  inc         ebx  &lt;br&gt;00000050  inc         ebx  &lt;br&gt;00000051  cmp         ebx,dword ptr [ebp-0Ch] &lt;br&gt;00000054  jl          00000042 &lt;br&gt;				return true;&lt;br&gt;00000056  mov         edi,1 &lt;br&gt;0000005b  jmp         0000006A &lt;br&gt;				return (candidate == 2);&lt;br&gt;0000005d  cmp         esi,2 &lt;br&gt;00000060  sete        al   &lt;br&gt;00000063  movzx       eax,al &lt;br&gt;00000066  mov         edi,eax &lt;br&gt;00000068  jmp         0000006A &lt;br&gt;		}&lt;br&gt;&amp;lt;/code&amp;gt;</description></item><item><title>re: Quiz: Primes in the BCL... part 2</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx#200923</link><pubDate>Thu, 29 Jul 2004 18:14:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200923</guid><dc:creator>Richard</dc:creator><description>&lt;br&gt;Use of prime number sizes for the array of hash table buckets is traditional. However the VC++ 7 &amp;amp; 7.1's hash based containers require a number of buckets that is a power of two:&lt;br&gt;&lt;br&gt;The integer constant min_buckets specifies the minimum number of buckets to maintain in the hash table. It must be a power of two and greater than zero. The value supplied by hash_compare is 8. &lt;br&gt;&lt;br&gt;(&lt;a target="_new" href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrfHashcompare.asp?frame=true"&gt;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrfHashcompare.asp?frame=true&lt;/a&gt;)&lt;br&gt;&lt;br&gt;(This is the description of the hash_compare template, which you specialise to override the defaults in hash_set and hash_map container instances.)&lt;br&gt;&lt;br&gt;It does some clever things to avoid rehashing everything as the number of buckets increases.&lt;br&gt;</description></item><item><title>re: Quiz: Primes in the BCL... part 2</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx#201356</link><pubDate>Fri, 30 Jul 2004 04:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:201356</guid><dc:creator>Brad Abrams</dc:creator><description>I was really going for the &amp;lt;= in line 151 as SN points out... Those off-by-one errors are really a kicker aren't they?  &lt;br&gt;But good to see the other comments as well... </description></item><item><title>re: Quiz: Primes in the BCL... part 2</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx#201936</link><pubDate>Fri, 30 Jul 2004 13:41:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:201936</guid><dc:creator>Eric Wilson</dc:creator><description>Is this fixed in Widbey?</description></item><item><title>re: Quiz: Primes in the BCL... part 2</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx#201946</link><pubDate>Fri, 30 Jul 2004 13:44:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:201946</guid><dc:creator>Brad Abrams</dc:creator><description>Yes, this is fixed in Whidbey</description></item><item><title>FNV hash</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx#202448</link><pubDate>Fri, 30 Jul 2004 18:51:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:202448</guid><dc:creator>Frank Hileman</dc:creator><description>Knuth's mod prime reference is so common, you would think that is the best way to do a hash table. But in fact, if you can get a better hash to start with, you don't need a mod prime, as the hash is already well distributed. Then you can use faster and better aligned power of 2 sized tables. That mod prime technique has been slow and obsolete for a long time now.&lt;br&gt;&lt;br&gt;For an example of a more modern hash algorithm, look at FNV:&lt;br&gt;&lt;a target="_new" href="http://www.isthe.com/chongo/tech/comp/fnv/"&gt;http://www.isthe.com/chongo/tech/comp/fnv/&lt;/a&gt;&lt;br&gt;&lt;br&gt;That algorithm has already proven itself, speeding up a number of OS hashtables that used the obsolete mod prime method.</description></item><item><title>re: Quiz: Primes in the BCL... part 2</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx#207223</link><pubDate>Tue, 03 Aug 2004 18:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:207223</guid><dc:creator>damien morton</dc:creator><description>FNV is for hashing strings, and is only indirectly related to hashtables and their hash functions.</description></item><item><title>Quiz Sharp. Non solo Adrian!</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx#221549</link><pubDate>Fri, 27 Aug 2004 19:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:221549</guid><dc:creator>Web Log di Gianluca Carucci</dc:creator><description /></item><item><title>Bug in the Hashtable Data Structure in .NET 1.x</title><link>http://blogs.msdn.com/brada/archive/2004/07/28/200365.aspx#406865</link><pubDate>Sun, 10 Apr 2005 04:28:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:406865</guid><dc:creator>Scott on Writing</dc:creator><description /></item></channel></rss>