Adept Scientific - English
The world's best software and hardware 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  
UKdedksvnofi
Home
Products
Training
Events
 Buy Online
Downloads
Academic Discounts
Support
My Adept
International |  About Us |  Adept Scientific Blog |  Contact Us |  Press Room |  Jobs
Adept Scientific on Facebook Adept Scientific on Twitter Adept Scientific on YouBube Adept Scientific on LinkedIn


The Next Steps

• Ask us a question
• Watch Maple Video Demonstrations
• Buy Maple Now
• View Maple Pricing
• Download a Brochure
• Request a Brochure
• Request an Evaluation
• Meet Our Team
• Read our RSS Feeds

Learn More

Maple Home
Maple 16 Overview
Maple 16 Professional
Maple 16 Academic
Maple 16 Student Use
What's New in Maple 16
Maple New Features
Datasheet

Maple History
Recorded Online Seminars

MapleSim
MapleNet
Maple T.A.
BlockImporter™
Maple Toolboxes

Maple Rave Reviews
Maple Study Guides
Books about Maple
System Requirements

Latest Information

New Features: Professional
New Features: Academic
Maple Features
The Maple Reporter Online

Service & Support

Maple Primes
blogs, forums etc

Elite Maintenance Program
Application Centre
Powertools
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 />
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-

Date: Fri, 14 Feb 2003 00:57:38 +0100
From: Helmut Kahovec /> To: /> 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 /> To: /> 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: /> Subject: fundamental idea in a procedure - counting primes
To: />

--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: /> From: peter lindsay /> 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" /> >
>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?

For more pricing information:
Visit our webstore, call us on +1 800 724 8380 or email us at info@adeptscience.com

Featured Downloads

Maple 16 & MapleSim 5 Professional Brochure
Maple 16 Academic Datasheet
Maple 16 & MapleSim 5 Academic Brochure
Maple 16 What is New datasheet
Maple 16 Professional Datasheet
Maple Whitepaper: Driving Innovation - How mathematical modeling and optimisation increase efficiency and productivity in vehicle design.
MapleSim Whitepaper - Technological Superiority in Multi-Domain Physical Modelling and Simulation

Latest Downloads

Maple 16 Programming Guide
Maple 16 User Manual
Maple 16 Academic Datasheet
Maple 16 Professional Datasheet
Maple 16 & MapleSim 5 Academic Brochure

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

Latest News

Connectivity to major CAD systems extended in Maple 16
MapleSim Breaks New Ground in Hardware-in-the-Loop real-time simulation for planetary rovers
MapleSim Breaks New Ground in Hardware-in-the-Loop real-time simulation for planetary rovers
Maths software usability reaches new heights with Maple 16
"MapleSim was an eye-opener for us.
adept

Top of the Page

Popular Links: ChemDraw | ChemOffice | Data Acquisition | Data Analysis | EndNote | Maple | MapleSim | Mathcad | MathType | Quality Analyst | Reference Manager | VisSim

EU ePrivacy Directive | Our Privacy and Terms and Conditions Statement
All Trademarks Recognised. Copyright © 2012, Adept Scientific plc.
Site designed and maintained by Lyndon Ash

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