I writing a mathematica function.

JohnMcBetty
Messages
12
Reaction score
0
The Farey sequence F_n (F subscript n) for any positive integer n is the set of irreducible rational numbers a/b with 0<(or equal to)a<(or equal to)b<(or equal to) n and (a,b)=1 arranged in increasing order. The first one should be:

F_1 = {0/1,1/1}

F_2 = {0/1,1/2,1/1}

F_3 = {0/1,1/3,1/2,2/3,1/1}

F_4 = {0/1,1/4,1/3,1/2,2/3,3/4,1/1}

F_5 = {0/1,1/5,1/4,1/3,2/5,1/2,3/5,2/3,3/4,4/5,1/1}

I need to write a mathematica function that takes a positive integer n and returns, as a list, the nth Farey sequence, I really need some help?
 
Physics news on Phys.org
Some hints that should help you get started.

What if you made a Table[] containing all i/j? where 0<=i<=n and 1<=j<=n?

There are several things wrong with that, it has extra {} and duplicate values and values in the wrong order and extra values you don't want.

What can you do to get rid of what you don't want and leave what you need?

Try addressing those problems, perhaps one at a time, and show what progress you have made.

You should be able to accomplish all this with a single line.

Note: Most Mathematica questions usually go in the OtherSciences/Math&ScienceSoftware section.
 
I tried this, but it still doesn't work, I am very new at using this program.


FareySequence[n_] := local a, b, c, d, k;
a := 0; b := 1; c := 1; d := n;
printf (` % d/% d \n `, a, b);
while (c < n) do
k := floor ((n + b)/d);
a := c; b := d; c := k*c - a; d := k*d - b;
printf (` % d/% d \n `, a, b);
od : end :
 
What you wrote isn't Mathematica.

FareyFirstStep[n_]:=Table[i/j,{i,0,n},{j,1,n}]

will do the very first step I suggested.

If you look at the result of that, for say FareyFirstStep[4], can you identify that it seems to be providing some interesting and useful bits that are going in the direction of what you need?

Now describe one thing that is wrong with the result from FareyFirstStep and how can you fix that using a single Mathematica supplied function?

Hint: There are several different things wrong and it may be easier to fix those in one particular order than trying to fix them in some other order so think about what fix might make your next needed fix easier to do. You are nibbling away at the problem a little at a time while you try to make sense of the Mathematica language.
 
Last edited:
Would I have to use the flatten command?
 
Is this a homework problem?
 
Back
Top