 |
|
List Archives > 
Maple User Group List Archive > 
Archive by date > 
This Month By Date > 
This Month By Topic
[MUG] Re: fundamental
| [MUG] Re: fundamental |
|
Author: Maple User Group
Posted: Mon, 17 Feb 2003 22:01:26 -0500
|
>> From: Maple User Group "maple_gr"
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
Date: Fri, 14 Feb 2003 00:57:38 +0100
From: Helmut Kahovec "helmut.kahovec"
To: "maple-list"
Subject: fundamental idea in a procedure - counting primes
"John A. Velling" wrote:
>| I seem to be missing the point of procedures. Consider the following:
>|
>| u := proc(k)
>| for counter from 0
>| while ithprime(counter+1) <= k
>| do counter := counter +1 od:
>| end proc;
>|
>| The intention is to count the primes less than or equal to k. I get u(4) =1
>| and u(5) =3. Somehow I just don't understand what is being returned here
>| and why. Any suggestions?
Firstly, you should accept Maple's warning messages. Secondly, you
should not alter the looping index within the loop. Thirdly, the looping
index does just what you are trying to accomplish: counting the loops.
Thus, the following might be a solution to your problem.
> restart;
> u:=proc(k)
local counter;
for counter from 0 while ithprime(counter+1)<=k do
end do;
counter
end proc:
> seq(
printf(
"There are %2d primes up to and including %2d.\n",
u(i),i
),
i=1..10
);
There are 0 primes up to and including 1.
There are 1 primes up to and including 2.
There are 2 primes up to and including 3.
There are 2 primes up to and including 4.
There are 3 primes up to and including 5.
There are 3 primes up to and including 6.
There are 4 primes up to and including 7.
There are 4 primes up to and including 8.
There are 4 primes up to and including 9.
There are 4 primes up to and including 10.
Kind regards,
Helmut
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
Date: Sat, 15 Feb 2003 05:48:36 -0500 (EST)
From: Carl Devore "devore"
To: "maple-list"
Subject: fundamental idea in a procedure - counting primes
On Wed, 12 Feb 2003, John A. Velling wrote:
> I seem to be missing the point of procedures. Consider the following:
>
> u := proc(k)
> for counter from 0
> while ithprime(counter+1) <= k
> do counter := counter +1 od:
> end proc;
You're not missing the point of procedures ao musch as missing te point of
'for'. 'For' automatically increments the index variable each time
through the loop, and then you are explicitly incrementing it again. SO
the procedure should be
u:= proc(k)
local counter;
for counter from 0 while ithprime(counter+1) <= k do od; # Do nothing
counter # last value in procedure is the return value
end proc;
Note the number of primes <= k can also be computed as numtheory[pi](k).
> What I really want is to make a list of all primes <= k. Here is my effort:
> u := proc(k)
> for counter from 0
> while ithprime(counter+1) <= k
> do counter := counter +1 od:
> P := array(1..counter-1):
> for n to counter-1
> do P[n] := ithprime(n) od:
> print(P)
> end proc;
>
> which indeed prints out the desired list. However a little testing
> indicates that the result of u(100) is not the list of primes up to 100.
> What am I missing and how can I make the procedure give an array as output?
'print' prints output as a side effect and returns NULL. Thus your
procedure returns NULL.
Try this:
primes:= proc(k)
local counter;
array([seq(ithprime(counter), counter= 1..u(k))])
end proc;
In the above, I used the prime-counting procedure u from above.
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
Date: Sat, 15 Feb 2003 23:05:43 -0800
From: "Maple"
Subject: fundamental idea in a procedure - counting primes
To: "maple-list"
--Boundary_(ID_vrTAayXvczhvvJEQGyq0Cg)
Content-type: text/plain; x-avg-checked=avg-ok-450D45C9; charset=us-ascii;
format=flowed
Content-transfer-encoding: 7BIT
How about this?
> u := proc(k)
> seq(ithprime(i),i=1..k);
> end proc:
> u(7);
2, 3, 5, 7, 11, 13, 17
>
By the way, I never realized Maple would accept "od" instead of "end do"
before now! ;-) Thanks for the tip. I wonder why it isn't documented
anywhere? I only started using Maple with version 7 and am a relative
newbie at it.
According to the Maple "return" documentation:
"One common form of return from a procedure invocation occurs when
execution 'falls through' the end of the statement sequence which makes up
the procedure body, in which case the value of the procedure invocation is
the value of the last statement executed."
-- Ron
At 12:23 PM 2/12/2003, John A. Velling wrote:
>What I really want is to make a list of all primes <= k. Here is my effort:
>
>u := proc(k)
> for counter from 0
> while ithprime(counter+1) <= k
> do counter := counter +1 od:
> P := array(1..counter-1):
> for n to counter-1
> do P[n] := ithprime(n) od:
> print(P)
> end proc;
>
>which indeed prints out the desired list. However a little testing
>indicates that the result of u(100) is not the list of primes up to 100.
>What am I missing and how can I make the procedure give an array as output?
>
>Thanks,
>
>John A. Velling
--Boundary_(ID_vrTAayXvczhvvJEQGyq0Cg)--
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
Date: Mon, 17 Feb 2003 17:23:37 +0000
To: "maple-list"
From: peter lindsay "plindsay"
Subject: fundamental idea in a procedure - counting primes
Hello,
is
pr2:=proc(k)
counter2:=0;
for counter from 0 to k do
if (ithprime(counter+1) <= k) then counter2:=counter2+1
else break:
end if;
od:
return counter2:
end proc;
any help ?
Pete Lindsay
> >> From: "John A. Velling" "jvelling"
>
>I seem to be missing the point of procedures. Consider the following:
>
>u := proc(k)
> for counter from 0
> while ithprime(counter+1) <= k
> do counter := counter +1 od:
> end proc;
>
>The intention is to count the primes less than or equal to k. I get u(4) =1
>and u(5) =3. Somehow I just don't understand what is being returned here
>and why. Any suggestions?
>
>What I really want is to make a list of all primes <= k. Here is my effort:
>
>u := proc(k)
> for counter from 0
> while ithprime(counter+1) <= k
> do counter := counter +1 od:
> P := array(1..counter-1):
> for n to counter-1
> do P[n] := ithprime(n) od:
> print(P)
> end proc;
>
>which indeed prints out the desired list. However a little testing
>indicates that the result of u(100) is not the list of primes up to 100.
>What am I missing and how can I make the procedure give an array as output?
>
>Thanks,
>
>John A. Velling
--
peter lindsay
scientific officer
r215 maths instutute
university of st andrews
ky16 9ss
phone 01334 463756
|
Previous by date: [MUG] Re: Pb with minimize, C W
Next by date: [MUG] restart, Peter Lindsay
Previous thread: [MUG] differentiation of a sum, Maple User Group
Next thread: [MUG] restart, Peter Lindsay
|
|
|