[Date Index]
[Thread Index]
[Author Index]
Re: Bug ???
If I understand correctly what you're trying to do, the reason the
results are different is that you're asking for, and getting different
results.
It's often a lot simpler to "think whole list at once" rather than to
think of an iterative process. And to see what's happening, form the
whole list and only then select from it the ones you want. Moreover,
let's try a smaller upper limit, say 10, instead of your 1000 so as to
avoid a huge amount of output.
First, here's the entire table of pairs:
Table[{n^2-(Floor[n^2 (2/3)])^2,n^2}, {n,1,10}]
{{1,1},{0,4},{-27,9},{-84,16},{-231,25},{-540,36},{-975,49},{-1700,64},{-2835,81},{-4256,100}}
Your "not working" code does the same thing as this:
Select[Table[{n^2-(Floor[n^2(2/3)])^2,n^2},{n,1,10}],First[#]==0&]
{{0, 4}}
But your second, "working", code does the same thing as this:
Select[Table[{n^2- (Floor[n^2 (2/3)])^2,n^2}, {n,1,10}], First[#]<1&]
{{0,4},{-27,9},{-84,16},{-231,25},{-540,36},{-975,49},{-1700,64},{-2835,81},{-4256,100}}
Quite evidently now, the two conditions (==0 vs. <1) are quite different
in their effects.
By the way, the "think whole list at once" method is faster for your
upper limit of 1000:
First@Timing[Select[Table[{n^2-(Floor[n^2 (2/3)])^2, n^2},{n,1,1000}],
First[#]<1 &];]
0.016
First@Timing[
a={};Do[k=n^2-(Floor[n^2(2/3)])^2;If[k<1,AppendTo[a,{k,n^2}]],{n,1,1000}];a;]
0.062
(These were timed with a fresh kernel.)
The timing advantage for the whole-list method by a factor of roughly 4
persists for a larger upper limit than 10000. For example, for an upper
limit of 10000, the whole-list method has timing 0.282 whereas the
iterative method has timing 1.687, roughly a factor of 5 to 6.
Of course if you really want to do the calculation for a VERY large
value of the upper limit, eventually memory space will become more
significant and you'll likely need to use the iterative method.
Artur wrote:
> Why doesn't work:
> a={};Do[k=n^2-(Floor[n^2(2/3)])^2;If[k=0,
> AppendTo[a,{k,n^2}]],{n,1,1000}];a
> but working:
> a={};Do[k=n^2-(Floor[n^2(2/3)])^2;If[k<1,
> AppendTo[a,{k,n^2}]],{n,1,1000}];a
>
> Artur Jasinski, Poland
--
Murray Eisenberg murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305
Prev by Date:
**Re: Re: Delayed Derivative Operator**
Next by Date:
**Re: Re: Simplifying Log[a] + Log[expr_] - Log[2 expr_]: Brute force necessary?**
Previous by thread:
**RE: Bug ???**
Next by thread:
**Re: Bug ???**
| |