var fib = function (a, b) {
var _current = a + b;
return {
current: _current,
next: function () {
return fib(b, _current);
}
}
}
var generator = fib(1,1);
console.log(generator.current); // will print out 2
var result = generator.next();
console.log(result.current); // will print out 3
var take = function(n, sequence) {
var result = [];
var temp = sequence;
for (var i = 0; i < n; i++) {
result.push(temp.current);
temp = temp.next();
}
return result;
}
var fib_generator = function *(){
var current = 0, next = 1;
while(true)
{
[next, current] = [next+current, next];
yield current;
}
}
var fib = fib_generator();
for(var i = 0; i < 10; i++)
{
console.log(fib.next().value);
}
for(var i = 0; i < 10; i++)
{
var currentResult = fib.next();
console.log(currentResult.value);
}
REPORT Z_FIBO.
PARAMETERS: N type i,
v1 RADIOBUTTON GROUP v default 'X',
v2 RADIOBUTTON GROUP v.
data: f type i,
t type i.
data: product_guid type comm_product-product_guid.
get run time field t.
case 'X'.
when v1. perform fibonacci using n changing f.
when v2. perform fibonacci_2 using n changing f.
endcase.
write: / 'Fibonacci(', n, ') =', f.
get run time field t.
write: / 'Runtime', t, 'microseconds'.
*&---------------------------------------------------------------------*
*& Form fibonacci
*&---------------------------------------------------------------------*
form fibonacci using in type i
changing fib type i.
data: f_1 type i, f_2 type i,
n_1 type i, n_2 type i.
case in.
when 0. fib = 1.
when 1. fib = 1.
when others.
n_1 = in - 1.
n_2 = in - 2.
perform fibonacci using n_1 changing f_1.
perform fibonacci using n_2 changing f_2.
fib = f_1 + f_2.
endcase.
endform. "fibonacci
*&---------------------------------------------------------------------*
*& Form fibonacci_2
*&---------------------------------------------------------------------*
form fibonacci_2 using in type i
changing fib type i.
data: f_1 type i, f_2 type i,
n_1 type i, n_2 type i,
l type i.
data: fibo type table of i.
append 1 to fibo. " fibonacci(0)
append 1 to fibo. " fibonacci(1)
n_1 = 1.
n_2 = 2.
l = in - 1.
do l times.
read table fibo index n_1 into f_1.
read table fibo index n_2 into f_2.
fib = f_1 + f_2.
add 1 to n_1. add 1 to n_2.
append fib to fibo.
enddo.
endform. "fibonacci_2
CLASS zcl_fibo DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
CLASS-METHODS next
RETURNING
VALUE(rv_result) TYPE int4 .
PROTECTED SECTION.
PRIVATE SECTION.
CLASS-DATA sv_current TYPE int4 VALUE 0 ##NO_TEXT.
CLASS-DATA sv_next TYPE int4 VALUE 1 ##NO_TEXT.
ENDCLASS.
CLASS ZCL_FIBO IMPLEMENTATION.
METHOD next.
rv_result = sv_next + sv_current.
sv_current = sv_next.
sv_next = rv_result.
ENDMETHOD.
ENDCLASS.
REPORT ZFIBO.
DO 10 TIMES.
WRITE: / zcl_fibo=>next( ).
ENDDO.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
2 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |