Procedures

Can anyone help me with the following two:
 

1) Can you write a procedure showCol which uses the for var in list do...od construction to print the elements of a list in a column.
         
     showCol([a,b,c]);
  
2) Write a procedure primeCol which print the primes between given integers m and n in a column.

Thanks.

 

Robert Israel's picture

Homework

These appear to be homework exercises.  They are not particularly difficult.

You shouldn't expect us to do your homework for you.  What have you tried, and what was the result?

Hint for #2: you will probably find isprime useful.

Closed Door Secrets

Then what should I expect? What is your purpose for being in this forum?

Yes, this is an easy exercise and I am not enrolled in any classes dealing with Maple as you assumed. I figured it out about 2 days after I posted it without any help. I don't have any experience  with any programming, but I know not to rely on hidebound individuals. Here is my procedure as requested:
Hint#2:  Isprime:= proc(m,n) local i:
                                 for i from m to n do
                                 if type(i,prime) then
                                  print(i); fi;od;
                                  end:

P.S: I don't have problems sharing information with anybody. These are aren't  any closed door secrets. Your hints are just an excuse for indolence on your behalf. Maybe you should give a *** for a change.

Doug Meade's picture

Code of Conduct

Just a minute, where did I put  my flame-retardant keyboard. I haven't had to use it lately, but it's here somewhere.

Found it.

Now, let's look at the Student Help Center Code of Conduct:

Student Help Center code of conduct (Please read!)

