is_smooth_over_product.pl 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # Date: 25 October 2018
  4. # https://github.com/trizen
  5. # A new algorithm for testing N for B-smoothness, given the product of a subset of primes <= B.
  6. # Returns a true value when N is the product of a subset of prime factors of B.
  7. # This algorithm can be useful in some modern integer factorization algorithms.
  8. # Algorithm:
  9. # 1. Let n be the number to be tested.
  10. # 2. Let k be the product of the primes in the factor base.
  11. # 3. Compute the greatest common divisor: g = gcd(n, k)
  12. # 4. If g is greater than 1, then n = r * g^e, for some e >= 1.
  13. # - If r = 1, then n is smooth over the factor base.
  14. # - Otherwise, set n = r and go to step 3.
  15. # 5. If this step is reached, then n is not smooth over the factor base.
  16. use 5.020;
  17. use warnings;
  18. use experimental qw(signatures);
  19. use ntheory qw(gcd valuation primorial factor);
  20. sub is_smooth_over_prod ($n, $k) {
  21. for (my $g = gcd($n, $k) ; $g > 1 ; $g = gcd($n, $k)) {
  22. $n /= $g; # remove one divisor g
  23. $n /= $g while ($n % $g == 0); # remove any divisibility by g
  24. return 1 if ($n == 1); # smooth if n == 1
  25. }
  26. return 0;
  27. }
  28. # Example for identifying 19-smooth numbers
  29. my $k = primorial(19); # product of primes <= 19
  30. for my $n (1 .. 1000) {
  31. say($n, " = prod(", join(', ', factor($n)), ")") if is_smooth_over_prod($n, $k);
  32. }