Vibescript Showcase

Big integers

Calculate factorials, Fibonacci numbers, and powers larger than 64 bits.

Showcase
Source showcase/numbers/big_integers.vibe
# title: Big integers
# category: Vibescript Showcase
# difficulty: Showcase
# summary: Calculate factorials, Fibonacci numbers, and powers larger than 64 bits.
# description: Integers grow as needed without losing precision. The sandbox charges memory based on size, and JSON keeps the full value.
# tags: numbers, bignum, math
# vibe: 0.4

def factorial(n)
  (1..n).to_a.reduce(:*)
end

def fibonacci(count)
  previous = 0
  current = 1
  count.times do
    previous, current = current, previous + current
  end
  previous
end

def exact_json_round_trip?(value)
  JSON.parse(JSON.stringify(value)) == value
end

def run
  huge = factorial(50)

  {
    factorial_50: huge,
    factorial_50_digits: huge.to_s.length,
    two_to_the_256: 2 ** 256,
    fibonacci_300: fibonacci(300),
    json_stays_exact: exact_json_round_trip?(huge),
    still_an_int: huge.even?
  }
end
Output
Press Run example to run this code.