How to resolve the algorithm Taxicab numbers step by step in the Rust programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Taxicab numbers step by step in the Rust programming language
Table of Contents
Problem Statement
A taxicab number (the definition that is being used here) is a positive integer that can be expressed as the sum of two positive cubes in more than one way.
The first taxicab number is 1729, which is:
Taxicab numbers are also known as:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Taxicab numbers step by step in the Rust programming language
Source code in the rust programming language
use std::collections::HashMap;
use itertools::Itertools;
fn cubes(n: u64) -> Vec<u64> {
let mut cube_vector = Vec::new();
for i in 1..=n {
cube_vector.push(i.pow(3));
}
cube_vector
}
fn main() {
let c = cubes(1201);
let it = c.iter().combinations(2);
let mut m = HashMap::new();
for x in it {
let sum = x[0] + x[1];
m.entry(sum).or_insert(Vec::new()).push(x)
}
let mut result = Vec::new();
for (k,v) in m.iter() {
if v.len() > 1 {
result.push((k,v));
}
}
result.sort();
for f in result {
println!("{:?}", f);
}
}
You may also check:How to resolve the algorithm Bitmap/Write a PPM file step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Zeckendorf number representation step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Longest common subsequence step by step in the Erlang programming language
You may also check:How to resolve the algorithm Map range step by step in the Rust programming language
You may also check:How to resolve the algorithm Pick random element step by step in the Julia programming language