# Programming Puzzle 14 - Sum the Squares

• 06-29-2011, 03:34 PM
Tom Cone Jr
Programming Puzzle 14 - Sum the Squares
Write an xbasic script that prompts the user to supply two integer values,
let's call them N1 and N2. Then your script should fetch through the integer
values from N1 to N2 (inclusive) and compute the sum of the squares of
each integer in the range.

Instructions:

values, and then the sum of the squared values.

Your script should provide a graceful way for the user to cancel if they
decide not to supply either input value.

Your script should verify that N2 is greater than N1. If it is not then
an error message should be displayed to the user.

Have fun!
• 06-30-2011, 01:06 PM
aschone
Re: Programming Puzzle 14 - Sum the Squares
No one has attempted this yet? If I have time tonight after work I will take a crack at it. Really curious to see others try it first.
• 06-30-2011, 11:01 PM
aschone
Re: Programming Puzzle 14 - Sum the Squares
Uploaded my script; very minimal testing was done, so please be kind :grin:
• 07-01-2011, 08:36 AM
pmanandhar
Re: Programming Puzzle 14 - Sum the Squares
Attachment 28645
Here is my code for this puzzle... Please forgive me for no comments..
• 07-02-2011, 07:07 PM
Tom Cone Jr
Re: Programming Puzzle 14 - Sum the Squares
Very nice work guys.

Two different approaches.

Andy's code for the input dialog looks complex, but was largely generated by action scripting. It's an example of xdialog code. His code anticipates that the user might enter a non-integer, and discards the decimal portion of both input values. The heavy lifting is done in a function he designed. Notice that the Function can be positioned anywhere in his script. He has it located AFTER his END statement. In the Function he loops through the integer values in the user supplied range without using a For ... Next loop. Instead, he uses While ... End While. Such a code block is executed repeatedly until the condition expression at the top of the loop evaluates to False. Notice that Andy does not pass the input values to his function. The function can "see" the shared variables in the top part of the script. Prof. Pickypicky gives Andy high marks for limiting the input range to integer values, but dings him a couple of demerits for relying on "shared" variables, instead of passing them in as arguments to the Function. Prof. Pickypicky is lazy. If the Function included two numeric arguments for the input values it could be saved to the Code page of the control panel and called from any other place in the application. Code reuse means less work down the line. Less error checking. Easier maintenance. Very satisfying to lazy programmers like Pickypicky.

Pratik's approach gets the same results as Andy's, provided input values are limited to integer values. However, if the user ignores instructions and enters values that have decimals, there is no error trapping or correction to prevent the calc from proceeding. Notice that Pratik's approach relies upon a mathematical expression that does all the heavy lifting in one line, without using a loop. Very handy if you're a trained mathematician. However Prof Pickypicky finds the absence of explanatory comments troubling. ( Perhaps because he's not a trained mathematician ! )

Thanks to both for tackling this. Stay tuned for other puzzles coming soon.
• 08-12-2011, 04:54 PM
jeb richardson
Re: Programming Puzzle 14 - Sum the Squares
Puz 14 attempt.

Attachment 29055
• 08-13-2011, 08:06 AM
Tom Cone Jr
Re: Programming Puzzle 14 - Sum the Squares
Hey, Jeb. Prof. Pickypicky wants to know why the program approves input values if all the user does is click the cancel buttons....

In his experience it's ususally best to provide a way for the user to bail out if they've started the script by mistake, especially in cases where they're asked to supply input values.
• 08-15-2011, 05:28 PM
jeb richardson
Re: Programming Puzzle 14 - Sum the Squares