How to resolve the algorithm Gapful numbers step by step in the V (Vlang) programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Gapful numbers step by step in the V (Vlang) 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 V (Vlang) programming language
Source code in the v programming language
fn commatize(n u64) string {
mut s := n.str()
le := s.len
for i := le - 3; i >= 1; i -= 3 {
s = '${s[0..i]},$s[i..]'
}
return s
}
fn main() {
starts := [u64(1e2), u64(1e6), u64(1e7), u64(1e9), u64(7123)]
counts := [30, 15, 15, 10, 25]
for i in 0..starts.len {
mut count := 0
mut j := starts[i]
mut pow := u64(100)
for {
if j < pow*10 {
break
}
pow *= 10
}
println("First ${counts[i]} gapful numbers starting at ${commatize(starts[i])}:")
for count < counts[i] {
fl := (j/pow)*10 + (j % 10)
if j%fl == 0 {
print("$j ")
count++
}
j++
if j >= 10*pow {
pow *= 10
}
}
println("\n")
}
}
You may also check:How to resolve the algorithm Speech synthesis step by step in the AmigaBASIC programming language
You may also check:How to resolve the algorithm Host introspection step by step in the R programming language
You may also check:How to resolve the algorithm Function definition step by step in the Free Pascal programming language
You may also check:How to resolve the algorithm String interpolation (included) step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Draw a sphere step by step in the Java programming language