//연산하기
BigInteger num1 = new BigInteger("1234");
BigInteger num2 = new BigInteger("10000000000000000000000000");
System.out.println("num1.add(num2) = " + num1.add(num2));
System.out.println("num2.subtract(num1) = " + num2.subtract(num1));
System.out.println("num1.subtract(num2) = " + num1.subtract(num2));
System.out.println("num1.multiply(num2) = " + num1.multiply(num2));
System.out.println("num1.divide(num2) = " + num1.divide(num2));
System.out.println("num2.divide(num1) = " + num2.divide(num1));
System.out.println("num2.remainer(num1) = " + num2.remainer(num1)); // 나머지
System.out.println("num2.byteValue() = " + num2.byteValue()); // one byte 값
//형변환
BigInteger num3 = BigInteger.valueOf(10000000);
int intNum3 = num3.intValue();
long longNum3 = num3.longValue();
float floatNum3 = num3.floatValue();
double doubleNum3 = num3.doubleValue();
String strNum3 = num3.toString();
System.out.println("intNum3 = " + intNum3);
System.out.println("longNum3 = " + longNum3);
System.out.println("floatNum3 = " + floatNum3);
System.out.println("doubleNum3 = " + doubleNum3);
System.out.println("strNum3 = " + strNum3);
//비교 : 서로 같으면 0, num4 가 더 크면 1, num5가 더 크면 -1
BigInteger num4 = new BigInteger("100001");
BigInteger num5 = new BigInteger("100000");
int compare = num4.compareTo(num5);
System.out.println("compare = " + compare);
//심지어는 최대공약수 구하는 함수도 있다.
BigInteger num6 = new BigInteger("1278916849368");
BigInteger num7 = new BigInteger("8124682192");
BigInteger gcd = num6.gcd(num7);
Solution 1. BigInteger 이용
package practice.algorithm.math;
import java.math.BigInteger;
import java.util.Scanner;
public class Bj2824_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
BigInteger a = new BigInteger("1");
while ( n > 0 ) {
a = BigInteger.valueOf(sc.nextInt()).multiply(a);
n--;
}
int m = sc.nextInt();
BigInteger b = new BigInteger("1");
while (m > 0) {
b = BigInteger.valueOf(sc.nextInt()).multiply(b);
m--;
}
if (a.compareTo(b) < 0) {
BigInteger temp = new BigInteger("0");
temp = a;
a = b;
b = temp;
}
String strGcd = gcd(a, b).toString();
System.out.println(strGcd.length() > 9 ? strGcd.substring(strGcd.length()-9, strGcd.length()) : strGcd);
}
private static BigInteger gcd(BigInteger a, BigInteger b) {
if (b.compareTo(new BigInteger("0")) == 0) return a;
return gcd(b, a.remainder(b));
}
}
BigInteger 내장함수를 쓰면 다음과 같이 알고리즘 구현 없이도 가능하다.
package practice.algorithm.math;
import java.math.BigInteger;
import java.util.Scanner;
public class Bj2824_1_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
BigInteger a = new BigInteger("1");
while ( n > 0 ) {
a = BigInteger.valueOf(sc.nextInt()).multiply(a);
n--;
}
int m = sc.nextInt();
BigInteger b = new BigInteger("1");
while (m > 0) {
b = BigInteger.valueOf(sc.nextInt()).multiply(b);
m--;
}
if (a.compareTo(b) < 0) {
BigInteger temp = new BigInteger("0");
temp = a;
a = b;
b = temp;
}
String strGcd = a.gcd(b).toString();
System.out.println(strGcd.length() > 9 ? strGcd.substring(strGcd.length()-9, strGcd.length()) : strGcd);
}
}
package practice.algorithm.math;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Bj2824_2 {
static final int MAX = 1000000000;
static int[] a, b;
static boolean isGcdBig = false;
public static void main(String[] args) throws IOException {
input();
Map<Integer, Integer> aMap = factorization(a);
Map<Integer, Integer> bMap = factorization(b);
long gcd = gcd(aMap, bMap);
if (isGcdBig) {
System.out.printf("%09d", gcd);
return;
}
System.out.println(gcd);
}
private static long gcd(Map<Integer, Integer> aMap, Map<Integer, Integer> bMap) {
long gcd = 1;
for (Integer primeA : aMap.keySet()) {
int overA = aMap.get(primeA);
int overB = bMap.getOrDefault(primeA, 0);
gcd *= pow(primeA, Math.min(overA, overB));
if (gcd >= MAX) {
isGcdBig = true;
gcd %= MAX;
}
}
return gcd;
}
private static long pow(Integer primeA, int over) {
long result = 1;
while (over --> 0) {
result *= primeA;
if (result > MAX) {
isGcdBig = true;
result %= MAX;
}
}
return result;
}
private static Map<Integer, Integer> factorization(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (int n : nums) {
map = factorization(n, map);
}
return map;
}
private static Map<Integer, Integer> factorization(int n, Map<Integer, Integer> map) {
if (n == 1) return map;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
map.put(i, map.getOrDefault(i, 0) + 1);
return factorization(n / i, map);
}
}
map.put(n, map.getOrDefault(n, 0) + 1);
return map;
}
private static void input() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
a = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(st.nextToken());
}
n = Integer.parseInt(br.readLine());
b = new int[n];
st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < b.length; i++) {
b[i] = Integer.parseInt(st.nextToken());
}
}
}