Skip to content

Commit

Permalink
resolucao LAB1
Browse files Browse the repository at this point in the history
  • Loading branch information
vigusmao committed Apr 5, 2021
1 parent 80e0679 commit bca759c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 12 deletions.
46 changes: 46 additions & 0 deletions Primos/src/ArrayComRodinhas.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.util.Arrays;

public class ArrayComRodinhas {

int[] arrayInterno;

private int quantElementos;


public ArrayComRodinhas() {
this.arrayInterno = new int[1];
this.quantElementos = 0;
}

public void add(int elemento) {
if (this.quantElementos == this.arrayInterno.length) {
redimensionarArrayInterno();
}

this.arrayInterno[this.quantElementos++] = elemento;
}

public int get(int posicao) {
if (posicao < 0 || posicao >= this.quantElementos) {
throw new ArrayIndexOutOfBoundsException(posicao);
}
return this.arrayInterno[posicao];
}

private void redimensionarArrayInterno() {
int[] novoArray = new int[this.arrayInterno.length * 2]; // cresce em PG
for (int i = 0; i < quantElementos; i++) {
novoArray[i] = this.arrayInterno[i];
}
this.arrayInterno = novoArray;
}

public int getQuantElementos() {
return this.quantElementos;
}

public String toString() {
return Arrays.toString(this.arrayInterno);
}

}
57 changes: 45 additions & 12 deletions Primos/src/Primos.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,44 @@ public class Primos {
* @return um array de inteiros com os primos no intervalo dado.
*/
public static int[] obterPrimos(int n) {
// ToDo IMPLEMENT ME!!!
ArrayComRodinhas meuArrayComRodinhas = new ArrayComRodinhas();

return null;
for (int x = 2; x <= n; x++) {
if (ehPrimo(x)) {
meuArrayComRodinhas.add(x);
}
}

int[] apenasPrimos = new int[meuArrayComRodinhas.getQuantElementos()];
for (int i = 0; i < meuArrayComRodinhas.getQuantElementos(); i++) {
apenasPrimos[i] = meuArrayComRodinhas.get(i);
}

return apenasPrimos;
}

private static boolean ehPrimo(int x) {
if (x < 2) {
return false;
}
if (x == 2) {
return true;
}
if (x % 2 == 0) {
return false;
}

for (int divisor = 3; divisor * divisor <= x; divisor += 2) {
if (x % divisor == 0) {
return false;
}
}

return true;
}



public static int[] obterPrimosViaCrivo(int n) {
// ToDo IMPLEMENT ME!!!

Expand All @@ -21,7 +54,7 @@ public static int[] obterPrimosViaCrivo(int n) {

public static void main(String[] args) {

for (int n = 10; n <= 10_000; n *= 10) {
for (int n = 1; n <= 20; n++) {

long inicio = System.currentTimeMillis();
int[] primos = obterPrimos(n);
Expand All @@ -37,17 +70,17 @@ public static void main(String[] args) {
if (i < primos.length - 1) {
System.out.printf("%d, ", x);
} else {
System.out.printf("%d", x);
System.out.printf("%d\n", x);
}
}

// ou...
for (int x : primos) { // for each... (para cada elemento de "primos"...)
System.out.println(x);
}

// ou...
System.out.println(Arrays.toString(primos));
//
// // ou...
// for (int x : primos) { // for each... (para cada elemento de "primos"...)
// System.out.println(x);
// }
//
// // ou...
// System.out.println(Arrays.toString(primos));
}
}
}
25 changes: 25 additions & 0 deletions Primos/src/Principal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.Random;

public class Principal {

public static void main(String[] args) {

Random random = new Random();

ArrayComRodinhas meuArray = new ArrayComRodinhas();

int N = 100_000;

long inicio = System.currentTimeMillis();
for (int i = 0; i < N; i++) {
meuArray.add(random.nextInt(100));
}
long duracao = System.currentTimeMillis() - inicio;

System.out.printf("duracao = %.3f\n", duracao / 1000f);

System.out.printf("O array agora tem %d elementos (o tamanho físico é %d).",
meuArray.getQuantElementos(), meuArray.arrayInterno.length);

}
}

0 comments on commit bca759c

Please sign in to comment.