congruence_of_squares_triangle.pl 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/perl
  2. # Highlight integers `k` in a triangle such that `k^2 (mod N)`
  3. # is a square and leads to a non-trivial factorization of `N`.
  4. use 5.010;
  5. use strict;
  6. use warnings;
  7. use GD::Simple;
  8. use ntheory qw(:all);
  9. # Composite integer N for which x^2 == y^2 (mod N)
  10. # and { gcd(x-y, N), gcd(x+y, N) } are non trivial factors of N.
  11. my $N = 43 * 79;
  12. my $i = 1;
  13. my $j = 1;
  14. my $n = shift(@ARGV) // 1000000;
  15. my $limit = int(sqrt($n)) - 1;
  16. my $img = GD::Simple->new($limit * 2, $limit + 1);
  17. $img->bgcolor('black');
  18. $img->rectangle(0, 0, $limit * 2, $limit + 1);
  19. my $white = 0;
  20. for (my $m = $limit; $m > 0; --$m) {
  21. $img->moveTo($m, $i - 1);
  22. for my $n ($j .. $i**2) {
  23. my $copy = $j;
  24. ## $j = ($copy*$copy + 3*$copy + 1);
  25. my $x = mulmod($j, $j, $N);
  26. my $root = sqrtint($x);
  27. my $r = gcd($root - $j, $N);
  28. my $s = gcd($root + $j, $N);
  29. if (is_square($x) and ($j % $N) != $root and (($r > 1 and $r < $N) and ($s > 1 and $s < $N))) {
  30. $white = 0;
  31. $img->fgcolor('white');
  32. }
  33. elsif (not $white) {
  34. $white = 1;
  35. $img->fgcolor('black');
  36. }
  37. $img->line(1);
  38. $j = $copy;
  39. ++$j;
  40. }
  41. ++$i;
  42. }
  43. open my $fh, '>:raw', 'congruence_of_squares.png';
  44. print $fh $img->png;
  45. close $fh;