Jordan-Pólya numbers: Difference between revisions

Add Rust implementation
(Add Scala implementation)
(Add Rust implementation)
Line 1,660:
say @result.first: * < 100_000_000, :end; </syntaxhighlight>
You may [https://ato.pxeger.com/run?1=bZNNbptAFMcX3XGKv12qYGKPwZWlyNROV110lUV3NrFwPNQjhg8NoMRKnIt0k0V7gh6h6iV6mj5gqEHNSAjNvP_7vY958-27CqLy5eVHWYSTqz9vfsdHbMLgrkiVCGSOJdwxnrDe2D5cxtwrD29xL4oDnIFh5OUOn1O1D5KbVB4DWBspYlGM8GgAqFDigRDWGcgifsxZKFReLHA-XhN-tYT2nk47JsaTvQfNs8xojI-K5yPCziizDsJhTDz4nlFJ7w9CcpgRPtRqxiWP8yYrnZiKiFDZ1mZETo0hTBVmYKzrNFlhI_-5au9IkrfZEqQPGyryzhoZ5AVECNKtmqq6xjTNukDNzDSu16JHneO27g_hTtT_jCvKNA6SO96jUMCsX3GQ7BtA5mNQ-_fj0qrMFPmplt1m_phUY73NGKOL0avneAKXeR1vuey1GK_xWZBldI2LKoE-prc7d2tK13N9Datp5AiDQWW0l9Rl4z_n9m9Gl5dGu1e8KFXSFO9WZRgnw6A2VwelLKji7uDObPu9AxqzPDhi-KnqPeaOlkxufv2sREkZ77jKF0Ovlq2ffZjb1YqFcWFdvJvnF6N6gHSE9e3c8ZlKaTwt1xl5RqZEUmC4Sb4cONWlvnIK8loE7DhxOFzHQSykFGmygA6q4e142HTdJNs6zTfGon4tzWvWj7p93H8B Attempt This Online!]
 
=={{header|Rust}}==
{{trans|C++}}
<syntaxhighlight lang="Rust">
use std::collections::{HashMap, HashSet};
 
const LIMIT: u64 = 1 << 53;
 
fn to_string(map: &HashMap<u64, u64>) -> String {
let mut result = String::new();
for (&key, &value) in map {
// println!("key={} value={}", key, value);
let part = if value == 1 {
format!("{}!", key)
} else {
format!("{}!^{}", key, value)
};
result = part + " * " + &result;
}
result.trim_end_matches(" * ").to_string()
}
 
fn insert_or_update(map: &mut HashMap<u64, u64>, entry: u64) {
let count = map.entry(entry).or_insert(0);
*count += 1;
}
 
fn create_jordan_polya() -> (HashSet<u64>, HashMap<u64, HashMap<u64, u64>>) {
let mut jordan_polya_set = HashSet::new();
let mut decompositions = HashMap::new();
 
jordan_polya_set.insert(1);
decompositions.insert(1, HashMap::new());
let mut factorial = 1u64;
 
for multiplier in 2..=20 {
factorial *= multiplier; // Using u64 for multiplier
let mut to_update = Vec::new();
 
for &number in &jordan_polya_set {
let mut current = number;
while current <= LIMIT / factorial {
to_update.push((current, current * factorial)); // Store original and new number
current *= factorial;
}
}
 
for (original, new_number) in to_update {
jordan_polya_set.insert(new_number);
let mut new_decomposition = decompositions[&original].clone();
insert_or_update(&mut new_decomposition, multiplier);
decompositions.insert(new_number, new_decomposition);
}
}
 
(jordan_polya_set, decompositions)
}
 
 
fn main() {
let (jordan_polya_set, decompositions) = create_jordan_polya();
let mut jordan_polya: Vec<_> = jordan_polya_set.into_iter().collect();
jordan_polya.sort();
 
println!("The first 50 Jordan-Polya numbers:");
for i in 0..50 {
print!("{:5}", jordan_polya[i]);
if i % 10 == 9 {
println!();
}
}
 
let hundred_million = jordan_polya.iter().position(|&x| x >= 100_000_000).unwrap();
println!(
"\nThe largest Jordan-Polya number less than 100 million: {}\n",
jordan_polya[hundred_million - 1]
);
 
for &i in &[800, 1050, 1800, 2800, 3800] {
println!(
"The {}th Jordan-Polya number is: {} = {}",
i,
jordan_polya[i - 1],
to_string(&decompositions[&jordan_polya[i - 1]])
);
}
}
</syntaxhighlight>
{{out}}
<pre>
The first 50 Jordan-Polya numbers:
1 2 4 6 8 12 16 24 32 36
48 64 72 96 120 128 144 192 216 240
256 288 384 432 480 512 576 720 768 864
960 1024 1152 1296 1440 1536 1728 1920 2048 2304
2592 2880 3072 3456 3840 4096 4320 4608 5040 5184
 
The largest Jordan-Polya number less than 100 million: 99532800
 
The 800th Jordan-Polya number is: 18345885696 = 2!^2 * 4!^7
The 1050th Jordan-Polya number is: 139345920000 = 2! * 5!^3 * 8!
The 1800th Jordan-Polya number is: 9784472371200 = 2!^15 * 6!^2 * 4!^2
The 2800th Jordan-Polya number is: 439378587648000 = 14! * 7!
The 3800th Jordan-Polya number is: 7213895789838336 = 2!^16 * 4!^8
 
</pre>
 
=={{header|Scala}}==
337

edits