Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I have to program in BASIC a code to do this:

From a collection of 10,000 cannonballs, a square based pyramid is built with a single cannonball on top and a square number on each layer. How many layers can be made? How many cannonballs are left over?

(I have tried 50 different codes BUT ALL of them throw the incorrect answers... Please if you can help me I would appreciate it very much!)

share|improve this question
1  
Build a loop that continuously adds square numbers until the sum reaches 10,000. The number before that is your answer. –  Joe Z. Mar 8 '13 at 16:40
1  
My condolences on BASIC. –  vonbrand Mar 8 '13 at 16:41
1  
@Amzoti I don't get your hint. A better one would be OEIS A000330, I think. –  Hagen von Eitzen Mar 8 '13 at 16:48
    
@HagenvonEitzen: maybe I read it wrong. I thought the statement was a single ball on top, then the next row would have a square number (2^2 = 4), then the next would have 3^2 and so on. The table was showing the sum of the total number of balls used. I removed the hint as I am not sure what was meant and seeing the OEIS makes me think you are reading it correctly. –  Amzoti Mar 8 '13 at 16:51
    
So basically "10 LET S = 0 | 20 LET N = 0 | 30 LET N = N + 1 | 40 LET S = N * N + S | 50 IF S < 10000 THEN 30 | 60 PRINT N - 1", right? –  Joe Z. Mar 8 '13 at 17:06

1 Answer 1

up vote 4 down vote accepted
  1. LET $\mathrm{BallsAvailable}\leftarrow 10000$
  2. LET $\mathrm{NextLevel}\leftarrow 1$
  3. IF ($\mathrm{BallsAvailable}<\mathrm{NextLevel}^2$) GOTO 7
  4. LET $\mathrm{BallsAvailable}\leftarrow \mathrm{BallsAvailable}-\mathrm{NextLevel}^2$
  5. LET $\mathrm{NextLevel}\leftarrow \mathrm{NextLevel}+1$
  6. GOTO 3
  7. PRINT ($\mathrm{NextLevel}-1$, " levels completed.")
  8. IF ($\mathrm{BallsAvailable}>0$) THEN PRINT("There are ",$\mathrm{BallsAvailable}$," balls left.")
share|improve this answer
    
Great thanks very much! –  Santz Mar 8 '13 at 20:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.