The number of proper fractions is the number of numbers in 1 .. N-1 that are coprime with N, i.e. whose greatest common divisor with N is 1.
#!/usr/bin/perl use warnings; use strict; sub gcd { my ($x, $y) = @_; ($x, $y) = ($y, $x) if $x > $y; return $y unless $x; return gcd($y - ($x * int($y / $x)), $x) } sub proper_fractions { my ($n) = @_; grep 1 == gcd($n, $_), 1 .. $n - 1 } use Test::More tests => 5; is proper_fractions(1), 0; is proper_fractions(2), 1; is proper_fractions(5), 4; is proper_fractions(15), 8; is proper_fractions(25), 20;
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
The number of proper fractions is the number of numbers in 1 .. N-1 that are coprime with N, i.e. whose greatest common divisor with N is 1.