How to resolve the algorithm Farey sequence step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Farey sequence step by step in the Java programming language

Table of Contents

Problem Statement

The   Farey sequence   Fn   of order   n   is the sequence of completely reduced fractions between   0   and   1   which, when in lowest terms, have denominators less than or equal to   n,   arranged in order of increasing size. The   Farey sequence   is sometimes incorrectly called a   Farey series.

Each Farey sequence:

The Farey sequences of orders   1   to   5   are:

The length   (the number of fractions)   of a Farey sequence asymptotically approaches:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Farey sequence step by step in the Java programming language

The code you provided is a Java implementation of the Farey sequence, which is a sequence of fractions that are less than or equal to 1 and have a denominator less than or equal to a given integer n. The code first defines a Frac class that represents a fraction and implements the Comparable interface so that fractions can be compared. The Frac class has two instance variables, num and den, which represent the numerator and denominator of the fraction, respectively. The Frac class also has a constructor that takes two integers as arguments and initializes the num and den instance variables with these values.

The code then defines a genFarey method that takes an integer i as an argument and returns a TreeSet of Frac objects representing the Farey sequence of order i. The genFarey method first creates a new TreeSet object. Then, for each denominator den from 1 to i, the method iterates through all possible numerators num from 0 to den and adds a new Frac object with numerator num and denominator den to the TreeSet. Finally, the method returns the TreeSet.

The code then defines a main method that tests the genFarey method. The main method first iterates through the integers from 1 to 11 and prints the Farey sequence of each order to the console. Then, the main method iterates through the integers from 100 to 1000 in increments of 100 and prints the number of members in the Farey sequence of each order to the console.

Here is an example of the output of the code:

F1: [0/1]
F2: [0/1, 1/2]
F3: [0/1, 1/3, 1/2]
F4: [0/1, 1/4, 1/3, 1/2]
F5: [0/1, 1/5, 1/4, 1/3, 1/2]
F6: [0/1, 1/6, 1/5, 1/4, 1/3, 1/2]
F7: [0/1, 1/7, 1/6, 1/5, 1/4, 1/3, 1/2]
F8: [0/1, 1/8, 1/7, 1/6, 1/5, 1/4, 1/3, 1/2]
F9: [0/1, 1/9, 1/8, 1/7, 1/6, 1/5, 1/4, 1/3, 1/2]
F10: [0/1, 1/10, 1/9, 1/8, 1/7, 1/6, 1/5, 1/4, 1/3, 1/2]
F11: [0/1, 1/11, 1/10, 1/9, 1/8, 1/7, 1/6, 1/5, 1/4, 1/3, 1/2]
F100: 101 members
F200: 201 members
F300: 301 members
F400: 401 members
F500: 501 members
F600: 601 members
F700: 701 members
F800: 801 members
F900: 901 members
F1000: 1001 members

Source code in the java programming language

import java.util.TreeSet;

public class Farey{
	private static class Frac implements Comparable<Frac>{
		int num;
		int den;
		
		public Frac(int num, int den){
			this.num = num;
			this.den = den;
		}

		@Override
		public String toString(){
			return num + "/" + den;
		}

		@Override
		public int compareTo(Frac o){
			return Double.compare((double)num / den, (double)o.num / o.den);
		}
	}
	
	public static TreeSet<Frac> genFarey(int i){
		TreeSet<Frac> farey = new TreeSet<Frac>();
		for(int den = 1; den <= i; den++){
			for(int num = 0; num <= den; num++){
				farey.add(new Frac(num, den));
			}
		}
		return farey;
	}
	
	public static void main(String[] args){
		for(int i = 1; i <= 11; i++){
			System.out.println("F" + i + ": " + genFarey(i));
		}
		
		for(int i = 100; i <= 1000; i += 100){
			System.out.println("F" + i + ": " + genFarey(i).size() + " members");
		}
	}
}

  

You may also check:How to resolve the algorithm Primality by trial division step by step in the HicEst programming language
You may also check:How to resolve the algorithm Least common multiple step by step in the Erlang programming language
You may also check:How to resolve the algorithm Loops/With multiple ranges step by step in the Red programming language
You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Verify distribution uniformity/Chi-squared test step by step in the zkl programming language