Hackerrank – Project Euler+ #012 – Highly divisible triangular number

Hackerrank – Problem description

The problem description – Hackerrank.

Solution

I check all numbers from 1 to N. I need to do a prime factorization of each number. In the end there is a following state:

    \[n = p_1^{e_1} * p_2^{e_2} * \ldots\]

. To calculate the prime numbers I use for example Sieve of Eratosthenes. I calculate the exponents and product of calculated exponent values:

    \[divisors = \prod_{i=1}^{count~of~e} {e_i + 1}\]

.
If there exists a number with

    \[divisors\]

value greater than given amount, it is the result.

Prime factorization is used for calculating all divisors. For example

    \[6\]

is divisible by

    \[2\]

and

    \[3\]

, so factorized

    \[6\]

is:

    \[6 = 2^1 \times 3^1\]

.

Let’s take for example the number

    \[24\]

:

    \[24 = 2^3 \times 3^1\]

All divisors of

    \[24\]

are combinations of numbers when changing range of calculated exponent.There is

    \[2\]

prime number to be combined from

    \[0\]

to

    \[3\]

exponent and

    \[3\]

from

    \[0\]

to

    \[1\]

. These are the combinations:

There are

    \[8\]

divisors for

    \[24\]

– one exponent is

    \[3\]

and second

    \[1\]

. We can use

    \[0\]

as an exponent, too. That’s why we have to multiply incremented exponent value.

    \[8 = (3 + 1) * (1 + 1)\]

I created solution in:

All solutions are also available on my GitHub profile.

Java

Leave a Reply

Your email address will not be published. Required fields are marked *