The Modern Cpp Challenge on Mobile — Abundant numbers

Hello ! I’m Xavier Jouvenot and here is the sixth part of a long series on The Modern C++ Challenge. In this article, I am going to explain how I solved the sixth problem in C++, and how I integrated the solution in an Android project.

The objective of this sixth problem is simple. We must that prints all all abundant numbers and their abundance, up to a limit entered by the user. The solution will be computed in C++ and the interface to get the user input and display the result will be handled with the Android Studio Framework.

I encourage you to read the previous part of this series, since we are going to continue our program created in it.

What is a abundant number ?

> An abundant number is a number for which the sum of its proper divisors is greater than the number itself. The amount by which the sum exceeds the number is the abundance.

For example, the number can be divided by , , , , and itself. So its proper divisors are , , , , , and if you sum them, you get which is greater than 12. So is a abundant number with an abundance of .

The C++ solution

First of all we have to be able to solve the problem using C++. To do so, we are going to implement 3 functions. 🙂

The first one allowing us to get the sum of all the divisor of a number.

The second function allows us to know if a number is an abundant number, and to get the abundance if it is an abundant number. This function will return a std::optional which will contain the value of the abundance, or std::nullopt if the input is not an abundant number.

The third and last function allows us to get all the abundant number with their abundance up to the limit given in parameters.

With those three functions, we can solve the problem without any problem 😉

The UI interface on Android Studio

The user interface for this problem is like the one of of the previous problem. So I won’t go into more detail, but I encourage you to go look to my blog post about it. 😉

But even if the interface for the problem is similar to the previous two problems, I want to talk to you about the user interface I had to put in place. Indeed, now that we have 6 problems solved in our application, having to click a lot of times on the next button to reach the last problem, is long, and is going to be longer each time that we add a new problem solution.

To avoid that, we have created a user interface similar to a menu where you can click on the problem you want to go to. To do so, we have created a new user interface with 6 buttons opening one problem interface.

Here are the methods I used:

Don’t worry, I am going to explain this code. 😉

First of all, the and methods create the button on the user interface with the right text and in the right position, by using a placed on the user interface in the xml definition of the user interface.

Then, we have the method which calls the methods previously defined to add the buttons with the right parameters.

And finally, the method is called when a button is clicked on and open the right activity depending on the button clicked. This implies that the class implements the class . 😉

And like that, we end up with a menu allowing us to go directly to the problem we want.

Using C++ native code

The linking the user interface to the C++ code is very similar to the previous problem.

We start by getting the results from the previously defined function, and we transform those data in a before sending it to the caller.


Voilà ! We now have an application which can solve the first six problems of The Modern C++ Challenge.

You can note that the solutions, written in this post, don’t include all the sources to make a running program, but only the interesting part of the sources to solve this problem. If you want to see the programs from end to end, you can go on my GitHub account, explore the full solution, add comments or ask questions if you want to, on the platform you read this article, it will also help me improve the quality of my articles.

Thank you all for reading this article, And until my next article, have a splendid day 😉

Interesting links

Originally published at on April 27, 2020.