From bdd644ad4a0eeabacf1b5916c5e4ab6389303f46 Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 9 Oct 2024 18:10:12 +0100 Subject: [PATCH] feat: rewrite 16 - power digit sum --- .gitignore | 3 +++ Cargo.lock | 1 - Cargo.toml | 1 - readme.md | 9 +++++---- src/bin/16.rs | 35 ++++++++++++----------------------- src/bin/19.rs | 2 +- 6 files changed, 21 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..5558522 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ /target + +# only the first 100 solutions are allowed to be published +src/bin/[1-9][0-9][0-9].rs diff --git a/Cargo.lock b/Cargo.lock index 4cd9c39..7db7882 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -363,7 +363,6 @@ version = "1.0.0" dependencies = [ "clap", "html-escape", - "num-bigint", "num-to-words", "once_cell", "owo-colors", diff --git a/Cargo.toml b/Cargo.toml index c5a3e2a..c96d6c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,6 @@ html-escape = "0.2.13" phf = { version = "0.11.2", features = ["macros"] } rayon = "1.8.0" owo-colors = "3.5.0" -num-bigint = "0.4.4" once_cell = "1.18.0" num-to-words = "0.1.1" diff --git a/readme.md b/readme.md index 8874073..c74e692 100644 --- a/readme.md +++ b/readme.md @@ -5,14 +5,15 @@ > My solutions to many of Project Euler's problems. -All of the solutions here are written in rust. [main.rs](src/main.rs) is the home to my helper command line that can scaffold files for me quickly, prefaced with a statement of the problem! As per the rules of the challenge, I may only publish the solutions to the first 100 problems here, so I will stop after that but still continue the challenge. +All of the solutions here are written in rust. [main.rs](src/main.rs) is the home to my helper command line that can scaffold files for me quickly, prefaced with a statement of the problem! As per the rules of the challenge, I may only publish the solutions to the first 100 problems here. I will continue the rest in my own time. + +Here is a [link to my profile](https://projecteuler.net/progress=newtykins). ### Allowed Dependencies -- [rayon](https://lib.rs/rayon) for concurrency -- [phf](https://lib.rs/phf) for compile-time static maps +- [rayon](https://lib.rs/rayon) for concurrent iteration +- [phf](https://lib.rs/phf) for compile-time hashmaps - [regex](https://lib.rs/regex) for regex (duh) -- [num_bigint](https://lib.rs/num_bigint) for bigints ## Challenge Completion diff --git a/src/bin/16.rs b/src/bin/16.rs index cfb52e3..e8b110b 100644 --- a/src/bin/16.rs +++ b/src/bin/16.rs @@ -5,36 +5,25 @@ Problem 16 - Power digit sum What is the sum of the digits of the number 2 1000 ? */ -use num_bigint::BigUint; +fn power_digit_sum(base: usize) -> usize { + let mut digits = [0u8; POWER]; + digits[0] = 1; // 2^0 = 1 -fn get_digits(number: usize) -> Vec { - return number - .to_string() - .chars() - .map(|d| d.to_digit(10).unwrap()) - .collect(); -} + for _ in 0..POWER { + let mut carry = 0; -fn power_digit_sum(base: usize, power: u32) -> usize { - let answer = BigUint::new(get_digits(base)) - .pow(power) - .to_string() - .chars() - .collect::>(); - - let mut sum = 0; - - for i in 0..answer.len() { - let number = answer[i].to_string().parse::().unwrap(); - - sum += number; + for digit in digits.iter_mut() { + let result = *digit as usize * base + carry; + *digit = result as u8 % 10; + carry = result / 10; + } } - sum + digits.iter().map(|&x| x as usize).sum() } pub fn main() { - let sum = power_digit_sum(2, 1000); + let sum = power_digit_sum::<1000>(2); println!("The sum of the digits of the number 2^1000 is {}!", sum); } diff --git a/src/bin/19.rs b/src/bin/19.rs index 099f681..4f18356 100644 --- a/src/bin/19.rs +++ b/src/bin/19.rs @@ -36,5 +36,5 @@ pub fn main() { } } - println!("The amount of Sundays that fell on the first of the month during the twentieth century is {}.", sundays_on_first); + println!("The amount of Sundays that fell on the first of the month during the twentieth century is {}!", sundays_on_first); }