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

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 problem 3 (Largest prime factor) is:

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

J has a primitive to compute the factors of an integer. So in J the solution is a one-line:

    >./ q: 600851475143  NB. max of factors

In Python 3, using a simple algorithm of trial division to compute the factors:

from math import sqrt
def factors( n ) :
    ret = [ 1 ]
    while n % 2 == 0 :
        n //= 2
        ret.append( 2 )
    lim = int( sqrt( n ) )
    i = 3
    while i <= lim :
        while n % i == 0 :
            ret.append( i )
            n //= i
            lim = int( sqrt( n ) )
        i += 2
    if n > 1 :
        ret.append( n )
    return ret
if __name__ == '__main__':
    print( "The solution is %d" % factors( 600851475143 )[-1] )

With this problem I started to implement some utility functions and some types in ABAP.

The first of the function is ZMLA_EULER=>FACTORS (see below, after the report ZMLA_EULERO_003) that take an integer as input and produce a list of integer.

Using ZMLA_EULER=>FACTORS we can solve the problem as follow:

REPORT  ZMLA_EULERO_003.


FIELD-SYMBOLS: <sol> TYPE ZMLA_EULER=>enumber.

DATA: n   TYPE ZMLA_EULER=>enumber        VALUE 600851475143,
      lst TYPE ZMLA_EULER=>listof_enumber,
      sol TYPE i,
      s   TYPE string.

CALL METHOD ZMLA_EULER=>FACTORS
  EXPORTING
    N = N
  IMPORTING
    ORET = lst.

READ TABLE lst ASSIGNING <sol> INDEX lines( lst ).
sol = <sol>.
WRITE / |The solution is { sol ZERO = NO }|.

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

class ZMLA_EULER definition
  public
  create public .

  public section.

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

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

CLASS ZMLA_EULER IMPLEMENTATION.


* <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.
    ENDWHILE.
    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 ).
      ENDWHILE.
      i = i + 2.
    ENDWHILE.
    IF n > 1.
      APPEND n to oret.
    ENDIF.
  endmethod.
ENDCLASS.