Colorful numbers: Difference between revisions

Added Easylang
m (→‎{{header|Raku}}: use 'hyper' not 'race')
(Added Easylang)
 
(22 intermediate revisions by 10 users not shown)
Line 92:
The total number of colorful numbers is 57256.
</pre>
 
=={{header|ALGOL 68}}==
Using brute force for the stretch.
<syntaxhighlight lang="algol68">
BEGIN # find colourful numbers: numbers where the product of every sub-group #
# of digits is uniue ( non-negative numbers only ) #
# note that (as stated in the task) for multi-digit numbers, the #
# digits 0 and 1 cannot appear #
 
# returns TRUE if n is colourful, FALSE otherswise #
PROC is colourful = ( INT n )BOOL:
IF n < 0 THEN FALSE
ELIF n < 10 THEN TRUE
ELSE
# more than 1 digit - must teat the digits and digit groups #
INT v := n;
# table to hold the digits and groups, as 0 and 1 can't #
# appear, there will be at most 8 digits so at most 36 groups #
[ 1 : 46 ]INT dg;
# split and count the digits #
[ 0 : 9 ]INT d count;
INT s end := 0; # position of the last single digit in dg #
FOR i FROM LWB d count TO UPB d count DO d count[ i ] := 0 OD;
BOOL unique := TRUE;
WHILE v > 0 AND unique DO
INT d = v MOD 10;
dg[ s end +:= 1 ] := d;
unique := 1 = ( d count[ d ] +:= 1 );
v OVERAB 10
OD;
IF unique THEN unique := d count[ 0 ] + d count[ 1 ] = 0 FI;
# form the group products, stopping if one is non-unique #
INT dg pos := s end;
FOR group width FROM 2 TO s end WHILE unique DO
FOR group start TO ( s end + 1 ) - group width WHILE unique DO
INT product := 1;
INT g pos := group start - 1;
FOR i TO group width DO product *:= dg[ g pos +:= 1 ] OD;
dg[ dg pos +:= 1 ] := product;
FOR i TO dg pos - 1 WHILE unique DO
unique := dg[ i ] /= dg[ dg pos ]
OD
OD
OD;
unique
FI # is colourful # ;
 
# show the colourful numbers under 100 #
print( ( "Colourful numbers less than 100:", newline ) );
INT c count := 0;
FOR i FROM 0 TO 99 DO
IF is colourful( i ) THEN
print( ( whole( i, -3 ) ) );
IF ( c count +:= 1 ) MOD 11 = 0 THEN print( ( newline ) ) FI
FI
OD;
print( ( newline ) );
# find the largest colourful number #
INT max colourful := 0;
FOR i FROM 98765432 BY -1 WHILE max colourful = 0 DO
IF is colourful( i ) THEN
max colourful := i
FI
OD;
print( ( "The largest colourful number is: ", whole( max colourful, 0 ), newline ) );
# show the number of colourful numbers by order of magnitude #
INT p10 := 10;
c count := 0;
INT t count := 0;
FOR i FROM 0 TO max colourful DO
IF i = p10 THEN
print( ( "Colourful numbers below ", whole( p10, -10 ), ": ", whole( c count, 0 ), newline ) );
t count +:= c count;
c count := 0;
p10 *:= 10
FI;
IF is colourful( i ) THEN
c count +:= 1
FI
OD;
print( ( "Colourful numbers below ", whole( p10, -10 ), ": ", whole( c count, 0 ), newline ) );
print( ( "Total number of colourful numbers : ", whole( t count + c count, 0 ), newline ) )
END
</syntaxhighlight>
{{out}}
<pre>
Colourful 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
 
