Additional Blogs by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos

Project Euler is a website dedicated to computational problems intended to be solved with computer programs.

At the time of this writing, it includes over 400 problems, with a new one added every week.

Problems are of varying difficulty but each is solvable in less than a minute using an efficient algorithm on a modestly powered computer.

I have already solved some of the problem, mainly using J (a language in the APL family) or Python.

Last year I learned ABAP and now, to test my skill with it, I decide to solve some of the Project Euler problem using ABAP.

To be able to execute ABAP programs, I have installed a SAP NetWeaver Trial Version ABAP (Windows) under VirtualBox.

The 5th problem (Smallest multiple) can be solved with the help of the ABAP function GCD:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?     

In J the solution is given by

  *./1+i.20

232792560

An ABAP solution is below (run the program to see the solution).

It suffice to loop on the number between 1 and 20, accumulating the least common multiple at each loop.

REPORT ZMLA_EULER_005.

DATA: lcm  TYPE F VALUE 1,    "least common multiple

      n    TYPE F VALUE 1,

      stop  TYPE F VALUE 20,

      gcd  TYPE F,

      s    TYPE STRING.

WHILE n <= stop.

  CALL FUNCTION 'GCD'

    EXPORTING

      I_NUMERATOR  = lcm

      I_DENOMINATOR = n

    IMPORTING

      E_GCD        = gcd.

  lcm = lcm * n / gcd.      "lcm( a, b ) is a * b / gcd( a, b )

  n = n + 1.

ENDWHILE.

s = |The solution is { lcm }|.

WRITE / s.