Skip to content

Commit 515986e

Browse files
committed
20250903 프로그래머스 피보나치수 문제
1 parent 65e4a63 commit 515986e

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// n번째 피보나치 수를 1234567로 나눈 나머지 반환
2+
class Solution {
3+
private static final int MOD = 1234567;
4+
5+
public int solution(int n) {
6+
return fibMod(n);
7+
}
8+
9+
private int fibMod(int n) {
10+
if (n == 0) return 0; // F(0)
11+
if (n == 1) return 1; // F(1)
12+
13+
long a = 0; // F(0)
14+
long b = 1; // F(1)
15+
16+
for (int i = 2; i <= n; i++) {
17+
long sum = (a + b) % MOD; // 합을 long으로 계산
18+
a = b;
19+
b = sum;
20+
}
21+
return (int)(b % MOD);
22+
}
23+
}

0 commit comments

Comments
 (0)