Skip to content

Instantly share code, notes, and snippets.

@niczky12
Created May 27, 2020 20:03
Show Gist options
  • Save niczky12/c1227c53533e7c511ef9bd4219987add to your computer and use it in GitHub Desktop.
Save niczky12/c1227c53533e7c511ef9bd4219987add to your computer and use it in GitHub Desktop.
sum of fibonacci in BQ
CREATE TEMP FUNCTION fibonacci(n INT64)
RETURNS INT64
LANGUAGE js AS
"""
var numbers = [0, 1]
while (n > 1) {
var new_num = (numbers[0] + numbers[1]) % 1000000;
numbers.shift();
numbers.push(new_num);
n -= 1;
}
return numbers[n];
""";
WITH raw_values as (
SELECT *
FROM UNNEST(GENERATE_ARRAY(0, 1000)) AS num
)
select
sum(fibonacci(num)) as result
from raw_values;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment