MaplePrimes Posts

MaplePrimes Posts are for sharing your experiences, techniques and opinions about Maple, MapleSim and related products, as well as general interests in math and computing.

Latest Post
  • Latest Posts Feed
  • Here is the simplest program for obtaining the set of prime numbers less than or equal to n,
    f:=n->select(isprime,{$1..n}):
    It is not Eratosthenes Sieve though. The program ES below implements the Eratosthenes Sieve,

    In this post, daniel asks about how to compute the list of primes less or equal to some integer n. This is often called the Sieve of Eratosthenes problem, but the term "Sieve of Eratosthenes" actually refers to a specific algorithm for solving this problem, not to the problem itself.

    This problem interested me, so I tried a few different ways of performing the task in Maple. As with my previous blog post, this exercise should be seen as an attempt to explore some of the issues faced when trying to make Maple code run faster, not as an attempt to find the fastest all-time Maple implementation of an algorithm to solve this problem.

    Here are seven different implementations:

    Implementation 1

    sp1 := n->select(isprime,{$1..n}):
    

    This first was suggested by alec in response to daniel's post. It's by far the simplest code, and somewhat surprisingly turns out to be (almost) the fastest of the seven.

    I had thought it wouldn't be, because the subexpression {$1..n}builds the expression sequence of all integers from 1 to n, and the code spends time checking the primality of all kinds of numbers which are obviously composite.

    > time( sp1( 2^16 ) );
                                 0.774
    

    Implementation 2

    sp2:=n->{seq(`if`(isprime(i),i,NULL),i=1..n)}:
    

    This second attempt avoids the principal problem of sp1, which is the construction of that expression sequence mentioned previously. However, the evaluation of all those `if` conditionals is also expensive, so this approach is essentially the same as sp1in runtime.

    > time( sp2( 2^16 ) );
                                 0.770
    

    Implementations 3 and 4

    sp3 := proc(N)
        local S, n;
        S := {};
        n := 2;
        while n < N do
            S := S union {n};
            n := nextprime(n);
        end do;
        S
    end proc:
    
    sp4 := proc(N)
        local S, n;
        S := {};
        n := prevprime(N);
        while n>2 do
            S := S union {n};
            n := prevprime(n);
        end do;
        S union {2}
    end proc:
    

    These two approaches are essentially the same: sp3 uses nextprime and ascends, while sp4 uses prevprimeand descends.

    The advantage to this approach is in avoiding all the unnecessary primality tests done by sp1 and sp2. Unfortunately, this advantage is offset by the fact that they rely on augmenting the set Sincrementally, which causes them to be slower than either of the previous two.

    > time( sp3( 2^16 ) );
                                 1.494
    
    > time( sp4( 2^16 ) );
                                 1.430
    

    Implementation 5

    There's really only one basic Maple command dealing with primes that we haven't yet used: ithprime. However, to use this effectively we need to know how far to go: i.e. what is the maximal m such that pm ≤ n?

    Well, that's equivalent to asking how many prime numbers there are between 1 and n, or equivalently, what's the value of π(n), where π(n) is the prime counting function. This is implemented in Maple as numtheory[pi], so we'll use that in our code.

    sp5 := N->{seq(ithprime(i), i=1..numtheory:-pi(N))}:
    

    Happily, this turns out to be much faster than anything we've seen yet:

    > time( sp5( 2^16 ) );
                                 0.037
    

    Implementation 6

    Just for fun, I wondered whether speed might be improved by approximating π(n) with the Prime Number Theorem using Li, the logarithmic integral. (There are lots of other, better, approximations that we could use, but I'm not going to bother with those here.)

    The approximation isn't perfect: as the help page for Li says, π(1000)=168, but Li(1000) ≅ 178. So we'll use ithprimeto find the first Li(N) primes (rounded down), remove all primes > N, and add in any primes ≤ N that might be missing.

    sp6 := proc(N)
        local M, S, n;
        # Approximate number of primes with Prime Number Theorem
        M := trunc(evalf(Li(N)));
        S := {seq(ithprime(i),i=1..M)};
        # Do some cleanup: since the theorem provides only an
        # approximation, we might have gone too far or not far enough.
        # Remove primes that are too big; add in any that are missing
        S := select( type, S, integer[2..N] );
        n := ithprime(M);
        while n < N do
            S := S union {n};
            n := nextprime(n);
        end do;
        S
    end proc:
    

    This is faster than the first four attempts, but is not faster than sp5. It's unlikely to be, since numtheory[pi] is fairly fast. However, its speed could be improved with a closer approximation to π(n).

    > time( sp6( 2^16 ) );
                                 0.290
    

    Implementation 7

    This last attempt, to see what we get, is to cast away all of Maple's built-in tools, and actually implement a real Sieve of Eratosthenes. We dynamically build up a set of primes, then check each successive number for divisibility by each member of this set, making sure to use short-circuit evaluation so we don't waste time doing divisions once we know something's composite.

    sp7 := proc(N)
        local S, n;
        S := {};
        for n from 2 to N do
            # if a prime p in S divides n, it's not prime
            if not ormap( p->irem(n, p)=0, S ) then
                S := S union {n};
            end if;
        end do;
        S
    end proc:
    

    This approach is, as we might expect, a lot slower than anything we've tried thus far, because it redoes a lot of things that Maple already does quite quickly. For comparison, I've done it for inputs of 2^12 and 2^16so you can see the blow-up.

    > time( sp7( 2^12 ) );
                                 0.527
    > time( sp7( 2^16 ) );
                                 77.665
    

    So, the conclusion is that sp5 is the fastest of the bunch. I'm not sure how far its runtime generalizes; it may be as fast as it is largely because it uses a precomputed list of primes. However, even for larger inputs I suspect it is probably still faster than any of the other approaches above, simply because it avoids the creation of large intermediate data structures or needless checks that most of the others perform.

    Yesterday I posted few screenshots of Maple 10. Here I am adding few different cmaple looks in Windows.    
    It would be good to have User Attachment Control Panel (as in phpBB) where I could look at my attached files, delete them if necessary, replace, and add files. Especially because the quota is not that big.
    Hello,

    this is just my first message here on the new beta.mapleprimes.com, and I hope there will be many more to follow. This site will hopefully be what I was missing for a long time : a user community for all things Maple.

    Considering I'm new to the site, I was trying to find what I could all do... so wouldn't it be a good idea to put some suggestions in the FAQ on where to put what? That is, suppose I have a question regarding some function, where's the best place to put it? In a forum, you say? But which one? Or, suppose I found out a neat feature, and I want to share it with all of you, where should I put that? On some forum, or on my blog?
    I did a Google search for site:beta.mapleprimes.com/blog +maple (I was trying to count how many blogs on Primes Google knew about - a lot!). And then I glance over at the "Sponsored links" over on the right hand side, and what do I see but a link to www.wolfram.com. Could it be that they have actually bought the word maple for their AdSense keywords? I tried a few other queries, even re-trying the one above, and that link did not come up again. Very odd. So it looks like it was some kind of Heisenbug. None of my ``obvious'' Google searches came up with any sponsored links from either Maplesoft or Wolfram Research (lots of page hits, more often of ``friendly'' sites than the corporate site).
    I was getting bored of having to convert between binary and decimal to find examples for binary arithmetc, so I decided to write a package which automatically interprets all input numeric values as binary numbers, makes the appropriate calculation (by converting back to decimal, doing the operation and then converting back to binary.
    FYI, Maplesoft has just released an e-book called Advanced Engineering Mathematics with Maple (R. Lopez author). Engineers among you will recognize the title as it is used by several popular texts. These books cover most of the primary mathematical topics encountered in engineering.
    It would be nice if there were a second preview button, this one near the Body: block (maybe aligned with its top right corner. That way it isn't necessary to scroll down to the bottom of the page to preview a complicated input. Leaving the submit button at the bottom is okay, that forces one to see the license section and choose something appropriate.
    FYI, Maplesoft has just released a patch for Maple 10. You can download the patch through the "Check for Updates" in Maple 10's "Tools" menu.
    Hi all, Would it be appropriate to have a forum dedicated to questions on Maple T.A. (I have many of them!) ? Many of the Maple T.A. issues are not exactly "mathematical" so they don't seem to fit in the other forum topics. Is there enough interest to warrant such a forum? Is there already a forum someplace else I haven't looked? My Maple T.A. question: Does altering the due date for an assignment after students have started adversely affect any of the student records? Any help or suggestion on where to post this would be great. Thanks, Marc
    The University of Toronto is Canada's largest university and considered one of its very best. Its downtown campus (St. George) campus is the original site with all of the older buildings (circ. mid 19th century) and most of its most prestigeous faculties. Of note for the math crowd would be the Fields Institute .
    The zipped code for "Pricing Arithmetic Average Asian Options" is missing sobol.h (to be used by the source ArithmeticAsian.cpp). Could someone please add that header file?
    Here are some screenshots of Functioning Maple V R1 DOS demo (circ. 1991).
    A while back, I ran across this little gem in an ASME archive. It's a DOS demo of Maple V Release 1 from 1991! Unzip and run maple.exe. On my XP machine, you get an initial error but if you simply ignore it, the demo launches and the demo will run. Press enter to work through the predefined examples. At the end you'll get a blank prompt. Comes with a reduced library so you can't do everything but you'll get a taste of what life used to be like ... T4.
    First 284 285 286 287 288 289 290 Last Page 286 of 297