You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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"));
}
Activity
andersoncpdq commentedon Sep 18, 2023
Boa noite.
A questão 14 resolvi desta forma:
Já a questão 17 é bem semelhante:
Espero ter ajudado. =)
cami-la commentedon 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 commentedon 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 commentedon May 10, 2024
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"));
}