-
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
1 parent
bc0ab16
commit b5583fc
Showing
8 changed files
with
114 additions
and
15 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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Project Euler Problem 21 | ||
I had to approach this by modifying the factors function from p0003, but it | ||
seemed to work fairly well. | ||
Revision 1: | ||
Rewrote the proper_divisors function to be significantly faster by leveraging | ||
the prime_factors object. | ||
Problem: | ||
Let d(n) be defined as the sum of proper divisors of n (numbers less than n | ||
which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b, then a and | ||
b are an amicable pair and each of a and b are called amicable numbers. | ||
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 | ||
and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and | ||
142; so d(284) = 220. | ||
Evaluate the sum of all the amicable numbers under 10000. | ||
*/ | ||
use std::collections::HashSet; | ||
|
||
use crate::include::factors::proper_divisors; | ||
use crate::include::utils::Answer; | ||
|
||
fn d(n: u64) -> u64 { | ||
return proper_divisors(n).sum(); | ||
} | ||
|
||
pub fn p0021() -> Answer { | ||
let mut answer = 0; | ||
let skip: HashSet<u64> = HashSet::new(); | ||
for a in 0..10000 { | ||
if skip.contains(&a) { | ||
continue; | ||
} | ||
let b = d(a); | ||
if a != b && d(b) == a { | ||
ret += a + b; | ||
skip.insert(b); | ||
} | ||
} | ||
return Answer::Int(answer.into()); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
Project Euler Problem 23 | ||
I had to approach this by modifying the factors function from p0003, but it | ||
seemed to work fairly well. | ||
Problem: | ||
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. | ||
*/ | ||
use std::collections::HashSet; | ||
|
||
use itertools::Itertools; | ||
|
||
use crate::include::factors::proper_divisors; | ||
use crate::include::utils::Answer; | ||
|
||
pub fn p0023() -> Answer { | ||
let mut abundant_sums: HashSet<u64> = HashSet::new(); | ||
abundant_sums.insert(24); | ||
let mut abundants: Vec<u64> = vec![]; | ||
|
||
for x in 12..28112 { | ||
if proper_divisors(x).sum() > x { | ||
abundants.push(x); | ||
} | ||
} | ||
for v in abundants.into_iter().combinations_with_replacement(2) { | ||
let x = v[0]; | ||
let y = v[1]; | ||
abundant_sums.insert(x + y); | ||
} | ||
let mut sum = 0; | ||
for x in 1..28124 { | ||
if !abundant_sums.contains(&x) { | ||
sum += x; | ||
} | ||
} | ||
return Answer::Int(sum.into()); | ||
} |