본문 바로가기
1 Day 1 Algorithms

[2019.03.15] Extra Long Factorials

by 곰돌찌 2019. 3. 15.

Problem


The factorial of the integer , written , is defined as:

Calculate and print the factorial of a given integer.

For example, if , we calculate  and get .

Function Description

Complete the extraLongFactorials function in the editor below. It should print the result and return.

extraLongFactorials has the following parameter(s):

  • n: an integer

Note: Factorials of  can't be stored even in a  long long variable. Big integers must be used for such calculations. Languages like Java, Python, Ruby etc. can handle big integers, but we need to write additional code in C/C++ to handle huge values.

We recommend solving this challenge using BigIntegers.

Input Format

Input consists of a single integer 

Constraints

Output Format

Print the factorial of .

Sample Input

Sample Output

Explanation

The factorial of the integer , written , is defined as:

Calculate and print the factorial of a given integer.르

For example, if , we calculate  and get .

Function Description

Complete the extraLongFactorials function in the editor below. It should print the result and return.

extraLongFactorials has the following parameter(s):

  • n: an integer

Note: Factorials of  can't be stored even in a  long long variable. Big integers must be used for such calculations. Languages like Java, Python, Ruby etc. can handle big integers, but we need to write additional code in C/C++ to handle huge values.

We recommend solving this challenge using BigIntegers.

Input Format

Input consists of a single integer 

Constraints

Output Format

Print the factorial of .

Sample Input

Sample Output

Explanation

25! = 25 x 24 x 23 x ....... x 3 x 2 x 1


How I solved the problem


# BigInteger Class 를 활용함.

# BigInteger는 숫자 크기에 제한이 없음. 즉, 무한대의 정수를 넣을 수 있음.


# BigInteger를 사용하여 사칙연산을 할 때는 +, -, *, / 등을 사용할 수 없음.

# 즉, 매서드를 활용해야함. 

# 더하기는 add(), 빼기는 substract(), 곱하기는 multiply(), 나누기는 divide()를 사용함...

# 그리고 BigInteger.ONE을 하면 1을 의미하고.. 정수의 값들을 BigInteger class에 담기 위해서는 BigInteger.valueOf()를 사용해줘야함!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
 
public class Solution {
 
    // Complete the extraLongFactorials function below.
    static void extraLongFactorials(int n) {
        BigInteger result = BigInteger.ONE;
 
        for (int i = 0; i < n; i++) {
            result = result.multiply(BigInteger.valueOf(i + 1));
        }
 
        System.out.println(result);
    }
 
    private static final Scanner scanner = new Scanner(System.in);
 
    public static void main(String[] args) {
        int n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
 
        extraLongFactorials(n);
 
        scanner.close();
    }
}
 
cs

[출처 : https://www.hackerrank.com ]


'1 Day 1 Algorithms' 카테고리의 다른 글

[2019.03.14] Find Digits  (0) 2019.03.14
[2019.03.13] Jumping on the Clouds: Revisited  (0) 2019.03.13
[2019.03.12] Sequence Equation  (0) 2019.03.12
[2019.03.11] Circular Array Rotation  (0) 2019.03.11
[2019.03.07] Save the Prisoner!  (0) 2019.03.07

댓글