How to determine number with same amount of odd and even divisors

With given number N, how to determine first number after N with same amount of odd and even divisors? For example if we have N=1, then next number we are searching for is :

2
because divisors:
odd : 1
even : 2

I figured out that this special number can't be odd and obviously it can't be prime. I can't find any formula for this or do i have just compute it one by one and check if it's this special number ? Obviously 1 and number itself is divisors of this number. Cheers


To get some idea of what's going on, we do like other scientists do, we experiment.

Special numbers will be even, so we write down the number of odd divisors, even divisors, for the even numbers, starting with $2$. If a number turns out to be special, we put a $\ast$ in its row.

So we make a table, giving the number, how many odd divisors it has, how many even. Calculations are easy, but we must be very careful, since errors could lead us down the wrong path.

$2 \qquad 1 \qquad 1\quad\ast$

$4 \qquad 1 \qquad 2$

$6 \qquad 2 \qquad 2\quad\ast$

$8 \qquad 1 \qquad 3$

$10 \qquad 2 \qquad 2\quad\ast$

$12 \qquad 2 \qquad 4$

$14 \qquad 2 \qquad 2\quad\ast$

$16 \qquad 1 \qquad 4$

$18 \qquad 3 \qquad 3\quad\ast$

We could easily go on for a while. It is definitely not a waste of time, since it is useful to be well-acquainted with the structure of the smallish numbers that we bump into often.

A pattern seems to jump out: every second even number seems to be special. It looks as if "special" numbers are not all that special! It can be dangerous to jump to conclusions from data about small integers. But in this case, the conclusion turns out to be correct.

The special numbers, so far, all have the shape $2a$, where $a$ is an odd number. They are divisible by $2$ but not by $4$. The even numbers in our list that are not special are all divisible by $4$.

Now we try to prove that every number that is divisible by $2$ but not by $4$ is special, and that the others are not.

Take an odd number $b$, and look at the number $2b$. Think about the divisors of $2b$. If $k$ is an odd divisor of $2b$, then $2k$ is an even divisor of $2b$, and vice-versa.

If $k$ is an odd divisor of $2b$, call $2k$ the friend of $k$. Split the divisors of $2b$ into pairs of friends. For example, if $b=45$, we have the following pairs of friends.

$$(1,2)\qquad (3,6) \qquad(5,10)\qquad(9,18)\qquad(15,30) \qquad (45,90)$$

We have split the divisors of $2b$ into pairs of friends. Each pair has one odd number and one even number, so $2b$ has exactly as many odd divisors as even divisors.

Now let's show that no number divisible by $4$ can be special. The idea is that if a number is divisible by $4$, then it has "too many" even divisors. I will not write out the details, but you should. The idea goes as follows. Take a number $n$ that is divisible by $4$, like $36$ or $80$. Split the divisors of $n$ into teams. If $k$ is an odd divisor of $n$, put into the same team as $k$ the numbers $2k$, $4k$, and so on however far you can go.

Here are the teams for $n=36$. $$(1,2,4) \qquad (3,6,12)\qquad (9,18,36)$$

Each team has more even numbers than odd numbers, so $n$ has more even divisors than odd divisors. That means $n$ can't be special.

Now let's get to your question: what is the first special number after $N$?

If $N$ is divisible by $4$, the first special number after $N$ is $N+2$. If $N$ is divisible by $2$ but not by $4$, the first special number after $N$ is $N+4$. If $N$ has remainder $1$ on division by $4$, the first special after $N$ is $N+1$, and if the remainder is $3$, the first special is $N+3$. These facts follow easily from what we have discovered about special numbers.

Formulas: We have been operating without formulas, just straight thinking. But I should mention a relevant formula. Let $n$ be an integer greater than $1$, and express $n$ as a product of powers of distinct primes. In symbols, let $$n=p_1^{a_1}p_2^{a_2} \cdots p_k^{a_k}$$ Then the number of divisors of $n$ is given by $$(a_1+1)(a_2+1) \cdots(a_k+1)$$

For example, $720=2^43^25^1$. The number of (positive) divisors of $n$ is $(4+1)(2+1)(1+1)$.

The formula that gives the number of divisors of $n$ is not hard to prove. Try to produce a proof! The formula could be adapted to give a count of the odd divisors of $n$, and of the even divisors. Then we could use these formulas to identify the special numbers. But formulas cannot do the thinking for you. So as a first approach, the way we tackled things is much better than trying to use a formula.


I wrote a C++ application. My results are all the following numbers:

0, 2, 6, 10, 14, 18, 22, 26, 30, 34, ....

So, first zero, then two and then each time plus 4.

This means in general that if $n \equiv 2 (mod 4) $ is true, your number is one of the kind you're searching. Except from the first zero.

Here is the code of the application:

for (int i = 0; i < 1000; i += 2)
// Always +2 because of odd numbers don't have any even divisors
{
    int even = 0;
    int odd = 0;
    for (int d = 1; d <= i; ++d)
    {
        if (i % d == 0)
        {
            if (d % 2 == 0)
            {
                even++;
            } else
            {
                odd++;
            }
        }
    }
    if (even == odd)
    {
        printf("%d\n", i);
    }
}