ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

SQL ANSI: Generate Fibonacci Sequence up to N terms

SQL ANSI

Goal -- WPM

Ready
Exercise Algorithm Area
1-- This SQL script generates the Fibonacci sequence up to N terms.
2-- It uses a procedural approach with variables to iteratively calculate each term.
3
4-- Declare a variable to hold the number of terms (N)
5-- In a real stored procedure or function, this would be a parameter.
6-- For this example, we'll set it directly.
7
8-- SET @N = 10; -- Example: Generate first 10 Fibonacci numbers
9
10-- Temporary table to store the sequence
11CREATE TEMPORARY TABLE FibonacciSequence (
12term_number INT PRIMARY KEY,
13fib_value BIGINT
14);
15
16-- Handle base cases for N
17-- If N is 0, the sequence is empty.
18-- If N is 1, the sequence is [0].
19-- If N is 2, the sequence is [0, 1].
20
21-- Initialize variables for the first two Fibonacci numbers
22DECLARE
23a BIGINT := 0;
24b BIGINT := 1;
25next_fib BIGINT;
26i INT := 1; -- Counter for terms, starting from the first term
27N INT := 10; -- Set the desired number of terms here
28BEGIN
29-- Insert the first term if N >= 1
30IF N >= 1 THEN
31INSERT INTO FibonacciSequence (term_number, fib_value) VALUES (1, a);
32END IF;
33
34-- Insert the second term if N >= 2
35IF N >= 2 THEN
36INSERT INTO FibonacciSequence (term_number, fib_value) VALUES (2, b);
37END IF;
38
39-- Generate remaining terms iteratively
40WHILE i < N DO
41next_fib := a + b;
42a := b;
43b := next_fib;
44
45-- Insert the calculated Fibonacci number into the table
46-- The term number is i + 1 because we already inserted terms 1 and 2
47INSERT INTO FibonacciSequence (term_number, fib_value) VALUES (i + 1, next_fib);
48
49i := i + 1;
50END WHILE;
51
52-- Select all generated Fibonacci numbers
53SELECT term_number, fib_value FROM FibonacciSequence ORDER BY term_number;
54
55-- Clean up the temporary table (optional, depending on execution context)
56-- DROP TEMPORARY TABLE FibonacciSequence;
57END;
Algorithm description viewbox

SQL ANSI: Generate Fibonacci Sequence up to N terms

Algorithm description:

This SQL script generates the Fibonacci sequence up to a user-defined number of terms (N). It iteratively calculates each subsequent number by summing the two preceding ones, starting with 0 and 1. The sequence is stored in a temporary table and then displayed. This is a fundamental example of iterative sequence generation in SQL.

Algorithm explanation:

The script uses a procedural approach within a SQL block (often found in stored procedures or anonymous blocks). It initializes two variables, `a` and `b`, to the first two Fibonacci numbers (0 and 1). A `WHILE` loop iterates `N-2` times (after handling base cases for N=1 and N=2). In each iteration, it calculates `next_fib` as the sum of `a` and `b`, then updates `a` to `b` and `b` to `next_fib`. The newly calculated `next_fib` is inserted into a temporary table `FibonacciSequence` along with its term number. The loop continues until `N` terms are generated. The time complexity is O(N) because each term is calculated once. The space complexity is O(N) to store the sequence in the temporary table. Edge cases handled include N=0 (empty sequence), N=1 (sequence [0]), and N=2 (sequence [0, 1]). The invariant is that at the start of each loop iteration, `a` and `b` hold the two most recently generated Fibonacci numbers, and `i` represents the count of terms already processed or about to be inserted.

Pseudocode:

Initialize a temporary table `FibonacciSequence` with columns `term_number` and `fib_value`.

Set N = desired number of terms.
Set a = 0.
Set b = 1.
Set i = 1.

IF N >= 1 THEN
  Insert (1, a) into `FibonacciSequence`.
END IF.

IF N >= 2 THEN
  Insert (2, b) into `FibonacciSequence`.
END IF.

WHILE i < N DO
  next_fib = a + b
  a = b
  b = next_fib
  Insert (i + 1, next_fib) into `FibonacciSequence`.
  Increment i.
END WHILE.

Select all rows from `FibonacciSequence` ordered by `term_number`.