package org.noob.codewars.train.learn.prac160829;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by FujiRen on 2016/8/29.
 * Examples:
 * <p>
 * gap(2, 5, 7) --> [5, 7] or (5, 7) or {5, 7}
 * <p>
 * gap(2, 5, 5) --> nil or in C++ {0, 0}
 * <p>
 * gap(4, 130, 200) --> [163, 167] or (163, 167) or {163, 167}
 * <p>
 * ([193, 197] is also such a 4-gap primes between 130 and 200 but it's not the first pair)
 */
public class GapInPrimes {

    public static void main(String[] args) {
        int g = 6;
        long m = 100;
        long n = 110;
        System.out.println(Arrays.toString(gap(g, m, n)));
    }


    public static long[] gap(int g, long m, long n) {
        // your code
        long[] result = new long[2];
        List<Long> primesList = getPrimes(m, n);
        if (null != primesList && primesList.size() > 0) {
            for (int i = 0; i < primesList.size()-1; i++) {
                Long gap = primesList.get(i+1) - primesList.get(i);
                if (gap.intValue() == g) {
                    result[0] = primesList.get(i);
                    result[1] = primesList.get(i+1);
                    return result;
                }
            }
        }
        return null;
    }

    /**
     * 获取m-n的素数
     *
     * @param m
     * @param n
     * @return
     */
    public static List<Long> getPrimes(long m, long n) {
        List<Long> list = new ArrayList<>();
        for (long i = m; i <= n; i++) {
            if (isPrimes(i)) {
                list.add(i);
            }
        }
        return list;
    }

    /**
     * 判断是否是素数
     *
     * @param p
     * @return
     */
    public static boolean isPrimes(long p) {
        for (int i = 2; i <= Math.sqrt(p); i++) {
            if (0 == (p% i)) {
                 return false;
            }
        }
        return true;
    }
}

@@ -651,17 +651,169 @@
 %0A    %7D%0A%0A
+    /**%0A     * 1.%E7%B4%A0%E6%95%B0%E7%9A%84%E5%88%A4%E6%96%AD%EF%BC%9B%0A     * 2.%E6%89%BE%E5%88%B0m-n%E8%8C%83%E5%9B%B4%E5%86%85%E7%9A%84%E6%89%80%E6%9C%89%E7%B4%A0%E6%95%B0%0A     * 3.%E5%9C%A8%E6%89%80%E6%9C%89%E7%B4%A0%E6%95%B0%E4%B8%AD%E6%9F%A5%E6%89%BE%E7%AC%A6%E5%90%88%E6%9D%A1%E4%BB%B6%E7%9A%84%E7%AC%AC%E4%B8%80%E5%AF%B9%E7%B4%A0%E6%95%B0%0A     *%0A     * @param g%0A     * @param m%0A     * @param n%0A     * @return%0A     */
 %0A
-
     publ
@@ -1073,17 +1073,19 @@
 t.size()
--
+ - 
 1; i++) 
@@ -1129,17 +1129,19 @@
 st.get(i
-+
+ + 
 1) - pri
@@ -1298,17 +1298,19 @@
 st.get(i
-+
+ + 
 1);%0A    
@@ -1941,16 +1941,17 @@
 = (p
+ 
 %25 i)) %7B%0A
     
@@ -1942,25 +1942,24 @@
  (p %25 i)) %7B%0A
- 
             

优化，添加注释