Rebol3 Code Examplex


Gapful numbers

Identify numbers divisible by the concatenation of their first and last digits.

Rebol [
    title: "Rosetta code: Gapful numbers"
    file:  %Gapful_numbers.r3
    url:   https://rosettacode.org/wiki/Gapful_numbers
    needs: 3.0.0
]

gapful-numbers: function[start count][
    collect [                   ;; Collect result values into a block
        num: start              ;; Start searching from given number
        took: 0                 ;; Counter of found gapful numbers
        while [took < count][   ;; Repeat until count gapful numbers have been found
            s: form num         ;; Convert current number to string form
            d: to integer! ajoin [first s last s]  ;; Form divisor by concatenating first and last digits
            if zero? num % d [  ;; Check if number is divisible by this divisor (gapful condition)
                keep num        ;; If yes, add number to collected results
                ++ took         ;; Increment found count
            ]
            ++ num              ;; Increment current number to test next candidate
        ]
    ]
]

;; Test various start/count pairs
foreach [start count][
    100 30
    1000000 15
    1000000000 10
    7123 25
][
    print ["First" count "gapful numbers starting from" start]
    probe new-line/skip gapful-numbers start count true 5
]

Output:

First 30 gapful numbers starting from 100
[
    100 105 108 110 120
    121 130 132 135 140
    143 150 154 160 165
    170 176 180 187 190
    192 195 198 200 220
    225 231 240 242 253
]
First 15 gapful numbers starting from 1000000
[
    1000000 1000005 1000008 1000010 1000016
    1000020 1000021 1000030 1000032 1000034
    1000035 1000040 1000050 1000060 1000065
]
First 10 gapful numbers starting from 1000000000
[
    1000000000 1000000001 1000000005 1000000008 1000000010
    1000000016 1000000020 1000000027 1000000030 1000000032
]
First 25 gapful numbers starting from 7123
[
    7125 7140 7171 7189 7210
    7272 7275 7280 7296 7350
    7373 7420 7425 7474 7488
    7490 7560 7575 7630 7632
    7676 7700 7725 7770 7777
]