# Programming code with BASIC

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!)

-
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
My condolences on BASIC. – vonbrand Mar 8 '13 at 16:41
@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. 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$
7. PRINT ($\mathrm{NextLevel}-1$, " levels completed.")
8. IF ($\mathrm{BallsAvailable}>0$) THEN PRINT("There are ",$\mathrm{BallsAvailable}$," balls left.")