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 second problem (Even Fibonacci numbers) is not very difficult:
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed 4 million, find the sum of the even-valued terms.
My ABAP solution was the following (run the program to see the solution):
REPORT ZMLA_EULERO_002.
DATA: total TYPE N LENGTH 60 VALUE 0,
s TYPE STRING.
DATA: a TYPE INT4 VALUE 1,
b TYPE INT4 VALUE 2,
limit TYPE INT4 VALUE 4000000,
tmp TYPE INT4,
lst TYPE TABLE OF INT4. "list of integers
* Append the initial items to the list
APPEND a to lst.
APPEND b to lst.
WHILE a + b < limit.
tmp = a + b.
a = b.
b = tmp.
APPEND b to lst. "Append the new item to the list
ENDWHILE.
total = 0.
LOOP AT lst INTO a.
IF a mod 2 = 0.
total = total + a.
ENDIF.
ENDLOOP.
a = total. "to make ABAP happy. 😞
s = |The solution is { a }|.
WRITE / s.