Colorful numbers: Difference between revisions

m
rust example
(add RPL)
m (rust example)
Line 2,175:
 
Total colorful numbers: 57256
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">use core::cmp::max;
use itertools::Itertools;
 
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 > &10 {
let dig: Vec<u64> = to_digits(n, 10);
let mut products: Vec<u64> = dig.clone().into_iter().unique().collect();
if dig.len() > products.len() || dig.contains(&1) || dig.contains(&0) {
return false;
}
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.push(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) + 1 } };
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 10 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>
 
4,102

edits