Can anybody help me, who is good with Mathematica, with writing a sequence.

In summary, the conversation discussed the topic of generating Farey sequences using Mathematica. The conversation included a link to a previous discussion on the same topic and a code implementation for generating the sequences. It also mentioned a possible connection between the two people involved in the conversation and suggested looking at the Mathematica documentation for further understanding.
  • #1
GetToTheChopa
2
0
I am very new with Mathematica and I need help writing a program that generates a Farey Sequence.

Farey[n] := ?

The result should appear as follows, for those values of N.

Farey[1] = {0/1,1/1}

Farey[2] = {0/1,1/2,1/1}

Farey[3] = {0/1,1/3,1/2,2/3,1/1}

Farey[4] = {0/1,1/4,1/3,1/2,2/3,3/4,1/1}

Farey[5] = {0/1,1/5,1/4,1/3,2/5,1/2,3/5,2/3,3/4,4/5,1/1}
 
Physics news on Phys.org
  • #2
This was just discussed (and is still at the top of the thread list) in https://www.physicsforums.com/showthread.php?t=489620" -- please look/search before you post!

By the way, are you and https://www.physicsforums.com/member.php?u=319108" doing the same course?

Anyway, here's another implementation for generating Farey sequences - this one implements the algorithm given in http://en.wikipedia.org/wiki/Farey_sequence" .
Code:
FareySequence = 
  Compile[{{n, _Integer}}, 
   Module[{FS = {{0, 1}, {1, n}}, a = 0, b = 1, c = 1, d = n, f = 0},
    While[c <= n, f = Quotient[n + b, d];
     {a, b, c, d} = {c, d, f c - a, f d - b};
     FS = Append[FS, {c, d}]];
    Most@FS], CompilationTarget -> "C"];
It outputs the numerators and denominators in pairs.
For n less than a few hundred, it is comparable in speed to the solution given by DaleSpam:
Code:
Union[Flatten[Table[j/i, {i, 1, n}, {j, 0, i}]]]

To make it print fractions in all cases - including those with unit denominator, try something like
Code:
DisplayForm /@ FractionBox @@@ FareySequenceFCC[5]

(* Returns: {0/1,1/5,1/4,1/3,2/5,1/2,3/5,2/3,3/4,4/5,1/1} *)

If you have questions about how this works, look at the above linked to thread, then read the Mathematica documentation, then post here!
 
Last edited by a moderator:

1. How can I write a sequence in Mathematica?

To write a sequence in Mathematica, you can use the Sequence function. For example, Sequence[1,2,3] will create a sequence with the values 1, 2, and 3.

2. How do I add elements to a sequence in Mathematica?

To add elements to a sequence in Mathematica, you can use the Append or Prepend functions. For example, Append[Sequence[1,2,3], 4] will add the element 4 to the end of the sequence.

3. Can I use variables in a sequence in Mathematica?

Yes, you can use variables in a sequence in Mathematica. You can define a variable and then use it within the Sequence function. For example, Sequence[x, x^2, x^3] will create a sequence with the values of x, x^2, and x^3.

4. How do I access specific elements in a sequence in Mathematica?

You can access specific elements in a sequence in Mathematica by using the Part function. For example, Sequence[1,2,3][[2]] will return the second element in the sequence, which is 2.

5. Can I perform operations on a sequence in Mathematica?

Yes, you can perform operations on a sequence in Mathematica. You can use functions such as Map, Apply, and Fold to perform operations on the elements of a sequence. For example, Map[#^2&, Sequence[1,2,3]] will square each element in the sequence, resulting in a new sequence of 1, 4, and 9.

Similar threads

  • Linear and Abstract Algebra
Replies
5
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • Set Theory, Logic, Probability, Statistics
Replies
20
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • General Math
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • Calculus and Beyond Homework Help
Replies
17
Views
10K
  • Programming and Computer Science
Replies
1
Views
2K
  • Special and General Relativity
Replies
7
Views
1K
Back
Top