factor_circles.pl 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 14 September 2016
  5. # Website: https://github.com/trizen
  6. # For each factor `f` of a composite number `n`, draw a circle
  7. # in such a way that the line of the circle passes through both `n` and `f`.
  8. use 5.014;
  9. use strict;
  10. use warnings;
  11. use Imager;
  12. use List::Util qw(uniq);
  13. use ntheory qw(is_prime factor);
  14. my $limit = 1000;
  15. my $scale = 10;
  16. my $red = Imager::Color->new('#ff0000');
  17. my $img = Imager->new(xsize => $limit * $scale,
  18. ysize => $limit * $scale,);
  19. sub get_circle {
  20. my ($n, $f) = @_;
  21. my $r = ($n * $scale - $f * $scale) / 2;
  22. ($r, $r + $f * $scale, $limit * $scale / 2);
  23. }
  24. foreach my $n (1 .. $limit) {
  25. if (not is_prime($n)) {
  26. foreach my $f (uniq(factor($n))) {
  27. my ($r, $x, $y) = get_circle($n, $f);
  28. $img->circle(
  29. x => $x,
  30. y => $y,
  31. r => $r,
  32. color => $red,
  33. filled => 0
  34. );
  35. }
  36. }
  37. }
  38. $img = $img->rotate(degrees => 90);
  39. $img->write(file => 'factor_circles.png');