Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

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 1 - Simple Loop

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

    #31
    Re: Programming Puzzle 1 - Simple Loop

    Very nice, Thomas.

    The message board left justifies your script. That makes it harder to read scripts because we lose the indents.

    To preserve the indents prepare your reply using the "Go Advanced" button, and then encapsulate your code with CODE tags, like this:

    Code:
    option strict
    
    dim target_num as n = 0 'used to determine if number is even
    dim i as n = 0
    dim result as n = 0 'result adds the total of the even numbers
    dim num_loops as n = 0 'counts the number of interations
    
    for i = 302 to 101 step-2
    	num_loops = num_loops + 1
    	target_num = i
    	if mod(target_num,2)= 0 then
    		result=result+target_num
    	end if
    next i
    
    ui_msg_box("Program Puzzle One","The answeres are Sum = "+ alltrim(str(result))+" Loops = "+alltrim(str(num_loops)))
    
    end
    The # button on the advanced editor toolbar will do this for you. Just select the text to be encapsulated, and click #.

    Also, when you develop a solution to the next puzzle, I suggest you learn how to "export" your script as a text file, and then simply attach it to a reply here. If you use the script export tool in Alpha Five the text file will be specially formatted to work with Alpha's import tool. This makes it easier for us to study your code and run it on our machines. We just import your previously exported script. To get to the export (and import) tools, save your script to the Code page of the A5 control panel. Then right click the script name. Choose Export off the drop down list (context menu).

    Welcome to the board!

    Comment


      #32
      Re: Programming Puzzle 1 - Simple Loop

      Just came across this part of the message board. Not sure whose idea it is to have a section for "Puzzles"..but I think it's a pretty good idea..
      On to the first puzzle..don't have time to scan over any others..nor to scan over the responses, but this puzzle, clearly, can be solved without any loops whatsoever, rather with one simple expression. But if I do that then I am in violation of the rules.
      For those who might be interested, here is the expression:
      Code:
      vtotal=int((end-start)/2)+1+(int((end-start)/2)+1)*if(mod(start,2)=0,start-2,int(start/2)*2)+(int((end-start)/2)+1)^2
      Where vtotal is the sum of all even values, start is the bottom value and end is the top value, all inclusive.

      If I want to be de facto in compliance with the rules, I could be a slick, clever, irritating smart alec, which I do not want to be, but again, for those who might be interest, I will use a single loop that does nothing and I will have my lawyer sue and demand the ultimate prize arguing that I am in full compliance with the rules the way they were written and offered.
      Here is the smart alec script:
      Code:
      option strict
      dim start as n'this is the bottom value
      dim end as n'this is the top value
      dim i as n 'to be used as a counter in the loop
      dim vtotal as n
      start=101 
      end = 302
      'Now we will use the same expression provided in the previous example and add a dummy loop
      for i=1 to 1
      vtotal=int((end-start)/2)+1+(int((end-start)/2)+1)*if(mod(start,2)=0,start-2,int(start/2)*2)+(int((end-start)/2)+1)^2
      next
      msgbox("The total sum is: "+vtotal+"  and the script used 1 loop")
      'The same could be done using while..end while loop
      Code:
      option strict
      dim start as n'this is the bottom value
      dim end as n'this is the top value
      dim i as n 'to be used as a counter in the loop
      dim vtotal as n
      start=101 
      end = 302
      
      while i<=1
      vtotal=int((end-start)/2)+1+(int((end-start)/2)+1)*if(mod(start,2)=0,start-2,int(start/2)*2)+(int((end-start)/2)+1)^2
      i=i+1
      end while
      msgbox("The total sum is: "+vtotal+"  and the script used 1 loop")
      There is only one loop, about as few as you can get unless I hire the real smart alec lawyer who will argue for my original expression to be the ultimate winner as it has zero loops!! But I don't think the panel of judges will buy that.

      As you can see, even if the judges accept the logic of zero loops or one loop, still I wont get any prizes since I am still in violation of the rules since I didn't explain how and why does that expression work, and I wont.. To understand this expression, you have to mentally translate this math puzzle into a geometric one..to explain that is a bit time consuming and outside the parameters of this puzzle, so we will move on to a more realistic solution, which I will start in a new post and and ask you to please ignore this post as a side show that may or may not have any redeeming value.

      Comment


        #33
        Re: Programming Puzzle 1 - Simple Loop

        So, on to a more realistic solution, one that complies with the spirit rather than the letter of the rules.
        My first instinct was to:
        1-identify the first even number
        2-Loop stepping by 2 adding the values until reach the top value.
        I ended up with 100 loops. That's embarrassing! a hundred loop to solve a simple puzzle like this? There got to be a better way.
        There is..
        One that uses a 20 loops or few more.
        Here it is:
        P.S.: To best read the introduction in case your screen rsolution is different from mine, copy/paste it into Word and read it in Word.
        Code:
        'Introduction:
        'The object is to sum all even values between 2 given numbers
        'Since these 2 numbers are provided in the puzzle, meaning I could hard-code these values which makes everything a lot simpler and faster but just in case the intent was to solve for any numbers not just the ones provided in the puzzle, the script is written so that you can substitute these number at will and get the correct result.
        '
        'Let me first start with the logic behind the script.
        'To add all even values between 2 given numbers, you identify the first even number, then add 2 to it and add the new number to the previous one and so on..
        'That's the standard way of thinking.. by doing that you will have to loop as many times as there are even values..
        'That's too many loops and too expensive..
        '
        'Let's look at the puzzle from a different perspective..
        '
        'In the example provided, the first even number is 102.
        'Let's call this number a base.
        'Every subsequent number is 2 points more than the one before..
        'Let's get away from this line of thinking.
        'A different way to think about is:
        'You have a base number, then
        'Every subsequent number has one more 2 than the one before, i.e.:
        'The base number is 102
        'The one after has one more 2 than the base: 104
        'The one after has two more 2's than the base: 106
        'The one after has three more 2's than the base: 108
        'and so on..
        '
        'Now if I strip the base of its 2, then we could re-state the above as follows:
        'Instead of the base, I am going to have new figure, will call it the "denominator" which is the base stripped off its 2, i.e. 102-2=100
        '
        'Now the first even value (the base, 102) has one 2 more than the denominator and
        'The second even value (104)  has two 2's more than the denominator
        'The third even value (106) has three 2's more than the denominator
        'and so on..
        '
        'so now I could deal with 2 sets of values:
        'The first set, we will call it the "Denominator-cluster" is basically made up of the even values stripped off their 2's. meaning, they are all 100
        '
        'The second set, we will call it the "Two-cluster" is made up of those 2's that are above and beyond the denominator. This is a series made up as one 2, two 2's, three 2's... ten 2's.
        '
        'Now I am going to break up all the even values between 101 and 302 into 10 clusters.
        'As you can see, the "Denominator-clusters" will each have 10 equal values. the first cluster will have 10 equal values each of 100. If I loop through these clusters and step increment each cluster by 20 (10 2's), the second cluster will have 10 equal values of 120 each, the third cluster will have 10 equal values of 140 each.. and so on.
        '
        'To this cluster, I need to add the cluster of 2's. I need to calculate this cluster only once and add it to each one of the "denominator-clusters" and then add all the clusters together which gives me the final total sum.
        '
        'To add the cluster of 2's, I could hard-code that:
        '2+4+6+8...+20=110
        'But once again, if the intent of the puzzle is to be able to substitute for other values, I will use a loop to add these incrementing 2's, which means I will loop 10 times to calculate the cluster of 2's then loop another 10 times to calculate the "denominator-clusters" and in each loop I am adding each one of the to the previous total plus the sum of the cluster of 2's so at the end of the last loop. After that I will find the orphan values, the ones that were not included in the clusters and add those to the total which gives me the grand total.
        'Now why did I choose 10 clusters?
        'That produces the "optimal" number of loops in this scenario:
        'If you increase the size of the cluster to reduce their number and thus the number of looping through them, you will increase the number of 2's and the number of their loops.
        'How to arrive at the optimal cluster size?
        'That's for another day another post, but regardless, any number of clusters will beat the standard looping method.
        Here is the script:
        Code:
        'For further explanations, please read the Introduction section
        option strict
        dim bottom_num as n
        dim top_num as n
        dim denominator as n
        dim span as n 'the span between the bottom and top numbers
        dim values_num as n
        dim i as n 'to be used as a counter
        dim twos as n'this will add the 2's that are above and beyond the denominator values
        dim vtotal as n=0
        dim trail_num as n'number of orphan values not in the clusters
        dim trail_values as n'the actual value of the orphan
        dim loop_num as n'how many times we looped so far
        dim last_even as n'this is the value of top-most even number in the series
        bottom_num=101
        top_num=302
        span=top_num-bottom_num
        values_num=int(span/2)+1 'This is how many even number in the span between the bottom and top numbers. The 1 is added to make it all inclusive, i.e. including the bottom as well as the top numbers
        trail_num=mod(values_num,10)
        denominator=bottom_num-if(mod(bottom_num,2)=0,0,1)
        'First will add the "Two-cluster"
        for i=1 to 10
        	twos=twos+i*2
        next i
        loop_num=i
        'Now we will iterate over the "denominator-clusterss" , each cluster will have a denomiator value of 20 more the one before
        for i=1 to 10
        vtotal=vtotal+(denominator+20*(i-1))*10+twos
        next i
        loop_num=loop_num+i
        'Now we will add the trailing numbers, those that were not included in the 10 clusters
        'Find the last even number:
        last_even=if(mod(top_num,2)=0,top_num,top_num-1)
        for i= 1 to trail_num step-1
        	trail_values=trail_values+last_even
        	last_even=last_even-2
        next
        loop_num=loop_num+1	
        vtotal=vtotal+trail_values
        msgbox("The sum total of all even numbers is: "+vtotal+" "+"and the number of loops is: "+loop_num)
        Last edited by G Gabriel; 12-26-2011, 02:49 PM.

        Comment

        Working...
        X