지수와 로그
1. 제곱, 제곱근, 지수
- 제곱
- 같은 수를 두번 곱함
- 거듭 제곱 : 같은 수를 거듭하여 곱함
- 제곱근(=root)
- a를 제곱하여 b가 될때 a를 b의 제곱근이라고 함
2. 로그
- logaB : a가 B가 되기위해 제곱해야 하는
(실습 : 지수와 로그)
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
// 기초 수학 - 지수와 로그
public class Main {
public static void main(String[] args) {
// 1. 제곱, 제곱근, 지수
System.out.println("== 제곱 ==");
System.out.println(Math.pow(2, 3));
System.out.println(Math.pow(2, -3));
System.out.println(Math.pow(-2, -3));
System.out.println(Math.pow(2, 30));
System.out.printf("%.0f\n", Math.pow(2, 30));
System.out.println("== 제곱근 ==");
System.out.println(Math.sqrt(16));
System.out.println(Math.pow(16, 1.0/2));
System.out.println(Math.pow(16, 1.0/4));
// 참고) 절대 값
System.out.println("== 절대 값 ==");
System.out.println(Math.abs(5));
System.out.println(Math.abs(-5));
// 2. 로그
System.out.println("== 로그 ==");
System.out.println(Math.log(2.7182818284590452353602874713527));
System.out.println(Math.log10(1000));
System.out.println(Math.log(4) / Math.log(2));
}
}
(실습 2 : 제곱근 구하기, 바빌로니아 방식) 출처 : https://innovation123.tistory.com/96
1
2
3
4
5
6
7
8
9
10
11
// 바빌로니아 방법
// Xn+1 = 1/2 * (Xn + S/Xn)
static double sqrt(int n) {
double result = 1;
for (int i = 0 ; i < 10 ; i++) {
result = (result + (n / result)) / 2;
}
return result;
}
This post is licensed under CC BY 4.0 by the author.