is_smooth_over_product.sf 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/ruby
  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 powers 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.
  16. func is_smooth_over_prod(n, k) {
  17. return true if (n == 1)
  18. return false if (n <= 0)
  19. for (var g = gcd(n,k); g > 1; g = gcd(n,g)) {
  20. n.remdiv!(g) # remove any divisibility by g
  21. return true if (n == 1) # smooth if n == 1
  22. }
  23. return false
  24. }
  25. # Example for checking 19-smooth numbers
  26. var k = 19.primorial # product of primes <= 19
  27. for n in (1..1000) {
  28. say (n, ' = ', n.factor) if is_smooth_over_prod(n, k)
  29. }