Created
May 27, 2020 19:51
-
-
Save niczky12/1b43ec935c0314a6e4226409f25f3b0b to your computer and use it in GitHub Desktop.
First 50 fibonacci numbers in BQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | |
numbers.shift(); | |
numbers.push(new_num); | |
n -= 1; | |
} | |
return numbers[n]; | |
"""; | |
WITH raw_values as ( | |
SELECT * | |
FROM UNNEST(GENERATE_ARRAY(0, 50, 1)) AS num | |
) | |
select | |
num, | |
fibonacci(num) as fib_n | |
from raw_values | |
where num <= 10 or mod(num, 10) = 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment