A colorful number is a non-negative base 10 integer where the product of every sub group of consecutive digits is unique.



E.G.
24753 is a colorful number. 2, 4, 7, 5, 3, (2×4)8, (4×7)28, (7×5)35, (5×3)15, (2×4×7)56, (4×7×5)140, (7×5×3)105, (2×4×7×5)280, (4×7×5×3)420, (2×4×7×5×3)840

Every product is unique.



2346 is not a colorful number. 2, 3, 4, 6, (2×3)6, (3×4)12, (4×6)24, (2×3×4)48, (3×4×6)72, (2×3×4×6)144

The product 6 is repeated.



Single digit numbers are considered to be colorful. A colorful number larger than 9 cannot contain a repeated digit, the digit 0 or the digit 1. As a consequence, there is a firm upper limit for colorful numbers; no colorful number can have more than 8 digits.



Task
 Write a routine (subroutine, function, procedure, whatever it may be called in your language) to test if a number is a colorful number or not.
 Use that routine to find all of the colorful numbers less than 100.
 Use that routine to find the largest possible colorful number.


Stretch
 Find and display the count of colorful numbers in each order of magnitude.
 Find and show the total count of all colorful numbers.

Colorful numbers have no real number theory application. They are more a recreational math puzzle than a useful tool.

with javascript_semantics
function colourful(integer n)
    if n<10 then return n>=0 end if
    sequence digits = sq_sub(sprintf("%d",n),'0'),
                 ud = unique(deep_copy(digits))
    integer ln = length(digits)
    if ud[1]<=1 or length(ud)!=ln then return false end if
    for i=1 to ln-1 do
        for j=i+1 to ln do
           atom prod = product(digits[i..j])
           if find(prod,ud) then return false end if
           ud &= prod
        end for
    end for
    return true
end function
 
atom t0 = time()
sequence cn = apply(true,sprintf,{{"%2d"},filter(tagset(100,0),colourful)})
printf(1,"The %d colourful numbers less than 100 are:\n%s\n",
         {length(cn),join_by(cn,1,10,"  ")})

sequence count = repeat(0,8),
         used = repeat(false,10)
integer largestcn = 0

procedure count_colourful(integer taken=0, string n="")
    if taken=0 then
        for digit='0' to '9' do
            integer dx = digit-'0'+1
            used[dx] = true
            count_colourful(iff(digit<'2'?9:1),""&digit)
            used[dx] = false
        end for
    else
        integer nn = to_integer(n)
        if colourful(nn) then
            integer ln = length(n)
            count[ln] += 1
            if nn>largestcn then largestcn = nn end if
        end if
        if taken<9 then
            for digit='2' to '9' do
                integer dx = digit-'0'+1
                if not used[dx] then
                    used[dx] = true
                    count_colourful(taken+1,n&digit)
                    used[dx] = false
                end if
            end for
        end if
    end if
end procedure
count_colourful()
printf(1,"The largest possible colourful number is: %,d\n\n",largestcn)
atom pow = 10
for dc=1 to length(count) do
    printf(1,"  %d digit colourful number count: %,6d - %7.3f%%\n",
               {dc, count[dc], 100*count[dc]/pow})
    pow = iff(pow=10?90:pow*10)
end for
printf(1,"\nTotal colourful numbers: %,d\n", sum(count))
?elapsed(time()-t0)
