‎2021 Sep 02 4:05 PM
Hi experts,
I'm facing an issue. I solved the problem but I wonder what is happening? What am I missing? I tried to calculate lcm(least common multiple) with the number range. After iteration, the result returns me zero.
When I changed it to an attribute instead of returning a parameter, it worked well. And also I tried to change my gcd method with a function, it also worked.
I just wonder what is the reason for this problem. I'm sharing my code and screen view.
I don't understand why rv_rcd parameter returns zero.
CLASS lcl_math DEFINITION.
PUBLIC SECTION.
TYPES ty_range TYPE TABLE OF int4.
METHODS gcd IMPORTING iv_val1 TYPE int4
iv_val2 TYPE int4
RETURNING VALUE(rv_gcd) TYPE int4.
METHODS findlcm IMPORTING it_range TYPE ty_range
RETURNING VALUE(rv_lcm) TYPE int4.
ENDCLASS.
CLASS lcl_math IMPLEMENTATION.
METHOD gcd.
IF ( iv_val2 = 0 ).
rv_gcd = iv_val1.
WRITE : / 'RV_GCD: ' , rv_gcd.
RETURN.
ENDIF.
me->gcd( iv_val1 = iv_val2 iv_val2 = iv_val1 MOD iv_val2 ).
ENDMETHOD.
METHOD findlcm.
DATA : lv_counter TYPE int4 VALUE 2,
lv_gcd TYPE int4.
rv_lcm = it_range[ 1 ].
WHILE lv_counter <= lines( it_range ).
DATA(val) = it_range[ lv_counter ].
TRY.
lv_gcd = me->gcd( iv_val1 = val iv_val2 = rv_lcm ).
WRITE : / 'LV_GCD: ' , lv_gcd.
rv_lcm = ( val * rv_lcm ) / lv_gcd.
CATCH cx_sy_zerodivide.
ENDTRY.
lv_counter = lv_counter + 1.
ENDWHILE.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(gref_math) = NEW lcl_math( ).
gref_math->findlcm(
EXPORTING
it_range = VALUE #( ( 10 ) ( 15 ) ( 6 ) ( 20 ) )
RECEIVING
rv_lcm = DATA(gv_lcm)
).
ULINE.
WRITE gv_lcm.

‎2021 Sep 02 4:23 PM
METHOD gcd.
IF ( iv_val2 = 0 ).
rv_gcd = iv_val1.
WRITE : / 'RV_GCD: ' , rv_gcd.
RETURN.
ENDIF.
me->gcd( iv_val1 = iv_val2 iv_val2 = iv_val1 MOD iv_val2 ). "<--- not assigned to anything
ENDMETHOD.
If not, you call the method again, without assigning its returning value, so the method returns zero.
‎2021 Sep 02 4:23 PM
METHOD gcd.
IF ( iv_val2 = 0 ).
rv_gcd = iv_val1.
WRITE : / 'RV_GCD: ' , rv_gcd.
RETURN.
ENDIF.
me->gcd( iv_val1 = iv_val2 iv_val2 = iv_val1 MOD iv_val2 ). "<--- not assigned to anything
ENDMETHOD.
If not, you call the method again, without assigning its returning value, so the method returns zero.
‎2021 Sep 02 4:30 PM