func smooth_generator(primes){ var s = primes.len.of{[1]}{ var n = s.map{.first}.min{|i| s[i].shiftif(s[i][0]== n) s[i]<<(n * primes[i])}* primes.len n}}forpin(primes(2,29)){ var g =smooth_generator(p.primes) say ("First 25 #{'%2d'%p}-smooth numbers: ",25.of{ g.run}.join(''))}say ''forpin(primes(3,29)){ var g =smooth_generator(p.primes) say ("3,000th through 3,002nd #{'%2d'%p}-smooth numbers: ",3002.of{ g.run}.last(3).join(''))}
Output:
Optionally, an efficient algorithm for checking if a given arbitrary large number is smooth over a given product of primes:
func is_smooth_over_prod(n, k) {
return true if (n == 1)
return false if (n <= 0)
for (var g = gcd(n,k); g > 1; g = gcd(n,k)) {
n /= g**valuation(n,g) # remove any divisibility by g
return true if (n == 1) # smooth if n == 1
}
return false
}
for p in (503, 509, 521) {
var k = p.primorial
var a = {|n| is_smooth_over_prod(n, k) }.first(30_019).last(20)
say ("30,000th through 30,019th #{p}-smooth numbers: ", a.join(' '))
}