+ Reply to Thread
Results 1 to 13 of 13

For...Next Loop question

  1. #1
    Forum Contributor
    Join Date
    02-27-2004
    Location
    California, United States
    MS-Off Ver
    Excel 2016
    Posts
    315

    For...Next Loop question

    I typed up a small for...next loop in Microsoft's Professional Basic and I was wondering if there was anyway to run this in Excel. The problem with Basic is that the sum is too large and it will lock my computer.

    Here is the code...

    ***************************************************

    CLS

    Goal# = 1000000000

    SumNUM# = 0

    FOR Count# = 0 TO Goal#

    SumNUM# = Count# + SumNUM#
    IF Count# = Goal# THEN
    EXIT FOR
    END IF
    NEXT Count#


    LOCATE 10, 10
    COLOR 14, 1
    PRINT SumNUM#

    LOCATE 15, 10
    COLOR 14, 1
    PRINT Count#


    ***********************************************


    The goal is to add all the whole numbers 0 to 1 billion (0+1+2+3+4+...1000000000)

    I know the formula for the Gauss trick (1 to n) but it doesn't work for this because it starts with zero instead of one... and then there is the other thing... 0 shouldn't matter because when you add zero... well, its obvious. Anyway, is there anyway to get Excel to do this?

    Thanks,

    Cliff Watson

  2. #2
    Gary Keramidas
    Guest

    Re: For...Next Loop question

    i think the largest value may be 32767. at least that's all that works for
    me. someone more knowledgeable may have some other ideas.

    --


    Gary


    "CWatsonJr" <[email protected]> wrote in
    message news:[email protected]...
    >
    > I typed up a small for...next loop in Microsoft's Professional Basic and
    > I was wondering if there was anyway to run this in Excel. The problem
    > with Basic is that the sum is too large and it will lock my computer.
    >
    > Here is the code...
    >
    > ***************************************************
    >
    > CLS
    >
    > Goal# = 1000000000
    >
    > SumNUM# = 0
    >
    > FOR Count# = 0 TO Goal#
    >
    > SumNUM# = Count# + SumNUM#
    > IF Count# = Goal# THEN
    > EXIT FOR
    > END IF
    > NEXT Count#
    >
    >
    > LOCATE 10, 10
    > COLOR 14, 1
    > PRINT SumNUM#
    >
    > LOCATE 15, 10
    > COLOR 14, 1
    > PRINT Count#
    >
    >
    > ***********************************************
    >
    >
    > The goal is to add all the whole numbers 0 to 1 billion
    > (0+1+2+3+4+...1000000000)
    >
    > I know the formula for the Gauss trick (1 to n) but it doesn't work for
    > this because it starts with zero instead of one... and then there is the
    > other thing... 0 shouldn't matter because when you add zero... well, its
    > obvious. Anyway, is there anyway to get Excel to do this?
    >
    > Thanks,
    >
    > Cliff Watson
    >
    >
    > --
    > CWatsonJr
    > ------------------------------------------------------------------------
    > CWatsonJr's Profile:
    > http://www.excelforum.com/member.php...fo&userid=6603
    > View this thread: http://www.excelforum.com/showthread...hreadid=466565
    >




  3. #3
    Gary Keramidas
    Guest

    Re: For...Next Loop question

    this is what i tried

    Sub test()
    Dim goal As Long
    Dim sumnum As Long
    Dim count As Integer

    goal = 32767

    sumnum = 0

    For count = 0 To goal

    sumnum = count + sumnum
    If count = goal Then
    Exit For
    End If
    Next count
    MsgBox sumnum

    End Sub

    --


    Gary


    "CWatsonJr" <[email protected]> wrote in
    message news:[email protected]...
    >
    > I typed up a small for...next loop in Microsoft's Professional Basic and
    > I was wondering if there was anyway to run this in Excel. The problem
    > with Basic is that the sum is too large and it will lock my computer.
    >
    > Here is the code...
    >
    > ***************************************************
    >
    > CLS
    >
    > Goal# = 1000000000
    >
    > SumNUM# = 0
    >
    > FOR Count# = 0 TO Goal#
    >
    > SumNUM# = Count# + SumNUM#
    > IF Count# = Goal# THEN
    > EXIT FOR
    > END IF
    > NEXT Count#
    >
    >
    > LOCATE 10, 10
    > COLOR 14, 1
    > PRINT SumNUM#
    >
    > LOCATE 15, 10
    > COLOR 14, 1
    > PRINT Count#
    >
    >
    > ***********************************************
    >
    >
    > The goal is to add all the whole numbers 0 to 1 billion
    > (0+1+2+3+4+...1000000000)
    >
    > I know the formula for the Gauss trick (1 to n) but it doesn't work for
    > this because it starts with zero instead of one... and then there is the
    > other thing... 0 shouldn't matter because when you add zero... well, its
    > obvious. Anyway, is there anyway to get Excel to do this?
    >
    > Thanks,
    >
    > Cliff Watson
    >
    >
    > --
    > CWatsonJr
    > ------------------------------------------------------------------------
    > CWatsonJr's Profile:
    > http://www.excelforum.com/member.php...fo&userid=6603
    > View this thread: http://www.excelforum.com/showthread...hreadid=466565
    >




  4. #4
    Helmut Weber
    Guest

    Re: For...Next Loop question

    Hi Cliff,

    like this, but only in theory:

    Dim dbl As Double
    Dim sum As Double ' unsufficient anyway
    For dbl = 0 To 1000000000
    sum = sum + dbl
    Next
    MsgBox sum

    did You think about how many digits would be needed
    to display this number in ordinary writing?

    Has nothing to do with Excel, by the way.

    I like playing around with large numbers.

    Have you ever thought of how many different
    pictures can be displayed on a screen with
    1064 x 768 pixels and 16777216 colors?

    There will be a pretty accurate portrait of you,
    when you are 64, among all these pictures.

    --
    Greetings from Bavaria, Germany

    Helmut Weber, MVP WordVBA

    Win XP, Office 2003
    "red.sys" & Chr$(64) & "t-online.de"







  5. #5
    Gary Keramidas
    Guest

    Re: For...Next Loop question

    that took about a minute and a half to calc.

    --


    Gary


    "Helmut Weber" <[email protected]> wrote in message
    news:[email protected]...
    > Hi Cliff,
    >
    > like this, but only in theory:
    >
    > Dim dbl As Double
    > Dim sum As Double ' unsufficient anyway
    > For dbl = 0 To 1000000000
    > sum = sum + dbl
    > Next
    > MsgBox sum
    >
    > did You think about how many digits would be needed
    > to display this number in ordinary writing?
    >
    > Has nothing to do with Excel, by the way.
    >
    > I like playing around with large numbers.
    >
    > Have you ever thought of how many different
    > pictures can be displayed on a screen with
    > 1064 x 768 pixels and 16777216 colors?
    >
    > There will be a pretty accurate portrait of you,
    > when you are 64, among all these pictures.
    >
    > --
    > Greetings from Bavaria, Germany
    >
    > Helmut Weber, MVP WordVBA
    >
    > Win XP, Office 2003
    > "red.sys" & Chr$(64) & "t-online.de"
    >
    >
    >
    >
    >
    >




  6. #6
    Helmut Weber
    Guest

    Re: For...Next Loop question

    Hi Gary,

    exactly this one:

    Dim dbl As Double
    Dim sum As Double ' unsufficient anyway
    For dbl = 0 To 1000000000
    sum = sum + dbl
    Next
    MsgBox sum

    what kind of futuristic mainframe have you got?

    There are 100 Million additions only
    between 900,000,000
    and 1,000,000,000

    and with each of the 100 millions a value
    of greater than 900,000,000 would be added.

    I wonder.

    --
    Greetings from Bavaria, Germany

    Helmut Weber, MVP WordVBA

    Win XP, Office 2003
    "red.sys" & Chr$(64) & "t-online.de"




  7. #7
    Gary Keramidas
    Guest

    Re: For...Next Loop question

    i only have an athlon x64.this is the code i used:

    Sub t3()
    Dim dbl As Double
    Dim sum As Double ' unsufficient anyway
    For dbl = 0 To 1000000000
    sum = sum + dbl
    Next
    MsgBox sum
    End Sub

    came out to some exponential number: 5.00000000067109E+17
    --


    Gary


    "Helmut Weber" <[email protected]> wrote in message
    news:[email protected]...
    > Hi Gary,
    >
    > exactly this one:
    >
    > Dim dbl As Double
    > Dim sum As Double ' unsufficient anyway
    > For dbl = 0 To 1000000000
    > sum = sum + dbl
    > Next
    > MsgBox sum
    >
    > what kind of futuristic mainframe have you got?
    >
    > There are 100 Million additions only
    > between 900,000,000
    > and 1,000,000,000
    >
    > and with each of the 100 millions a value
    > of greater than 900,000,000 would be added.
    >
    > I wonder.
    >
    > --
    > Greetings from Bavaria, Germany
    >
    > Helmut Weber, MVP WordVBA
    >
    > Win XP, Office 2003
    > "red.sys" & Chr$(64) & "t-online.de"
    >
    >
    >




  8. #8
    SmilingPolitely
    Guest

    Re: For...Next Loop question

    One way to check whether the answer is correct is to use the fact:

    1+2=3
    1+2+3=6
    1+2+3+4=10
    1+2+3+.....+n=(0.5*n)*(n+1)

    so if n=1,000,000,000
    0.5*n=500,000,000
    n+1=1,000,000,001

    result=500,000,000,500,000,000

    I'd check your Athlon processor! LOL

    :D

    Gary Keramidas wrote:
    > i only have an athlon x64.this is the code i used:
    >
    > Sub t3()
    > Dim dbl As Double
    > Dim sum As Double ' unsufficient anyway
    > For dbl = 0 To 1000000000
    > sum = sum + dbl
    > Next
    > MsgBox sum
    > End Sub
    >
    > came out to some exponential number: 5.00000000067109E+17


  9. #9
    Gary Keramidas
    Guest

    Re: For...Next Loop question

    it adds the numbers i need to use ok<g>

    --


    Gary


    "SmilingPolitely" <[email protected]> wrote in message
    news:[email protected]...
    > One way to check whether the answer is correct is to use the fact:
    >
    > 1+2=3
    > 1+2+3=6
    > 1+2+3+4=10
    > 1+2+3+.....+n=(0.5*n)*(n+1)
    >
    > so if n=1,000,000,000
    > 0.5*n=500,000,000
    > n+1=1,000,000,001
    >
    > result=500,000,000,500,000,000
    >
    > I'd check your Athlon processor! LOL
    >
    > :D
    >
    > Gary Keramidas wrote:
    >> i only have an athlon x64.this is the code i used:
    >>
    >> Sub t3()
    >> Dim dbl As Double
    >> Dim sum As Double ' unsufficient anyway
    >> For dbl = 0 To 1000000000
    >> sum = sum + dbl
    >> Next
    >> MsgBox sum
    >> End Sub
    >>
    >> came out to some exponential number: 5.00000000067109E+17




  10. #10
    Helmut Weber
    Guest

    Re: For...Next Loop question

    Hi Gary,

    thank you,

    maybe I should update my hardware.

    --
    Greetings from Bavaria, Germany

    Helmut Weber, MVP WordVBA

    Win XP, Office 2003
    "red.sys" & Chr$(64) & "t-online.de"

  11. #11
    Forum Contributor
    Join Date
    02-27-2004
    Location
    California, United States
    MS-Off Ver
    Excel 2016
    Posts
    315
    I agree with your answer - both the scientific notation and the hard number. The problem is with this website for math teachers:

    http://www.nctm.org/high/asolutions.asp?ID=488

    They have this problem posted with an alternate way to solve it and it comes back with an answer that doesn't make sense. That is why I wanted to something in Excel that would show the the math teacher how the long way would match Gauss's method (sum=n*(n+1)/2).

    Here is the reference for the Gauss trick (in case there are any questions):

    http://www.nzmaths.co.nz/HelpCentre/Seminars/Gauss.htm

    Thanks for the help... I don't know what I am going to do with this teacher if I can't prove my case. She said my answer is wrong using the Gauss method and the answer on the math teachers page is right. When I use the formula in Excel, I come up with the same answers you guys did and not her answer.

    Cliff Watson

  12. #12
    Tom Ogilvy
    Guest

    Re: For...Next Loop question

    Your trying to solve the wrong problem. The problem states:

    What is the sum of all the digits needed to write down these numbers?

    It doesn't say what is the sum of all the numbers from 0 to 1 Billion

    As they used to affectionately write on my papers: RTP (read the problem)

    --
    Regards,
    Tom Ogilvy


    "CWatsonJr" <[email protected]> wrote
    in message news:[email protected]...
    >
    > I agree with your answer - both the scientific notation and the hard
    > number. The problem is with this website for math teachers:
    >
    > http://www.nctm.org/high/asolutions.asp?ID=488
    >
    > They have this problem posted with an alternate way to solve it and it
    > comes back with an answer that doesn't make sense. That is why I
    > wanted to something in Excel that would show the the math teacher how
    > the long way would match Gauss's method (sum=n*(n+1)/2).
    >
    > Here is the reference for the Gauss trick (in case there are any
    > questions):
    >
    > http://www.nzmaths.co.nz/HelpCentre/Seminars/Gauss.htm
    >
    > Thanks for the help... I don't know what I am going to do with this
    > teacher if I can't prove my case. She said my answer is wrong using
    > the Gauss method and the answer on the math teachers page is right.
    > When I use the formula in Excel, I come up with the same answers you
    > guys did and not her answer.
    >
    > Cliff Watson
    >
    >
    > --
    > CWatsonJr
    > ------------------------------------------------------------------------
    > CWatsonJr's Profile:

    http://www.excelforum.com/member.php...fo&userid=6603
    > View this thread: http://www.excelforum.com/showthread...hreadid=466565
    >




  13. #13
    Forum Contributor
    Join Date
    02-27-2004
    Location
    California, United States
    MS-Off Ver
    Excel 2016
    Posts
    315
    OH MAN... My brain cells have been hosed...

    Thanks for the wakeup call!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1