2

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

1 Answers1

4
  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.")