본문 바로가기
CodingTest/Programmers

[프로그래머스] Lv.0 팩토리얼

by yoondoo 2022. 12. 1.
728x90
class Solution {
    public int solution(int n) {
       	int i=1;
	int mul=1;
    
	while(true) {
		mul *= i;
		if(mul==n) break;
		else if(mul>n) {
			i--;
			break;
		}
		i++;
	}
    
    return i;
    }
}

팩토리얼은 a! = a*a-1*a-2*....3*2*1 이다.

여기서 팩토리얼 한게 n이거나 n보다 작거나 큰 수가 나왔을 때는 작은 것을 출력해줘야 하므로

mul==n일 때와

mul>n 일 때 i를 하나 빼서 출력시켜줬다.

반응형

댓글