Comments on: Fast way to test whether a number is a square http://www.johndcook.com/blog/2008/11/17/fast-way-to-test-whether-a-number-is-a-square/ The blog of John D. Cook Sat, 11 Feb 2012 22:42:11 -0500 http://wordpress.org/?v=2.8.4 hourly 1 By: Řrřola http://www.johndcook.com/blog/2008/11/17/fast-way-to-test-whether-a-number-is-a-square/comment-page-1/#comment-33286 Řrřola Sat, 20 Feb 2010 17:55:28 +0000 http://www.johndcook.com/blog/?p=893#comment-33286 I think you’d speed up the code considerably if you used the <i>table-in-a-constant</i> trick: <code>if ((1<<0 | 1<<1 | 1<<4 | 1<> (n & 0xF) & 1) do_the_sqrt;</code> or, if you have 64-bit integers, <code>if (0x2001002010213ULL >> (n & 0x3F) & 1) do_the_sqrt;</code> I think you’d speed up the code considerably if you used the table-in-a-constant trick:

if ((1<<0 | 1<<1 | 1<<4 | 1<> (n & 0xF) & 1) do_the_sqrt;

or, if you have 64-bit integers,

if (0x2001002010213ULL >> (n & 0x3F) & 1) do_the_sqrt;

]]>
By: How to speed up retrieval without any index? http://www.johndcook.com/blog/2008/11/17/fast-way-to-test-whether-a-number-is-a-square/comment-page-1/#comment-10019 How to speed up retrieval without any index? Fri, 21 Nov 2008 03:06:44 +0000 http://www.johndcook.com/blog/?p=893#comment-10019 [...] Cook gives us a nice recipe to quickly find all squares in a set of integers. For example, given 3, 4, 9, 15, you want your algorithm to identify 4 and 9 as [...] [...] Cook gives us a nice recipe to quickly find all squares in a set of integers. For example, given 3, 4, 9, 15, you want your algorithm to identify 4 and 9 as [...]

]]>
By: Daniel Lemire http://www.johndcook.com/blog/2008/11/17/fast-way-to-test-whether-a-number-is-a-square/comment-page-1/#comment-9922 Daniel Lemire Wed, 19 Nov 2008 02:19:45 +0000 http://www.johndcook.com/blog/?p=893#comment-9922 Useless but... oh!... so beautiful. Thank you. Useless but… oh!… so beautiful. Thank you.

]]>
By: Michael Lugo http://www.johndcook.com/blog/2008/11/17/fast-way-to-test-whether-a-number-is-a-square/comment-page-1/#comment-9898 Michael Lugo Tue, 18 Nov 2008 13:34:27 +0000 http://www.johndcook.com/blog/?p=893#comment-9898 The <a href="http://www.research.att.com/~njas/sequences/A023105" rel="nofollow">encyclopedia of integer sequences</a> gives the simple formula g(n) = [(2^n + 10)/6], where [x] is the greatest integer less than or equal to x. (g(6) is in fact 12; the possible residues are 0, 1, 4, 9, 16, 17, 25, 33, 36, 41, 49, 57.) So {g(n)/2^n} is a sequence which decreases towards a limit of 1/6, although it never gets there. The encyclopedia of integer sequences gives the simple formula g(n) = [(2^n + 10)/6], where [x] is the greatest integer less than or equal to x. (g(6) is in fact 12; the possible residues are 0, 1, 4, 9, 16, 17, 25, 33, 36, 41, 49, 57.) So {g(n)/2^n} is a sequence which decreases towards a limit of 1/6, although it never gets there.

]]>