For...Next Loop question

C

CWatsonJr

I typed up a small for...next loop in Microsoft's Professional Basic an
I was wondering if there was anyway to run this in Excel. The proble
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 billio
(0+1+2+3+4+...1000000000)

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

Thanks,

Cliff Watso
 
G

Gary Keramidas

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.
 
G

Gary Keramidas

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
 
H

Helmut Weber

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"
 
H

Helmut Weber

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"
 
G

Gary Keramidas

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
 
S

SmilingPolitely

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
 
H

Helmut Weber

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"
 
C

CWatsonJr

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
 
T

Tom Ogilvy

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top