The largest colourful number is: 98746253
Colourful numbers below 10: 10
Colourful numbers below 100: 56
Colourful numbers below 1000: 328
Colourful numbers below 10000: 1540
Colourful numbers below 100000: 5514
Colourful numbers below 1000000: 13956
Colourful numbers below 10000000: 21596
Colourful numbers below 100000000: 14256
Total number of colourful numbers : 57256
</pre>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">(*
This observes the task requirement that colorful numbers be identified using a test handler,
but uses the information regarding 1s and 0s and some logic to skip obvious non-starters
and save a few minutes' running time. Since multiplication is commutative, every number's
"colorful" status is the same as its reverse's. Working up from 0, the lower number of each
mirrored colorful pair is met first and its reverse derived. If the largest reversed number
obtained so far is actually reached, it's the last colorful number at that magnitude. The
search either end there or jumps to the lowest number worth trying at the next level.
*)
on isColorful(n)
if ((n > 98765432) or (n < 0) or (n mod 1 > 0)) then return false
set products to {n mod 10}
repeat while (n > 9)
set n to n div 10
set digit to n mod 10
if ((digit < 2) or (digit is in products)) then return false
set end of products to digit
end repeat
set nDigits to (count products)
repeat with i from 1 to (nDigits - 1)
set prod to products's item i
repeat with j from (i + 1) to nDigits
set prod to prod * (products's item j)
if (prod is in products) then return false
set end of products to prod
end repeat
end repeat
return true
end isColorful
 
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
on task()
set colorfuls to {}
set counter to 0
set counts to {}
set magnitude to 1 -- 10 ^ (number of digits - 1).
set largestReverse to 9 -- Largest reverse of a single-digit number!
set n to 0
repeat
if (isColorful(n)) then
if (n < 100) then set end of colorfuls to text -3 thru -1 of (" " & n)
set counter to counter + 1
if (n = largestReverse) then
set end of counts to counter
set counter to 0
set magnitude to magnitude * 10
if (magnitude > 98765432) then exit repeat
set n to 2.3456789 * magnitude div 1 - 1
else
set temp to n
set |reverse| to temp mod 10
repeat while (temp > 9)
set temp to temp div 10
set |reverse| to |reverse| * 10 + temp mod 10
end repeat
if (|reverse| > largestReverse) then set largestReverse to |reverse|
end if
end if
if (n mod 10 = 9) then
set n to n + 3
else
set n to n + 1
end if
end repeat
set output to {"The colorful numbers below 100:"}
repeat with i from 1 to 66 by 11
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 " & largestReverse
set counter to 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 counts)
set end of output to "The number with " & i & " digits is " & (counts's item i)
set counter to counter + (counts's item i)
end repeat
set end of output to "The total number overall is " & counter
return join(output, linefeed)
end task
 
task()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"The colorful numbers below 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
 
The largest colorful number is 98746253
 
The number of them with 1 digit is 10
The number with 2 digits is 56
The number with 3 digits is 328
The number with 4 digits is 1540
The number with 5 digits is 5514
The number with 6 digits is 13956
The number with 7 digits is 21596
The number with 8 digits is 14256
The total number overall is 57256"</syntaxhighlight>
 
Or here's an alternative using the same <code>isColorful</code> handler but with a translation of the [[#Phix|Phix]] solution's <code>count_colourful</code> function feeding in the numbers. Multi-digit numbers containing 0, 1, or duplicated digits are studiously not produced, so the execution time is very much shorter. (Just over nine seconds on the test machine as opposed to just over six minutes.) Same output as above.
 
<syntaxhighlight lang="applescript">on isColorful(n)
if ((n > 98765432) or (n < 0) or (n mod 1 > 0)) then return false
set products to {n mod 10}
repeat while (n > 9)
set n to n div 10
set digit to n mod 10
if ((digit < 2) or (digit is in products)) then return false
set end of products to digit
end repeat
set nDigits to (count products)
repeat with i from 1 to (nDigits - 1)
set prod to products's item i
repeat with j from (i + 1) to nDigits
set prod to prod * (products's item j)
if (prod is in products) then return false
set end of products to prod
end repeat
end repeat
return true
end isColorful
 
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
on task()
set colorfuls to {}
repeat with n from 0 to 100
if (isColorful(n)) then set end of colorfuls to (" " & n)'s text -3 thru -1
end repeat
script cc
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)
set used's item x to true
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 > 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
count_colourful(taken + 1, dx, n * 10 + digit)
end if
end repeat
end if
set used's item x to false
end count_colourful
end script
repeat with digit from 0 to 9
cc's count_colourful(((digit < 2) as integer) * 8 + 1, digit + 1, digit)
end repeat
set output to {"The colorful numbers below 100:"}
repeat with i from 1 to 66 by 11
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
return join(output, linefeed)
end task
 
task()</syntaxhighlight>
 
=={{header|C}}==
Line 187 ⟶ 489:
}</syntaxhighlight>
 
{{out>@TIO.RUN}}
<pre>
Colorful numbers less than 100:
Line 212 ⟶ 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 840 ⟶ 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,178 ⟶ 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}}==
All colorful candidates larger than 1 digit must be a permutation of digits [2,3,4,5,6,7,8,9], so test only those:
<syntaxhighlight lang="ruby">def colorful?(ar)
products = []
(1..ar.size).all? do |chunk_size|
ar.each_cons(chunk_size) do |chunk|
product = chunk.inject(&:*)
return false if products.include?(product)
products << product
end
end
end
 
below100 = (0..100).select{|n| colorful?(n.digits)}
puts "The colorful numbers less than 100 are:", below100.join(" "), ""
puts "Largest colorful number: #{(98765432.downto(1).detect{|n| colorful?(n.digits) })}", ""
 
total = 0
(1..8).each do |numdigs|
digits = (numdigs == 1 ? (0..9).to_a : (2..9).to_a)
count = digits.permutation(numdigs).count{|perm| colorful?(perm)}
puts "#{numdigs} digit colorful numbers count: #{count}"
total += count
end
 
puts "\nTotal colorful numbers: #{total}"
</syntaxhighlight>
{{out}}
<pre>
The colorful numbers less than 100 are:
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
 
1 digit colorful numbers count: 10
2 digit colorful numbers count: 56
3 digit colorful numbers count: 328
4 digit colorful numbers count: 1540
5 digit colorful numbers count: 5514
6 digit colorful numbers count: 13956
7 digit colorful numbers count: 21596
8 digit colorful numbers count: 14256
 
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>
 
=={{header|Wren}}==
Line 1,183 ⟶ 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,244 ⟶ 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,973

edits