Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP 3 Classic ASP Active Server Pages 3.0 > Classic ASP Basics
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Display Modes
  #1 (permalink)  
Old June 28th, 2003, 03:59 PM
Registered User
Join Date: Jun 2003
Location: Chicago, IL, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sum of All Integers Between 2 numbers

I have a beginning value and an end value inputted by a user. I need to sum all of the numbers between the first and last value. What is a good do way to do this? Is a Do While loop or a For Each loop. I inderstand that I need to take the beginning variable and increment it by one until it equals that end value and then add them all together but I'm not sure how to put it together with the code.
Reply With Quote
  #2 (permalink)  
Old June 30th, 2003, 05:12 AM
arp arp is offline
Authorized User
Join Date: Jun 2003
Location: Mumbai, Maharashtra, India.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts

hi!! arp here.

You can use following code for your requirement.

    Dim numOne, numTwo, count
    count = 0
    numOne = Request.Form("FirstVal")
    numTwo = Request.Form("SecondVal")

    For i = (numOne+1) to (numTwo -1)
        count = count + i

    Response.Write("count = " & count)


If you want to include starting number and end number also simply change

"For i = (numOne+1) to (numTwo -1)" To "For i = (numOne) to (numTwo)"
Reply With Quote
  #3 (permalink)  
Old June 30th, 2003, 06:21 AM
Friend of Wrox
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post

You don't need a loop for this because there's a known mathematical formula for summing numbers from 1 to n, which is T(n) = n(n+1)/2, e.g. sum of all integers from 1 to 10 is T(10) = 10(10+1)/2 = 55.

So if you want to sum all numbers from say, 3 to 10, just do T(10) - T(2) = 55 - 3 = 52.
Also, if you don't want to include the 3 and 10 in the sum then just take them off the answer.

If you really insist on a loop-type thing, then recursion is much neater:
Function CalcSum(nStart, nEnd)

    If nEnd = nStart Then
        CalcSum = nStart
        CalcSum = nEnd + CalcSum(nStart, nEnd - 1)
    End If

End Function
Reply With Quote
  #4 (permalink)  
Old July 3rd, 2003, 04:17 PM
Authorized User
Join Date: Jun 2003
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts

I think it is best to follow the mathematical formula example that PGTIPS pointed out. If you put no bounds on this calculation, suppose the user "wanted" to sum 1 to 1,000,000,000? Whether you use recursion or a loop, this is going to take a lot of processing time, not good for a web server.

If someone wanted to crash your site and you used recursion w/o boundary conditions, they could open multiple sessions and request huge sums as above. Each recursion is going to use more memory as variables and status registers get pushed onto the stack. Eventually your system could run out of memory and be unable to continue the recursion, yet unable to release memory by "unwinding" the recursive calls. Thus, locking up the server.

Of course, if you do not need to access any server info, you could just make this script local to the client. They might still be able to crash their own computer though (or at least their browser) just from simple ignorance.

For this reason, I would not use recursion w/o some boundary checking, even though it is the more elegant solution to some problems. Of course, it might take billions of recursions to actually use up all or a significant portion of the servers memory :)
Reply With Quote
  #5 (permalink)  
Old July 6th, 2003, 01:01 PM
Registered User
Join Date: Jun 2003
Location: Chicago, IL, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts

I got -- thanks for everyone's help!

Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unsigned integers AndrewH Java Basics 1 September 26th, 2006 02:46 PM
Help: Running Sum (or Cumulative Sum) timdasa VB Databases Basics 1 August 22nd, 2006 03:12 PM
How Do I Sum Numbers In a TextBox wb8bgy Visual Basic 2005 Basics 1 December 10th, 2005 12:29 PM
Sum of random numbers in a table with XSLT Tschuri XSLT 0 May 8th, 2005 04:14 AM
how can i convert strings into integers amahja56 C++ Programming 2 February 27th, 2004 05:27 PM

All times are GMT -4. The time now is 04:13 AM.

Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.