Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Desafio 14 e 17 #10

Open
jjgirotto opened this issue Sep 14, 2023 · 4 comments
Open

Desafio 14 e 17 #10

jjgirotto opened this issue Sep 14, 2023 · 4 comments

Comments

@jjgirotto
Copy link

Oi Cami, tudo bem?
Estou com dificuldade no desafio 14 e 17 por conta dos números primos.
Poderia me ajudar com o código por favor?
tentei o 14 assim:
public class Desafio14 {
public static void main(String[] args) {
List numeros = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 4, 3);

    //Encontre o maior número primo da lista:
    int maiorPrimo = numeros.stream()
        .filter(n -> (n % n == 0 && n % 1 == 0));
        .mapToInt(Integer::intValue)
        .max()
    System.out.println(maiorPrimo);
}

}
mas não faz sentido, então não consegui fazer o 17 por ser parecido também

@andersoncpdq
Copy link

Boa noite.

A questão 14 resolvi desta forma:

  Integer maxPrime = numbers.stream().filter(n -> {
      if (n < 2)
          return false;
      for (int i = 2; i < n; i++) {
          if (n % i == 0)
              return false;
      }
      return true;
  }).max(Comparator.naturalOrder()).orElse(null);

Já a questão 17 é bem semelhante:

    numbers.stream().filter(n -> {
        if (n < 2)
            return false;
        for (int i = 2; i < n; i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }).forEach(System.out::println);

Espero ter ajudado. =)

@cami-la
Copy link
Collaborator

cami-la commented Sep 25, 2023

Temos várias formas de resolver esses desafios, tá?
Segue uma issue que eu respondi, sugerindo uma forma de resolver: #11 (comment)

Veja se te ajuda. Qualquer dúvida é só falar. (:

@williamlimasilva
Copy link

williamlimasilva commented May 10, 2024

Caso alguém tenha duvidas no desafio 17 resolvi desse jeito
public static void main(String[] args) {
List numeros = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 4, 3);
System.out.print("Numeros primos da lista: "+numeros.stream()
.filter(n -> {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
})
.toList());
}

@williamlimasilva
Copy link

Quem tiver dificuldade para o Desafio 14
public static void main(String[] args) {
List numeros = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 4, 3);
numeros.stream()
.filter(n -> {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
})
.max(Comparator.naturalOrder())
.ifPresentOrElse(n-> System.out.println("Maior numero primo da lista: "+n),()-> System.out.println("Não foi encontrado numeros primos"));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants