Colorful numbers: Difference between revisions

Added Easylang
(→‎{{header|AppleScript}}: Added an alternative using a translation from the Phix solution.)
(Added Easylang)
 
(13 intermediate revisions by 8 users not shown)
Line 344:
on task()
set colorfuls to {}
repeat 100with timesn from 0 to 100
if (isColorful(n)) then set end of colorfuls to missing(" " & n)'s text -3 thru value-1
end repeat
set counts to {0, 0, 0, 0, 0, 0, 0, 0}
setscript largest to 0cc
script o
property used : {false, false, false, false, false, false, false, false, false, false}
property counts : {0, 0, 0, 0, 0, 0, 0, 0}
property largest : 0
on count_colourful(taken, x, n)
ifset (takenused's =item 0)x thento true
if (isColorful(n)) repeat with digit from 0 to 9then
set ln to 1
repeat until ((10 ^ ln) > n)
set ln to ln + 1
end repeat
set counts's item ln to (counts's item ln) + 1
if (n > largest) then set largest to n
end if
if (taken < 9) then
repeat with digit from 2 to 9
set dx to digit + 1
setif (not used's item dx) to truethen
count_colourful((9taken -+ digit)1, divdx, 8n * 810 + 1, digit)
setend used's item dx to falseif
end repeat
else
if (isColorful(n)) then
set ln to 1
repeat until ((10 ^ ln) > n)
set ln to ln + 1
end repeat
set counts's item ln to (counts's item ln) + 1
if (n < 100) then set colorfuls's item (n + 1) to (" " & n)'s text -3 thru -1
if (n > largest) then set largest to n
end if
if (taken < 9) then
repeat with digit from 2 to 9
set dx to digit + 1
if (not used's item dx) then
set used's item dx to true
count_colourful(taken + 1, n * 10 + digit)
set used's item dx to false
end if
end repeat
end if
end if
set used's item x to false
end count_colourful
end script
repeat with digit from 0 to 9
o's count_colourful(0, 0)
cc's count_colourful(((digit < 2) as integer) * 8 + 1, digit + 1, digit)
set colorfuls to colorfuls's every text
end repeat
set output to {"The colorful numbers below 100:"}
Line 390 ⟶ 382:
set end of output to join(colorfuls's items i thru (i + 10), "")
end repeat
set end of output to linefeed & "The largest colorful number is " & cc's largest
set counter to cc's counts's beginning
set end of output to linefeed & "The number of them with 1 digit is " & counter
repeat with i from 2 to (count cc's counts)
set end of output to "The number with " & i & " digits is " & (cc's counts's item i)
set counter to counter + (cc's counts's item i)
end repeat
set end of output to "The total number overall is " & counter
Line 497 ⟶ 489:
}</syntaxhighlight>
 
{{out>@TIO.RUN}}
<pre>
Colorful numbers less than 100:
Line 522 ⟶ 514:
Total: 57,256
 
Elapsed time: 0.024598031256 seconds</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
 
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <vector>
 
std::vector<uint32_t> count(8, 0);
std::vector<bool> used(10, false);
uint32_t largest = 0;
 
bool is_colorful(const uint32_t& number) {
if ( number > 98'765'432 ) {
return false;
}
 
std::vector<uint32_t> digit_count(10, 0);
std::vector<uint32_t> digits(8, 0);
uint32_t number_digits = 0;
 
for ( uint32_t i = number; i > 0; i /= 10 ) {
uint32_t digit = i % 10;
if ( number > 9 && ( digit == 0 || digit == 1 ) ) {
return false;
}
if ( ++digit_count[digit] > 1 ) {
return false;
}
digits[number_digits++] = digit;
}
 
std::vector<uint32_t> products(36, 0);
for ( uint32_t i = 0, product_count = 0; i < number_digits; ++i ) {
for ( uint32_t j = i, product = 1; j < number_digits; ++j ) {
product *= digits[j];
for ( uint32_t k = 0; k < product_count; ++k ) {
if ( products[k] == product ) {
return false;
}
}
products[product_count++] = product;
}
}
return true;
}
 
void count_colorful(const uint32_t& taken, const uint32_t& number, const uint32_t& digits) {
if ( taken == 0 ) {
for ( uint32_t digit = 0; digit < 10; ++digit ) {
used[digit] = true;
count_colorful(digit < 2 ? 9 : 1, digit, 1);
used[digit] = false;
}
} else {
if ( is_colorful(number) ) {
++count[digits - 1];
if ( number > largest ) {
largest = number;
}
}
if ( taken < 9 ) {
for ( uint32_t digit = 2; digit < 10; ++digit ) {
if ( ! used[digit] ) {
used[digit] = true;
count_colorful(taken + 1, number * 10 + digit, digits + 1);
used[digit] = false;
}
}
}
}
}
 
int main() {
std::cout << "Colorful numbers less than 100:" << std::endl;
for ( uint32_t number = 0, count = 0; number < 100; ++number ) {
if ( is_colorful(number) ) {
std::cout << std::setw(2) << number << ( ++count % 10 == 0 ? "\n" : " ");
}
}
std::cout << "\n" << std::endl;
 
count_colorful(0, 0, 0);
std::cout << "Count of colorful numbers by number of digits:" << std::endl;
uint32_t total = 0;
for ( uint32_t digit = 0; digit < 8; ++digit ) {
std::cout << digit + 1 << ": " << count[digit] << std::endl;
total += count[digit];
}
std::cout << std::endl;
 
std::cout << "The largest possible colorful number is: " << largest << "\n" << std::endl;
std::cout << "The total number of colorful numbers is: " << total << std::endl;
}
</syntaxhighlight>
{{ out }}
<pre>
Colorful numbers less than 100:
0 1 2 3 4 5 6 7 8 9
23 24 25 26 27 28 29 32 34 35
36 37 38 39 42 43 45 46 47 48
49 52 53 54 56 57 58 59 62 63
64 65 67 68 69 72 73 74 75 76
78 79 82 83 84 85 86 87 89 92
93 94 95 96 97 98
 
Count of colorful numbers by number of digits:
1: 10
2: 56
3: 328
4: 1540
5: 5514
6: 13956
7: 21596
8: 14256
 
The largest possible colorful number is: 98746253
 
The total number of colorful numbers is: 57256
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
procedure ShowColorfulNumbers(Memo: TMemo);
var I,P,Cnt,OldCnt,Start,Stop: integer;
var S: string;
var Largest: integer;
 
 
function IsColorful(N: integer): boolean;
var IA: TIntegerDynArray;
var PList: TList;
var I,J,D,P: integer;
begin
PList:=TList.Create;
try
Result:=False;
GetDigits(N,IA);
{Number of digits at a time}
for D:=1 to Length(IA) do
{For all digits in number}
for I:=High(IA) downto D-1 do
begin
P:=1;
{Product of digits in a group}
for J:=0 to D-1 do P:=P * IA[I-J];
{Has it already been used}
if PList.IndexOf(Pointer(P))>=0 then exit;
{Store in list}
PList.Add(Pointer(P));
end;
Result:=True;
finally PList.Free; end;
end;
 
 
begin
Cnt:=0; S:='';
Memo.Line.Add('Colorful Number less than 100');
for I:=0 to 100-1 do
if IsColorful(I) then
begin
Inc(Cnt);
S:=S+Format('%7D',[I]);
If (Cnt mod 5)=0 then S:=S+CRLF;
end;
Memo.Lines.Add(S);
Memo.Lines.Add('Count = '+IntToStr(Cnt));
for Largest:=98765432 downto 1 do
if IsColorful(Largest) then break;
Memo.Lines.Add('Largest Colorful Number = '+IntToStr(Largest));
 
Start:=0; Stop:=9;
Cnt:=0; OldCnt:=0;
for P:=1 to 8 do
begin
for I:=Start to Stop do
if IsColorful(I) then Inc(Cnt);
Memo.Lines.Add(Format('Colorful Numbers from %10.0n to %10.0n: %5D', [Start+0.0,Stop+0.0,Cnt-OldCnt]));
Start:=Stop+1;
Stop:=(Start*10)-1;
OldCnt:=Cnt;
end;
for I:=Stop+1 to Largest do
if IsColorful(I) then Inc(Cnt);
Memo.Lines.Add('Total All Colorful = '+IntToStr(Cnt));
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
Colorful Numbers less than 100
0 1 2 3 4
5 6 7 8 9
23 24 25 26 27
28 29 32 34 35
36 37 38 39 42
43 45 46 47 48
49 52 53 54 56
57 58 59 62 63
64 65 67 68 69
72 73 74 75 76
78 79 82 83 84
85 86 87 89 92
93 94 95 96 97
98
Count = 66
 
Largest Colorful Number = 98746253
 
Colorful Numbers from 0 to 9: 10
Colorful Numbers from 10 to 99: 56
Colorful Numbers from 100 to 999: 328
Colorful Numbers from 1,000 to 9,999: 1540
Colorful Numbers from 10,000 to 99,999: 5514
Colorful Numbers from 100,000 to 999,999: 13956
Colorful Numbers from 1,000,000 to 9,999,999: 21596
Colorful Numbers from 10,000,000 to 99,999,999: 14256
Total All Colorful = 57256
Elapsed Time: 01:36.252 min
 
</pre>
 
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
func colorful n .
len digcnt[] 10
arrbase digcnt[] 0
len digits[] 8
m = n
while m > 0
d = m mod 10
if n > 9 and d <= 1
return 0
.
digcnt[d] += 1
if digcnt[d] > 1
return 0
.
ndigs += 1
digits[ndigs] = d
m = m div 10
.
len products[] 36
for i to ndigs
p = 1
for j = i to ndigs
p *= digits[j]
for k to prodcnt
if products[k] = p
return 0
.
.
prodcnt += 1
products[prodcnt] = p
.
.
return 1
.
len cnt[] 8
len used[] 10
arrbase used[] 0
largest = 0
proc cnt_colorful taken n digits . .
if taken = 0
for d = 0 to 9
used[d] = 1
h = 1
if d < 2
h = 9
.
cnt_colorful h d 1
used[d] = 0
.
return
.
if colorful n = 1
cnt[digits] += 1
largest = higher largest n
.
if taken < 9
for d = 2 to 9
if used[d] = 0
used[d] = 1
cnt_colorful (taken + 1) (n * 10 + d) (digits + 1)
used[d] = 0
.
.
.
.
print "Colorful numbers less than 100:"
for n = 0 to 99
if colorful n = 1
write n & " "
.
.
cnt_colorful 0 0 0
print "\n\nLargest colorful number:" & largest
#
print "\nCount of colorful numbers by number of digits:\n"
for d to 8
print d & " " & cnt[d]
total += cnt[d]
.
print "\nTotal: " & total
</syntaxhighlight>
{{out}}
<pre>
Colorful numbers less than 100:
0 1 2 3 4 5 6 7 8 9 23 24 25 26 27 28 29 32 34 35 36 37 38 39 42 43 45 46 47 48 49 52 53 54 56 57 58 59 62 63 64 65 67 68 69 72 73 74 75 76 78 79 82 83 84 85 86 87 89 92 93 94 95 96 97 98
 
Largest colorful number:98746253
 
Count of colorful numbers by number of digits:
 
1 10
2 56
3 328
4 1540
5 5514
6 13956
7 21596
8 14256
 
Total: 57256
</pre>
 
Line 1,150 ⟶ 1,475:
 
57256</pre>
 
=={{header|Nim}}==
{{trans|Python}}
<syntaxhighlight lang="Nim">import std/[math, intsets, strformat]
 
func digits(n: Natural): seq[int] =
## Return the digits of "n" in reverse order.
var n = n
while true:
result.add n mod 10
n = n div 10
if n == 0: break
 
var largest = 0
 
proc isColorful(n: Natural): bool =
## Return true if "n" is colorful.
if n in 0..9: return true
let digSeq = n.digits
var digSet: set[0..9]
for d in digSeq:
if d <= 1 or d in digSet:
return false
digSet.incl d
var products = digSeq.toIntSet()
for i in 0..<digSeq.high:
for j in (i + 1)..digSeq.high:
let p = prod(digSeq.toOpenArray(i, j))
if p in products:
return false
products.incl p
if n > largest: largest = n
result = true
 
echo "Colorful numbers for 1:25, 26:50, 51:75, and 76:100:"
for i in countup(1, 100, 25):
for j in 0..24:
if isColorful(i + j):
stdout.write &"{i + j: 5}"
echo()
 
echo()
var csum = 0
for i in 0..7:
let j = if i == 0: 0 else: 10^i
let k = 10^(i+1) - 1
var n = 0
for x in j..k:
if x.isColorful: inc n
inc csum, n
echo &"The count of colorful numbers between {j} and {k} is {n}."
 
echo()
echo &"The largest possible colorful number is {largest}."
echo &"The total number of colorful numbers is {csum}."
</syntaxhighlight>
 
{{out}}
<pre>Colorful numbers for 1:25, 26:50, 51:75, and 76:100:
1 2 3 4 5 6 7 8 9 23 24 25
26 27 28 29 32 34 35 36 37 38 39 42 43 45 46 47 48 49
52 53 54 56 57 58 59 62 63 64 65 67 68 69 72 73 74 75
76 78 79 82 83 84 85 86 87 89 92 93 94 95 96 97 98
 
The count of colorful numbers between 0 and 9 is 10.
The count of colorful numbers between 10 and 99 is 56.
The count of colorful numbers between 100 and 999 is 328.
The count of colorful numbers between 1000 and 9999 is 1540.
The count of colorful numbers between 10000 and 99999 is 5514.
The count of colorful numbers between 100000 and 999999 is 13956.
The count of colorful numbers between 1000000 and 9999999 is 21596.
The count of colorful numbers between 10000000 and 99999999 is 14256.
 
The largest possible colorful number is 98746253.
The total number of colorful numbers is 57256.
</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
I've created a lexical permutation of [2,3,4,5,6,7,8,9] which results in 40320 arrangements.<br>
So 0,1 are missing. The test of 3 digits is simple a test of the first three digits of these arrangments every delta = (8-3)! = 120 (for one digit = (8-1)! = 5040 ).<BR>
At home, the [[#C|C]]-Version compiled with -Ofast is still faster with best 15.03 ms vs 18ms of my version<br>But such short timings differ extrem from run to run.
 
<syntaxhighlight lang="pascal">{$IFDEF FPC}
{$mode Delphi}
{$Optimization ON,All}
{$Coperators ON}
{$ENDIF}
{$IFDEF WINDOWS}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
sysutils;
const
EightFak = 1*2*3*4*5*6*7*8;
type
tDgt = Int8;
tpDgt = pInt8;
tDgtfield = array[0..7] of tdgt;
tpDgtfield = ^tDgtfield;
tPermfield = tDgtfield;
tAllPermDigits = array[0..EightFak] of tDgtfield;
tMarkNotColorful = array[0..EightFak-1] of byte;
 
tDat = Uint32;
tusedMultiples = array[0..8*9 DIV 2-2] of tDat;
var
AllPermDigits :tAllPermDigits;
MarkNotColorful:tMarkNotColorful;
permcnt,
totalCntColorFul,CntColorFul : Int32;
 
procedure PermLex(n: Int32;StartVal:Int8);
var
Perm : tPermField;
k,j : Int32;
temp: tDgt;
pDat :tpDgt;
begin
For j := 0 to n-1 do
Perm[j]:= j+StartVal;
permcnt := 0;
dec(n);
repeat
AllPermDigits[permcnt] := Perm;
inc(permcnt);
k := N-1;
pDat := @Perm[k];
while (pDat[0]> pDat[1]) And (k >=Low(Perm) ) do
begin
dec(pDat);
dec(k);
end;
 
if k >= Low(Perm) then
begin
j := N;
pDat := @Perm[j];
temp := Perm[k];
while (temp > pDat[0]) And (J >K) do
begin
dec(j);
dec(pDat);
end;
 
Perm[k] := pDat[0];
pDat[0] := temp;
j := N;
pDat := @Perm[j];
Inc(k);
 
while j>k do
begin
temp := pDat[0];
pDat[0] := Perm[k];
Perm[k] := temp;
 
dec(j);
dec(pDat);
inc(k);
end;
end
else
break;
until false;
end;
 
procedure OutNum(const num:tPermfield;dgtCnt: Int32);
var
i : integer;
Begin
For i := 0 to dgtcnt-1 do
write(num[i]);
writeln;
end;
 
function isAlreadyExisting(var uM :tusedMultiples; maxIdx : Int32;dat :tDat):boolean;
var
I : Int32;
begin
if maxIdx >= 0 then
begin
i := maxIdx;
repeat
if dat = uM[i] then
EXIT(true);
Dec(i);
until i <0;
end;
uM[maxIdx+1]:= dat;
result := false;
end;
 
function CalcValue(pDgtfield : tpDgtfield;TestDgtCnt:Int32):int32;
begin
result := 1;
repeat
result *=pDgtfield[TestDgtCnt];
dec(TestDgtCnt);
until TestDgtCnt <0;
end;
function isCheckColorful(dgtCnt: NativeInt;idx:Int32):boolean;
var
usedMultiples : tusedMultiples;
pDgtfield : ^tDgtfield;
TestDgtCnt,StartIdx,value,maxIdx : Int32;
begin
//needn't to test product of all digits.It's a singular max value
dec(dgtCnt);
pDgtfield := @AllPermDigits[idx];
maxIdx := -1;
 
//multiples of TestDgtCnt digits next to one another
For TestDgtCnt := 0 to dgtCnt do
begin
For StartIdx := 0 to dgtCnt-TestDgtCnt do
begin
value := CalcValue(@pDgtfield[StartIdx],TestDgtCnt);
// value := 1; For l := 0 to TestDgtCnt do value *=pDgtfield[StartIdx+l];
if isAlreadyExisting(usedMultiples,maxIdx,value) then
EXIT(false);
inc(MaxIdx);
end;
end;
inc(totalCntColorFul);
inc(CntColorFul);
result := true;
end;
 
procedure CheckDgtCnt(dgtCnt,delta:Int32);
var
i,j : int32;
begin
i := 0;
CntColorFul := 0;
if dgtCnt = 1 then
CntColorFul := 2;//0,1
 
if delta = 1 then
begin
For i := i to EightFak-1 do
IF (MarkNotColorful[i]=0) AND not isCheckColorful(dgtCnt,i)then
MarkNotColorful[i] := dgtcnt;
end
else
Begin
if dgtcnt<3 then
//always colorful
begin
repeat
isCheckColorful(dgtCnt,i);
inc(i,delta);
until i>EightFak-1;
end
else
begin
repeat
IF (MarkNotColorful[i]=0) AND not isCheckColorful(dgtCnt,i) then
begin
//mark a range as not colorful
j := i+delta-1;
repeat
MarkNotColorful[j] := dgtcnt;
dec(j);
until j < i;
end;
inc(i,delta);
until i>EightFak-1;
end;
end;
end;
 
var
T0 : Int64;
i,j,delta: INteger;
Begin
T0 := GetTickCount64;
PermLex(8,2);
//takes ~1 ms til here
delta := 15;
writeln('First colorful numbers less than 100');
For i := 0 to 9 do
Begin
write(i:4);
dec(delta);
end;
For i := 2 to 9 do
For j := 2 to 9 do
if j<> i then
begin
//write(i:3,j);
dec(delta);
if delta = 0 then
begin
delta:= 15;
Writeln;
end;
end;
writeln;
writeln;
Writeln(' digits count of colorful numbers');
totalCntColorFul := 2;//0,1,
delta := EightFak-1;
delta := (delta+1) DIV 8;
j := 7;
repeat
CheckDgtCnt(8-j,delta);
writeln(8-j:10,CntColorFul:10);
if j = 0 then
BREAK;
delta := delta DIV j;
dec(j);
until false;
Writeln;
Writeln('Total number of colorful numbers: ',totalCntColorFul);
Write('Highest Value :');
For i := High(AllPermDigits) downto low(AllPermDigits) do
if MarkNotColorful[i] = 0 then
Begin
OutNum(AllPermDigits[i],8);
BREAK;
end;
 
Writeln('Runtime in ms ',GetTickCount64-T0);
end.
</syntaxhighlight>
{{out|@TIO.RUN}}
<pre>
First colorful numbers less than 100
0 1 2 3 4 5 6 7 8 9 23 24 25 26 27
28 29 32 34 35 36 37 38 39 42 43 45 46 47 48
49 52 53 54 56 57 58 59 62 63 64 65 67 68 69
72 73 74 75 76 78 79 82 83 84 85 86 87 89 92
93 94 95 96 97 98
 
digits count of colorful numbers
1 10
2 56
3 328
4 1540
5 5514
6 13956
7 21596
8 14256
 
Total number of colorful numbers: 57256
Highest Value :98746253
 
Runtime in ms 38 (= best TIO.Run up to 51 ms )
Runtime in ms 18 (= best up to 27 ms @home )
</pre>
 
=={{header|Perl}}==
Line 1,488 ⟶ 2,168:
 
Total colorful numbers: 57256</pre>
 
=={{header|RPL}}==
We have assumed that the largest colored number has 8 digits. This means we only need to check the permutations of 98765432. Brute force is here used, generating these permutations - and many other numbers - through decrementation by 9. Before testing the colorfulness, the sum of the digits of the generated number is compared to that of 98765432. This improves execution time a little bit.
 
If the search had been unsuccessful, we would have tested the different possibilities of 7-digit pandigital numbers - but luckily it was not necessary.
{{works with|HP|48G}}
{| class="wikitable" ≪
! RPL code
! Comment
|-
|
≪ →STR DUP SIZE → num dig
≪ '''CASE'''
dig 1 == '''THEN''' 1 '''END'''
num "0" POS num "1" POS OR '''THEN''' 0 '''END'''
1 SF
{ } 1 dig '''FOR''' j num j DUP SUB STR→ + '''NEXT'''
DUP SORT ΔLIST 1 + ΠLIST NOT '''THEN''' DROP 0 '''END'''
DUP
1 dig 1 - '''FOR''' k
1 dig k - '''FOR''' c
OVER c DUP k + SUB ΠLIST
'''IF''' DUP2 POS
'''THEN''' DROP 1 CF dig DUP 'c' STO 'k' STO
'''ELSE''' + '''END'''
'''NEXT NEXT'''
DROP2 1 FS? '''END'''
≫ ≫ '<span style="color:blue">COLOR?</span>' STO
|
<span style="color:blue">COLOR?</span> ( num → boolean )
if num has only 1 digit then return true
if num contains 0 or 1 then return false
color = true
convert n into a list of digits
if 2 digits are identical then return false
list of products = list of digits
for k = 1 to ndig-1
for c = 1 to ndig-k
p = digit[c]*..*digit[c+k]
if p in list of products
then color = false, exit loops
else append p to list of products
end loops
clean stack, return color
|}
≪ →STR 0
1 3 PICK SIZE '''FOR''' j
OVER j DUP SUB NUM +
'''NEXT''' SWAP DROP
≫ '<span style="color:blue">DSTAMP</span>' STO
≪ { }
1 100 '''FOR''' j IF j <span style="color:blue">COLOR?</span> '''THEN''' + '''END NEXT'''
98765432 DUP <span style="color:blue">DSTAMP</span> SWAP
'''WHILE''' DUP <span style="color:blue">COLOR?</span> NOT '''REPEAT'''
'''DO''' 9 - '''UNTIL''' DUP2 <span style="color:blue">DSTAMP</span> == '''END'''
'''END''' SWAP DROP
≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
2: { 1 2 3 4 5 6 7 8 9 23 24 25 26 27 28 29 32 34 35 36 37 38 39 42 43 45 46 47 48 49 52 53 54 56 57 58 59 62 63 64 65 67 68 69 72 73 74 75 76 78 79 82 83 84 85 86 87 89 92 93 94 95 96 97 98 }
1: 98746253
</pre>
Largest colorful number found in 10 minutes 22 seconds on a HP-48G.
 
 
=={{header|Ruby}}==
Line 1,533 ⟶ 2,280:
 
Total colorful numbers: 57256
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">use core::cmp::max;
use std::collections::HashSet;
 
fn to_digits(mut n: u64, base: u64) -> Vec<u64> {
if n == 0 {
return [0].to_vec();
}
let mut v: Vec<u64> = Vec::new();
while n > 0 {
let d = n % base;
n /= base;
v.push(d);
}
return v;
}
 
fn is_colorful(n: u64) -> bool {
if &n > &9 {
let dig: Vec<u64> = to_digits(n, 10);
if dig.contains(&1) || dig.contains(&0) {
return false;
}
let mut products: HashSet<u64> = HashSet::new();
for i in 0..dig.len() {
if products.contains(&dig[i]) {
return false;
}
products.insert(dig[i]);
}
for i in 0..dig.len() {
for j in i+2..dig.len()+1 {
let p: u64 = (dig[i..j]).iter().product();
if products.contains(&p) {
return false;
}
products.insert(p);
}
}
}
return true;
}
 
fn main() {
println!("Colorful numbers for 1:25, 26:50, 51:75, and 76:100:");
for i in (1..101).step_by(25) {
for j in 0..25 {
if is_colorful(i + j) {
print!("{:5}", i + j);
}
}
println!();
}
println!();
 
let mut csum: u64 = 0;
let mut largest: u64 = 0;
let mut n: u64;
for i in 0..8 {
let j: u64 = { if i == 0 { 0 } else { 10_u64.pow(i) } };
let k: u64 = 10_u64.pow(i + 1) - 1;
n = 0;
for x in j..k+1 {
if is_colorful(x) {
largest = max(largest, x);
n += 1;
}
}
println!("The count of colorful numbers within the interval [{j}, {k}] is {n}.");
csum += n;
}
println!("The largest possible colorful number is {largest}.");
println!("The total number of colorful numbers is {csum}.")
}
</syntaxhighlight>{{out}}
<pre>
Colorful numbers for 1:25, 26:50, 51:75, and 76:100:
1 2 3 4 5 6 7 8 9 23 24 25
26 27 28 29 32 34 35 36 37 38 39 42 43 45 46 47 48 49
52 53 54 56 57 58 59 62 63 64 65 67 68 69 72 73 74 75
76 78 79 82 83 84 85 86 87 89 92 93 94 95 96 97 98
 
The count of colorful numbers within the interval [0, 9] is 10.
The count of colorful numbers within the interval [10, 99] is 56.
The count of colorful numbers within the interval [100, 999] is 328.
The count of colorful numbers within the interval [1000, 9999] is 1540.
The count of colorful numbers within the interval [10000, 99999] is 5514.
The count of colorful numbers within the interval [100000, 999999] is 13956.
The count of colorful numbers within the interval [1000000, 9999999] is 21596.
The largest possible colorful number is 98746253.
The total number of colorful numbers is 57256.
</pre>
 
Line 1,539 ⟶ 2,379:
{{libheader|Wren-math}}
{{libheader|Wren-set}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int, Nums
import "./set" for Set
import "./seq" for Lst
import "./fmt" for Fmt
 
Line 1,600 ⟶ 2,438:
var cn = (0..99).where { |i| isColorful.call(i) }.toList
System.print("The %(cn.count) colorful numbers less than 100 are:")
for (chunk in Lst.chunks(cn, 10)) Fmt.printtprint("$2d", chunkcn, 10)
 
countColorful.call(0, "")
1,981

edits