How to do it...

  1. Let's implement the APIs in the com.packt.math.MathUtil class, starting with the isPrime(Integer number) API:
        public static Boolean isPrime(Integer number){
          if ( number == 1 ) { return false; }
          return IntStream.range(2,num).noneMatch(i -> num % i == 0 );
        }
  1. Implement the sumOfFirstNPrimes(Integer count) API:
        public static Integer sumOfFirstNPrimes(Integer count){
          return IntStream.iterate(1,i -> i+1)
                          .filter(j -> isPrime(j))
                          .limit(count).sum();
        }
  1. Let's write a function to check whether the number is even:
        public static Boolean isEven(Integer number){
          return number % 2 == 0;
        }
  1. The negation of isEven tells us whether the number is odd. We ...

Get Java 11 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.