일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 자바
- testing
- 컬렉션프레임워크
- 부트캠프
- 백엔드
- 페어프로그래밍
- 계산기만들기
- Publishing
- 첫글자대문자
- java
- 그리디
- 스프링
- 자료구조
- CSS
- fibonacci
- CLI명령어
- 백준알고리즘
- HTML
- Spring Data JDBC
- FilterChain
- 거듭제곱
- 문자열뒤집기
- 회고
- 인텔리제이
- Spring Security
- spring data jpa
- 알고리즘
- 데일리코딩
- 깃허브
- 제네릭스
Archives
- Today
- Total
나의 모양
[DailyCoding] 25 | power 본문
✏️ Description
Return power input two numbers
- input
- base: int (base >= 2)
- exponent: int (exponent >= 0)
- output
- return long
- return rest of divided by 94,906,249 of the actual calculation results
- caution
- Avoid using Math.pow, power operators
- Time Complexity O(logN)
- example of in/output
ong output = power(3, 40);
System.out.println(output); // --> 19334827
📚 TIL
- Power: Multiply the same number multiple times
- Why return with the rest divided by 94,906,249?
- Sometimes description shows the rest of the answer divided by a specific number, which is to prevent overflow in the final operation.
- temp * temp || temp * temp * base
is under 94,906,249both can exceed or not 94_906_249,but when the exponent is odd, multiply the base once more will exceed the specific number, so the rest must be obtained one more timeso divide it make it doesn't exceed this number in the middle of the operation!
👩🏻💻 Implementation
final static int MOD = 94_906_249;
power(int base, int exponent) {
if(exponent == 0) return 1;
long half = power(base, exponent / 2);
long result = half * half % MOD;
if(exponent % 2 == 1)
return result * base % MOD;
return result;
}
'SEB > Daily Coding' 카테고리의 다른 글
[DailyCoding] 24 | isSubsetOf (0) | 2022.08.25 |
---|---|
[DailyCoding] 21 | largestProductOfThree (0) | 2022.08.22 |
[DailyCoding] 17 | computeSquareRoot (0) | 2022.08.12 |
[DailyCoding] 16 | isIsogram (0) | 2022.08.11 |
[DailyCoding] 08 | convertDoubleSpaceToSingle (0) | 2022.08.01 |
Comments