algorithm - Please discussing my Java code to find Factors(It's Correct?) -
do have solution solve factor problem?
a few comments. firstly, @opensource pointed out, code not work correctly. should simplify approach forgetting primes @ top level. primes not need treated separately.
some comments on specific lines of code:
arraylist<integer> list = new arraylist<integer>();
at point know there 2 factors, 1 , n. why not add them list?
if(i > n/2) break; //optimize
why recalculate n/2
if n hasn't changed since last time?
if(n % == 0) list.add(new integer(i));
if i
factor (n / i)
factor. every time n % == 0
have found 2 factors.
}else if(n%3 == 0 && n%2 != 0 && n != 3 && n != 1){ //odd number
this doesn't work , takes far effort it. have looked @ numbers, left must odd.
}else{ //prime
no, left isn't prime. , there 1 prime number well.
for(int a:list){ system.out.println(a); }
you might want sort list
first, before printing.
Comments
Post a Comment