semiprime_equationization_uncached.pl 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 09 July 2015
  5. # Website: https://github.com/trizen
  6. # Split a semiprime into a group of equations.
  7. use 5.010;
  8. use strict;
  9. use integer;
  10. use warnings;
  11. sub semiprime_equationization {
  12. my ($semiprime, $xlen, $ylen) = @_;
  13. $xlen -= 1;
  14. $ylen -= 1;
  15. my @map;
  16. my $mem = '0';
  17. foreach my $j (0 .. $ylen) {
  18. foreach my $i (0 .. $xlen) {
  19. my $n = '(' . join(' + ', "(x[$i] * y[$j])", grep { $_ ne '0' } $mem) . ')';
  20. if ($i == $xlen) {
  21. push @{$map[$j]}, "($n % 10)", "int($n / 10)";
  22. $mem = '0';
  23. }
  24. else {
  25. push @{$map[$j]}, "($n % 10)";
  26. $mem = "int($n / 10)";
  27. }
  28. }
  29. my $n = $ylen - $j;
  30. if ($n > 0) {
  31. push @{$map[$j]}, ((0) x $n);
  32. }
  33. my $m = $ylen - $n;
  34. if ($m > 0) {
  35. unshift @{$map[$j]}, ((0) x $m);
  36. }
  37. }
  38. my @number = reverse split //, $semiprime;
  39. my @result;
  40. my @mrange = (0 .. $#map);
  41. foreach my $i (0 .. $#number) {
  42. my $n = '(' . join(' + ', grep { $_ ne '0' } (map { $map[$_][$i] } @mrange), $mem) . ')';
  43. if ($i == 0 or $i == $#number) {
  44. push @result, "$number[$i] = $n";
  45. $mem = '0';
  46. }
  47. else {
  48. push @result, "$number[$i] = ($n % 10)";
  49. $mem = "int($n / 10)";
  50. }
  51. }
  52. return @result;
  53. }
  54. # 71 * 43
  55. #say for semiprime_equationization('3053', 2, 2);
  56. # 251 * 197
  57. say for semiprime_equationization('49447', 3, 3);
  58. # 37975227936943673922808872755445627854565536638199 * 40094690950920881030683735292761468389214899724061
  59. #say for semiprime_equationization('1522605027922533360535618378132637429718068114961380688657908494580122963258952897654000350692006139', 50, 50);