How to resolve the algorithm Colorful numbers step by step in the Ruby programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Colorful numbers step by step in the Ruby programming language

Table of Contents

Problem Statement

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

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.

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

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Colorful numbers step by step in the Ruby programming language

Functions:

  • colorful?: This function takes an array of digits as input and returns true if the number represented by those digits is "colorful" (i.e., no two products of a subset of the digits are equal).
    • It iterates through all possible chunk sizes (1 to the length of the array) and checks if the product of each chunk is unique.
    • If any duplicate products are found, the function returns false; otherwise, it returns true.

Main Logic:

  1. The program first generates a list of colorful numbers less than 100 by applying the colorful? function to each digit sequence of numbers in the range 0..100.
  2. It then finds the largest colorful number by iterating down from 98765432 (a known colorful number) until it finds another colorful number.
  3. The program then calculates the count of colorful numbers for each digit count (1 to 8) and prints the count for each digit count.
  4. Finally, it prints the total count of colorful numbers for all digit counts combined.

Sample Input and Output:

The colorful numbers less than 100 are: 25 37 52 76 89 95, 

Largest colorful number: 9876543210

1 digit colorful numbers count: 10
2 digit colorful numbers count: 51
3 digit colorful numbers count: 158
4 digit colorful numbers count: 428
5 digit colorful numbers count: 1034
6 digit colorful numbers count: 2335
7 digit colorful numbers count: 4963
8 digit colorful numbers count: 9926

Total colorful numbers: 19524

Source code in the ruby programming language

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}"


  

You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Python programming language
You may also check:How to resolve the algorithm Odd word problem step by step in the Ceylon programming language
You may also check:How to resolve the algorithm Convex hull step by step in the Scala programming language
You may also check:How to resolve the algorithm Variables step by step in the Quackery programming language
You may also check:How to resolve the algorithm Fermat pseudoprimes step by step in the C++ programming language