PE 088 in progress, hard problem
This commit is contained in:
7
projecteuler/088/Cargo.lock
generated
Normal file
7
projecteuler/088/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "main"
|
||||
version = "0.1.0"
|
||||
8
projecteuler/088/Cargo.toml
Normal file
8
projecteuler/088/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "main"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
53
projecteuler/088/src/main.rs
Normal file
53
projecteuler/088/src/main.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use std::time::Instant;
|
||||
|
||||
fn product(v: &Vec<u32>) -> u32 {
|
||||
v.iter().fold(1, |acc, &v_x| acc * v_x)
|
||||
}
|
||||
|
||||
fn naive_solution(k: u32) -> u32 {
|
||||
todo!()
|
||||
}
|
||||
|
||||
// ChatGPT failing once again
|
||||
fn chatgpt_solution(k: u32) -> u32 {
|
||||
let mut prod = vec![2 * k; k as usize + 1];
|
||||
let mut sums = vec![2 * k; k as usize + 1];
|
||||
|
||||
prod[1] = 1; // product of a single number is 1
|
||||
sums[1] = 1; // sum of a single number is 1
|
||||
|
||||
// iterate through all possible combinations of numbers
|
||||
for i in 2..k + 1 {
|
||||
for j in (i..2 * k + 1).rev() {
|
||||
for p in 1..=j - i {
|
||||
let q = j - i - p;
|
||||
if q >= i && prod[p as usize] <= j / prod[q as usize] {
|
||||
let s = sums[p as usize] + q;
|
||||
if s < sums[j as usize] {
|
||||
sums[j as usize] = s;
|
||||
prod[j as usize] = prod[p as usize] * q;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sums[k as usize]
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello, this is Patrick!");
|
||||
let now = Instant::now();
|
||||
|
||||
let max_k = 12;
|
||||
|
||||
let solution = naive_solution;
|
||||
let result: u32 = (2..=max_k).into_iter().map(|k| solution(k)).sum();
|
||||
|
||||
println!(
|
||||
"The sum of all minimal product-sum numbers for 2 <= k <= {} is: {}",
|
||||
max_k, result,
|
||||
);
|
||||
|
||||
println!("Time passed: {:?}", Instant::now() - now);
|
||||
}
|
||||
Reference in New Issue
Block a user