How to resolve the algorithm Gapful numbers step by step in the Crystal programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Gapful numbers step by step in the Crystal programming language
Table of Contents
Problem Statement
Numbers (positive integers expressed in base ten) that are (evenly) divisible by the number formed by the first and last digit are known as gapful numbers.
Evenly divisible means divisible with no remainder.
All one─ and two─digit numbers have this property and are trivially excluded. Only numbers ≥ 100 will be considered for this Rosetta Code task.
187 is a gapful number because it is evenly divisible by the number 17 which is formed by the first and last decimal digits of 187.
About 7.46% of positive integers are gapful.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Gapful numbers step by step in the Crystal programming language
Source code in the crystal programming language
struct Int
def gapful?
a = self.to_s.chars.map(&.to_i)
self % (a.first*10 + a.last) == 0
end
end
specs = {100 => 30, 1_000_000 => 15, 1_000_000_000 => 10, 7123 => 25}
specs.each do |start, count|
puts "first #{count} gapful numbers >= #{start}:"
puts (start..).each.select(&.gapful?).first(count).to_a, "\n"
end
struct Int
def gapful?
a = self.to_s.chars.map(&.to_i)
self % (a.first*10 + a.last) == 0
end
end
specs = {100 => 30, 1_000_000 => 15, 1_000_000_000 => 10, 7123 => 25}
specs.each do |start, count|
puts "first #{count} gapful numbers >= #{start}:"
i, gapful = 0, [] of Int32
(start..).each { |n| n.gapful? && (gapful << n; i += 1); break if i == count }
puts gapful, "\n"
end
You may also check:How to resolve the algorithm Integer comparison step by step in the MiniScript programming language
You may also check:How to resolve the algorithm 15 puzzle game step by step in the LiveCode programming language
You may also check:How to resolve the algorithm String concatenation step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Niue programming language
You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the J programming language