Java Arithmetic primeFactorize(int m)

PreviousNext

//package com.java2s;

import java.util.HashMap;

import java.util.Map;

public class Main {
    public static Map<Integer, Integer> primeFactorize(int m) {
        Map<Integer, Integer> map = new HashMap<>();
        int i = 2;
        while (i <= Math.sqrt(Integer.valueOf(m).doubleValue())) {
            while (m % i == 0) {
                if (map.containsKey(i)) {
                    int n = map.get(i) + 1;
                    map.put(i, n);/* w w   w  .  d   em   o   2  s  .  c o   m*/
                } else {
                    map.put(i, 1);
                }
                m = m / i;
            }
            i++;
        }
        if (m != 1) {
            map.put(m, 1);
        }
        return map;
    }
}
PreviousNext

Related