Complex yet real?

We call the complexity of an integer n n as the least number of 1s required to build the integer n n using only additions, multiplications and parentheses.

Let C ( n ) \mathcal C (n) denote the complexity of the integer n n . Find the value of C ( 2016 ) \mathcal C(2016) .

Details and Assumptions

As an explicit example, the least number of 1s required to build the integer 6 is 5 because 6 = ( 1 + 1 ) × ( 1 + 1 + 1 ) 6 = (1+1)\times(1+1+1) and we cannot build the integer 6 with four 1's or less.


The answer is 22.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Arjen Vreugdenhil
Dec 22, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
int complexity[] = new int[2017];
complexity[1] = 1;

for (int n = 2; n <= 2016; n++) {
    int best = -1;

    for (int k = 1, l = n-1; k <= l; k++, l--) {
        int addc = complexity[k] + complexity[l];
        if (best < 0 || addc < best) best = addc;
    }

    for (int k = 2; k < n; k++) if (n % k == 0) {
        int l = n/k; if (k > l) break;
        int mulc = complexity[k] + complexity[l];
        if (best < 0 || mulc < best) best = mulc;
    }

    complexity[n] = best;
}

out.println(complexity[2016]);

Output:

1
22

Abdelhamid Saadi
Dec 19, 2015

My solution in python 3:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def C(n):
    Cn = [0, 1]
    for j in range(2, n+1):
        a = min(Cn[k] + Cn[j -k] for k in range(1, j//2 + 1 ))
        b = min([Cn[k] + Cn[j//k] for k in range(2, j//2 + 1) if j%k == 0] + [j])
        Cn.append(min(a,b))
    return Cn[n]

"Complex yet real?"
print(C(2016))

Output:

1
22

Nice solution.(+1)

But by pen and paper 2016 = 2 5 . 3 2 . 7 = 2 5 . 3 2 . ( 2 × 3 + 1 ) 2016=2^{5} . 3^{2} . 7=2^{5} . 3^{2} . (2 \times 3 +1) . Hence C ( 2016 ) = 22 C(2016)=\boxed{22} .

EDIT: that was by luck.. here is why.

Mahdi Al-kawaz - 5 years, 5 months ago

Log in to reply

Exactly what I did! +1


We actually need to prove the following claim to justify the method in your comment :

Claim: If, for positive integer a a , a = i p i q i a=\large\prod\limits_i p_i^{q_i} is the unique prime representation of a a , then C ( a ) = i q i C ( p i ) \mathcal{C}(a)=\sum\limits_iq_i\mathcal{C}(p_i) and for any prime p p , we have C ( p ) = 1 + C ( p 1 ) \mathcal{C}(p)=1+\mathcal{C}(p-1) with the obvious initial value C ( 1 ) = 1 \mathcal{C}(1)=1 .


Intuitively, this seems to be true and I also verified the above claim to be true for numerous values of a a by checking the result given by it with the one given by the posted code, but I can't think of a proper formal proof to the above claim.

Prasun Biswas - 5 years, 5 months ago

Log in to reply

"Guy asks if a(p) = a(p-1) + 1 for prime p. Martin Fuller found the least counterexample p = 353942783 in 2008"

Isaac Buckley - 5 years, 5 months ago

Log in to reply

@Isaac Buckley Well, I'll be damned!

Prasun Biswas - 5 years, 5 months ago

This is actually a very complex problem (no pun intended).

For eg, why not C ( p ) = ( 1 + 1 ) + C ( p 2 ) \mathcal C (p) = (1 + 1) + \mathcal C (p - 2) .

Or by considering your approach, C ( p ) = 1 + C ( p 1 ) \mathcal C (p) = 1 + \mathcal C (p - 1) ,

what if p 1 = x q p - 1 = x \cdot q where q q is a large prime again? It can be solved easily with computers but it's a hard job manually.

Arulx Z - 5 years, 5 months ago

Log in to reply

@Arulx Z Because....... this will explain better.

Mahdi Al-kawaz - 5 years, 5 months ago

It's funny that you tried that approach, because I started that way too. Then I realized that the complexity of a slightly smaller number (e.g. 2015) might well be significantly less... So that's where I started coding.

Arjen Vreugdenhil - 5 years, 5 months ago

You need to prove that 22 is the minimum.

Pi Han Goh - 5 years, 5 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...