Before the conditions of the development of a program were issued, I had already made an example, but I did everything using arrays, but it is not allowed to use data structures, or arrays, or lists, or other elements in addition to the main one (main ):
import java.util.Scanner;
import java.util.Arrays;
public class Tarea {
public static void main(String[] args) {
Scanner l = new Scanner(System.in);
int[] valores = {15, 94, 100, 7, 6, 71};
int numAbus;
System.out.print("ingrese el numero a buscar: ");
numAbus = l.nextInt();
int pos = buscar(valores, numAbus);
if (pos == -1) {
System.out.println("el valor no esta dentro del arreglo");
} else {
System.out.print("\nel numero se encuentra en la posicion: " + pos);
}
}
static int buscar(int[] valores, int numAbus) {
Arrays.sort(valores);
return buscarB(valores, numAbus, 0, valores.length - 1);
}
static int buscarB(int[] valores, int numAbus, int posIzq, int posDer) {
int centro = (posIzq + posDer) / 2;
if (posIzq > posDer) {
return -1;
} else if (valores[centro] == numAbus) {
return centro;
} else if (numAbus < valores[centro]) {
return buscarB(valores, numAbus, posIzq, centro - 1);
} else {
return buscarB(valores, numAbus, centro + 1, posDer);
}
}
}
But given the following statement:
I got lost, all I got is this:
int n = (); //lo que haya ingresado el usuario
int posicion = 0;
float suma = 0;
for (int i = 0; i < n; i++) {
posicion = i;
suma += (); //la función en términos de posicion
}
Can you tell me what else I need?
The first thing you have to do is discover what the formula of the sequence is so that you can then apply it within a cycle.
From what I could analyze, this would be composed of several parts, which would be:
()^(1/n)
, and if n is odd=()^(n)
. Edit: It can also be implemented as()^((n)^((-1)^((n + 1) % 2)))
round((2n)^(1/2))
, therefore, to obtain the sign we can implement the following:(-1)^(round((2n)^(1/2))
.(n-2)
and the previous numeratorThe implementation in a program would be something like this (It's in C++, I don't have Java, but the logic is the same):
The data delivered by the program for the sum of the first 6 elements is: