Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
codehippo committed Sep 14, 2021
0 parents commit de4dd7c
Show file tree
Hide file tree
Showing 27 changed files with 1,306 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/euler.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 87 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions project_euler_12/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions project_euler_12/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "project_euler_12"
version = "0.1.0"
authors = ["Jakub Hornáček <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
62 changes: 62 additions & 0 deletions project_euler_12/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#[derive(Clone, Copy)]
struct PrimeFactorWithMultiplicity {
prime: u64,
multiplicity: u64
}

impl PrimeFactorWithMultiplicity {
fn new(prime: u64, multiplicity: u64) -> Self {
PrimeFactorWithMultiplicity {prime, multiplicity}
}

fn increase_multiplicity(&mut self) {
self.multiplicity += 1;
}
}

struct PrimeFactorisedNumberWithMultiplicity {
num: u64,
prime_factors_with_multiplicity: Vec<PrimeFactorWithMultiplicity>,
factors: u64
}

impl PrimeFactorisedNumberWithMultiplicity {
fn new(num: u64) -> Self {
let mut result:Vec<PrimeFactorWithMultiplicity> = Vec::new();
let mut tmp_num = num;

while tmp_num != 1 {
for test_num in 2..=tmp_num {
if tmp_num % test_num == 0 {
if result.is_empty() {
result.push(PrimeFactorWithMultiplicity::new(test_num, 1));
} else if result.iter().find(|x| x.prime == test_num).is_none() {
result.push(PrimeFactorWithMultiplicity::new(test_num, 1));
} else {
result.iter_mut().find(|x| x.prime == test_num).unwrap().increase_multiplicity();
}

tmp_num /= test_num;

break;
}
}
}

PrimeFactorisedNumberWithMultiplicity {
num,
factors: number_of_divisors(result.clone()),
prime_factors_with_multiplicity: result
}
}
}

fn number_of_divisors(prime_factors_with_multiplicity: Vec<PrimeFactorWithMultiplicity>) -> u64 {
prime_factors_with_multiplicity.iter().map(|x| x.multiplicity + 1).product()
}

fn main() {
println!("{}", (1..)
.map(|x| PrimeFactorisedNumberWithMultiplicity::new(x * (x + 1) / 2 as u64))
.find(|x| x.factors > 500).unwrap().num);
}
204 changes: 204 additions & 0 deletions project_euler_13/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit de4dd7c

Please sign in to comment.