0

Pretty new to Java. I'm trying to create code that finds out which square pyramidal number is itself a perfect square.

The square pyramidal number basically refers to the total number of balls in a pyramid that has a layer number of n. I'm looking for n whose total number of balls is a perfect square. I've figured it out mathematically (there are only two values of n that work--1 and 24, which produce a square pyramidal number of 1 and 4900, respectively), but I don't know how to make code that will figure it out automatically.

This is the code that I have right now, which requires inputting integers into n in order to find your answer. I'm trying to figure out how to make a loop that will produce the answers of 1 and 24 on it's own.

class SquarePyramidalNumber
{
    public static void main(String[] args)
    {
        double t,n=24;
        t=(n*(n+1)*(2*n+1))/6;
        System.out.println("\n");
        System.out.println("Layer number = " + n );
        System.out.println("Number of balls in layer = " + n*n );
        System.out.println("Total number of balls = " + t );
        System.out.println("Square root of total number of balls = " + Math.sqrt(t) );
        System.out.println("\n");
    }
}

Hopefully I explained myself well! Thank you for any and all help.

Cœur
  • 37,241
  • 25
  • 195
  • 267
kr3000
  • 3
  • 2

1 Answers1

0

First of all, you will notice that each layer of balls in the pyramid is formed by a number of balls that is a perfect square (1, 4, 9, 16, ...) So the total number of balls of a pyramid that has a base of, lets say n balls, the pyramid number is going to be

n + 
(sqrt(n)-1)^2 +
(sqrt(n)-2)^2 +
.. +
1

So, start by splitting your problem. First of all, you have to know if a number is a pyramid, so let's create a method like this:

private boolean isPyramid(int n) 

You also have to know if a number is a perfect square:

private boolean isPerfectSquare(int n)

after filling these methods, you could just loop and look for the numbers that fulfil both conditions:

int i=1;
while(true){ //this will run forever so be careful
  if (isPyramid(i) && isPerfectSquare(i)){
    System.out.println("Found one! "+i);
  }
  i++;
}

Now, your task is to fill both methods with code :)

Sebastian Breit
  • 6,137
  • 1
  • 35
  • 53
  • I'm not completely understanding your answer. Like I said, I'm pretty new to Java. Is the equation I have above not sufficient? I was thinking I needed to find a loop that somehow terminates when the result of Math.sqrt(t) is an integer. Am I going in the wrong direction? My code works for finding my answer, but it requires manually inputting different values of n until you reach two values of n whose square root of t is an integer. I apologize for not quite grasping your answer. Thank you so much for the response! – kr3000 Apr 01 '16 at 16:19
  • Thank you, this makes a lot of sense to me! I think I should be able to figure out my problem from here. Again, thank you, I really appreciate it! – kr3000 Apr 01 '16 at 21:00