Rust Implementation of Problem 12
View source code here on GitHub!
Includes
Problem Solution
- pub fn problems::p0012::p0012() -> utils::Answer
1/*
2Project Euler Problem 12
3
4I'm very glad that I did the most expansive version of my prime infrastructure
5from the start.
6
7Problem:
8
9The sequence of triangle numbers is generated by adding the natural numbers. So
10the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten
11terms would be:
12
131, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
14
15Let us list the factors of the first seven triangle numbers:
16
17 1: 1
18 3: 1,3
19 6: 1,2,3,6
2010: 1,2,5,10
2115: 1,3,5,15
2221: 1,3,7,21
2328: 1,2,4,7,14,28
24
25We can see that 28 is the first triangle number to have over five divisors.
26
27What is the value of the first triangle number to have over five hundred
28divisors?
29*/
30use crate::include::factors;
31use crate::include::utils::Answer;
32
33pub fn p0012() -> Answer {
34 let mut num: u64 = 0;
35 for x in 1.. {
36 num += x;
37 if factors::proper_divisors(num).collect::<Vec<u64>>().len() > 500 {
38 return Answer::Int(num.into());
39 }
40 }
41 unreachable!();
42}