Skip to content

Instantly share code, notes, and snippets.

@mnadeem
Last active August 6, 2020 17:35
Show Gist options
  • Save mnadeem/fab9c666b5a415298dc16848c980eb35 to your computer and use it in GitHub Desktop.
Save mnadeem/fab9c666b5a415298dc16848c980eb35 to your computer and use it in GitHub Desktop.
int start = 1, end = 10;
List<Integer> primeNumbers = IntStream.range(start, end)
.filter(number -> number > 1 && IntStream.range(2, number).noneMatch(divider -> number % divider == 0))
.boxed().collect(Collectors.toList());
System.out.println(primeNumbers);
Integer sumOfPrimes = IntStream.range(start, end).filter(
number -> number > 1 && IntStream.range(2, number - 1).noneMatch(divider -> number % divider == 0))
.reduce(0, (a, b) -> a + b);
System.out.println(sumOfPrimes);
Integer sumOfSquaresOfPrimes = IntStream.range(start, end).filter(
number -> number > 1 && IntStream.range(2, number - 1).noneMatch(divider -> number % divider == 0))
.map(x -> x * x).reduce(0, (a, b) -> a + b);
System.out.println(sumOfSquaresOfPrimes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment