Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Fibonacci series

Former Member
12,986

Hi Gurus,

Can any one tell me how to write the code for Fibinacci series ?

Thanks in Advance

Sri..

Hi Gurus,

Can any one tell me how to write the code for Fibinacci series ?

Thanks in Advance

Sri..

14 REPLIES 14
Read only

Former Member
0 Likes
6,900

Hello Krishna,

Here is the code... Its working..

REPORT zsample .

DATA: a TYPE i,

b TYPE i,

c TYPE i.

MOVE 1 TO b.

WRITE: a.

a = 0.

b = 1.

DO 10 TIMES.

c = a + b.

a = b.

b = c.

write: c.

ENDDO.

<b><REMOVED BY MODERATOR></b>

-


Sasi.

Message was edited by:

Alvaro Tejada Galindo

Read only

0 Likes
6,900

Hi krishna ,

DATA: output TYPE i.
PARAMETERS: p_num TYPE i.
START-OF-SELECTION.
PERFORM fibo USING p_num
CHANGING output.
WRITE:/ output.
*&---------------------------------------------------------------------*
*& Form fibo
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM fibo USING num
CHANGING out.
DATA: lv_num1 TYPE i,
lv_num2 TYPE i,
out1 TYPE i,
out2 TYPE i.
IF num > 2.
lv_num1 = num - 1.
PERFORM fibonacci USING lv_num1
CHANGING out1.
lv_num2 = num - 2.
PERFORM fibonacci USING lv_num2
CHANGING out2.
out = out1 + out2.
ELSE.
out = num.
ENDIF.
ENDFORM. 

Read only

Former Member
0 Likes
6,900

Hi,

The usual Fibonacci sequence is

0,1,1,2,3,5,8,13,21,...

but you may wish to have parameters for the first two terms (defaulting maybe to 0 and 1).

Then you could get the likes of

1,4,5,9,14,23,37,60,...

Also worth reporting is the ratio of consecutive terms (avoiding division by zero). The ratio's limiting value is the "golden ratio".

So you may wish also to have a parameter for how many terms to report.

John

Read only

former_member189059
Active Contributor
0 Likes
6,900
report  zkris_fibonacci.

data: lv_one type i value 0.
data: lv_two type i value 1.
data: lv_last type i value 1.

write:/ lv_one.
write:/ lv_two.
do 20 times.
  lv_last = lv_one + lv_two.
  lv_one = lv_two.
  lv_two = lv_last.
  write:/ lv_last.
enddo.
Read only

Former Member
0 Likes
6,900

DATA: a TYPE i value 0,

b TYPE i value 1 ,

c TYPE i ,

j type i value 1 .

write : a .

while j ne 10 .

c = a + b.

a = b .

b = c .

j = j + 1 .

write: c.

endwhile .

Read only

0 Likes
6,900

Accept a number ‘N’ from the user and print the first ‘N’ numbers of the fibonacci series using a subroutine. Implement this using recursion technique.

REPORT zsr_mod_techni_sub3_1.
*------------declare the types----------------------------------------*
DATA: output TYPE i,
      input1 TYPE i VALUE 0,
      input2 TYPE i VALUE 1.
PARAMETERS: p_num TYPE i.
*-------------------build logic----------------------------------------*
START-OF-SELECTION.
write: / input1, input2.
DO p_num TIMES.
PERFORM fibbonaci USING input1 input2
CHANGING output.
WRITE: output.
ENDDO.

*&---------------------------------------------------------------------*
*&      Form  FIBBONACI
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_INPUT1  text
*      -->P_INPUT2  text
*      <--P_OUTPUT  text
*----------------------------------------------------------------------*
FORM fibbonaci  USING    p_input1
                         p_input2
                CHANGING p_output.
p_output = p_input1 + p_input2.
p_input1 = p_input2.
p_input2 = p_output.
ENDFORM.<br>
Read only

6,900

14 year old question, answered with obsolete ABAP like FORM.

Read only

6,900

