Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Programming Puzzle 20 - Display primes

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Programming Puzzle 20 - Display primes

    Your manager has gone on sabbatical, but a colleague in the cubicle next door has been reading about "prime numbers". He's wondering if xbasic can be used to compute primes. You recall that a number is "prime" if it's an integer greater than 1 that can only be evenly divided by 1 or itself. For example, 2, 3, 5 and 7 are primes, but 4, 6, 8, and 9 are not.

    Your challenge, grasshopper, is to write an xbasic script that will display the first 50 prime numbers in five lines, each of which contains ten numbers.

    Have fun, your manager won't be back for a week!

  • #2
    Re: Programming Puzzle 20 - Display primes

    puz 20 attempt

    puz 20.txt

    Comment


    • #3
      Re: Programming Puzzle 20 - Display primes

      Thanks, Jeb.

      Prof. Pickypicky will be happy to review your code when you add comments to explain the logic of your design.

      -- tom

      Comment


      • #4
        Re: Programming Puzzle 20 - Display primes

        Prof. Pickypicky, I do apologize about the no comments. I feel as if I'm not good at commenting the script, but I guess that's why you are pushing us to do this. That way we can comment a script and come back to it later and know exactly what it does. Prof knows best. Well here's my revised and commented script. Hope this works. Thanks.

        puz 20.txt

        P.S. as you know my script is very repetative. I went ahead and copied each comment to the corresponding line. Therefore, there is a lot of repeats. Just trying to prevent confusion. Thanks again.

        Comment


        • #5
          Re: Programming Puzzle 20 - Display primes

          Jeb,

          The professor thanks you for the thorough job you did documenting your code. Here are some suggestions for your consideration.

          1) Remember that a string of text characters can be broken into lines for display on screen by inserting a crlf() in the string wherever you want a carriage return/linefeed to appear. Instead of duplicating the code processing for each of the 5 lines you want to generate, why not run the numbers once until you find all 50 primes, building your output string as you go. A simple counter would let your script know when 10 numbers have been inserted into the string, so the script could then do a " + crlf() + " and continue.

          2) Finding prime numbers has been studied a lot and mathematicians are often challenged to find the most efficient way. In your case the Prof sees a couple of easily remedied inefficiencies. In your design you test each number by dividing it by both the integer 1 and itself. These 2 "factors" are always present. You determine a number is prime if there are only "2" factors that can evenly divide the candidate number. Consider the savings that could be achieved if you never divided the number by 1 ? Dividing by 1 tells you nothing. You know 1 is always going to be a factor right? So most would begin their divisions with "2", skipping "1" entirely. Not a big savings in our small sample, but if you were trying to produce a list of the first 10,000 primes... ? Also, consider that in any case where the denominator is greater than half the numerator an even division is impossible. Say, for example, we're trying to see if "11" is prime. We need to divide by 5. But do we ever need to divide by "6", "7", "8", "9", or "10". Won't all these divisions by definition always have a remainder? If so there's no need to see they could be a factor of the candidate. In your design you continue dividing past the mid-point, even though all those divisions will always have a remainder. So, recognizing this most would limit the loop that tests for factors so that it stops when it gets to a number thats greater than half the candidate being tested. i.e
          Code:
           int(Candidate_Nbr / 2)
          The savings here becomes huge as the candidate numbers get larger and larger. Half of the loops you currently are using can be omitted. See?

          Comment


          • #6
            Re: Programming Puzzle 20 - Display primes

            puz 20-3.txt

            Prof Pickypicky,
            This is my script with some changes to it. I have got my script to now only look for one factor because t_val is always a factor of t_val. Therefore, I have it finding how many other factors it has. So, now if it only has one factor (itself not included) it's a prime. Also, I have shortned it. Check it out. Hope this is much better. Thanks.

            P.S. still working on the divisor greater than half the t_val. It's line 20 in this script it is commented out.
            Last edited by jeb richardson; 08-17-2011, 11:14 AM.

            Comment


            • #7
              Re: Programming Puzzle 20 - Display primes

              Dear Professor Pickypicky,

              I humbly submit this code for your consideration.

              I await, primed for your response.

              TJK
              Attached Files
              Tim Kiebert
              Eagle Creek Citrus
              A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine.

              Comment


              • #8
                Re: Programming Puzzle 20 - Display primes

                Tim,

                The Professor is impressed that you skipped all the EVENLY numbered candidates. No reason to test them is there?

                Your approach gets the correct answer, but is based on an hypothesis for which no proof is offered.

                ' The largest divisor needed to test. This is found by getting the integer of the
                ' square root of the number being tested. Any value larger than this integer that
                ' divides evenly would have been the result of a division by a number smaller
                ' than the root
                On behalf of the mathematically challenged the Professor is curious. What proof might be offered that this is a true statement in all cases?

                -- tom

                PS - Kudos on the comments in your code! An excellent example. Worthy of being framed and displayed on the mantle for all to follow.
                Last edited by Tom Cone Jr; 08-17-2011, 11:40 AM.

                Comment


                • #9
                  Re: Programming Puzzle 20 - Display primes

                  Funny, I thought about skipping all even numbers also. I didn't think there was a proof needed, it's a mathematical truth, like the definition of a prime number, an even number is that which can be divided by 2 with no remainder. So I'm guilty too.
                  Attached Files
                  Mike W
                  __________________________
                  "I rebel in at least small things to express to the world that I have not completely surrendered"

                  Comment


                  • #10
                    Re: Programming Puzzle 20 - Display primes

                    Mike,

                    I think prof Pickypicky is referring to the use of the square root as the upper limit that needs testing not the elimination of even numbers.
                    Tim Kiebert
                    Eagle Creek Citrus
                    A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine.

                    Comment


                    • #11
                      Re: Programming Puzzle 20 - Display primes

                      Professor Pickypicky,

                      Thanks for the feed back.

                      With regards to the not giving of proof for the 'square root upper limit' theory, I had to leave an obvious issue to distract you from all the other short comings.
                      It is something I remembered from the distant past.
                      Actually, I thought that you would pick me up on the assumption that 2 is a prime number.

                      I will try to put together an explanation for this. I am not a mathematician so the best I can probably come up with is a general description of the idea with some examples. ('All' when playing with numbers can be a long list) I might also cheat and take a look at wikipedia for some references. I may have to ask for an extension because Professor DayJob has given this student way too many assignments.

                      TJK
                      Last edited by Tim Kiebert; 08-17-2011, 08:34 PM.
                      Tim Kiebert
                      Eagle Creek Citrus
                      A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine.

                      Comment


                      • #12
                        Re: Programming Puzzle 20 - Display primes

                        I recognized this later and believed I added this as a Later: edit statement in my post, but it didn't get through apparently.

                        I added a 5th premise to skip testing 2 digit values with both characters being the same number except 11 because those will always be divisible by 11. This applies to 4 digit numbers where first and last digits are the same value but middle 2 digits are either one greater (1221,2332,) or one less (2112,3223,5445). Not sure this removes too much more time from processing. But kick me in the shins when I have to come up with a proof of this for Professor Pickypicky.
                        Attached Files
                        Mike W
                        __________________________
                        "I rebel in at least small things to express to the world that I have not completely surrendered"

                        Comment


                        • #13
                          Re: Programming Puzzle 20 - Display primes

                          Nice addition MIke,

                          I haven't provided a proof for my last hypothesis yet but while on a break from my assignments for professor Dayjob I thought of something else. Since even numbers are divisible by two and therefore not prime numbers, may be divisors that are even can also be eliminated from the processing. In other words, any number that is divisible by an even number is also divisible by two, and therefore not a prime.

                          I changed the line
                          CurrDivisor = CurrDivisor + 1 (line 65)
                          to
                          CurrDivisor = CurrDivisor + 2

                          and added an additional count of process steps at line 49 and 55 (TestsDone = TestsDone + 1)

                          changing line 65 reduced the process steps from 525 to 307

                          The results did not change. I wonder if that is enough proof.
                          Attached Files
                          Tim Kiebert
                          Eagle Creek Citrus
                          A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine.

                          Comment


                          • #14
                            Re: Programming Puzzle 20 - Display primes

                            Excellent Tim!
                            Mike W
                            __________________________
                            "I rebel in at least small things to express to the world that I have not completely surrendered"

                            Comment


                            • #15
                              Re: Programming Puzzle 20 - Display primes

                              I humbly submit my homework. I explain my change of display format. I didn't peek at others desks while working.

                              BTW, I love prime numbers. My sister once told me that she, her husband and all four of their kids were all prime numbers for several months between birthdays. Jealousy coursed through my veins!
                              Attached Files
                              Stephen Pilon
                              Associate Librarian
                              Christendom College

                              Comment

                              Working...
                              X