Skip to Content
Author's profile photo Former Member

Project Euler Problem 0005 in ABAP

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



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.


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.



      I_NUMERATOR  = lcm



      E_GCD        = gcd.

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

  n = n + 1.


s = |The solution is { lcm }|.

WRITE / s.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.