How could I print the odd pyramid numbers and the sum between them?
In order to add the odd ones we need to know the index and the maximum and final position of each row of the pyramid.
For example, these could be the initial indices:
1 < i = 0
3 < i = 1
7 < i = 3
13 < i = 6
21 < i = 10
So the variable i
represents the initial index of each element of the pyramid ({1, 3, 7, 13, 21}
).
However, we need to know what the final index of X
pyramid row.
The final indices would be:
Indice: Inicial Final
1 (i = 0, n = 1)
3 5 (i = 1, n = 3)
7 9 11 (i = 3, n = 6)
13 15 17 19 (i = 6, n = 10)
21 23 25 27 29 (i = 10, n = 15)
What is the use of knowing these indices?
It helps us to go through each row of the pyramid and in this way we can know which are the odd or even numbers. An analogy would be an array of integers, each row has X
elements, in which, we could get the sum of all the even numbers. This is similar to a pyramid of elements. The difference is that in a matrix the final index is always constant (the size of the column is the same for each row) and in a pyramid of elements it varies.
The million dollar question: How on earth do I get the final index of each row?
With this simple formula:
N = row + i;
Where:
N
is the final index.
row
is the number of the row.
i
is the initial index.
Let's start evaluating some rows:

In the 1st row i = 0
Y row = 1
, at the time of replacing in the formula gives us as a result: N = 1 + 0 = 1
.

In the 2nd row i = 1
Y row = 2
, when replacing gives us: N = 2 + 1 = 3
.

In the 3rd i = 3
Y row = 3
, when replacing, N = 3 + 3 = 6
.
And so on we are replacing the values in each row.
With this we already have the problem solved. Now let's start modeling our classes.
The first class we will have will be PyramidRow
. In this class we will have the necessary attributes to be able to store the initial and final index of X
row.
Example:
class PyramidRow
{
private int begin; //índice inicial
private int max;//índice final
public PyramidRow(int begin, int max)
{
this.begin = begin;
this.max = max;
}
public int getBegin()
{
return begin;
}
public void setBegin(int begin)
{
this.begin = begin;
}
public int getMax()
{
return max;
}
public void setMax(int max)
{
this.max = max;
}
}
Then we will create another class called PyramidRowList
in which it will serve to create a list of objects of type PyramidRow
. In this way we will have the information (initial and final index) of each row in a list.
class PyramidRowList
{
private List listRows;
public PyramidRowList()
{
listRows = new ArrayList<>();
}
public List getListRows()
{
return listRows;
}
public void setListRows(List listRows)
{
this.listRows = listRows;
}
//El parámetro length es para guardar el tamaño del vector
public void calculateRow(int length)
{
int i = 0, n, row = 1;
while(true)
{
n = row + i;
if(n > length)
break;
listRows.add(new PyramidRow(i, n));
for(; i < n; i++){}
row++;
}
}
}
In the method calculateRow
is where we are going to calculate the starting and ending index of each row of the pyramid and then we will save it as an object in the list.
Finally, we only need to go through the list of objects and have access to the initial and final index of each row:
public class Program
{
public static void main(String() arg)
{
int impares = 0;
int lengthPyramid;
int() nums = new int(){1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29};
//Creamos el objeto
PyramidRowList listRows = new PyramidRowList();
//Calculamos los índice iniciales y finales de cada fila
listRows.calculateRow(nums.length);
//Almacenamos la cantidad de filas que tenga la piramide
lengthPyramid = listRows.getListRows().size();
//Recorremos la lista
for(PyramidRow row : listRows.getListRows())
{
for(int i = lengthPyramid  1; i != 0; i)
System.out.print("t");
for(int i = row.getBegin(); i < row.getMax(); i++)
{
System.out.print(nums(i) + "tt");
if(nums(i) % 2 != 0)
impares += nums(i);
}
System.out.print("= "+ impares);
System.out.println("");
lengthPyramid;
impares = 0;
}
}
}
class Program
{
public static void main(String() arg)
{
int suma = 0;
int() nums = new int(){1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29};
for(int i = 0; i != nums.length; ++i)
{
if(nums(i) % 2 != 0)
{
suma += nums(i);
System.out.println(nums(i));
}
}
System.out.println("La suma de los numeros impares es: "+ suma);
}
}
And ready, on the screen you should give us this: