16
\$\begingroup\$

Given a positive integer N, output the number of pairs of integers 0 <= a <= b < 2**N such that a*b >= 2**N.

Rules

  • You may assume that N is less than or equal to the maximum bit width for integers in your language (e.g. for C, N will not exceed 32 or 64, depending on the architecture of the machine). If your language is capable of handling arbitrary-width integers, then there is no upper bound on N.

Test Cases

1 0
2 3
3 19
4 96
5 437
6 1876
7 7804
8 31904
9 129170
10 520135
11 2088143
12 8369175
13 33512744
14 134128704
15 536681553
16 2147082274
\$\endgroup\$
12
  • \$\begingroup\$ Note: I'm working on generating larger test cases now. My brute force approach is really slow. \$\endgroup\$
    – user45941
    Commented Jun 8, 2017 at 5:44
  • \$\begingroup\$ @user202729 You're duplicating some pairs by not following the a <= b condition. \$\endgroup\$
    – user45941
    Commented Jun 8, 2017 at 5:52
  • 1
    \$\begingroup\$ Some more testcases: {0, 3, 19, 96, 437, 1876, 7804, 31904, 129170, 520135, 2088143, 8369175, 33512744, 134128704, 536681553, 2147082274, 8589086503, 34357951447} \$\endgroup\$
    – user202729
    Commented Jun 8, 2017 at 6:03
  • 1
    \$\begingroup\$ Isn't there likely to be closed form formula for this problem? I must have missed something. \$\endgroup\$
    – user9206
    Commented Jun 8, 2017 at 6:30
  • 1
    \$\begingroup\$ Closely related: en.wikipedia.org/wiki/Divisor_summatory_function. There is no closed form known. \$\endgroup\$
    – orlp
    Commented Jun 8, 2017 at 7:32

12 Answers 12

8
\$\begingroup\$

Python 2, 75 68 bytes

n=input()
a=1<<n
s=~-a*a/2
x=y=0
while y<1:s+=y;x-=1;y=a/x-x
print s

Try it online!

This runs in O(2n/2) operations rather than O(2n) or O(2n), so it works on much larger inputs.

(Note that an even faster O(2n/3) algorithm exists.)

1 0
2 3
3 19
4 96
5 437
6 1876
7 7804
8 31904
9 129170
10 520135
11 2088143
12 8369175
13 33512744
14 134128704
15 536681553
16 2147082274
17 8589086503
18 34357951447
19 137435198086
20 549747939928
21 2199006781125
22 8796058620153
23 35184300378083
24 140737339120148
25 562949643323164
26 2251799170232606
27 9007197921321922
28 36028794259096612
29 144115182370060793
30 576460740519709546
31 2305842984902014765
32 9223371986742908935
33 36893488044218344323
34 147573952377320833218
35 590295809922086353118
36 2361183240537767708679
37 9444732963897547996897
38 37778931859178411534913
39 151115727444080615797321
40 604462909791437463796926
41 2417851639196741979223299
42 9671406556850476410936322
43 38685626227531971124247499
44 154742504910394112443480979
45 618970019642121099638818409
46 2475880078569598086230187969
47 9903520314280668496162705117
48 39614081257127323838921620439
49 158456325028518790167805606609
50 633825300114094540502620959956
51 2535301200456417702087608942034
52 10141204801825751449333352568660
53 40564819207303170200956592005599
54 162259276829213015854387448792578
55 649037107316852746005301421147606
56 2596148429267412374169967907532731
57 10384593717069652326923914077600197
58 41538374868278615068076777292632146
59 166153499473114471992855423428749242
60 664613997892457911812090466987383188
61 2658455991569831695728843704244440740
62 10633823966279326881474627069404687424
63 42535295865117307726213589942623257944
\$\endgroup\$
6
  • \$\begingroup\$ Very nice improvement! \$\endgroup\$
    – user9206
    Commented Jun 8, 2017 at 10:00
  • 2
    \$\begingroup\$ You can swap x=0;y=0 for x=y=0 \$\endgroup\$
    – Cyoce
    Commented Jun 8, 2017 at 17:31
  • \$\begingroup\$ It would be super cool if you implemented the 2^{N/3} solution too. \$\endgroup\$
    – user9206
    Commented Jun 8, 2017 at 18:04
  • 1
    \$\begingroup\$ A full program would be 4 bytes shorter. \$\endgroup\$
    – Dennis
    Commented Jun 9, 2017 at 4:23
  • 1
    \$\begingroup\$ Swapping some signs saves one more byte. tio.run/… \$\endgroup\$
    – Dennis
    Commented Jun 9, 2017 at 5:00
