Skip to Content

Project Euler Problem 0007 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 7th problem (10001st prime) is the following:

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10.001st prime number?

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

The program uses a class with some utility functions and some types that I had already implemented for problem 3.

The source code of this classes is below the program.

To solve the problem I use a list of primes that initially contains the primes [2,3,5,7,11,13].

The loop try to add new primes to that list starting with n=15 and incrementing by 2 at each iteration.

When the list of primes contains 10.001 items we break the loop, read the last item of primes, and print the solution.

REPORT zmla_euler_007.

FIELD-SYMBOLS: <i> TYPE zmla_euler=>enumber.

DATA: add_lim TYPE int4              VALUE 10001,
      lim     TYPE int4,
      n       TYPE int4              VALUE 17,     ” n is initially the first prime greater than 13
      primes  TYPE zmla_euler=>listof_enumber,
      sol     TYPE int4.

APPEND  2 TO primes.
APPEND  3 TO primes.
APPEND  5 TO primes.
APPEND  7 TO primes.
APPEND 11 TO primes.
APPEND 13 TO primes.

  IF lines( primes ) >= add_lim.
  lim = ceil( sqrt( n ) ).
  DO lines( primes ) TIMES.
    READ TABLE primes ASSIGNING <i> INDEX sy-index.
    IF ( ( n MOD <i> ) = 0 ) OR ( <i> > lim ).
  IF ( n MOD <i> ) <> 0.    ” True if n is prime.
    APPEND n TO primes.     ” Append n to the list of primes.
  n = n + 2.

READ TABLE primes ASSIGNING <i> INDEX add_lim.
sol = <i>.

WRITE / |The result is { sol }|.

#### ZMLA_EULER class source code ####

class ZMLA_EULER definition
  create public .

  public section.

      enumber        TYPE          N  LENGTH 60 .
      listof_enumber  TYPE TABLE OF enumber .

    class-methods FACTORS
        value(N) type ENUMBER
        value(ORET) type listof_enumber .
  protected section.
  private section.


* <SIGNATURE>———————————————————————–+
* | Static Public Method ZMLA_EULER=>FACTORS
* +———————————————————————————+
* | [—>] N                              TYPE        ENUMBER
* | [<—] ORET                          TYPE        LISTOF_ENUMBER
* +———————————————————————-</SIGNATURE>
  method FACTORS.
    CLEAR oret.
    APPEND 1 to oret.
    WHILE n mod 2 = 0.
      n = n / 2.
      APPEND 2 to oret.
    DATA: lim type enumber,
          i  type enumber.
    lim = sqrt( n ).
    i  = 3.
    WHILE i <= lim.
      WHILE n mod i = 0.
        APPEND i to oret.
        n = n / i.
        lim = sqrt( n ).
      i = i + 2.
    IF n > 1.
      APPEND n to oret.

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