Estou tentando desenvolver um algoritmo que exiba a fatoração dos números ex: √12 = 2√3, porém o programa não fatora corretamente alguns números como 84 por exemplo, alguém sabe como resolver isso?

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Fatorar
{

private static ArrayList<Integer> primos;
private static ArrayList<Integer> fatores;
private static HashMap<Integer, Integer> map;
private static int num;   
private static int pro;
private static ArrayList<Integer> antes;
private static ArrayList<Integer> eles; 
private static int antesDaRaiz;
private static int padrao;

public static void main(String[] args) 
{
    
    antes = new ArrayList<Integer>();
    
    Scanner sc = new Scanner(System.in);
    num = sc.nextInt();
    padrao = num;

    
    primos = new ArrayList<Integer>();

    for (int i = 2; i < 1000; i++)
    {
        boolean flag = true;
        for (int j = 2; j < i - 1; j++) 
     {
            if (i % j == 0)
            {
                flag = false;
                break;
            }
        }
        if (flag == true) 
        {
            primos.add(i);
        }
    }

    fatores = new ArrayList<Integer>();

    while (num % primos.get(pro) != 0)
    {
        ++pro;
    }

    while (num % primos.get(pro) == 0) 
    {
        fatores.add(primos.get(pro));
        num = num / primos.get(pro);

        if (num % primos.get(pro) != 0)
        {
        ++pro;
        }

    }

    if (num != 1)
    {
     fatores.add(num);
    }

  System.out.println(fatores);
  sc.close();
  paraRaiz();

}

private static void paraRaiz()

{
map = new HashMap<Integer, Integer>();

  for (Integer f : fatores) 
  {
     int qtd = map.getOrDefault(f, 0);

     map.put(f, qtd + 1);
  }


  eles = new ArrayList<Integer>();

  for (int item: map.keySet()) 
  {  
     eles.add(item);
  } 
  // System.out.println(eles);

  va();

}

public static void va()
{
try{

  int iguais = 0;
  for (int i =0; i < eles.size(); i++)
  {  
     for (int ii =0; ii < fatores.size();ii++)
     {
        if (fatores.get(ii) == eles.get(i))
        {
           while (iguais == 2)
           {  
              antes.add((iguais - 1) * fatores.get(ii));  
              iguais = 0;
           }
           iguais += 1;

        }

     }

  }
  if (antes.size() > 1)
  {
     antesDaRaiz = antes.get(0);
     for (int ant = 0; ant < antes.size(); ant++)
     {      
        antesDaRaiz *= antes.get(ant);

     }

  }
  else
  {
     System.out.println(antes.get(0) + "√" + iguais * fatores.get(fatores.size() - 1));
  }

  if (antesDaRaiz != 0)
  {
     System.out.println(antesDaRaiz + "√" + iguais * fatores.get(fatores.size() - 1));
  }
  
  }catch(Exception e)
  {
    System.out.println("√"+padrao);
  }

}

}

Não fatora corretamente = ???

Alguns números, 84 por exemplo.

Cara, não fatora corretamente pode, simplesmente, ser um erro do tipo, mostra só múltiplos de 5. Mostra os números “quebrados”, só a parte inteira, etc.