Java recursion to find prime factors doesn't work -


i trying prime factors of number. for loop should work until finds match , should break , jump next if statement checks if number not equal zero.

public class factor {      public static arraylist <integer> holdnum = new arraylist();       public static void main(string[]args){         factor object = new factor();         object.factor(104);         system.out.println(holdnum.get(0));     }      public static int factor(int number){         int new_numb = 0;         int n=0;         for( n = 1; n < 9; n++) {                              if (number % n == 0) {                 holdnum.add(n);                 new_numb = number/n;                 break;                        }         }          system.out.println(new_numb);         if(new_numb < 0) {             holdnum.add(new_numb);             return 1;         } else {             return factor(new_numb);         }                         } } 

there @ least 3 errors :

  • as okiharaherbst wrote, counter not incremented.
  • you start loop @ 1, yourval % 1 equals 0 , new_numb equals input val, you'll loop endlessly on 104.
  • new_numb never lesser 0.

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -