Skip to content

Desafio 14 e 17 #10

Open
Open

Description

@jjgirotto

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

Activity

  1. andersoncpdq commented on Sep 18, 2023

    @andersoncpdq

    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. =)

  2. cami-la commented on Sep 25, 2023

    @cami-la
    Contributor

    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. (:

  3. williamlimasilva commented on May 10, 2024

    @williamlimasilva

    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());
    }

  4. williamlimasilva commented on May 10, 2024

    @williamlimasilva

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions