Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.maple > #232

Re: Problem with Seq. Delayed Evaluation?

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From Joe Riel <joer@san.rr.com>
Newsgroups comp.soft-sys.math.maple
Subject Re: Problem with Seq. Delayed Evaluation?
Date Wed, 14 Sep 2011 08:20:09 -0700
Organization A noiseless patient Spider
Lines 94
Message-ID <874o0f2lsm.fsf@san.rr.com> (permalink)
References <Xns9F5FB13FEA264tomdeanspeakeasyorg@216.196.97.142> <878vpr3lak.fsf@san.rr.com> <Xns9F5FE447B4D92tomdeanspeakeasyorg@216.196.97.142>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Injection-Info mx04.eternal-september.org; posting-host="7daQ3AF9ALJlnU9jGWSG5Q"; logging-data="20371"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+vjHGz/N7XvZchhkwXkZbA"
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux)
Cancel-Lock sha1:lIKgcrQjoJht0HJzn3brBtnxmug= sha1:tLM3RM//QK015mn0lXV7HKw6Kvo=
Xref x330-a1.tempe.blueboxinc.net comp.soft-sys.math.maple:232

Show key headers only | View raw


Thomas Dean <tomdean@speakeasy.org> writes:

> Joe Riel <joer@san.rr.com> wrote in news:878vpr3lak.fsf@san.rr.com:
>
>> seq(add(t,t=factorset(n)),n=1..1000);
>> 
>
> Thanks for the reply.
>
> I think I must have done something 'not bright' (stupid?).  Yes, I did 
> with(numtheory)
>
> I was calculating the Ruth-Aaron sequence.  I looked at OEIS A039752 and 
> the maple code there was with loops.  I thought using maple seq would be 
> faster than looping. I ended with
>
> a:=10000;
> s:=seq(add(t, t = Array([seq(factorset(n), n = 1 .. a)])[n]), n = 1..a)
>
> This seemed really strange, but gathered the correct data.
>
> Now, I wanted to extract the values where s[n] = s[n+1] but could not 
> find anything other than looping
>
> for idx from 1 to a-1 do  if s[ idx ] = s[ idx+1 ] then 
>     lprint(idx, s[ idx ]); fi: od:
>
> That approach was slower than 
>
> RuthAaron := proc(n)
> local idx, fs1, fs2, s1, s2:
> fs1 := factorset(1):
> for idx from 1 to n do
>   fs2:=factorset(idx+1):
>   s1:= add(t,t=fs1):
>   s2:= add(t,t=fs2):
>   if s1 = s2 then lprint(idx,s1): fi:
>   fs1 := fs2:
>   od:
> end proc:
>
> by a factor of 4 or 5.  I believe this was bacause of not storing the 
> results.
>
> I want to return an array from the proc, but, do not know the size of 
> the array.  Since I am looping, I want to avoid Maple duplicating the 
> array when growing.  If I define an array
> A:=Array(1..n), I know it is greater than the number of AR numbers I 
> will find in 1..n.  Then I do something like A[1..numFound] to return 
> the array.

The usual way create an Array, not knowing the size, is to create a
table, then convert to an Array.  For example,

RuthAaron := proc(n)
local A, cnt, idx, s1, s2;
uses numtheory;
    cnt := 0;
    s1 := add(t,t=factorset(1));
    for idx from 2 to n do
        s2 := add(t,t=factorset(idx));
        if s1 = s2 then
            cnt := cnt+1;
            A[cnt] := [idx,s1];
        end if;
        s1 := s2;
    end do;
    return Array(1..cnt, [seq(A[cnt], cnt=1..cnt)]);
end proc:

With a recent Maple you can use resizable Arrays.
The trick is to index them with parentheses:

RuthAaron := proc(n)
local A, cnt, idx, s1, s2;
uses numtheory;
    cnt := 0;
    A := Array();
    s1 := add(t,t=factorset(1));
    for idx from 2 to n do
        s2 := add(t,t=factorset(idx));
        if s1 = s2 then
            cnt := cnt+1;
            A(cnt) := [idx,s1];
        end if;
        s1 := s2;
    end do;
    return A;
end proc:



-- 
Joe Riel

Back to comp.soft-sys.math.maple | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Problem with Seq.  Delayed Evaluation? Thomas Dean <tomdean@speakeasy.org> - 2011-09-13 19:25 -0500
  Re: Problem with Seq.  Delayed Evaluation? Joe Riel <joer@san.rr.com> - 2011-09-13 19:33 -0700
    Re: Problem with Seq.  Delayed Evaluation? Thomas Dean <tomdean@speakeasy.org> - 2011-09-14 00:26 -0500
      Re: Problem with Seq.  Delayed Evaluation? Joe Riel <joer@san.rr.com> - 2011-09-14 08:20 -0700

csiph-web