diff options
| author | Federico Igne <git@federicoigne.com> | 2020-12-26 17:48:38 +0000 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2021-11-03 18:55:08 +0000 |
| commit | 02481656966b0a8dfc95cf3c22bcc049660ff7d4 (patch) | |
| tree | 8e39798fcaf27931d91c2088423fd4e97adcfc2d /grains/src/lib.rs | |
| parent | 4e2052c4d792540c2f742b2c2a081b11117ed41d (diff) | |
| download | exercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.tar.gz exercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.zip | |
Move Rust exercises in a subdirectory
Diffstat (limited to 'grains/src/lib.rs')
| -rw-r--r-- | grains/src/lib.rs | 23 |
1 files changed, 0 insertions, 23 deletions
diff --git a/grains/src/lib.rs b/grains/src/lib.rs deleted file mode 100644 index 05449be..0000000 --- a/grains/src/lib.rs +++ /dev/null | |||
| @@ -1,23 +0,0 @@ | |||
| 1 | pub fn square(s: u32) -> u64 { | ||
| 2 | assert!(s > 0 && s < 65,"Square must be between 1 and 64"); | ||
| 3 | 2_u64.pow(s-1) | ||
| 4 | } | ||
| 5 | |||
| 6 | /// This version uses the already defined `square(s)` function. | ||
| 7 | /// | ||
| 8 | /// There is also a faster way to compute this according to | ||
| 9 | /// [Wolfram|Alpha](https://www.wolframalpha.com/input/?i=sum_0%5En+2%5En) | ||
| 10 | /// | ||
| 11 | /// 2_u64.pow(65) - 1 == 18_446_744_073_709_551_615 | ||
| 12 | /// | ||
| 13 | /// this solution however doesn't work because | ||
| 14 | /// | ||
| 15 | /// 2_u64.pow(65) == 18_446_744_073_709_551_616 | ||
| 16 | /// | ||
| 17 | /// which overflows since | ||
| 18 | /// | ||
| 19 | /// std::u64::MAX == 18_446_744_073_709_551_615 | ||
| 20 | /// | ||
| 21 | pub fn total() -> u64 { | ||
| 22 | (1..65).map(square).sum() // std::u64::MAX | ||
| 23 | } | ||