Welcome to the Maple Student Help Center. This is a great place to get help on effectively using Maple for your courses. This forum will be supported by Maplesoft staff as well as experienced users around the world. Please take the time to read the following code of conduct:

  • No illegal, obscene, defamatory, hateful, and other forms of offensive content.
  • Be respectful to other members. No name-calling or "flaming". We want everyone to find this forum useful. If you think someone has posted a "stupid" question, then ignore the post. You don't have to express your personal opinion on the person or question in public.
  • Respect copyright. Do not post other peoples' images or work without permission or the proper level of acknowledgment.
  • Do your own homework. Don't expect other people to do your math homework for you. But, if you're genuinely stuck on a Maple question or a math question, you'll likely be able to get help.
  • Post in the appropriate forum. The student forums are intended for math-related questions covering topics from late high school to second year college/university. For more advanced topics, please use the regular MaplePrimes forums.
  • How do I do x in Maple? (student) is meant for finding out how to solve specific types of problems in Maple
  • Math advice (student) is meant for seeking help on math as opposed to the software
  • Help Center suggestions are meant for giving feedback to Maplesoft on ways to improve this service
  • Student chatter is meant to capture posts on a wide range of topics (math, computers, school, politics, music, etc.)
  • We reserve the right to remove any posts that do not, in our opinion, fall within the guidelines of conduct for this Web community.

    It appears to me that you have not followed three of the first 4 items in this list: obscene, disrespectful, and asking ohers to do your homework. There is no excuse for this. The way you have responded does not make me interested in trying to help you do your homework. There is a big difference IMHO between asking someone to do your homework for you and getting help with your homework.

    One reason most of us want to see something that you have done towards your problems is that we want to see the level on which you trying to use Maple. You never told us what type of class this is for, or anything about your background. What version of Maple are you using? These are very important pieces of information I seek to have answered before I answer any question in this forum or in face-to-face interaction with students.

    The post that offended you so much did contain some very useful suggestions, which you appear to have chosen to ignore. I'll focus on the second question. Maple has a command, isprime, that returns true if the argument is prime and false otherwise. For example, the following procedure returns all primes between its two arguments:

    IP := (m,n) -> select( isprime, [$m..n] );
    IP(10,20);
                                  [11, 13, 17, 19]

    This implementation does not use any explicit looping. It will be much more efficient than your attempt, but this might not be anywhere close to what you would be expected to understand or come up with on your own.

    Doug

    ---------------------------------------------------------------------
    Douglas B. Meade  <><
    Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu       
    Phone:  (803) 777-6183         URL:    http://www.math.sc.ed

     

    Mr. Meade: As far as not

    Mr. Meade: As far as not following the three things on the list, where is it that I was obscene? As far as being disrepectful, I don't appreciate smart alec resonpses or assumption made about me. All three of your accusations are greatly exaggerated. I just said that I will not ask anybody with a hidebound attitude for help any longer on this forum, since the last person assumed I was asking for help on my homework such as yourself. First of all, I am not a student. Second, I am not your student either. Third, my reason for being on this forum is because I purchased Maple and was browsing through some of its features. Alas! I stumbled on this site. Lastly, you did not answer my question for # 2 as I stated originally.

    P.S: Since I answered my own questions for both #1 and #2, there was no reason for you to write back to me. I do not care to engage in such frivolities nor to cow- tow to your sarcasms. If you have any problems regarding this post, I am sure I can arrange to meet with you in person and discuss these issues.

    where you posted

    You posted your original questions in one of the "Student Help" forums, and not in one of the non-Student-related "Get Help" or "General Discussion" forums.

     

    Observation

    A minor correction.  It is not true that explicit looping in Maple is necessarily inefficient.  In fact, for the particular task given, which consists of printing a column of primes, most of the time is likely to be spent in the print routine, if not in the primality testing. Note by the way (this is intended for the original poster) that printing, required here, is not something one ordinarily does with Maple.  It is usually more effective to return output (say as a list of primes) rather than to print it, since we can further manipulate the output but can do little but look at printed output.

    To demonstrate my point about explicit looping not necessarily being inefficient, here are two procedures that return a list of primes, from m to n.  The second one, which is essentially what Doug described, is the superior procedure. It is short, clear, and does the job. However, it really is is only marginally faster than the first.  The call to time shows the elapsed duration required for each to return all primes from 1 to 100,000.

    GetPrimes1 := proc(m,n)
    local cnt,T,i;
        cnt := 0;
        T := table();
        for i from m to n do
            if isprime(i) then
                cnt := cnt+1;
                T[cnt] := i;
            end if;
        end do;
        convert(T,'list');
    end proc:
    
    GetPrimes2 := proc(m,n)
        select(isprime,[seq(m..n)]);
    end proc:
    
    N := 100\000:
    time(GetPrimes1(1,N));
                                         1.880
    
    time(GetPrimes2(1,N));
                                         1.824
    

     

    lemelinm's picture

    Here is how I did it

    for #1

    The list "L" must be define before running showCol.

    > showCol := proc (L::list)
    local x;
    for x in L do
    print(L[x]);
    end do;
    end proc;

    > showCol(L);
     

    for #2

    > primeCol := proc (m::integer, n::integer)
    local L, i;
    L := select(isprime, [seq(m .. n)]);
    for i to nops(L) do
    print(L[i]);
    end do;
    end proc;

    > primeCol(1, 100);
     

    Have you find a shorter way to do it?

    mario.lemelin@cgocable.ca

    alternative

    PrimeCol := proc(m::integer, n::integer)
    local L;
        L := select(isprime, [seq(m..n)]);
        printf("%{t}d\n", Matrix([L]));  # ?rtable_printf for details. t transposes.
    end proc:
    
    lemelinm's picture

    Yes, but here a more practical ....

    ... way so you can use the output

     

    > primeCol := proc (m::integer, n::integer)
    
    local L, V; 
    
    L := select(isprime, [seq(m .. n)]); 
    
    V := convert(L, Vector); 
    
    RETURN(V)
    
    end proc;
    
    
    > Vec := primeCol(1, 20);
    
    

     

    mario.lemelin@cgocable.ca

    JacquesC's picture

    This can be simplified to

    primeCol := proc(m::posint, n::posint)
    Vector(select(isprime, [seq(m .. n)]));
    end proc

    alec's picture

    RETURN

    As ?return help page says,

    Use of the RETURN procedure is strongly discouraged :)

    Alec

    Comment viewing options

    Select your preferred way to display the comments and click "Save settings" to activate your changes.
    }