How to resolve the algorithm Gapful numbers step by step in the PowerShell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Gapful numbers step by step in the PowerShell 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 PowerShell programming language

Source code in the powershell programming language

function Get-FirstDigit {
  param ( [int] $Number )
  [int]$Number.ToString().Substring(0,1)
}

function Get-LastDigit {
  param ( [int] $Number )
  $Number % 10
}

function Get-BookendNumber {
  param ( [Int] $Number )
  10 * (Get-FirstDigit $Number) + (Get-LastDigit $Number)
}

function Test-Gapful {
  param ( [Int] $Number )
  100 -lt $Number -and 0 -eq $Number % (Get-BookendNumber $Number)
}

function Find-Gapfuls {
  param ( [Int] $Start, [Int] $Count )
  $result = @()

  While ($result.Count -lt $Count)  {
    If (Test-Gapful $Start) {
      $result += @($Start)
    }
    $Start += 1
  }
  return $result
}

function Search-Range {
  param ( [Int] $Start, [Int] $Count )
  Write-Output "The first $Count gapful numbers >= $($Start):"
  Write-Output( (Find-Gapfuls $Start $Count) -join ",")
  Write-Output ""
}

Search-Range 1 30
Search-Range 1000000 15
Search-Range 1000000000 10


  

You may also check:How to resolve the algorithm Five weekends step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the PowerShell programming language
You may also check:How to resolve the algorithm String comparison step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Strip a set of characters from a string step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the ALGOL-M programming language