U E D R , A S I H C RSS

Factorial Factors/이동현

n! 를 구는 2 <= k <= n 인 k의 인수를 구하는 방법과,
k가 는 방법 따라 가 결된다.

기본로 n만 fac 는다. (ex.fac8 면 8 3다.)
렇게 배 를들 16 를 구다면 16 = 8x2 므로 fac16 = fac8+1 = 4가 된다.
fac8 미리 구 던 값 므로 바로 나다.

만 k가 라면 방법 는다. 는 나 따라 단. 되는데 ^^


~cpp 
import java.util.*;
import java.math.*;

public class FactorialFactors2 {
	final int CASE_N = 1000000;	//  
	int fac[] = new int[CASE_N+1];	//
	ArrayList<Integer> prime  = new ArrayList<Integer>();
	
	boolean isPrime(int num){
		for(int k=0;k<prime.size();k++){
			if(num%prime.get(k)==0)
				return false;		
		}		
		return true;
	}	
	
	void printResult(){
		int total=0;
		for(int i=2;i<=CASE_N; i++)
			total+=fac[i];
		System.out.println(total);
	}
	
	void run(){
		long time = System.currentTimeMillis();
		prime.add(2);
		fac[2] = 1;
		int sqrt = (int)Math.sqrt(CASE_N);
		//n!므로 고리 2는  미리 고 3~n까 각각  다.
		for(int i=3;i<=CASE_N;i++){
			int num = i;
			//num 를 구는 루.
			for(int j=2; num!=1;){
				if(num%j == 0){
					num = num/j;
					fac[i]++;
					fac[i]+=fac[num];					
					break;
				}
				else
					j++;	
				//7로 나때까   본다.
				if (j == 7 && isPrime(num) == true) {
					fac[i]++;
					if (num <= sqrt) // 리가 는 sqrt보다   된다.
						prime.add(num);
					break;
				}
			}			
		}
		printResult();
		System.out.print(System.currentTimeMillis()-time);	
	}
	public static void main(String[] args) {
		new FactorialFactors2().run();
	}
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:15
Processing time 0.0082 sec