you could use cl_demo_output=>display to avoid WRITE statement

Read only

matt
Active Contributor
0 Likes
6,900

Do you think you might edit your solution using the "code" button in the editor?

And you are aware that FORMs are obsolete. I'd rewrite it as a class with methods - and unit tests. You know, make it really modern code.

Read only

0 Likes
6,900
REPORT zfib_program.

*SAMPLE Fibonacci no: 0 1 1 2 3 5 8 13 21 34 ....

PARAMETERS: input TYPE i DEFAULT 5.

CLASS fibonacci DEFINITION.

  PUBLIC SECTION.

    TYPES: BEGIN OF hash,

             key TYPE i,

             val TYPE i,

           END OF hash.

    DATA hashtab TYPE HASHED TABLE OF hash WITH UNIQUE KEY key.

    DATA hash_row TYPE hash.

    DATA: output TYPE i.

    METHODS compute IMPORTING input TYPE i RETURNING VALUE(output) TYPE i.

ENDCLASS.

CLASS fibonacci IMPLEMENTATION.

  METHOD compute.    

    DATA: lv_num1 TYPE i,  lv_num2 TYPE i.

    DATA: out1 TYPE i,

          out2 TYPE i.

    IF ( input > 2 ).

      lv_num1 = input - 1.

      READ TABLE hashtab INTO hash_row WITH KEY key = lv_num1.

      IF sy-subrc NE 0.

        out1 = me->compute( input = lv_num1 ).

      ELSE.

        out1 = hash_row-val.

      ENDIF.

      lv_num2 = input - 2.

      READ TABLE hashtab INTO hash_row WITH KEY key = lv_num2.

      IF sy-subrc NE 0.

        out2 = me->compute( input = lv_num2 ).

      ELSE.

        out2 = hash_row-val.

      ENDIF.      output = out1 + out2.

      READ TABLE hashtab INTO hash_row WITH KEY key = input.

      IF sy-subrc NE 0.

        hash_row-key = input.

        hash_row-val = output.

        INSERT hash_row INTO TABLE hashtab.

      ENDIF.

    ELSE.

      output = input.

    ENDIF.

  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

  DATA(fibonacci) = NEW fibonacci(  ).  

  IF  input < 0.

   WRITE:/ 'OOPS!!! Computing not possible for negative integer. Enter positive interger.'.

   RETURN.

 ENDIF.

DATA(out) = fibonacci->compute( input - 1 ).

cl_demo_output=>display( |Fibonacci({ input }):{ out  }| ).
Read only

6,900

use the button [CODE] to display code in a msg

Read only

0 Likes
6,900

To improve the performance used hashtable.

Read only

0 Likes
6,900

SAP ABAP Fibonacci Series

REPORT  ZFIBONACCI.
DATA:
      w_a type i value 0,
      w_b type i value 1,
      w_c type i.
do 10 times.


  w_c = w_a + w_b.
  w_a = w_b.
  w_b = w_c.
  WRITE w_c.
  ENDDO.
Read only

0 Likes
6,900

" 0 ,1 , 1 ,2 , 3, 5 , 8 , 13, 21... .......

class fibonacii_series DEFINITION.
PUBLIC SECTION.
methods : fibonacii IMPORTING value(lenght) type i
CHANGING value(series) type string.
endclass.
class FIBONACII_SERIES IMPLEMENTATION.
method FIBONACII.
do LENGHT times.
data : a type i value 0,
b type i value 1,
c type i.
series = |{ series }| & |{ a }| & |,| .
c = a + b.
a = b.
b = c.
enddo.
free : a , b , c.
"remove last comma from string
data(str_len) = strlen( series ).
subtract 1 from str_len.
series = series+0(str_len).
endmethod.
endclass.
START-OF-SELECTION.
data : result type string.
new FIBONACII_SERIES( )->FIBONACII(
EXPORTING
LENGHT = 10
CHANGING
SERIES = result
).
CL_DEMO_OUTPUT=>DISPLAY( result )