java - Divisors inside an array -


i need write method takes array of integers , checks every element if divisors (except number , 1) present in array. if yes, method return true.

for example, following array return true:

4,5,10,2 

i can't think of efficient enough implemented. guys me out here?

i've been thinking iterate through every element in array, search of divisors, put them on array, return array , compare elements in original array.

this possible solution , work want know of other possible solutions.

edit: here code i've came super slow. guys me optimise little bit?:

import java.util.arrays;  public class divisors {          public static void main(string[] args) {                 int[] numbers = { 4, 5, 10, 2 };                 boolean flag = true;                 (int num : numbers) {                         if (num % 2 != 0) {                                 (int subnum = 1; subnum < num / 2; num += 2) {                                         if(num%subnum == 0 && subnum != 1) {                                                 if(!arrays.aslist(numbers).contains(subnum)) {                                                         flag = false;                                                 }                                         }                                 }                         } else {                                 (int subnum = 1; subnum < num / 2; num++) {                                         if(num%subnum == 0 && subnum != 1) {                                                 if(!arrays.aslist(numbers).contains(subnum)) {                                                         flag = false;                                                 }                                         }                                 }                         }                 }                  system.out.println("result is: "+flag);         } } 

i think following alogorithm solves need. have tested on few cases , seems work. example array:

int[] set = {2, 3, 4, 5, 7, 10, 11, 15, 18, 35};   

executes instantly giving answer "true". try removing 7 give answer "false".

you call thus:

reduce(set, 0, 0) 

the principle used iterative recursively through array, reducing array through factorization of array each element. if find element smaller last factor, means can't factored. works if array sorted. once reach end of array, know elements have been factored.

private static boolean reduce (int[] set, int index, int factor) {     // note: set must sorted set of integers     if (index == set.length) {         return true;     } else {         int divisor = set[index];         if (divisor != 1) {             if (divisor < factor) return false;             (int = index; < set.length; i++) {                 while ((set[i]%divisor) == 0) {                 set[i] = set[i]/divisor;                 }             }             return reduce(set, index+1, divisor);         } else {             return reduce(set, index+1, factor);         }      } } 

see if works, let me know if run problems.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -