-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
22 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,24 @@ | ||
pub fn solve() -> i64 { | ||
assert_eq!(303963552391i64, 303963552391); | ||
303963552391 | ||
// Calculate Euler's totient of every number up to the limit. | ||
let mut totients = (0..=1000000).collect::<Vec<i64>>(); | ||
for num in 2..totients.len() { | ||
if totients[num] == num as i64 { | ||
// This number is prime. The totient of each of its multiples will | ||
// contain the term | ||
// 1/(1 - num) | ||
// in its product expression. Instead of multiply-assigning that | ||
// term, we use an equivalent subtract-assign. | ||
for multiple in (num..totients.len()).step_by(num) { | ||
totients[multiple] -= totients[multiple] / num as i64; | ||
} | ||
} | ||
} | ||
|
||
// The number of reduced fractions with a particular denominator is the | ||
// totient of the denominator. Hence, the total number of fractions is the | ||
// sum. | ||
let result = totients[2..].iter().sum(); | ||
|
||
assert_eq!(result, 303963552391); | ||
result | ||
} |