feat: rewrite 16 - power digit sum

This commit is contained in:
newt 2024-10-09 18:10:12 +01:00
parent adaa608e31
commit bdd644ad4a
6 changed files with 21 additions and 30 deletions

3
.gitignore vendored
View file

@ -1 +1,4 @@
/target /target
# only the first 100 solutions are allowed to be published
src/bin/[1-9][0-9][0-9].rs

1
Cargo.lock generated
View file

@ -363,7 +363,6 @@ version = "1.0.0"
dependencies = [ dependencies = [
"clap", "clap",
"html-escape", "html-escape",
"num-bigint",
"num-to-words", "num-to-words",
"once_cell", "once_cell",
"owo-colors", "owo-colors",

View file

@ -15,7 +15,6 @@ html-escape = "0.2.13"
phf = { version = "0.11.2", features = ["macros"] } phf = { version = "0.11.2", features = ["macros"] }
rayon = "1.8.0" rayon = "1.8.0"
owo-colors = "3.5.0" owo-colors = "3.5.0"
num-bigint = "0.4.4"
once_cell = "1.18.0" once_cell = "1.18.0"
num-to-words = "0.1.1" num-to-words = "0.1.1"

View file

@ -5,14 +5,15 @@
> My solutions to many of Project Euler's problems. > 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 ### Allowed Dependencies
- [rayon](https://lib.rs/rayon) for concurrency - [rayon](https://lib.rs/rayon) for concurrent iteration
- [phf](https://lib.rs/phf) for compile-time static maps - [phf](https://lib.rs/phf) for compile-time hashmaps
- [regex](https://lib.rs/regex) for regex (duh) - [regex](https://lib.rs/regex) for regex (duh)
- [num_bigint](https://lib.rs/num_bigint) for bigints
## Challenge Completion ## Challenge Completion

View file

@ -5,36 +5,25 @@ Problem 16 - Power digit sum
What is the sum of the digits of the number 2 1000 ? What is the sum of the digits of the number 2 1000 ?
*/ */
use num_bigint::BigUint; fn power_digit_sum<const POWER: usize>(base: usize) -> usize {
let mut digits = [0u8; POWER];
digits[0] = 1; // 2^0 = 1
fn get_digits(number: usize) -> Vec<u32> { for _ in 0..POWER {
return number let mut carry = 0;
.to_string()
.chars()
.map(|d| d.to_digit(10).unwrap())
.collect();
}
fn power_digit_sum(base: usize, power: u32) -> usize { for digit in digits.iter_mut() {
let answer = BigUint::new(get_digits(base)) let result = *digit as usize * base + carry;
.pow(power) *digit = result as u8 % 10;
.to_string() carry = result / 10;
.chars() }
.collect::<Vec<char>>();
let mut sum = 0;
for i in 0..answer.len() {
let number = answer[i].to_string().parse::<usize>().unwrap();
sum += number;
} }
sum digits.iter().map(|&x| x as usize).sum()
} }
pub fn main() { 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); println!("The sum of the digits of the number 2^1000 is {}!", sum);
} }

View file

@ -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);
} }