Skip to content
← All answers
AmeriGA · AI answers archive

A function that returns the n-th Fibonacci number for large n, as a string

Asked on November 20, 2026

What the AIs say

Gemini 3.8 Flash, Nemotron 3 Ultra, and ERNIE 5.5 Flash all implement the optimal O(log n) fast doubling algorithm required for large n. Nemotron 3 Ultra is selected as the best for its clean code and readability, while ERNIE 5.5 Flash offers the most comprehensive input validation.

Each AI's answer

Gemini 3.1 Flash Lite

Google

To handle large Fibonacci numbers without overflow, the solution uses JavaScript's built-in `BigInt` type, which supports arbitrary-precision integers. It iterates from 2 to n, maintaining the previous two numbers in the sequence using BigInt addition, and finally converts the result to a string as requested.

function solve(n) {
  if (n < 0) return "0";
  if (n === 0) return "0";
  if (n === 1) return "1";

  let prev = BigInt(0);
  let curr = BigInt(1);

  for (let i = 2; i <= n; i++) {
    let next = prev + curr;
    prev = curr;
    curr = next;
  }

  return curr.toString();
}

Gemini 3.5 Flash

Google

Calculates the n-th Fibonacci number using BigInt in JavaScript to handle arbitrarily large integers without precision loss. Iteratively updates values up to n and returns the result converted to a string.

function nthFibonacci(n) {
  if (n === 0) return "0";
  if (n === 1) return "1";
  let a = 0n;
  let b = 1n;
  for (let i = 2; i <= n; i++) {
    const c = a + b;
    a = b;
    b = c;
  }
  return b.toString();
}

Gemini 3.8 Flash

Google

Uses the fast doubling Fibonacci algorithm with native JavaScript `BigInt` for arbitrary-precision arithmetic. By computing recursively for n / 2 using identities F(2k) = F(k)[2F(k+1) - F(k)] and F(2k+1) = F(k)^2 + F(k+1)^2, it achieves O(log n) multiplications, efficiently handling very large values of n before converting the result to a string.

function fibonacci(n) {
  n = BigInt(n);
  if (n < 0n) throw new Error("Input must be non-negative");
  function fastFib(k) {
    if (k === 0n) return [0n, 1n];
    const [a, b] = fastFib(k >> 1n);
    const c = a * (2n * b - a);
    const d = a * a + b * b;
    if (k & 1n) {
      return [d, c + d];
    }
    return [c, d];
  }
  return fastFib(n)[0].toString();
}

Nemotron 3 Ultra ★

NVIDIA

Uses fast doubling with BigInt for O(log n) time complexity, efficiently computing large Fibonacci numbers and returning them as strings.

function solve(n) {
    // Convert input to BigInt to handle large n
    const bigN = BigInt(n);
    
    // Fast doubling algorithm
    function fib(k) {
        if (k === 0n) return [0n, 1n];
        const [a, b] = fib(k >> 1n);
        const c = a * (2n * b - a);
        const d = a * a + b * b;
        if ((k & 1n) === 0n) {
            return [c, d];
        } else {
            return [d, c + d];
        }
    }
    
    return fib(bigN)[0].toString();
}

ERNIE 5.5 Flash

Baidu

Uses BigInt fast doubling in O(log n) arithmetic steps and returns F(n) as a decimal string, with F(0)=0 and F(1)=1.

function solve(n) {
  let k;
  if (typeof n === 'bigint') {
    k = n;
  } else if (typeof n === 'number') {
    if (!Number.isSafeInteger(n)) throw new RangeError('n must be a safe integer');
    k = BigInt(n);
  } else if (typeof n === 'string') {
    const s = n.trim();
    if (s.length === 0) throw new TypeError('n must not be empty');
    let decimal = true;
    for (const ch of s) {
      if (ch < '0' || ch > '9') { decimal = false; break; }
    }
    if (!decimal) throw new TypeError('n must be a decimal integer string');
    k = BigInt(s);
  } else {
    throw new TypeError('n must be a non-negative integer or decimal string');
  }
  if (k < 0n) throw new RangeError('n must be non-negative');
  function fibPair(x) {
    if (x === 0n) return [0n, 1n];
    const half = fibPair(x / 2n);
    const a = half[0];
    const b = half[1];
    const c = a * ((b << 1n) - a);
    const d = a * a + b * b;
    if (x % 2n === 0n) return [c, d];
    return [d, c + d];
  }
  return fibPair(k)[0].toString();
}

Ask your own question

Ask anything: many AIs answer at once, side by side.

Ask your own question