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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Farey sequence step by step in the Picat 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 Picat programming language

Source code in the picat programming language

go ?=>
  member(N,1..11),
  Farey = farey(N),
  println(N=Farey),
  fail,
  nl.
go => true.

farey(N) = M =>
  M1 = [0=$(0/1)] ++ 
       [I2/J2=$(I2/J2) : I in 1..N, J in I..N, 
        GCD=gcd(I,J),I2 =I//GCD,J2=J//GCD].sort_remove_dups(),
  M = [ E: _=E in M1]. % extract the rational representation

go2 => 
  foreach(N in 100..100..1000)
     F = farey(N),
     println(N=F.length)
  end,
  nl.

  

You may also check:How to resolve the algorithm Zig-zag matrix step by step in the Erlang programming language
You may also check:How to resolve the algorithm Descending primes step by step in the Prolog programming language
You may also check:How to resolve the algorithm Solve a Holy Knight's tour step by step in the Elixir programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Ring programming language
You may also check:How to resolve the algorithm Pig the dice game step by step in the JavaScript programming language