pascal_s_triangle_multiples.pl 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 29 November 2015
  5. # Website: https://github.com/trizen
  6. # Highlight multiples inside the Pascal's triangle.
  7. use 5.010;
  8. use strict;
  9. use warnings;
  10. use Imager qw();
  11. use ntheory qw(binomial);
  12. my $div = 3; # highlight multiples of this integer
  13. my $size = 243; # the size of the triangle
  14. my $img = Imager->new(xsize => $size * 2, ysize => $size);
  15. my $black = Imager::Color->new('#000000');
  16. my $red = Imager::Color->new('#ff00000');
  17. $img->box(filled => 1, color => $black);
  18. sub pascal {
  19. my ($rows) = @_;
  20. for my $n (1 .. $rows - 1) {
  21. my $i = 0;
  22. for my $elem (map { binomial(2 * $n, $_) } 0 .. 2 * $n) {
  23. if ($elem % $div == 0) {
  24. $img->setpixel(x => $rows - $n + $i++, y => $n, color => $black);
  25. }
  26. else {
  27. $img->setpixel(x => $rows - $n + $i++, y => $n, color => $red);
  28. }
  29. }
  30. }
  31. }
  32. pascal($size);
  33. $img->write(file => "pascal_s_triangle.png");