goldbach_conjecture_possibilities.pl 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 29 July 2015
  5. # Website: https://github.com/trizen
  6. # Plot the number of possibilities of each number for the Goldbach conjecture.
  7. # Example:
  8. # 16 = {3+13; 5+11} => 2 possibilities for the number 16
  9. use 5.010;
  10. use strict;
  11. use warnings;
  12. use Imager qw();
  13. use ntheory qw(primes is_prime);
  14. my $limit = 1e4;
  15. my $xsize = $limit;
  16. my $ysize = int($limit / (1 / 5 * log($limit)**2)); # approximation
  17. my ($x, $y) = (0, $ysize);
  18. my $img = Imager->new(xsize => $xsize, ysize => $ysize);
  19. my $white = Imager::Color->new('#ffffff');
  20. my $gray = Imager::Color->new('#5f5d5d');
  21. $img->box(filled => 1, color => $white);
  22. my @primes;
  23. my $last_n = 2;
  24. foreach my $i (3 .. $limit) {
  25. my $n = 2 * $i;
  26. push @primes, @{primes($last_n, $n - 2)};
  27. $last_n = $n - 2;
  28. my %seen;
  29. my $count = 0;
  30. foreach my $prime (@primes) {
  31. exists($seen{$prime}) && last;
  32. if (is_prime($n - $prime)) {
  33. ++$count;
  34. undef $seen{$n - $prime};
  35. }
  36. }
  37. foreach my $i (1 .. $count) {
  38. $img->setpixel(x => $x, y => $y - $i, color => $gray);
  39. }
  40. $x += 1;
  41. }
  42. $img->write(file => "goldbach_possibilities.png");