Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Numbers in sequence

Status
Not open for further replies.

Crazier

Programmer
Apr 24, 2002
10
US
I need to write a program that adds numbers in sequence that add up to 10000. My problem is that I don't know how many numbers it has. I have been racking my brains trying to figure out how to do this. I figured a For,Next with a Do while loop in the for next but how do I write these numbers without knowing how many.

Any suggestions would be appreciated.
Thanks
 
Starting number for the sequence? or ending number for the sequence ? and must it be exactly equal to 10,000 ?
For/next loops are fine for stuff, but you can build your own loops without worrying about the ideosyncracies of for/next.
Total numbers in the sequence are variable over a wide range, depending on where the sequence starts or ends. Ed Fair
unixstuff@juno.com
Any advice I give is my best judgement based on my interpretation of the facts you supply. Help increase my knowledge by providing some feedback, good or bad, on any advice I have given.
 
The starting number is supposed to be determined by the computer in the program. And yes it has to be exactly 10000.
The actual question is "Write a program that will output the first integer and last integer for each and every series of positive, consecutive integers whose sum is equal to 10000.
First answer 18 .... 2002.
Any suggestions with this added info.
Thank you so much.
 
never mind, i misread your second post, but is that like what you want?
 
This is homework? right?
Post what you have and you might get help.
There is a strong TT policy against doing any original work for students. Help is OK. Ed Fair
unixstuff@juno.com
Any advice I give is my best judgement based on my interpretation of the facts you supply. Help increase my knowledge by providing some feedback, good or bad, on any advice I have given.
 
This is what I have please let me know if I am on the right track
CLS
DIM total(1 TO 5000)
firstNumber = 1
lastNumber = 2

FOR Sequence = 1 TO 4999
FOR lastSequence = 1 TO 5000
total = firstNumber + lastNumber
IF total + lastNumber <> 10000 THEN
lastNumber = lastNumber + 1
ELSE
PRINT firstNumber, lastNumber
END IF
NEXT lastSequence
firstNumber = firstNumber + 1
IF total = 10000 THEN
PRINT firstNumber, lastNumber
END IF
NEXT Sequence
 
I, too, don't quite understand what it is that you are trying to do.

However, I can tell you that you do not need to DIM the variable TOTAL since it will be automatically assinged as a SINGLE precision (max/min: ±3.402823 E+38 / ±1.401298 E-45 respectfully) variable and 10,000 should not exceed the threshold.

Unless you typecast it otherwise. --MiggyD

Never be afraid to try something new. Remember that amateurs built the Ark. Professionals built the Titanic.
 
nested for/next loop is all you need

for f=1 to 3333
l=f+1
t=0
for n=f to l
t=t+n
next n
conditional branches based on t
if less than 10000 l=l+1 and loop thru total again
if 10000 then print f & l
next f

Slow but ends up with the 4 sets of numbers you need. Ed Fair
unixstuff@juno.com
Any advice I give is my best judgement based on my interpretation of the facts you supply. Help increase my knowledge by providing some feedback, good or bad, on any advice I have given.
 
Actually, you can do this in linear time (this means FAST) :)
[tt]
first& = 0
last& = 0
sum& = 0
DO
DO

last& = last& + 1
sum& = sum& + last&
LOOP UNTIL sum& >= 10000
DO WHILE sum& > 10000
sum& = sum& - first&
first& = first& + 1
LOOP
IF
sum& = 10000 THEN PRINT first&; &quot;..&quot;; last&
LOOP UNTIL first& > 5001
[/tt]
Enjoy ^_^
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top