I writing a mathematica function.

Click For Summary

Discussion Overview

The discussion revolves around writing a Mathematica function to generate the nth Farey sequence, which consists of irreducible rational numbers arranged in increasing order. Participants are exploring the implementation details, syntax, and logic needed to achieve this in Mathematica.

Discussion Character

  • Technical explanation
  • Homework-related
  • Exploratory

Main Points Raised

  • One participant defines the Farey sequence and provides examples for various values of n.
  • Another participant suggests using a Table[] function to generate fractions i/j for 0 ≤ i ≤ n and 1 ≤ j ≤ n, but notes that the output will have duplicates and incorrect ordering.
  • A participant shares an initial attempt at writing a function but presents code that does not conform to Mathematica syntax.
  • One participant corrects the previous attempt by providing a valid Mathematica expression to generate the first step of the Farey sequence.
  • Another participant questions whether the problem is a homework assignment.
  • There is a suggestion to use the flatten command to address issues with the output from the Table[] function.

Areas of Agreement / Disagreement

Participants are generally exploring the problem collaboratively, but there is no consensus on the final implementation or approach to take. Some participants are focused on syntax issues while others are addressing logical steps in the function development.

Contextual Notes

Some limitations include unresolved issues with duplicate values, ordering of fractions, and the need for further refinement of the Mathematica code. There is also uncertainty regarding the classification of the problem as homework.

Who May Find This Useful

Individuals interested in Mathematica programming, specifically those looking to understand how to generate mathematical sequences programmatically.

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?
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
8
Views
5K
  • · Replies 4 ·
Replies
4
Views
7K