Part 1: https://brilliant.org/discussions/thread/how-many-numbers-do-a-given-set-of-primes-can/
In the previous post we showed that if you want to calculate how many numbers a given set of primes generate as a fraction of all natural numbers using a brute force methodology would result in an efficiency of O(n!).
To understand how inefficient this method is, let's assume we have in our possession an exaflop computer (one of his kind has never been built yet), which can make a billion billion calculations per second. Then to get the answer to our question with a list containing all primes up to only the 33th prime, 137, would take us approximately the age of the universe, or almost 14 billion years.
In order to calculate the answer for the list we need to find a method by which we could simplify the process.
Our previous solution was what is called in the school of computer science a Greedy Algorithm, in which we calculate each element of the solution separately, potentially doing the same calculations over and over again. As such the method we are going to use to solve it would be Dynamic Optimization (or Dynamic Programming).
As you may recall we reached the following equation to calculate our answer
fm(S)= the sum of all products of all permutations of size m of S
p1×p2...×pnfn−1(S)−fn−2(S)+fn−3(S)−fn−4(S)+fn−5(S)...±1
First, we can do away with the denominator because it would always be p1×p2×p3...×pn, seeing it is not influenced by the numerator we can calculate it separately.
So we are left only with
fn−1(S)−fn−2(S)+fn−3(S)−fn−4(S)+fn−5(S)...±1
To really understand the solution we need to understand fm(S).
For you convenience here are a few examples for it:
f2(2,3,5)=(2×3)+(2×5)+(3×5)
f2(2,3,5,7)=(2×3)+(2×5)+(2×7)+(3×5)+(3×7)+(5×7)
f3(2,3,5,7)=(2×3×5)+(2×3×7)+(2×5×7)+(3×5×7)
f1(2,3,5,7,11)=(2)+(3)+(5)+(7)+(11)
f5(2,3,5,7,11)=(2×3×5×7×11)
As we will see, the key to solving this problem is realizing that in it's essence fm(S) depends deeply upon the result of all fn(S) where m>n.
For starts, you can see I already replaced it with 1 in the main equation, but the actual main equation does not end with 1 but with f0(S)=1 which would be our first equation.
Seeing we already found the solution for f0(S) we might as well go for another low hanging fruit and try to solve f1(S).
The definition of fm(S) is that it's the sum of all products of all permutations of size m of S.
But no matter which S we would be given, all permutations of size 1 would result in all the elements comprising S. Since we only need to add them together we can see that f1(S)=S1+S2+S3+S4...+SL where L is simply the length of S.
Another simple observation is that if we are given fL(S) where L is simply the length of S there is only 1 permutation which fits. As such fL(S)=S1×S2×S3×S4...×SL where L is the length of S.
And so we are only left with the greatest obstacle: finding the solution for fn(S) where n=0,1,L
Let's examine f2(2,3,5)
We already showed that f2(2,3,5)=(2×3)+(2×5)+(3×5), but it can also be written as 2×(3+5)+(3×5).
In order for us to not jump into wrong conclusions right ahead let's examine f2(2,3,5,7) as well.
f3(2,3,5,7)=(2×3×5)+(2×3×7)+(2×5×7)+(3×5×7)=2×((3×5)+(3×7)+(5×7))+(3×5×7).
And for good measures let's try f3(2,3,5,7,11) as well
f3(2,3,5,7,11)=(2×3×5)+(2×3×7)+(2×3×11)+(2×5×7)+(2×5×11)+(2×7×11)+(3×5×7)+(3×5×11)+(3×7×11)+(5×7×11)
=2×((3×5)+(3×7)+(3×11)+(5×7)+(5×11)+(7×11))+(3×5×7)+(3×5×11)+(3×7×11)+(5×7×11)
We can notice right away that when we try to factor our resulting equation by extracting 2 from f3(2,3,5,7), we get 2×f2(3,5,7)+ some remainder. also, when we try factor f2(2,3,5) we get 2×f1(3,5)+ some remainder.
We can see that for any S, fn(S) where n=0,1,L will have the form of S1×fn−1(S−S1)+some remainder
where S−S1 denotes the removal of the first item of S.
So all we left to do is to find what this "remainder" will be.
For f2(2,3,5) the reminder is (3×5) =f2(3,5)
For f3(2,3,5,7) the reminder is (3×5×7) =f3(3,5,7)
For f3(2,3,5,7,11) the reminder is (3×5×11)+(3×7×11)+(5×7×11) =f3(3,5,7,11)
So we can clearly see that the reminder takes the form of fn(S−S1), where again S−S1 denotes the removal of the first item of S.
As such our final equation is complete fn(S)=S1×fn−1(S−S1)+fn(S−S1)
We finally covered all possible outcomes of fm(S)
f0(S)=1
f1(S)=S1+S2+S3+S4...+SL where L is the length of S.
fL(S)=S1×S2×S3×S4...×SL where L is the length of S.
fn(S)=S1×fn−1(S−S1)+fn(S−S1) where n=0,1,L
and S−S1 denotes the removal of the first item of S.
Actually we can see from our newest definition of fn(S) that we can deduce f1(S)without having a specific equation for it.
f1(S)=S1×f0(S−S1)+S2×f0(S−S1−S2)+S3×f0(S−S1−S2−S3)...+SL×f0(0)=S1+S2+S3...+SL
The equation for f1(S) is useful, but we really only need 3 equations
f0(S)=1
fL(S)=S1×S2×S3×S4...×SL where L is the length of S.
fn(S)=S1×fn−1(S−S1)+fn(S−S1) where n=0,L
and S−S1 denotes the removal of the first item of S.
It seems like we are done, we would just have to run our program recursively over all fm(S) and calculate fn−1(S)−fn−2(S)+fn−3(S)−fn−4(S)+fn−5(S)...±f0(S).
However running our program that way is too brute, there is a simpler more elegant way of doing it.
since we can build up our equation from the bottom up we can visualize it in this way:
S=2,3,5,7
2f0(2,3,5,7)f1(2,3,5,7)f2(2,3,5,7)f3(2,3,5,7)3f0(3,5,7)f1(3,5,7)f2(3,5,7)f3(3,5,7)5f0(5,7)f1(5,7)f2(5,7)f3(5,7)7f0(7)f1(7)f2(7)f3(7)
Bare with me for a second because I'm going to do things that seem counter intuitive but I would explain why they are that way in a bit.
I will use (X,Y) notation do denote a location in this table. both X and Y will start from 0 and would be colored green for sake of clarity. The value of X will denote distance from the top, and Y would denote distance from the left.
The corresponding values from S would be displayed above each column in red color.
Sk would symbolize S−S1−S2−S3...−Sk where S−Sn denotes the removal of the nth item of S.
So for example in S=2,3,5,7, S2 would give us
S−S1−S2=S−2−3=5,7
it's Important to note that S0=S−0=S
The table Final shape will be:
0123S00(0,0)(1,0)(2,0)(3,0)S11(0,1)(1,1)(2,1)(3,1)S22(0,2)(1,2)(2,2)(3,2)S33(0,3)(1,3)(2,3)(3,3)
Now for the fun and (hopefully) easier part
Using our definition of Sn we will define:
(X,Y) = fX(SY)
So for example (1,2) would represent f1(S2)=f1(5,7)=5+7
We would insert simplest the values we know of first, that is
f0(S)=1
fL(S)=S1×S2×S3×S4...×SL
01232,3,5,7013,5,7113×5×75,7215×77317
Now we insert
fn(S)=S1×fn−1(S−S1)+fn(S−S1)
01232,3,5,7012×(0,1)+(1,1)2×(1,1)+(2,1)2×(2,1)+(3,1)3,5,7113×(0,2)+(1,2)3×(1,2)+(2,2)3×5×75,7215×(0,3)+(1,3)5×77317
Where the table is of the form:
01230(0,0)(1,0)(2,0)(3,0)1(0,1)(1,1)(2,1)(3,1)2(0,2)(1,2)(2,2)(3,2)3(0,3)(1,3)(2,3)(3,3)
We can see immediately that in order to calculate the value of a cell we only need to take the first value in SY that correspond to the cell column, multiply it by the value of the cell above it to the right, and add to it the value of the cell to the right.
So we keep only the first value in SY and discard the rest such that out final table shape would be
01232012×(0,1)+(1,1)2×(1,1)+(2,1)2×(2,1)+(3,1)3113×(0,2)+(1,2)3×(1,2)+(2,2)3×5×75215×(0,3)+(1,3)5×77317
Now we can even do the calculation by hand!
we only need to go from right to left and from up to down to get all the values we need!
012320117101247311157110552112357317
Now finally we get to calculate
fL−1(S)−fL−2(S)+fL−3(S)−fL−4(S)...±f0(S)
since
(X,Y) = fX(SY)
Then
fL−1(S)−fL−2(S)+fL−3(S)−fL−4(S)...±f0(S)
Translates to
(L−1,0)−(L−2,0)+(L−3,0)−(L−4,0)...±(0,0)
In other words we only need to look at the first column!
For S=2,3,5,7
(3,0)−(2,0)+(1,0)−(0,0)=247−101+17−1=162
So to calculate our final answer
2×3×5×7=210
p1×p2...×pnfL−1(S)−fL−2(S)+fL−3(S)−fL−4(S)= 210162=10581=3527
Finally, since we only need to create a table of size L2 where L is the length of S and go over it once to get to our answer, our computational efficiency is O(n2) instead of the original O(n!)
#NumberTheory
Easy Math Editor
This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.
When posting on Brilliant:
*italics*
or_italics_
**bold**
or__bold__
paragraph 1
paragraph 2
[example link](https://brilliant.org)
> This is a quote
\(
...\)
or\[
...\]
to ensure proper formatting.2 \times 3
2^{34}
a_{i-1}
\frac{2}{3}
\sqrt{2}
\sum_{i=1}^3
\sin \theta
\boxed{123}
Comments
There are no comments in this discussion.