#!/usr/bin/perl
# Here is a simple mathematical program I wrote in Perl which computes a Farey Sequence.
# Feel free to copy and distribute for learning purposes as you wish!
my $welcome = "To create a Farey Sequence F_n, please enter a value for n. \n\n";
print $welcome;
my $n = <>;
my @r = ();
for( $i = $n ; $i > 0 ; $i-- ){unshift(@r, $i)};
pop( @r );
my @s = ();
for( $j = $n ; $j > 0 ; $j-- ){unshift(@s, $j)};
my @t = ();
for ( $l = $n ; $l > 0 ; $l--) {
foreach $k (@r) { push (@t, ($k/@s) )};
pop( @r );
pop( @s );
}
my @u = (0,1);
push (@u, @t);
my @v;
foreach my $x ( @u ) {
push @v , $x if !grep{$_ eq $x} @v;
}
# Removes duplicate elements from the list.
my @final = sort {$a <=> $b} @v;
# Puts sequence in ascending order.
print "The Farey Sequence F_";
print $n;
print "with order ";
print $n;
print "is as follows: \n\n";
print "@final"."\n";
# Adds spaces between elements of the list.