r/dailyprogrammer • u/Cosmologicon 2 3 • Mar 14 '18
[2018-03-14] Challenge #354 [Intermediate] Integer Complexity 2
Background
Consider all the different ways of representing a positive integer using nothing but positive integers, addition, multiplication, and parentheses. For 5678, a few such ways are:
5678 = 2*17*167
5678 = 5678
5678 = 23*59+29*149
5678 = (1+4*4)*(1+3*3*(1+3*3*4))
5678 = 2*(1+2*(1+2*(1+2*2*(1+2*2*2*2*(1+2*(1+2*2))))))
For each such representation, consider the sum of the integers involved:
2*17*167 => 2+17+167 = 186
5678 => 5678 = 5678
23*59+29*149 => 23+59+29+149 = 260
(1+4*4)*(1+3*3*(1+3*3*4)) => 1+4+4+1+3+3+1+3+3+4 = 27
2*(1+2*(1+2*(1+2*2*(1+2*2*2*2*(1+2*(1+2*2)))))) =>
2+1+2+1+2+1+2+2+1+2+2+2+2+1+2+1+2+2 = 30
For 5678, the minimum possible sum for any such representation is 27. The minimum possible sum for a given integer is known as its integer complexity, so the integer complexity of 5678 is 27. The integer complexity of the numbers 1, 2, 3, ... is:
1 2 3 4 5 5 6 6 6 7 8 7 8 8 8 8 9 8 9 9 ...
The sum of the integer complexities for all numbers from 1 to 100 inclusive is 1113.
Challenge
Find the sum of the integer complexities for all numbers from 1 to 1000, inclusive.
It's not sufficient to write a program that will eventually get the solution after a very long time. Actually run your program through to completion before posting.
Tips
There are an impossibly large number of different formulas for a given integer. You can't check them all. But you don't have to. You can always express the complexity of a number in terms of the complexity of two smaller numbers. The complexity of a number A > 1 is always equal to the minimum possible complexity of B plus the complexity of C, where either B*C = A or B+C = A. In mathematical terms:
complexity(A) = min(P(A), S(A))
P(A) = min(complexity(B) + complexity(C) | B*C = A)
S(A) = min(complexity(B) + complexity(C) | B+C = A)
If you have a minimal formula, you can tell which it is. For instance, the minimal formula for 5678 is:
5678 = (1+4*4)*(1+3*3*(1+3*3*4))
This is essentially saying 5678 = 17*334, where 17 = 1+4*4 (with a complexity of 9) and 334 = 1+3*3*(1+3*3*4) (with a complexity of 18), with a total complexity of 9+18 = 27. By checking every such pair, we would see that 27 is the minimum possible sum.
The simplest thing to do is check every possible pair of numbers whose sum is 5678, and every possible pair of numbers whose product is 5678, and take the minimum sum of the complexity of the two numbers in the pair.
Notes
Integer complexity was featured in this subreddit 6 years ago in Challenge #31 [intermediate]. I tried to make it easier this time by giving some tips. Also, you don't need to find a formula, just get the value of the complexity.
Optional bonus
How fast can you get the sum of the integer complexities for all numbers from 1 to 1,000,000 inclusive? For this bonus, you may assume that the complexity of A is always equal to one of the following:
- 1 + the complexity of
A-1 - the sum of the complexity of two factors of
A
Using the notation above, this means:
S(A) = 1 + complexity(A-1)
In fact this is not true in general, but the smallest A for which it's false is 353,942,783.
7
u/Cosmologicon 2 3 Mar 16 '18
Optimized C++, solves the bonus in about 0.1 seconds, using
-O3. Solves 10x the bonus (up to 10 million) in about 1.0 seconds, and 100x the bonus (up to 100 million) in about 15 seconds.I haven't seen anyone use this optimization yet:
5
u/zatoichi49 Mar 19 '18 edited Mar 20 '18
Method:
Create a dictionary to hold the values for the integer complexity of each n. Loop through the values of n, taking the value of key[n-1] +1 as a default. Use the previous values in the dictionary (from keys 2 to n) to create keys for any multiples of n, replacing the key value if it's less than the current value. Return the sum of all values in the dictionary.
Python 3: with Bonus
Output:
3
u/zqvt Mar 14 '18 edited Mar 15 '18
Haskell
=> 18274Bonus:
replacing the line for the sums with
(\y -> y ++ [1 + d DM.! (n - 1)])takes about ~30 seconds (
=> 39519866)3
u/comma_at Mar 21 '18 edited Mar 21 '18
Freeforth, runs in no measurable time. I accidentally read the magic word in u/Cosmologicon's hint so I'm probably using the same technique.
Freeforth runs in a 1MB space by default and I'm too lazy to rewrite the code to use malloc or to increase the space for the bonus. Up to 100,000 it calculates in 30ms.
1
u/IntolerableBalboa Mar 15 '18
This link https://oeis.org/A005245 seems to show this is with only using 1's, but the example above uses other numbers in the addition and multiplication. Which is it?
2
u/playnwin Mar 14 '18 edited Mar 14 '18
PowerShell
So this one takes care of the 1-1000 without much problem (though not super quickly), but totally blows up when I try 5678. Though it could probably do 1-5678 (eventually).
I can't help but feel that there's a better way to do this, even without changing languages.
Code:
Output:
2
u/Specter_Terrasbane Mar 14 '18 edited Mar 14 '18
Python 2 with Optional Bonus (time of
32.38.0 s)Note: utilizing the assumption from "Optional bonus", so this solution is only accurate up to
n=353942782... but it raises aMemoryErrorwhen attempting that maximum, so ...Testing
Results
Edit 1: Minor change to how I calculated the factors of all ints up to
nresulted in a huge speed improvement! Changedfactors[i::i] = [s + [i] for s in factors[i::i]]tofor fac in factors[i::i]: fac.append(i)2
u/[deleted] Mar 17 '18 edited Mar 18 '18
Python 3.6
This uses caching to use previously calculated complexity values instead of recalculating them. Like the C solution by /u/gabyjunior. If there's a way to handle values not being present in a dict in python without using exception handling, please let me know.
edit 2: changed exception handling to if/else. edit 3: changed back to exception handling.
2
u/Chomatic Mar 18 '18
Scala
Brute force implementation. Output is 18274.
2
u/gabyjunior 1 2 Mar 14 '18
C
Works by computing complexity iteratively from 1 to n and reusing previously cached results
Output
The program is using the tip given for the challenge, but not the one for the optional bonus.
2
u/popillol Mar 15 '18
Go / Golang Playground Link
Can do up to 10000 fairly instantly, playground times out on 100000. Since the first 20 or so were given in the description I put those in to the
compsslice. Really though it just needscomps := []int{0}in order to work.Output
1
u/ff8c00 Mar 15 '18
JavaScript Gist with bonus
1
u/ckafi Mar 15 '18
Rust:
Output:
I'm just a beginner in Rust, and I'm not sure the casts to
usizeare the way to go.1
u/h2g2_researcher Mar 15 '18
C++
Solves pretty much instantly. Most of the program time is writing to stdout, rather than calculation.
For show-offness, the complexity of a number is multi-threaded.
Makes heavy use of the fact that:
By searching these ranges, and keeping y < x and z < x, we can instantly refer to caches results from previous answers which saves a huge amount of calculation.
1
u/zookeeper_zeke Mar 19 '18 edited Mar 20 '18
Here's a solution in C:
1
u/Xeonfobia Mar 21 '18 edited Mar 21 '18
Lua 5.2
Something is not optimal with my solution, but I cannot find a better approach.
Output: