blob: 05449bef5f80656683f1524ea753705f4b7b1f0f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
pub fn square(s: u32) -> u64 {
assert!(s > 0 && s < 65,"Square must be between 1 and 64");
2_u64.pow(s-1)
}
/// This version uses the already defined `square(s)` function.
///
/// There is also a faster way to compute this according to
/// [Wolfram|Alpha](https://www.wolframalpha.com/input/?i=sum_0%5En+2%5En)
///
/// 2_u64.pow(65) - 1 == 18_446_744_073_709_551_615
///
/// this solution however doesn't work because
///
/// 2_u64.pow(65) == 18_446_744_073_709_551_616
///
/// which overflows since
///
/// std::u64::MAX == 18_446_744_073_709_551_615
///
pub fn total() -> u64 {
(1..65).map(square).sum() // std::u64::MAX
}
|