class Solution {\n static long fibSum(long N) {\n long MOD = 1000000007;\n if (N == 0)…
- User
class Solution {\n static long fibSum(long N) {\n long MOD = 1000000007;\n if (N == 0) return 0;\n if (N == 1) return 1;\n\n long a = 0;\n long b = 1;\n long sum = 1;\n\n for (long i = 2; i <= N; i++) {\n long c = (a + b) % MOD;\n sum = (sum + c) % MOD;\n\n a = b;\n b = c;\n }\n\n return sum;\n }\n}
convert java code to javascript
Assistant
Here's the JavaScript equivalent of your Java code:
function fibSum(N) { const MOD = 1000000007; if (N === 0) return 0; if (N === 1) return 1; let a = 0; let b = 1; let sum = 1; for (let i = 2; i <= N; i++) { let c = (a + b) % MOD; sum = (sum + c) % MOD; a = b; b = c; } return sum; }Notes:
- In JavaScript, we don't specify types like
long. Numbers can be treated as integers and will work fine for reasonably large values. - The logic remains exactly the same: computing the Fibonacci numbers iteratively and maintaining a running sum modulo .
- Use
letorconstinstead oflongfor variable declarations.
Example Usage:
console.log(fibSum(0)); // 0 console.log(fibSum(5)); // 1 + 1 + 2 + 3 + 5 = 12 console.log(fibSum(100000)); // very large, but handled safely due to mod- In JavaScript, we don't specify types like
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.