feat: rewrite 16 - power digit sum
This commit is contained in:
parent
adaa608e31
commit
bdd644ad4a
6 changed files with 21 additions and 30 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1 +1,4 @@
|
|||
/target
|
||||
|
||||
# only the first 100 solutions are allowed to be published
|
||||
src/bin/[1-9][0-9][0-9].rs
|
||||
|
|
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -363,7 +363,6 @@ version = "1.0.0"
|
|||
dependencies = [
|
||||
"clap",
|
||||
"html-escape",
|
||||
"num-bigint",
|
||||
"num-to-words",
|
||||
"once_cell",
|
||||
"owo-colors",
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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<const POWER: usize>(base: usize) -> usize {
|
||||
let mut digits = [0u8; POWER];
|
||||
digits[0] = 1; // 2^0 = 1
|
||||
|
||||
fn get_digits(number: usize) -> Vec<u32> {
|
||||
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::<Vec<char>>();
|
||||
|
||||
let mut sum = 0;
|
||||
|
||||
for i in 0..answer.len() {
|
||||
let number = answer[i].to_string().parse::<usize>().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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue