2 ) of ring_element - f is the matrix you apply to a column containing (F_n, F_) - could be a more general matrix (given as a generic parameter) to deal with - other linear sequences of order 2 f : constant matrix := ( 1 => ( 0, 1 ), 2 => ( 1, 1 )) function "*" ( a, b : matrix ) return matrix is ( 1 => ( a ( 1, 1 ) * b ( 1, 1 ) + a ( 1, 2 ) * b ( 2, 1 ), a ( 1, 1 ) * b ( 1, 2 ) + a ( 1, 2 ) * b ( 2, 2 )), 2 => ( a ( 2, 1 ) * b ( 1, 1 ) + a ( 2, 2 ) * b ( 2, 1 ), a ( 2, 1 ) * b ( 1, 2 ) + a ( 2, 2 ) * b ( 2, 2 ))) function square ( m : matrix ) return matrix is ( m * m ) - Fast_Pow could be non recursive but it doesn't really matter since - the number of calls is bounded up by the size (in bits) of Big_Int (e. With ada.text_io use ada.text_io procedure fast_fibo is - We work with biggest natural integers in a 64 bits machine type Big_Int is mod 2 ** 64 - We provide an index type for accessing the fibonacci sequence terms type Index is new Big_Int - fibo is a generic function that needs a modulus type since it will return - the n'th term of the fibonacci sequence modulus this type (use Big_Int to get the - expected behaviour in this particular task) generic type ring_element is mod with function "*" ( a, b : ring_element ) return ring_element is function fibo ( n : Index ) return ring_element function fibo ( n : Index ) return ring_element is type matrix is array ( 1. This keeps it simple for the CPU, which isn't aware of the intended size of each entry of the table.įor the purpose of this example, assume that both this code and the table are in the. Unfortunately, when using lookup tables you have very little choice, since trying to conditionally change the scaling of the index would more than likely take more code than encoding all data as the maximum size regardless of the contents, as was done here. We're wasting a lot of bytes to store very low numbers at the beginning (each takes up 4 bytes regardless of how many digits you see). For one, you end up taking up a lot of space. The elements of the table are ordered by index, so you can simply create a function that takes an offset as the parameter and returnsĪlthough lookup tables are very fast, there are some drawbacks to using them. With old computers it was common to use lookup tables to fetch pre-calculated values that would otherwise take some time to compute. Traders use the Fibonacci Extension to determine. OUTPUTS TO AX push BP push BX push AX mov BX, DI COPY INPUT TO BX xor AX, AX MOV AX,0 test BX, BX SET FLAGS ACCORDING TO BX je LBB0_4 IF BX = 0 RETURN 0 cmp BX, 1 IF BX = 1 RETURN 1 jne LBB0_3 mov AX, 1 ELSE, SET AX = 1 AND RETURN jmp LBB0_4 LBB0_3: lea DI, WORD PTR DI = BX - 1 call fib RETURN FIB(BX-1) mov BP, AX STORE THIS IN BP add BX, -2 mov DI, BX call fib GET FIB(DI - 2) add AX, BP RETURN FIB(DI - 1) + FIB(DI - 2) LBB0_4: add sp, 2 pop BX pop BP ret Using A Lookup Table Leonardo Fibonacci da Pisa is a thirteenth century mathematician who discovered the Fibonacci sequence. Signon: db 'Fibonacci number sequence:',cr,lf,'$'įib: WRITTEN IN C WITH X86-64 CLANG 3.3 AND DOWNGRADED TO 16-BIT X86 INPUT: DI = THE NUMBER YOU WISH TO CALC THE FIBONACCI NUMBER FOR. HL holds 16-bit unsigned binary number to print calculate nth Fibonacci number (max n = 24) Lhld oldstk all done get CP/M's stack back Nterms equĒ0 number of terms (max=24) to displayĬpi nterms+1 have we reached our limit? RETURN (14,12),RC=0 restore registers from calling sav L R13,4(0,R13) restore previous savearea pointer * packed dec (PL8) = 15 decimals => max fibo(73) LM R14,R12,12(R13) restore previous savearea pointer STM STM R14,R12,12(R13) save previous contextĬVD R4,PW n convert binary to packed (PL8)ĬVD R3,PW f(n) binary to packed decimal (PL8) SAVEAREA B STM-SAVEAREA(R15) skip savearea If you want precise integer arithmetic, you should ensure that all numbers involved are safe integers, by declaring and defining base at the top and then using !Number.isSafeInteger(base + 4) also, it's more idiomatic to just return posBias || negBias because both of those variables are true.įinally, you should use throw for error conditions rather than returning: if (typeof Number.* integer (31 bits) = 10 decimals -> max fibo(46) The comment is a bit misleading: It's actually testing whether the number is in 54-bit signed integer space.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |