Anonymous user
N-smooth numbers: Difference between revisions
→{{header|Ruby}}
(Added Wren) |
|||
Line 2,511:
=={{header|Ruby}}==
{{trans|Python}}
<lang ruby>
return false unless n.gcd(6) == 1
sqrtN = Integer.sqrt(n)
▲ return false if n < 2
while (pc += 6)
end
true▼
end
def gen_primes(a, b)
(a..b).select { |pc| pc if prime? pc }
end
def nsmooth(n, limit)
raise "Exception(
raise "Exception(
primes = gen_primes(2, n)
bn, ok = n, false
raise "Exception(
ns = [0] * limit
ns[0] = 1
nextp = []
indices = [0] * nextp.size
Line 2,551 ⟶ 2,547:
if ns[m] == nextp[i]
indices[i] += 1
nextp[i] =
end
end
Line 2,558 ⟶ 2,554:
end
▲$primes.each do |prime|
print "The first 25 #{prime}-smooth numbers are: \n"
print nsmooth(prime, 25)
Line 2,566 ⟶ 2,560:
end
puts
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
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>
{{out}}
|