jacknpoe

Fibonacci (english)

Jun 1st, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 0.92 KB | None | 0 0
  1. #
  2. # Return the first <number_seq> numbers of Fibonacci sequence,
  3. # omits (<omit_0>) or not the initial zero
  4. #
  5.  
  6. DELIMITER |
  7. CREATE OR REPLACE FUNCTION Fibonacci( number_seq INT, omit_0 BOOLEAN) RETURNS VARCHAR( 512) DETERMINISTIC NO SQL
  8. BEGIN
  9.     DECLARE first INT DEFAULT 0;
  10.     DECLARE second INT DEFAULT 1;
  11.     DECLARE temporary INT DEFAULT 0;
  12.     DECLARE cont INT DEFAULT 0;
  13.     DECLARE acumulator VARCHAR( 512) DEFAULT "";
  14.  
  15.     IF omit_0 THEN
  16.         SET first = 1;
  17.     END IF;
  18.  
  19.     IF number_seq > 0 THEN
  20.         SET acumulator = cast( first AS CHAR);
  21.     END IF;
  22.  
  23.     IF number_seq > 1 THEN
  24.         SET acumulator = concat( acumulator, ' / ', second);
  25.     END IF;
  26.  
  27.     IF number_seq > 2 THEN
  28.         REPEAT
  29.             SET temporary = second;
  30.             SET second = second + first;
  31.             SET first := temporary;
  32.  
  33.             SET acumulator = concat( acumulator, ' / ', second);       
  34.  
  35.             SET cont := cont + 1;
  36.         UNTIL cont = number_seq - 2 END REPEAT;
  37.     END IF;
  38.  
  39.     RETURN acumulator;
  40. END |
Add Comment
Please, Sign In to add comment