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?

From Joe Riel <joer@san.rr.com>
Newsgroups comp.soft-sys.math.maple
Subject Re: Problem with Seq. Delayed Evaluation?
Date 2011-09-14 08:20 -0700
Organization A noiseless patient Spider
Message-ID <874o0f2lsm.fsf@san.rr.com> (permalink)
References <Xns9F5FB13FEA264tomdeanspeakeasyorg@216.196.97.142> <878vpr3lak.fsf@san.rr.com> <Xns9F5FE447B4D92tomdeanspeakeasyorg@216.196.97.142>

Show all headers | 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