Skip to main content
added 208 characters in body
Source Link
LionH
  • 101
  • 1

It can be easily and clearly resolved by this:

UnaryOperator<BigInteger[]> fibonacci = (m) -> new BigInteger[]{m[1], m[0].add(m[1])};

Stream.iterate(new BigInteger[]{BigInteger.ONE,BigInteger.ONE}, fibonacci)
            .limit(32) 
            .map(n -> fibonacci.apply(n)[0])
            .filter(n->n.compareTo(BigInteger.valueOf(4000000))<1)
            .filter(n->n.mod(BigInteger.valueOf(2))==BigInteger.ZERO)
            .reduce((m,n)->m.add(n)).ifPresent(System.out::println);

First define the Fibonacci operation based on mathematical definition. After begin to iterate defining the seed (1,1). Here for simplicity, we used an array of BigInteger but we could have defined a class (e.g. Tuple). Finally you filter based on upper hypotesis that max value should be less than 4,000,000 and that we only retain odd number. We then sum all the values (reduce operation) and we output it to the user. Remind that all stream is infinite. In this case the operation continue until the limit(32) is defined. You can limit by maximux iteration in jdk8 or wait to jdk9 to limit it by value.

It can be easily and clearly resolved by this:

UnaryOperator<BigInteger[]> fibonacci = (m) -> new BigInteger[]{m[1], m[0].add(m[1])};

Stream.iterate(new BigInteger[]{BigInteger.ONE,BigInteger.ONE}, fibonacci)
            .map(n -> fibonacci.apply(n)[0])
            .filter(n->n.compareTo(BigInteger.valueOf(4000000))<1)
            .filter(n->n.mod(BigInteger.valueOf(2))==BigInteger.ZERO)

First define the Fibonacci operation based on mathematical definition. After begin to iterate defining the seed (1,1). Here for simplicity, we used an array of BigInteger but we could have defined a class (e.g. Tuple). Finally you filter based on upper hypotesis that max value should be less than 4,000,000 and that we only retain odd number. Remind that all stream is infinite. In this case the operation continue. You can limit by maximux iteration in jdk8 or wait to jdk9 to limit it by value.

It can be easily and clearly resolved by this:

UnaryOperator<BigInteger[]> fibonacci = (m) -> new BigInteger[]{m[1], m[0].add(m[1])};

Stream.iterate(new BigInteger[]{BigInteger.ONE,BigInteger.ONE}, fibonacci)
            .limit(32) 
            .map(n -> fibonacci.apply(n)[0])
            .filter(n->n.compareTo(BigInteger.valueOf(4000000))<1)
            .filter(n->n.mod(BigInteger.valueOf(2))==BigInteger.ZERO)
            .reduce((m,n)->m.add(n)).ifPresent(System.out::println);

First define the Fibonacci operation based on mathematical definition. After begin to iterate defining the seed (1,1). Here for simplicity, we used an array of BigInteger but we could have defined a class (e.g. Tuple). Finally you filter based on upper hypotesis that max value should be less than 4,000,000 and that we only retain odd number. We then sum all the values (reduce operation) and we output it to the user. Remind that all stream is infinite. In this case the operation continue until the limit(32) is defined. You can limit by maximux iteration in jdk8 or wait to jdk9 to limit it by value.

added 511 characters in body
Source Link
LionH
  • 101
  • 1

It can be easily and clearly resolved by this:

UnaryOperator<BigInteger[]> fibonacci = (m) -> new BigInteger[]{m[1], m[0].add(m[1])};

Stream.iterate(new BigInteger[]{BigInteger.ONE,BigInteger.ONE}, fibonacci)
            .map(n -> fibonacci.apply(n)[0])
            .filter(n->n.compareTo(BigInteger.valueOf(4000000))<1)
            .filter(n->n.mod(BigInteger.valueOf(2))==BigInteger.ZERO)

First define the Fibonacci operation based on mathematical definition. After begin to iterate defining the seed (1,1). Here for simplicity, we used an array of BigInteger but we could have defined a class (e.g. Tuple). Finally you filter based on upper hypotesis that max value should be less than 4,000,000 and that we only retain odd number. Remind that all stream is infinite. In this case the operation continue. You can limit by maximux iteration in jdk8 or wait to jdk9 to limit it by value.

It can be easily and clearly resolved by this:

UnaryOperator<BigInteger[]> fibonacci = (m) -> new BigInteger[]{m[1], m[0].add(m[1])};

Stream.iterate(new BigInteger[]{BigInteger.ONE,BigInteger.ONE}, fibonacci)
            .map(n -> fibonacci.apply(n)[0])
            .filter(n->n.compareTo(BigInteger.valueOf(4000000))<1)
            .filter(n->n.mod(BigInteger.valueOf(2))==BigInteger.ZERO)

It can be easily and clearly resolved by this:

UnaryOperator<BigInteger[]> fibonacci = (m) -> new BigInteger[]{m[1], m[0].add(m[1])};

Stream.iterate(new BigInteger[]{BigInteger.ONE,BigInteger.ONE}, fibonacci)
            .map(n -> fibonacci.apply(n)[0])
            .filter(n->n.compareTo(BigInteger.valueOf(4000000))<1)
            .filter(n->n.mod(BigInteger.valueOf(2))==BigInteger.ZERO)

First define the Fibonacci operation based on mathematical definition. After begin to iterate defining the seed (1,1). Here for simplicity, we used an array of BigInteger but we could have defined a class (e.g. Tuple). Finally you filter based on upper hypotesis that max value should be less than 4,000,000 and that we only retain odd number. Remind that all stream is infinite. In this case the operation continue. You can limit by maximux iteration in jdk8 or wait to jdk9 to limit it by value.

Source Link
LionH
  • 101
  • 1

It can be easily and clearly resolved by this:

UnaryOperator<BigInteger[]> fibonacci = (m) -> new BigInteger[]{m[1], m[0].add(m[1])};

Stream.iterate(new BigInteger[]{BigInteger.ONE,BigInteger.ONE}, fibonacci)
            .map(n -> fibonacci.apply(n)[0])
            .filter(n->n.compareTo(BigInteger.valueOf(4000000))<1)
            .filter(n->n.mod(BigInteger.valueOf(2))==BigInteger.ZERO)