入门算法 分解质因数算法 202001061516
package org.jimmy.autosearch.test; import java.util.ArrayList; import java.util.HashMap; /** * 质因数分解练习,by:ラピスラズリ(Dawn) * */ public class PrimeFactorizationTest2019123101 { //1-1000以内的质数 public static final int MAX_NUMBER = 1000; //1-100范围内的数质因数分解 public static final int MAX_NUMBER2 = 100; //质数集合 public static ArrayList<Integer> primeNumberList = new ArrayList<Integer>(); //key:数字,value:被整除的数 public static final HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>(); //key:数字,value:被整除的质数(分解的质因数) public static final HashMap<Integer, ArrayList<Integer>> map2 = new HashMap<Integer, ArrayList<Integer>>(); //积的初始值 public static final int INITIAL_PRODUCT = 1; //质因数集合 public static Integer product = 1; public static void main(String[] args) { try { test(); } catch (Exception e) { e.printStackTrace(); } } public static void test() throws Exception{ for(int i = 1; i <= MAX_NUMBER; i++){ ArrayList<Integer> list = new ArrayList<Integer>(); for(int j = 1; j <= i; j++){ double value = (double)i / j; if(value % 1 == 0){ if(!map.containsKey(i)){ list.add(j); map.put(i, list); }else{ list = map.get(i); map.remove(i); list.add(j); map.put(i, list); } } } } //筛选出质数,只能被1和自己整除的数 map.entrySet().forEach(entry -> { int count = entry.getValue().size(); if(count == 2){ primeNumberList.add(entry.getKey()); } }); //分解质因数 for(int i = 1; i <= MAX_NUMBER2; i++){ //跳过质数 if(primeNumberList.contains(i)){ continue; } test2(i, i); } //分解质因数整理: map2.entrySet().forEach(entry -> { ArrayList<Integer> list = entry.getValue(); list = test5(entry.getKey(), list); entry.setValue(list); }); //分解质因数结果: map2.entrySet().forEach(entry -> { ArrayList<Integer> list = entry.getValue(); final StringBuffer expression = new StringBuffer(); expression.append(entry.getKey() + "="); list.forEach(number -> { expression.append(number + "*"); }); expression.delete(expression.length() - 1, expression.length()); System.out.println(expression); }); } public static ArrayList<Integer> test5(int number, ArrayList<Integer> list){ // System.out.println("number:" + number); // System.out.println(list.size()); //计算所有质因数的乘积 ArrayList<Integer> resultList = new ArrayList<Integer>(); int value = 1; for(int i = 0; i < list.size(); i++){ int currentNumber = list.get(i); // System.out.println("currentNumber:" + currentNumber + ",value:" + value); value *= currentNumber; if(value > number){ break; } resultList.add(currentNumber); } return resultList; } public static void test3(){ int count = 2; /*map2.entrySet().forEach(entry -> { Integer number = entry.getKey(); ArrayList<Integer> list = entry.getValue(); int value = 1; int index = 1; for(int i = 0; i < list.size(); i++){ for(int j = i + index; j < list.size(); j++){ } } });*/ } public static void test4(int max, int count, int index, int index2) throws Exception { for(int i = index2; i < max; i++){ int tempIndex = 1; int tempIndex2 = i; int j = i + tempIndex; int k = i + tempIndex + 1; if(i >= max - 1 && j >= max - 1 && k >= max - 1){ throw new RuntimeException(); } // System.out.println("i:" + i + ",j:" + j + ",k:" + k); if(k >= max - 1){ tempIndex = 0; tempIndex2++; }else{ k++; } tempIndex++; if(i <= max - 1 && j <= max - 1){ test4(max, count, tempIndex, tempIndex2); } } } public static void test2(int initialNumber, int number){ for(int i = 0; i < primeNumberList.size(); i++){ Integer primeNumber = primeNumberList.get(i); ArrayList<Integer> list = null; double value = (double)number / primeNumber; // System.out.println("number:" + number + ",primeNumber:" + primeNumber + ",value:" + value); if(value % 1 == 0 && value >= 1){ if(!map2.containsKey(initialNumber)){ list = new ArrayList<Integer>(); list.add(primeNumber); map2.put(initialNumber, list); }else{ list = map2.get(initialNumber); map2.remove(initialNumber); list.add(primeNumber); map2.put(initialNumber, list); } int result = (int) value; // System.out.println("initialNumber:" + initialNumber + ",p:" + product); test2(initialNumber, result); } } } }
效果图:
好了.
2015年10月-2023年6月 总计7年.
所有总计:7年.
我是唯物唯心二元论的,暂时主学法语.
所有总计:7年.
我是唯物唯心二元论的,暂时主学法语.