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

Click For Summary
SUMMARY

This discussion focuses on generating Farey sequences using Mathematica. The user seeks assistance in writing a program that outputs Farey[n] for various values of n, with examples provided for n = 1 to 5. Two implementations are discussed: one using the Compile function for performance and another using a simpler Union and Flatten approach. The conversation emphasizes the importance of consulting existing resources, such as the linked Physics Forums thread and the Wikipedia page on Farey sequences, for further understanding.

PREREQUISITES
  • Understanding of Mathematica programming language
  • Familiarity with Farey sequences and their properties
  • Knowledge of the Compile function in Mathematica
  • Basic concepts of number theory related to fractions
NEXT STEPS
  • Learn how to implement the Compile function in Mathematica for performance optimization
  • Explore the Mathematica documentation for advanced sequence generation techniques
  • Study the algorithm for Farey sequences as outlined on the Wikipedia page
  • Investigate the use of DisplayForm and FractionBox for formatting output in Mathematica
USEFUL FOR

Mathematica users, students in computational mathematics, and anyone interested in number theory and sequence generation.

GetToTheChopa
Messages
2
Reaction score
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
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:

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K