package com.codewars.gapinprimes;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.LongStream;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

public class GapInPrimes {
    public static void main(String[] args) {
        System.out.println("[337,347] == " + Arrays.toString(GapInPrimes.gap(10,300,400)));

    }

    public static long[] gap(int g, long m, long n) {
        List<Long> primes = new LinkedList<>();
        LongStream.rangeClosed(m, n)
                .filter(i -> LongStream.rangeClosed(2, (int)Math.sqrt(i))
                        .allMatch(j -> i%j != 0))
                .forEach(primes::add);

        if (primes.size() < 2) {
            System.out.println("p");
            return null;
        }

        for (int i = 1; i < primes.size()-1; i++) {
            if (primes.get(i) - primes.get(i-1) == g) {
                return new long[] {primes.get(i-1), primes.get(i)};
            }
        }

        return null;
    }
}
