# Counting N-bit integer multiplication overflows

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

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

# 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

• Very nice improvement!
– user9206
Jun 8 '17 at 10:00
• You can swap x=0;y=0 for x=y=0 Jun 8 '17 at 17:31
• It would be super cool if you implemented the 2^{N/3} solution too.
– user9206
Jun 8 '17 at 18:04
• A full program would be 4 bytes shorter. Jun 9 '17 at 4:23
• Swapping some signs saves one more byte. tio.run/… Jun 9 '17 at 5:00

# 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.


# 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


# Brachylog, 21 bytes

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


Try it online!

• You don't need to explicitly give the name A to that variable, saving one byte. Jun 8 '17 at 10:18

# 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

• Just P is enough here. Jun 8 '17 at 6:47
• @Emigna it is, thanks. I will edit that Jun 8 '17 at 6:48

## JavaScript (ES7), 7065 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))
}

## 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.

## Clojure, 78 bytes

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


# Actually, 13 bytes

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


Try it online!

A literal implementation of the problem. Quite slow.

# PHP, 62 bytes

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


Try it online!

# Python 2, 53 bytes

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


Try it online!

# Jelly, 11 bytes

2*’©ŒċP€>®S


Try it online!

A literal implementation of the problem. Quite slow.