1 2 Previous Next 28 Replies Latest reply: Mar 24, 2008 10:29 AM by 807601 RSS

    Calculating prime factors for a number

    807601
      Well this code is supposed to print all the prime factors for a given number. For example, 8 gives 2 2 2 ,4 gives 2 2 and 9 gives 3 3, but when you try a number like 6, it only prints 2. Any ideas?
      public void pFactors(int n)
          {   
              for(int i=2;i<n;i++)
              {   
                  if(isPrime(i))
                  {   
                      while(n%i==0)
                      {   
                          System.out.println(i);
                          n/=i;
                      }
                  }
              }
          }
        1 2 Previous Next