How to calculate the ratio of two primes in a given set of primes?
3 views (last 30 days)
Show older comments
Hi all,
I'm working on a personal project here. My goal is this: Fix some natural number n (greater than or equal to 3) and look at the set primes = primes(n). This is the set of all primes less than or equal to n. From here we take any two primes, p_i and p_j and look at the ratio (p_i)/(p_j) = r_ij. If r_ij is less than 1, we "keep" this value and put it into a set. Otherwise (that is, if r_ij >= 1) we ignore it.
Lastly, as this is my ultimate goal, I want to take any r_ij value that is less than 1 and convert it into base 3. From here I will check certain things about the base 3 decimal expansion of any r_ij value and if it satisfies certain things, sort said value into a set, and if it doesn't, another. Ideally, I would be able to see back in base 10 what these values are in each set respectively.
The entire motivation of this little experiment comes from the Cantor set. In my Measure Theory class we were talking about it and I noticed a peculiar pattern. That is, I couldn't find a lot of prime fractions that were in the Cantor set. I know something is in the Cantor set (call it C) if in its base 3 decimal expansion, it includes multiple 1s. This is what I want (or rather, trying) to check. Take any two primes less than or equal to n, compute their fraction. If greater than or equal to 1, ignore said ratio, but if less than one, convert it to base 3, look at its expansion, and if it has zero or one 1's in its decimal expansion, put it in C and have it be presented back as a readable base 10 fraction. Otherwise, put said ratio into a set (call it Nc--"not Cantor") as a readable base 10 fraction. I hope this makes sense. I apologize for the long winded post.
Currently, here is all I have:
n = input('Enter value for n: '); if floor(n) == n primes = primes(n) else error('Enter a positive integer value for n.') end
I am stuck on making the for loop that takes any two elements in my set denoted as "primes" and computing their ratio. I would love some input for this. Thanks!
4 Comments
Guillaume
on 9 Oct 2018
To me the hardest would be to compute the base 3 expansion of the numbers (not sure what base 3 decimal expansion is, how does it differ from normal base 3?) particularly since I have a feeling that the base 3 expansion of fractions of prime are going to be infinite strings.
Answers (1)
John D'Errico
on 9 Oct 2018
Edited: John D'Errico
on 9 Oct 2018
As long as the set is sorted, you never need to worry about the ratio being greater than than 1.
How do you compute a base 3 expansion? That too is easy enough. I'm not sure why you want to do that. But it is not too difficult.
P1 = 5;
P2 = 7;
5/7
ans =
0.714285714285714
The trick is to convert it to an integer. Then just use dec2base.
Tern = dec2base(floor(P1/P2*3^33),3,33)
Tern =
'201021201021201021201021201021201'
(Tern stands for a Ternary expansion.) So if we take a few terms...
2*1/3 + 1/27+ 2*1/243 + 1/729
ans =
0.713305898491084
2*3^-1 + 1*3^-3 + 2*3^-5 + 1*3^-6
ans =
0.713305898491084
We can get the complete approximation as:
dot(Tern - '0',3.^(-1:-1:-33))
ans =
0.714285714285714
As you can see, this is the ternary expansion of the fraction 5/7. This is essentially as good as we can do in terms of double precision arithmetic. I used 33 terms in that expansion because 3^33 is the largest integer power of 3 that is less than 2^53-1.
In fact, dec2base even gets the leading zeros in that expansion correct.
Can you do better than that? Well, yes. But it would require the use of higher precision. I'll use my vpi toolbox here to give you 100 ternary digits in the expansion of 13/17.
char(vpi2base(vpi(vpi(3)^100*13/17),3)+'0')
ans =
'2021221102010011202122110201001120212211020100112021221102010011202122110201001120212211020100112021'
Be careful, in that vpi2base will miss any leading zeros as I did it here. That is easily fixed. But I'm not really sure why you need more ternary digits than a double can provide.
See Also
Categories
Find more on Number Theory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!