feat: 16 - power digit sum

depends on num_bigint::BigUint
This commit is contained in:
newt 2024-10-09 18:10:11 +01:00
parent 0b99867345
commit 2c64ab4657
4 changed files with 69 additions and 2 deletions

31
Cargo.lock generated
View file

@ -251,6 +251,7 @@ version = "1.0.0"
dependencies = [
"clap",
"html-escape",
"num-bigint",
"owo-colors",
"regex",
"requestty",
@ -719,6 +720,36 @@ version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
[[package]]
name = "num-bigint"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.15.0"

View file

@ -13,6 +13,7 @@ reqwest = "0.11"
tokio = { version = "1.23", features = ["full"] }
regex = "1"
html-escape = "0.2.13"
num-bigint = "0.4.3"
[profile.release]
opt-level = "z"

View file

@ -11,7 +11,7 @@ I originally started the challenge in [the-honk](https://github.com/newtykins/th
## Challenge Completion
### <!-- completed -->19<!-- completed --> out of 100 public challenges completed.
### <!-- completed -->20<!-- completed --> out of 100 public challenges completed.
- [x] 1 - [Multiples of 3 or 5](src/bin/1.rs)
- [x] 2 - [Even Fibonacci numbers](src/bin/2.rs)
@ -28,7 +28,7 @@ I originally started the challenge in [the-honk](https://github.com/newtykins/th
- [x] 13 - [Large sum](src/bin/13.rs)
- [x] 14 - [Longest Collatz sequence](src/bin/14.rs)
- [x] 15 - [Lattice paths](src/bin/15.rs)
- [ ] 16 - Power digit sum
- [x] 16 - [Power digit sum](src/bin/16.rs)
- [ ] 17 - Number letter counts
- [ ] 18 - Maximum path sum I
- [ ] 19 - Counting Sundays

35
src/bin/16.rs Normal file
View file

@ -0,0 +1,35 @@
/*
Problem 16 - Power digit sum
2 15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 2 1000 ?
*/
use num_bigint::BigUint;
fn get_digits(number: usize) -> Vec<u32> {
return number
.to_string()
.chars()
.map(|d| d.to_digit(10).unwrap())
.collect();
}
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;
}
sum
}
fn main() {
let sum = power_digit_sum(2, 1000);
println!("The sum of the digits of the number 2^1000 is {}!", sum);
}