feat(23): complete non-abundant sums
This commit is contained in:
parent
6ab9e999b5
commit
100f2c03be
6 changed files with 75 additions and 4 deletions
|
@ -17,7 +17,7 @@ Here is a [link to my profile](https://projecteuler.net/progress=newtykins).
|
|||
|
||||
## Challenge Completion
|
||||
|
||||
### 25 out of 100 public challenges completed.
|
||||
### 26 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)
|
||||
|
@ -41,7 +41,7 @@ Here is a [link to my profile](https://projecteuler.net/progress=newtykins).
|
|||
- [x] 20 - Factorial digit sum
|
||||
- [x] 21 - Amicable numbers
|
||||
- [x] 22 - [Names scores](src/bin/22.rs)
|
||||
- [ ] 23 - Non-abundant sums
|
||||
- [x] 23 - Non-abundant sums
|
||||
- [ ] 24 - Lexicographic permutations
|
||||
- [ ] 25 - 1000-digit Fibonacci number
|
||||
- [ ] 26 - Reciprocal cycles
|
||||
|
|
|
@ -12,6 +12,7 @@ And on leap years, twenty-nine.
|
|||
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
|
||||
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
|
||||
*/
|
||||
|
||||
fn is_leap_year(year: u16) -> bool {
|
||||
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0
|
||||
}
|
||||
|
|
65
src/bin/23.rs
Normal file
65
src/bin/23.rs
Normal file
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
Problem 23 - Non-Abundant Sums
|
||||
|
||||
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of $28$ would be $1 + 2 + 4 + 7 + 14 = 28$, which means that $28$ is a perfect number.
|
||||
A number $n$ is called deficient if the sum of its proper divisors is less than $n$ and it is called abundant if this sum exceeds $n$.
|
||||
As $12$ is the smallest abundant number, $1 + 2 + 3 + 4 + 6 = 16$, the smallest number that can be written as the sum of two abundant numbers is $24$. By mathematical analysis, it can be shown that all integers greater than $28123$ can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.
|
||||
Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
|
||||
*/
|
||||
|
||||
const UPPER_LIMIT: u16 = 28123;
|
||||
|
||||
fn proper_divisors(number: usize) -> Vec<usize> {
|
||||
let mut divisors = vec![1];
|
||||
|
||||
for divisor in 2..=(number / 2) {
|
||||
if number % divisor == 0 {
|
||||
divisors.push(divisor);
|
||||
}
|
||||
}
|
||||
|
||||
divisors
|
||||
}
|
||||
|
||||
fn is_abundant(number: usize) -> bool {
|
||||
proper_divisors(number).iter().sum::<usize>() > number
|
||||
}
|
||||
|
||||
fn abundant_numbers(lower_bound: usize, upper_bound: usize) -> Vec<usize> {
|
||||
let mut abundant_numbers = Vec::new();
|
||||
|
||||
for number in lower_bound..=upper_bound {
|
||||
if is_abundant(number) {
|
||||
abundant_numbers.push(number);
|
||||
}
|
||||
}
|
||||
|
||||
abundant_numbers
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let numbers = abundant_numbers(1, UPPER_LIMIT as usize);
|
||||
let mut sums = vec![0; UPPER_LIMIT as usize + 1];
|
||||
|
||||
for i in 0..numbers.len() {
|
||||
for j in i..numbers.len() {
|
||||
let sum = numbers[i] + numbers[j];
|
||||
|
||||
if sum <= UPPER_LIMIT as usize {
|
||||
sums[sum] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let answer = sums
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, &sum)| sum == 0)
|
||||
.map(|(number, _)| number)
|
||||
.sum::<usize>();
|
||||
|
||||
println!(
|
||||
"The sum of all the positive integers which cannot be written as the sum of two abundant numbers is {}!",
|
||||
answer
|
||||
);
|
||||
}
|
|
@ -70,7 +70,9 @@ pub async fn execute(number: Option<u8>) -> Result<()> {
|
|||
// add new mod
|
||||
let mod_regex = Regex::new(r#"#\[path = ".+"\]\nmod [A-Za-z_]+;"#)?;
|
||||
let final_mod = mod_regex.find_iter(&content).last().unwrap();
|
||||
let word_number = num_to_words(problem.number as i64)?.replace(" ", "_");
|
||||
let word_number = num_to_words(problem.number as i64)?
|
||||
.replace(" ", "_")
|
||||
.replace("-", "_");
|
||||
|
||||
content.insert_str(
|
||||
final_mod.end(),
|
||||
|
|
|
@ -48,6 +48,8 @@ mod twenty;
|
|||
mod twenty_one;
|
||||
#[path = "../bin/27.rs"]
|
||||
mod twenty_seven;
|
||||
#[path = "../bin/23.rs"]
|
||||
mod twenty_three;
|
||||
#[path = "../bin/22.rs"]
|
||||
mod twenty_two;
|
||||
#[path = "../bin/2.rs"]
|
||||
|
@ -93,6 +95,7 @@ pub async fn execute(
|
|||
19 => nineteen::main(),
|
||||
20 => twenty::main(),
|
||||
21 => twenty_one::main(),
|
||||
23 => twenty_three::main(),
|
||||
_ => {
|
||||
exists = false;
|
||||
println!(
|
||||
|
|
|
@ -107,7 +107,7 @@ Problem {} - {}
|
|||
|
||||
{}
|
||||
*/
|
||||
|
||||
|
||||
pub fn main() {{
|
||||
println!(\"Hello World!\");
|
||||
}}",
|
||||
|
|
Loading…
Reference in a new issue