Esthetic numbers: Difference between revisions

Added Rust solution
(Added Forth solution)
(Added Rust solution)
Line 5,239:
............
123456789898987898 123456789898989876 123456789898989878 123456789898989898</pre>
 
=={{header|Rust}}==
<lang rust>// [dependencies]
// radix_fmt = "1.0"
 
// Returns the next esthetic number in the given base after n, where n is an
// esthetic number in that base or one less than a power of base.
fn next_esthetic_number(base: u64, n: u64) -> u64 {
if n + 1 < base {
return n + 1;
}
let mut a = n / base;
let mut b = a % base;
if n % base + 1 == b && b + 1 < base {
return n + 2;
}
a = next_esthetic_number(base, a);
b = a % base;
a * base + if b == 0 { 1 } else { b - 1 }
}
 
fn print_esthetic_numbers(min: u64, max: u64, numbers_per_line: usize) {
let mut numbers = Vec::new();
let mut n = next_esthetic_number(10, min - 1);
while n <= max {
numbers.push(n);
n = next_esthetic_number(10, n);
}
let count = numbers.len();
println!(
"Esthetic numbers in base 10 between {} and {} ({}):",
min, max, count
);
if count > 200 {
for i in 0..numbers_per_line {
if i != 0 {
print!(" ");
}
print!("{}", numbers[i]);
}
println!("\n............\n");
for i in 0..numbers_per_line {
if i != 0 {
print!(" ");
}
print!("{}", numbers[count - numbers_per_line + i]);
}
println!();
} else {
for i in 0..count {
print!("{}", numbers[i]);
if (i + 1) % numbers_per_line == 0 {
println!();
} else {
print!(" ");
}
}
if count % numbers_per_line != 0 {
println!();
}
}
}
 
fn main() {
for base in 2..=16 {
let min = base * 4;
let max = base * 6;
println!(
"Esthetic numbers in base {} from index {} through index {}:",
base, min, max
);
let mut n = 0;
for index in 1..=max {
n = next_esthetic_number(base, n);
if index >= min {
print!("{} ", radix_fmt::radix(n, base as u8));
}
}
println!("\n");
}
 
let mut min = 1000;
let mut max = 9999;
print_esthetic_numbers(min, max, 16);
println!();
 
min = 100000000;
max = 130000000;
print_esthetic_numbers(min, max, 8);
println!();
 
for i in 0..3 {
min *= 1000;
max *= 1000;
let numbers_per_line = match i {
0 => 7,
1 => 5,
_ => 4,
};
print_esthetic_numbers(min, max, numbers_per_line);
println!();
}
}</lang>
 
{{out}}
<pre>Esthetic numbers in base 2 from index 8 through index 12:
10101010 101010101 1010101010 10101010101 101010101010
 
Esthetic numbers in base 3 from index 12 through index 18:
1210 1212 2101 2121 10101 10121 12101
 
Esthetic numbers in base 4 from index 16 through index 24:
323 1010 1012 1210 1212 1232 2101 2121 2123
 
Esthetic numbers in base 5 from index 20 through index 30:
323 343 432 434 1010 1012 1210 1212 1232 1234 2101
 
Esthetic numbers in base 6 from index 24 through index 36:
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234
 
Esthetic numbers in base 7 from index 28 through index 42:
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232
 
Esthetic numbers in base 8 from index 32 through index 48:
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212
 
Esthetic numbers in base 9 from index 36 through index 54:
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 1012 1210
 
Esthetic numbers in base 10 from index 40 through index 60:
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012
 
Esthetic numbers in base 11 from index 44 through index 66:
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 a98 a9a 1010
 
Esthetic numbers in base 12 from index 48 through index 72:
543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba ba9 bab
 
Esthetic numbers in base 13 from index 52 through index 78:
545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb cba
 
Esthetic numbers in base 14 from index 56 through index 84:
565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc
 
Esthetic numbers in base 15 from index 60 through index 90:
567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd
 
Esthetic numbers in base 16 from index 64 through index 96:
654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd ded def edc
 
Esthetic numbers in base 10 between 1000 and 9999 (61):
1010 1012 1210 1212 1232 1234 2101 2121 2123 2321 2323 2343 2345 3210 3212 3232
3234 3432 3434 3454 3456 4321 4323 4343 4345 4543 4545 4565 4567 5432 5434 5454
5456 5654 5656 5676 5678 6543 6545 6565 6567 6765 6767 6787 6789 7654 7656 7676
7678 7876 7878 7898 8765 8767 8787 8789 8987 8989 9876 9878 9898
 
Esthetic numbers in base 10 between 100000000 and 130000000 (126):
101010101 101010121 101010123 101012101 101012121 101012123 101012321 101012323
101012343 101012345 101210101 101210121 101210123 101212101 101212121 101212123
101212321 101212323 101212343 101212345 101232101 101232121 101232123 101232321
101232323 101232343 101232345 101234321 101234323 101234343 101234345 101234543
101234545 101234565 101234567 121010101 121010121 121010123 121012101 121012121
121012123 121012321 121012323 121012343 121012345 121210101 121210121 121210123
121212101 121212121 121212123 121212321 121212323 121212343 121212345 121232101
121232121 121232123 121232321 121232323 121232343 121232345 121234321 121234323
121234343 121234345 121234543 121234545 121234565 121234567 123210101 123210121
123210123 123212101 123212121 123212123 123212321 123212323 123212343 123212345
123232101 123232121 123232123 123232321 123232323 123232343 123232345 123234321
123234323 123234343 123234345 123234543 123234545 123234565 123234567 123432101
123432121 123432123 123432321 123432323 123432343 123432345 123434321 123434323
123434343 123434345 123434543 123434545 123434565 123434567 123454321 123454323
123454343 123454345 123454543 123454545 123454565 123454567 123456543 123456545
123456565 123456567 123456765 123456767 123456787 123456789
 
Esthetic numbers in base 10 between 100000000000 and 130000000000 (911):
101010101010 101010101012 101010101210 101010101212 101010101232 101010101234 101010121010
............
 
123456787678 123456787876 123456787878 123456787898 123456789876 123456789878 123456789898
 
Esthetic numbers in base 10 between 100000000000000 and 130000000000000 (6225):
101010101010101 101010101010121 101010101010123 101010101012101 101010101012121
............
 
123456789898767 123456789898787 123456789898789 123456789898987 123456789898989
 
Esthetic numbers in base 10 between 100000000000000000 and 130000000000000000 (44744):
101010101010101010 101010101010101012 101010101010101210 101010101010101212
............
 
123456789898987898 123456789898989876 123456789898989878 123456789898989898
 
</pre>
 
=={{header|Sidef}}==
1,777

edits