Option Explicit Declare Function AllocConsole Lib "kernel32" () As Long Declare Function FreeConsole Lib "kernel32" () As Long Declare Function CloseHandle Lib "kernel32" ( ByVal hObject As Long ) As Long Declare Function GetStdHandle Lib "kernel32" ( ByVal nStdHandle As Long ) As Long Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" _ ( ByVal hConsoleOutput As Long, lpBuffer As Any, ByVal _ nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, _ lpReserved As Any ) As Long Declare Function Sleep Lib "kernel32" ( ByVal dwMilliseconds As Long ) As Long Public Function Fibonacci ( ByVal n As Integer ) As Integer If ( n <= 2 ) Then Fibonacci = 1 Else Fibonacci = Fibonacci ( n - 1 ) + Fibonacci ( n - 2 ) End If End Function Private Sub Main () 'create a console instance AllocConsole 'get handle of console output Dim hOut As Long hOut = GetStdHandle ( - 11 & ) 'output string to console output Dim s As String Dim i As Integer For i = 1 To 16 Step 1 s = Fibonacci ( i ) & ", " WriteConsole hOut, ByVal s, Len ( s ), vbNull, vbNull Next i s = "." & vbCrLf WriteConsole hOut, ByVal s, Len ( s ), vbNull, vbNull 'make a pause to look at the output Sleep 2000 'close the handle and destroy the console CloseHandle hOut FreeConsole End Sub Example for versions This way most numbers are stored twice, so only first number of each row is included in the result. Each row of recursive query contains two consecutive numbers of the sequence, and next row is calculated as (last number, sum of numbers) of previous row. This example uses a kind of iterative definition of Fibonacci numbers, implemented with a recursive query. c1 : output three dots Example for versions c1c2 : print comma and space block : actually calculate next Fibonacci in c6 > c5v0 : move c5 to c6 and c4 (need to preserve it) > c6v0 : move c6 with sum to c5 <<< - c3 : decrement loop counter ] << ++. ![]() Writing long arithmetics in Brainfuck is a bit of overkill, so in this example we assume that memory cells can store integer values. This example uses one minor cheat: classic Brainfuck interpreter uses byte variables to store values of memory cells, so Fibonacci numbers 14 through 16 will cause overflow. A low-level description is given in the comments, notation “cXvY” meaning that after execution of the commands in the line the data pointer is at cell X, and the value at this cell is Y. ![]() A high-level description of what it does is: store two last numbers in variables c4 and c5 (initially c4=0, c5=1), print the number stored in c5 (this operation takes the major part of the code), calculate next number (c6 = c5+c4), and move the numbers sequence one number back (c4 = c5, c5 = c6). This example uses iterative definition of Fibonacci numbers. ![]() 5, n )) / sqrt ( 5 )) fib from ( select level n from dual connect by level <= 16 ) t1 ) t2 ) START WITH r = 1 CONNECT BY PRIOR r = r - 1 Example for versions SELECT REPLACE ( MAX ( SYS_CONNECT_BY_PATH ( fib || ', ', '/' )), '/', '' ) || '.' fiblist FROM ( SELECT n, fib, ROW_NUMBER () OVER ( ORDER BY n ) r FROM ( select n, round (( power (( 1 + sqrt ( 5 )) * 0. This example uses recursive definition of Fibonacci numbers. The least efficient way which illustrates the usage of recurrent functions. Note that this example can be implemented in several ways: ![]() (Note that this is different than the traditional definition of the Fibonacci function, where F(0) = F(1) = 1, but it doesn’t really matter, as the output is the same.) Fibonacci numbers is a numerical sequence, in which first two elements are equal to 1, and each remaining number is equal to the sum of the previous two: F(1) = F(2) = 1, F(n) = F(n-1) + F(n-2).
0 Comments
Leave a Reply. |