N-smooth numbers: Difference between revisions

(Added Wren)
Line 2,511:
=={{header|Ruby}}==
{{trans|Python}}
<lang ruby>$primesdef = [2, 3, 5, 7, 11, 13, 17, 19, 23]prime?(n)
return falsen|1 == 3 if n < 25
 
return false unless n.gcd(6) == 1
def isprime(n)
sqrtN = Integer.sqrt(n)
return false if n < 2
$primes.eachpc do= |i|-1
while (pc += 6) return true if n =<= isqrtN
return false if n % ipc == 0 || n % (pc + 2) == 0
return true if i * i > n
end
true
puts "Oops, #{n} is too large"
end
 
def gen_primes(a, b)
def init
(a..b).select { |pc| pc if prime? pc }
s = 24
while s < 600
$primes << (s - 1) if isprime(s - 1) and (s - 1) > $primes[-1]
$primes << (s + 1) if isprime(s + 1) and (s + 1) > $primes[-1]
s += 6
end
end
 
def nsmooth(n, limit)
raise "Exception("n")" if n < 2 or|| n > 521
raise "Exception("n")" if limit < 1
 
primes = gen_primes(2, n)
bn, ok = n, false
$primes.each { |prime| (ok = true; break) if bn == prime }
raise "Exception("must be a prime number: n")" if not ok
ns = [0] * limit
ns[0] = 1
nextp = []
$primes.each { |prime| break if prime > bn; nextp << prime }
 
indices = [0] * nextp.size
Line 2,551 ⟶ 2,547:
if ns[m] == nextp[i]
indices[i] += 1
nextp[i] = $primes[i] * ns[indices[i]]
end
end
Line 2,558 ⟶ 2,554:
end
 
$primesgen_primes(2, 29).each do |prime|
init
$primes.each do |prime|
break if prime > 29
print "The first 25 #{prime}-smooth numbers are: \n"
print nsmooth(prime, 25)
Line 2,566 ⟶ 2,560:
end
puts
$primes[1..-1]gen_primes(3, 29).each do |prime| # for ruby >= 2.6: x[1..].each
break if prime > 29
print "The 3000 to 3202 #{prime}-smooth numbers are: "
print nsmooth(prime, 3002)[2999..-1] # for ruby >= 2.6: (..)[2999..]
Line 2,573 ⟶ 2,566:
end
puts
[gen_primes(503, 509, 521]).each do |prime|
print "The 30,000 to 30,019 #{prime}-smooth numbers are: \n"
print nsmooth(prime, 30019)[29999..-1] # for ruby >= 2.6: (..)[29999..]
puts
end</lang>
}</lang>
 
{{out}}
Anonymous user