# Why is my code for density using Folner Sequences giving incorrect values? [duplicate]

I am creating a code that calculates or approximates

$$D(S\cap[a,b])=\lim_{n\to\infty}\frac{\left|S\cap{F_n\cap[a,b]}\right|}{\left|F_n\cap[a,b]\right|}$$

where $$D$$ is the density of $$S\cap[a,b]$$ (in $$A\cap[a,b]$$), $$[a,b]$$ is an interval for $$a,b\in\mathbb{R}$$, $$F_n$$ is the Folner Sequence of $$A$$, and $$S\subseteq A$$. For more information, click here (replace $$G,X,i,g$$ with $$A,S,n,a$$), here and here.

I attempted to approximate $$D(S\cap[0,1])$$ when $$S=\left\{\frac{m^2}{n^2}:m,n\in\mathbb{Z},n\neq 0\right\}$$, $$A=\mathbb{Q}$$ and $$F_n=\left\{\frac{p}{2^k(2q+1)}:p,k,q\in\mathbb{Z},2^k \le n, |2q+1|\le n, \left|\frac{p}{2^k(2q+1)}\right|\le n\right\}$$.

(In my code I replaced $$A$$ with A[x_,y_,z_,...], $$F_n$$ with F[x_,y_,z_,...], $$S$$ with S[x_,y_,z_,...], and $$D$$ with d. I also set $$a=0$$ and $$b=1$$.)

Clear[A, F, f, p, Ff, S, X, Y, d, j, k];
A[p_, k_, q_] := p/((2^k)*(2*q + 1));
F[p_, n_] :=
Table[A[p, k, q], {k, 0, Floor[Log[2, n]]}, {q, 0,
Floor[(n - 1)/2]}];
f[n_, a_, b_] :=
p /. Table[
Solve[a <= A[p, k, q] <= b, p, Integers], {k, 0,
Floor[Log[2, n]]}, {q, 0, Floor[(n - 1)/2]}];
Ff[n_, a_, b_] :=
DeleteDuplicates@
Flatten@Table[
F[f[n, a, b][[v]][[u]], n][[v]][[u]], {v, 1,
Floor[Log[2, n]] + 1}, {u, 1, Floor[(n - 1)/2] + 1}];
S[j_, k_] := j^2/k^2;
X[n_, a_, b_] :=
Count[Resolve[
Exists[{j, k},
S[j, k] == # && {j, k} а \[Element] Integers]] & /@
Ff[n, a, b], True];
Y[n_, a_, b_] := Length[Ff[n, a, b]];
d[n_, a_, b_] := N[(X[n, a, b])/Y[n, a, b]];

Ff[4, 1, 2]
X[4, 1, 2]
Y[4, 1, 2]
d[4, 1, 2]
Limit[d[n, 1, 2], n -> Infinity]


I get

Out= {1, 2, 4/3, 5/3, 3/2, 7/6, 11/6, 5/4, 7/4, 13/12, 17/12, \
19/12, 23/12}

Out= 0

Out= 13

Out= 0.

During evaluation of In:= Table::iterb: Iterator {v,1,1+Floor[Log[n]/Log]} does not have appropriate bounds.

During evaluation of In:= Table::iterb: Iterator {v,1,1+Floor[Log[n]/Log]} does not have appropriate bounds.

During evaluation of In:= Table::iterb: Iterator {v,1,1+Floor[Log[n]/Log]} does not have appropriate bounds.

During evaluation of In:= General::stop: Further output of Table::iterb will be suppressed during this calculation.

During evaluation of In:= Table::nliter: Non-list iterator (Resolve[\!$$\*SubscriptBox[\(\[Exists]$$, $${j, k}$$]$$(S[j, k] == #1 && {j, k}\ а \[Element] \*TemplateBox[{}, "Integers"])$$\)]&)[{v,1,1+Floor[Log[n]/Log]}] at position 2 does not evaluate to a real numeric value.

Out= 0.


Why is X[4,1,2] not equal to 1? Why is Limit[d[n,1,2],n->Infinity] showing an error? How do we correct both?

• In RHS of definition of X you see that symbol a is not colored in green by Mathematica. This means this is not the same symbol as used in X[n_, a_, b_], just change it to right a. Probably you typed this in different keyboard layout. Then X[4,1,2] gives 1. – Alx Oct 2 '19 at 2:28
• @Alx How do we correctly solve Limit[d[n,a,b],n->Infinity]? If I set S[j_,k_]:=j/k^3 I still get $0$ and error messages instead of $1$. – Arbuja Oct 2 '19 at 13:35