# How to restrict a list of elements based on the values of their denominators?

EDIT: Why is my post downvoted? Can somone explain?

Say we have a list of simplified elements where

$$\left\{\left.\frac{(2i+1)^2}{(2z+1)^2}\right|i,z\in\mathbb{Z}\right\}\cap[1,2]$$

and we rearrange the list from elements with the smallest denominator to elements with the largest denominator. From there, I need to shorten the list to elements with a denominator value between $1$ and $p$.

So far I was able to list the simplified elements of the set in $[1,2]$ and arrange the elements from those with the smallest denominator to those with the largest denominator.

    h=(2#+1)^2&
l=((2#+1)^2)/((2#2+1)^2)&
DeleteDuplicates[SortBy[Flatten[Table[Table[l[i,z],
{i,Ceiling[((h[z])^(1/2)-1)/2],Floor[((2h[z])^(1/2)-1)/2]}],{z,0,1000}]],{Denominator}]]


Which gives the following result

{1,49/25,81/49,121/81,169/121,225/121,225/169,289/169,289/225,361/225,361/289,44
1/289,529/289, \[CenterEllipsis]168157\[CenterEllipsis]
,7789681/4004001,7812025/4004001,7823209/4004001,7845601/4004001,7856809/4004001
,7879249/4004001,7890481/4004001,7924225/4004001,7946761/4004001,7958041/4004001
,7980625/4004001,7991929/4004001}


However, I don't know how to restrict the list of elements to those with a denominator value between $1$ and $p$.

• Why the downvote? – Arbuja Apr 23 '17 at 19:24

Just quickly sir, but would something like a Cases pattern match:

h = (2 # + 1)^2 &;
l = ((2 # + 1)^2)/((2 #2 + 1)^2) &;
lst = DeleteDuplicates[SortBy[Flatten[Table[Table[
l[i, z], {i, Ceiling[((h[z])^(1/2) - 1)/2],
Floor[((2 h[z])^(1/2) - 1)/2]}], {z, 0,
1000}]], {Denominator}]];
Flatten@{First@lst, Cases[lst, Rational[n_, d_] /; d <= 1000]}


return you a list with denominators between 1 and less than and equal to say 1000

• Yes this is perfect, thanks! – Arbuja Apr 23 '17 at 19:58

In:

h = (2 # + 1)^2 &;
l = ((2 # + 1)^2)/((2 #2 + 1)^2) &;
xs = DeleteDuplicates[
SortBy[Flatten[
Table[Table[
l[i, z], {i, Ceiling[((h[z])^(1/2) - 1)/2],
Floor[((2 h[z])^(1/2) - 1)/2]}], {z, 0,
1000}]], {Denominator}]];

p = 1000;
xs // Select[Denominator[#] <= p &]


Out:

{1, 49/25, 81/49, 121/81, 169/121, 225/121, 225/169, 289/169, \
289/225, 361/225, 361/289, 441/289, 529/289, 441/361, 529/361, \
625/361, 529/441, 625/441, 841/441, 625/529, 729/529, 841/529, \
961/529, 729/625, 841/625, 961/625, 1089/625, 841/729, 961/729, \
1225/729, 1369/729, 961/841, 1089/841, 1225/841, 1369/841, 1521/841, \
1681/841, 1089/961, 1225/961, 1369/961, 1521/961, 1681/961, 1849/961}

• This is even better. Thank You. – Arbuja Apr 26 '17 at 13:17