Adept Scientific - English
The world's best software for research, science and engineering.
flag arrow
clearclear
 

 Adept Store | register Join My Adept | Flags  
Adept Scientific | Amor Way | Letchworth Garden City | Herts | SG6 1ZA | Tel: +44 (0)1462 480055  
UKusdedksvnofi
Home
Products
Training
Consultancy
 Buy Online
Downloads
Education
Support
My Adept
International |  About Us |  Contact Us |  Press Room |  Jobs


The Next Steps

• Ask us a question
• Maple Product Tour
• Buy Maple Now
• View Maple Pricing
• Find out about Online Training
• Download a Brochure
• Request a Brochure
• Download a Demo
• Request a Demo
• Meet Our Team
• Read our RSS Feeds

Learn More

Maple Home
Maple 11 Professional
Maple 11 Academic
Maple 11 Student Use
Recorded Online Seminars
FREE Training Resources


MapleNet
Maple T.A.
MapleConnect
BlockImporter for Simulink
BlockBuilder for Simulink
Maple Toolboxes
Maple Rave Reviews
Maple Study Guides
Books about Maple
System Requirements

View Maple 10 in Action
Product Comparison Chart

Latest Information

New Features: Professional
New Features: Academic
The Maple Reporter
The Maple Reporter Online
Numerical Algorithms Group
(NAG)


Service & Support

Maple 10 Training Videos
MaplePrimes, blogs, forums
Elite Maintenance Program
Application Centre
Powertools
Maple User Group (MUG)
Join the Maple User Group
(MUG)

Search the Knowledge Base
Technical Support request

List Archives >  Maple User Group List Archive >  Archive by date >  This Month By Date >  This Month By Topic

[MUG] Re: fundamental

Search email archive for  

[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



Ready to buy?

Maple - single user licence
Add to shopping basket
$ 1,895.00
Upgrade to Maple 12 from v11
Add to shopping basket
$ 995.00
Upgrade to Maple 12 from v10 & below
Add to shopping basket
$ 1,395.00

Featured Downloads

Maple White Paper: Technical Knowledge - An Asset You Can Afford to Lose?
Maple in Electronics Application Pack
Maple in Robotics & Aerospace Application Pack
Maple in Finance Application Pack

Product Reviews

"Without the Maple software, we would have to spend weeks generating the equations of motion for every experiment. Then the chances that we did it right would basically be near zero. There would always be a mistake somewhere. It is very difficult to set up a dynamic motion model by hand."
- Jean-Claude PiedBeouf, Ph.D Manager of Robotics, Canadian Space Agency

"Its very good - highly accurate and easy to use. The speed of Maple allows me to change equations and quickly reintegrate them into the application, so more possibilities can be explored to achieve the precise effect desired."
Shawn Neely, Senior R & D Director for PDI/Dreamworks
adept

Top of the Page

Our Privacy and Terms and Conditions Statement
All Trademarks Recognised. Copyright © 2007, Adept Scientific plc.
Site designed and maintained by Adeptise

Adept Scientific | Amor Way | Letchworth Garden City | Herts | SG6 1ZA | Tel: +44 (0)1462 480055