How to resolve the algorithm N-smooth numbers step by step in the Crystal programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm N-smooth numbers step by step in the Crystal programming language
Table of Contents
Problem Statement
n-smooth numbers are positive integers which have no prime factors > n. The n in the expression n-smooth is always prime; there are no 9-smooth numbers. 1 (unity) is always included in n-smooth numbers.
2-smooth numbers are non-negative powers of two. 5-smooth numbers are also called Hamming numbers. 7-smooth numbers are also called humble numbers.
A way to express 11-smooth numbers is:
All ranges (for n) are to be inclusive, and only prime numbers are to be used. The (optional) n-smooth numbers for the third range are: 503, 509, and 521. Show all n-smooth numbers for any particular n in a horizontal list. Show all output here on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm N-smooth numbers step by step in the Crystal programming language
Source code in the crystal programming language
require "big"
def prime?(n) # P3 Prime Generator primality test
return false unless (n | 1 == 3 if n < 5) || (n % 6) | 4 == 5
sqrt_n = Math.isqrt(n) # For Crystal < 1.2.0 use Math.sqrt(n).to_i
pc = typeof(n).new(5)
while pc <= sqrt_n
return false if n % pc == 0 || n % (pc + 2) == 0
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(n or limit)" if n < 2 || n > 521 || limit < 1
raise "Exception(must be a prime number: n)" unless prime? n
primes = gen_primes(2, n)
ns = [0.to_big_i] * limit
ns[0] = 1.to_big_i
nextp = primes[0..primes.index(n)].map { |prm| prm.to_big_i }
indices = [0] * nextp.size
(1...limit).each do |m|
ns[m] = nextp.min
(0...indices.size).each do |i|
if ns[m] == nextp[i]
indices[i] += 1
nextp[i] = primes[i] * ns[indices[i]]
end
end
end
ns
end
gen_primes(2, 29).each do |prime|
print "The first 25 #{prime}-smooth numbers are: \n"
print nsmooth(prime, 25)
puts
end
puts
gen_primes(3, 29).each do |prime|
print "The 3000 to 3202 #{prime}-smooth numbers are: "
print nsmooth(prime, 3002)[2999..]
puts
end
puts
gen_primes(503, 521).each do |prime|
print "The 30,000 to 30,019 #{prime}-smooth numbers are: \n"
print nsmooth(prime, 30019)[29999..]
puts
end
You may also check:How to resolve the algorithm Queue/Definition step by step in the Action! programming language
You may also check:How to resolve the algorithm Man or boy test step by step in the Phix programming language
You may also check:How to resolve the algorithm Variable size/Get step by step in the Delphi programming language
You may also check:How to resolve the algorithm Singly-linked list/Element definition step by step in the Lang programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Haskell programming language