일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 컬렉션프레임워크
- CLI명령어
- FilterChain
- 자바
- HTML
- 자료구조
- 거듭제곱
- 백엔드
- 그리디
- Publishing
- Spring Security
- 계산기만들기
- Spring Data JDBC
- spring data jpa
- CSS
- 데일리코딩
- 인텔리제이
- 첫글자대문자
- 페어프로그래밍
- testing
- 알고리즘
- 백준알고리즘
- 스프링
- fibonacci
- 회고
- 깃허브
- java
- 문자열뒤집기
- 제네릭스
- 부트캠프
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