Colorful numbers

From Rosetta Code
Revision as of 22:42, 22 February 2022 by Thundergnat (talk | contribs) (New draft task and Raku example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Colorful numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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)54, (4×7×5)140, (7×5×3)105, (2×4×7×5)280, (4×7×5×3)140, (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.


Raku

<lang perl6>sub is-colorful (Int $n) {

   return True if 0 <= $n <= 9;
   return False if $n.contains(0) || $n.contains(1);
   my @digits = $n.comb;
   my %sums = @digits.Bag;
   return False if %sums.values.max > 1;
   for 2..@digits -> $group {
       @digits.rotor($group => 1 - $group).map: { %sums{ [×] $_ }++ }
       return False if %sums.values.max > 1;
   }
   True

}

put "Colorful numbers less than 100:\n" ~ (^100).race.grep( &is-colorful).batch(10)>>.fmt("%2d").join: "\n";

my ($start, $end, $total) = 23456789, 98765432, 10;

print "\nLargest magnitude colorful number: "; .put and last if .&is-colorful for ($start .. $end).reverse;


put "\nCount of colorful numbers for each order of magnitude:\n" ~

   "1 digit colorful number count: $total";

for 2..8 {

  put "$_ digit colorful number count: ",
  my $c = +(+$start.substr(0,$_) .. +$end.substr(0,$_)).race(:2500batch).grep(&is-colorful);
  $total += $c;

}

say "\nTotal colorful numbers: $total";</lang>

Output:
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 magnitude colorful number: 98746253

Count of colorful numbers for each order of magnitude:
1 digit colorful number count: 10
2 digit colorful number count: 56
3 digit colorful number count: 328
4 digit colorful number count: 1540
5 digit colorful number count: 5514
6 digit colorful number count: 13956
7 digit colorful number count: 21596
8 digit colorful number count: 14256

Total colorful numbers: 57256