5
\$\begingroup\$

Jelly, 12 10 bytes

2*ṖµṀ:«ạ¹S

Finishes the combined test cases in under 3 seconds.

Try it online!

How it works

2*ṖµṀ:«ạ¹S  Main link. Argument: n

2*          Yield 2ⁿ.
  Ṗ         Pop; yield A := [1, ..., 2ⁿ-1].
   µ        New monadic chain. Argument: A
    Ṁ       Maximum; yield 2ⁿ-1.
     :      Divide 2ⁿ-1 by each k in A.
      «     Dyadic minimum; yield min((2ⁿ-1)/k, k) for each k in A.
        ¹   Identity; yield A.
       ạ    Absolute difference; yield k - min((2ⁿ-1)/k, k) for each k in A.
         S  Take the sum.
\$\endgroup\$
5
\$\begingroup\$

MATL, 10 9 bytes

Wqt:&*R<z

Try it online!

This tries all possible pairs. It runs out of memory in the online interpreter for input exceeding 12.

Explanation

W      % Implicitly input N. Push 2^N ('^' denotes power)
q      % Subtract 1: gives 2^N-1
t:     % Duplicate, range: pushes [0 1 2 ... 2^N-1]
&*     % Matrix of all pair-wise products
R      % Upper triangular part (including diagonal)
<      % Less-than comparison; element-wise. This gives true for products
       % that are greater than 2^N-1
z      % Number of non-zeros- Implicitly display
\$\endgroup\$
4
\$\begingroup\$

Brachylog, 21 bytes

;2↔^P>B≥Aℕ;B C×≥P∧C≜ᶜ

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You don't need to explicitly give the name A to that variable, saving one byte. \$\endgroup\$
    – Fatalize
    Commented Jun 8, 2017 at 10:18
3
\$\begingroup\$

05AB1E, 13 12 bytes

-1 byte thanks to Emigna

oDL<ã€{ÙP›_O

Try it online!

Explanation

oDL<ã€{ÙP›_O   Argument n
oD             2^n, push twice to the stack
  L<           List: [0 .. a]
    ã          Cartesian product with itself
     €{        Sort each element
       Ù       Uniquify
        P      Total product of each element
         ›_    Each element is greater or equal than 2^n
           O   Total sum
\$\endgroup\$
2
  • \$\begingroup\$ Just P is enough here. \$\endgroup\$
    – Emigna
    Commented Jun 8, 2017 at 6:47
  • \$\begingroup\$ @Emigna it is, thanks. I will edit that \$\endgroup\$
    – kalsowerus
    Commented Jun 8, 2017 at 6:48
3
\$\begingroup\$

JavaScript (ES7), 70 65 60 bytes

n=>[...Array(k=2**n-1)].reduce(p=>p+=k<++i*i&&i-(k/i|0),i=0)

Test cases

let f =

n=>[...Array(k=2**n-1)].reduce(p=>p+=k<++i*i&&i-(k/i|0),i=0)

for(n = 1; n <= 16; n++) {
  console.log(n, f(n))
}

\$\endgroup\$
2
\$\begingroup\$

Mathematica, 37 bytes

Sum[#-⌈#/a⌉~Max~a,{a,#-1}]&[2^#]&

Try it online at http://sandbox.open.wolframcloud.com. Mathematica does not have limit on integers, and this algorithm run in time 2n, so it is very slow for large n.

\$\endgroup\$
1
\$\begingroup\$

Clojure, 78 bytes

#(count(for[l[(bit-shift-left 1 %)]a(range l)b(range a l):when(>=(* a b)l)]1))
\$\endgroup\$
1
\$\begingroup\$

Actually, 13 bytes

╙;r⌠;R*i⌡M♀≥Σ

Try it online!

A literal implementation of the problem. Quite slow.

\$\endgroup\$
2
1
\$\begingroup\$

PHP, 62 bytes

for($x=$n=2**$argn;$y=--$x;)for(;$y;)$x*$y--<$n?:$r++;echo+$r;

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Python 2, 53 bytes

lambda n:sum(k-min(~-2**n/k,k)for k in range(1,2**n))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Jelly, 11 bytes

2*’©ŒċP€>®S

Try it online!

A literal implementation of the problem. Quite slow.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.