How to resolve the algorithm Gapful numbers step by step in the Mathematica / Wolfram Language programming language
How to resolve the algorithm Gapful numbers step by step in the Mathematica / Wolfram Language 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 Mathematica / Wolfram Language programming language
Purpose: The Wolfram programming language code snippet you provided identifies and returns "gapful" numbers within a specified range. A gapful number is a number whose first and last digits form a divisor of the number itself.
Breakdown:
ClearAll[GapFulQ]
: Clears any previous definitions of a function named GapFulQ
.
GapFulQ[n_Integer]
:
- Defines a function that takes a positive integer as input.
- Converts the integer into a list of its digits using
IntegerDigits
. - Extracts the first and last digits from the list and creates a new number from them using
FromDigits
. - Checks if the original number is divisible by the first and last digit number.
- Returns
True
if it is divisible, andFalse
otherwise.
i = 100
: Initializes a variable i
with the value 100.
res = {}
: Initializes an empty list res
that will store the gapful numbers found.
**While[Length[res] < 30, ... ]
:
- Uses a
While
loop to continue searching for gapful numbers untilres
contains 30 elements. - Inside the loop, the
If
statement checks ifi
is a gapful number using theGapFulQ
function. - If it is gapful, it appends the value of
i
to theres
list. - Then,
i
is incremented by 1 to check the next number.
The same loop is repeated two more times:
- Once with
i
starting at 10^6 and a target of finding 15 gapful numbers. - Again with
i
starting at 10^9 and a target of finding 10 gapful numbers.
Finally, the program prints the list res
for each of the three ranges.
Source code in the wolfram programming language
ClearAll[GapFulQ]
GapFulQ[n_Integer] := Divisible[n, FromDigits[IntegerDigits[n][[{1, -1}]]]]
i = 100;
res = {};
While[Length[res] < 30,
If[GapFulQ[i], AppendTo[res, i]];
i++
]
res
i = 10^6;
res = {};
While[Length[res] < 15,
If[GapFulQ[i], AppendTo[res, i]];
i++
]
res
i = 10^9;
res = {};
While[Length[res] < 10,
If[GapFulQ[i], AppendTo[res, i]];
i++
]
res
You may also check:How to resolve the algorithm AKS test for primes step by step in the Zig programming language
You may also check:How to resolve the algorithm Create a file on magnetic tape step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Pharo programming language
You may also check:How to resolve the algorithm Largest proper divisor of n step by step in the Java programming language
You may also check:How to resolve the algorithm Count occurrences of a substring step by step in the BQN programming language