From 2c64ab4657f61cd4b7b597a4cd698ae41818a6c3 Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 9 Oct 2024 18:10:11 +0100 Subject: [PATCH] feat: 16 - power digit sum depends on num_bigint::BigUint --- Cargo.lock | 31 +++++++++++++++++++++++++++++++ Cargo.toml | 1 + readme.md | 4 ++-- src/bin/16.rs | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/bin/16.rs diff --git a/Cargo.lock b/Cargo.lock index be3411c..b683006 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index c2f1f49..eb902f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/readme.md b/readme.md index 137e426..c908a11 100644 --- a/readme.md +++ b/readme.md @@ -11,7 +11,7 @@ I originally started the challenge in [the-honk](https://github.com/newtykins/th ## Challenge Completion -### 19 out of 100 public challenges completed. +### 20 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 diff --git a/src/bin/16.rs b/src/bin/16.rs new file mode 100644 index 0000000..3896ac4 --- /dev/null +++ b/src/bin/16.rs @@ -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 { + 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::>(); + + let mut sum = 0; + + for i in 0..answer.len() { + let number = answer[i].to_string().parse::().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); +}