package com.greensnow25.Primes;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * public class Primes.
 *
 * @author greensnow25.
 * @version 1.
 * @since 10.05.2017.
 */
public class Primes implements Iterator {
    /**
     * array.
     */
    private final int[] arrray;

    /**
     * position.
     */
    private int position = 0;

    /**
     * constructor.
     *
     * @param arrray
     */
    public Primes(int[] arrray) {
        this.arrray = arrray;
    }

    /**
     * method checks contain array more primes.
     *
     * @return true if contain.
     */
    @Override
    public boolean hasNext() {

        for (int i = position; i != arrray.length; i++) {
            if (arrray[i] == 2 || arrray[i] == 3) {
                return true;
            } else if (arrray[i] % 2 != 0) {
                List<Integer> divi = this.createDividers(arrray[i]);
                int size = 0;
                for (Integer j : divi) {
                    if (arrray[i] % j != 0) {
                        size++;
                    }
                    if (size == divi.size()) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * method return prime number, and move pointer to one wright.
     *
     * @return prime number.
     */
    @Override
    public Object next() {
        Integer res = 0;
        for (int i = position; ; i++) {
            if (arrray[i] == 2 || arrray[i] == 3) {
                res = arrray[i];
                position = ++i;
                return res;
            } else if (arrray[i] % 2 != 0) {
                List<Integer> divi = this.createDividers(arrray[i]);
                int size = 0;
                for (Integer j : divi) {
                    if (arrray[i] % j != 0) {
                        size++;
                    }
                    if (size == divi.size()) {
                        res = arrray[i];
                        position = ++i;
                        return res;

                    }
                }
            }

        }
    }

    /**
     * method generate dividers for number.
     *
     * @param number number.
     * @return list of dividers, of the number.
     */
    public List<Integer> createDividers(int number) {
        ArrayList<Integer> dividers = new ArrayList<>();
        int res = (int) Math.ceil(Math.sqrt(number));
        for (int i = 2; i <= res; i++) {
            if (i % 2 != 0) {
                dividers.add(i);
            }
        }
        return dividers;
    }
}

@@ -653,25 +653,24 @@
 ext() %7B%0A
-%0A
         
 for (int
@@ -665,529 +665,655 @@
     
-for (int i = position; i != arrray.length; i++) %7B
+return this.check() %3E 0;%0A%0A    %7D%0A
 %0A    
-  
+/**%0A
      
- if (arrray%5Bi%5D == 2 %7C%7C arrray%5Bi%5D == 3) %7B%0A    
+* method return prime number, and move pointer to one wright.%0A
      
+*%0A
      
-  
+* @
 return 
-true;
+prime number.
 %0A     
+*/%0A
     
-   %7D else if (arrray%5Bi%5D %25 2 != 0) %7B%0A                List%3CInteger%3E divi = this.createDividers(arrray%5Bi%5D);
+@Override%0A    public Object next() %7B%0A        this.check();%0A        return (Integer) arrray%5Bposition++%5D;%0A
 %0A    
+%7D%0A%0A
     
+/**%0A
      
-   int size = 0;
+* method generate dividers for number.
 %0A     
+*%0A
      
-      for (Integer j : divi) %7B%0A           
+* @param number number.%0A     * @return list of dividers, of the number.%0A
      
+*/%0A
     
-if (arrray%5Bi%5D %25 j != 0) %7B%0A                        size++;%0A                    %7D%0A                    if (size == divi.size()) %7B%0A                        return true;%0A                    %7D
+public List%3CInteger%3E createDividers(int number) %7B%0A        ArrayList%3CInteger%3E dividers = new ArrayList%3C%3E();%0A        int res = (int) Math.ceil(Math.sqrt(number));%0A        for (int i = 2; i %3C= res; i++) %7B%0A            if (i %25 2 != 0) %7B
 %0A   
@@ -1317,33 +1317,48 @@
                 
-%7D
+dividers.add(i);
 %0A            %7D%0A 
@@ -1385,13 +1385,16 @@
 urn 
-false
+dividers
 ;%0A  
@@ -1390,32 +1390,33 @@
 ividers;%0A    %7D%0A%0A
+%0A
     /**%0A     * m
@@ -1425,170 +1425,107 @@
 hod 
-return prime number, and move pointer to one wright.%0A     *%0A     * @return prime number.%0A     */%0A    @Override%0A    public Object next() %7B%0A        Integer res = 0;
+move pointer on position.%0A     *%0A     * @return position in array.%0A     */%0A    public int check() %7B
 %0A   
@@ -1552,16 +1552,34 @@
 sition; 
+i != arrray.length
 ; i++) %7B
@@ -1635,41 +1635,8 @@
 ) %7B%0A
-                res = arrray%5Bi%5D;%0A
     
@@ -1650,34 +1650,32 @@
      position = 
-++
 i;%0A             
@@ -1684,19 +1684,17 @@
  return 
-res
+1
 ;%0A      
@@ -2023,49 +2023,8 @@
 ) %7B%0A
-                        res = arrray%5Bi%5D;%0A
     
@@ -2054,18 +2054,16 @@
 ition = 
-++
 i;%0A     
@@ -2092,13 +2092,10 @@
 urn 
-res;%0A
+1;
 %0A   
@@ -2141,25 +2141,24 @@
           %7D%0A
-%0A
         %7D%0A  
@@ -2163,471 +2163,21 @@
     
-%7D%0A%0A    /**%0A     * method generate dividers for number.%0A     *%0A     * @param number number.%0A     * @return list of dividers, of the number.%0A     */%0A    public List%3CInteger%3E createDividers(int number) %7B%0A        ArrayList%3CInteger%3E dividers = new ArrayList%3C%3E();%0A        int res = (int) Math.ceil(Math.sqrt(number));%0A        for (int i = 2; i %3C= res; i++) %7B%0A            if (i %25 2 != 0) %7B%0A                dividers.add(i);%0A            %7D%0A        %7D%0A        return dividers
+    return -1
 ;%0A  

Update date 10.05.17