-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'euler/main' into tmp-euler
- Loading branch information
Showing
72 changed files
with
5,404 additions
and
0 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,10 @@ | ||
[workspace] | ||
resolver = "2" | ||
|
||
members = [ | ||
"rust/euler-*", | ||
] | ||
|
||
exclude = [ | ||
"rust/deprecated", | ||
] |
Large diffs are not rendered by default.
Oops, something went wrong.
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,8 @@ | ||
|
||
# About | ||
This crate is source code of solutions in [project euler][euler]. | ||
|
||
![Badge][badge] | ||
|
||
[euler]: https://projecteuler.org | ||
[badge]: https://projecteuler.net/profile/xushaohua.png |
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,2 @@ | ||
[toolchain] | ||
channel = "nightly" |
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,94 @@ | ||
// Copyright (c) 2020 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
#![feature(test)] | ||
extern crate euler; | ||
extern crate test; | ||
|
||
use euler::primes::get_prime_list; | ||
|
||
/// Problem: | ||
/// | ||
/// The prime factors of 13195 are 5, 7, 13 and 29. | ||
/// What is the largest prime factor of the number 600851475143 ? | ||
const LARGEST_PRIME: u64 = 600851475143; | ||
|
||
fn method1(num: u64) -> u64 { | ||
// Largest possible prime factor of an integer is its square root. | ||
let sqrt: usize = (num as f64).sqrt().ceil() as usize; | ||
|
||
// Now get prime list smaller than square root. | ||
let prime_list = get_prime_list(sqrt); | ||
for prime in prime_list.into_iter().rev() { | ||
if num % (prime as u64) == 0 { | ||
return prime as u64; | ||
} | ||
} | ||
|
||
0 | ||
} | ||
|
||
fn method2(num: u64) -> u64 { | ||
for i in 2..=num { | ||
if num % i == 0 { | ||
return if num == i { num } else { method2(num / i) }; | ||
} | ||
} | ||
0 | ||
} | ||
|
||
fn method3(mut num: u64) -> u64 { | ||
let mut i = 2; | ||
while i < num { | ||
if num % i == 0 { | ||
num /= i; | ||
} | ||
i += 1; | ||
} | ||
i | ||
} | ||
|
||
fn method4(mut num: u64) -> u64 { | ||
let mut i = 2; | ||
while i <= num { | ||
if num % i == 0 { | ||
num /= i; | ||
} else { | ||
if i == 2 { | ||
i += 1; | ||
} else { | ||
i += 2; | ||
} | ||
} | ||
} | ||
i | ||
} | ||
|
||
fn main() { | ||
println!("method1: {}", method1(LARGEST_PRIME)); | ||
println!("method2: {}", method2(LARGEST_PRIME)); | ||
println!("method3: {}", method3(LARGEST_PRIME)); | ||
println!("method4: {}", method4(LARGEST_PRIME)); | ||
} | ||
|
||
#[bench] | ||
fn bench_method1(b: &mut test::Bencher) { | ||
b.iter(|| assert_eq!(method1(LARGEST_PRIME), 6857)); | ||
} | ||
|
||
#[bench] | ||
fn bench_method2(b: &mut test::Bencher) { | ||
b.iter(|| assert_eq!(method2(LARGEST_PRIME), 6857)); | ||
} | ||
|
||
#[bench] | ||
fn bench_method3(b: &mut test::Bencher) { | ||
b.iter(|| assert_eq!(method3(LARGEST_PRIME), 6857)); | ||
} | ||
|
||
#[bench] | ||
fn bench_method4(b: &mut test::Bencher) { | ||
b.iter(|| assert_eq!(method4(LARGEST_PRIME), 6857)); | ||
} |
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,49 @@ | ||
// Copyright (c) 2020 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
#![feature(test)] | ||
extern crate test; | ||
|
||
use std::cmp::max; | ||
|
||
use euler::primes::{get_prime_factors, get_prime_list, PrimeFactor}; | ||
|
||
/// Problem: | ||
/// | ||
/// 2520 is the smallest number that can be divided by each of the numbers from | ||
/// 1 to 10 without any remainder. What is the smallest positive number | ||
/// that is evenly divisible by all of the numbers from 1 to 20? | ||
fn method1(max_num: usize) -> usize { | ||
let ls = get_prime_list(max_num); | ||
let mut minimum_factors = Vec::with_capacity(ls.len()); | ||
for factor in &ls { | ||
minimum_factors.push(PrimeFactor { | ||
num: *factor, | ||
count: 0, | ||
}); | ||
} | ||
|
||
for i in 2..=max_num { | ||
let factors = get_prime_factors(i, &ls); | ||
for factor in &factors { | ||
for m in &mut minimum_factors { | ||
if m.num == factor.num { | ||
m.count = max(m.count, factor.count); | ||
} | ||
} | ||
} | ||
} | ||
|
||
minimum_factors.iter().fold(1, |p, f| p * f.num.pow(f.count as u32)) | ||
} | ||
|
||
fn main() { | ||
println!("method1 {}", method1(20)); | ||
} | ||
|
||
#[bench] | ||
fn bench_method1(b: &mut test::Bencher) { | ||
b.iter(|| assert_eq!(method1(20), 232792560)); | ||
} |
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 @@ | ||
// Copyright (c) 2020 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
#![feature(test)] | ||
extern crate test; | ||
|
||
/// Problem: | ||
/// | ||
/// The sum of the squares of the first ten natural numbers is, | ||
/// | ||
/// 1^2 + 2^2 + ... + 10^2 = 385 | ||
/// | ||
/// The square of the sum of the first ten natural numbers is, | ||
/// (1 + 2 + ... + 10)^2 = 55^2 = 3025 | ||
/// | ||
/// Hence the difference between the sum of the squares of the first | ||
/// ten natural numbers and the square of the sum is 3025−385=2640 . | ||
/// | ||
/// Find the difference between the sum of the squares of the first | ||
/// one hundred natural numbers and the square of the sum. | ||
fn method1(max_num: i64) -> i64 { | ||
let mut square_sum = 0; | ||
for i in 1..=max_num { | ||
square_sum += i * i; | ||
} | ||
|
||
let mut sum = 0; | ||
for i in 1..=max_num { | ||
sum += i; | ||
} | ||
sum * sum - square_sum | ||
} | ||
|
||
fn method2(max_num: i64) -> i64 { | ||
let square_sum: i64 = (1..=max_num).map(|i| i * i).sum(); | ||
let sum: i64 = (1..=max_num).sum(); | ||
sum * sum - square_sum | ||
} | ||
|
||
fn main() { | ||
let max_num = 100; | ||
println!("result: {}", method1(max_num)); | ||
println!("result: {}", method2(max_num)); | ||
} | ||
|
||
#[bench] | ||
fn bench_method1(b: &mut test::Bencher) { | ||
b.iter(|| assert_eq!(method1(100), 25164150)); | ||
} | ||
|
||
#[bench] | ||
fn bench_method2(b: &mut test::Bencher) { | ||
b.iter(|| assert_eq!(method2(100), 25164150)); | ||
} |
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,51 @@ | ||
// Copyright (c) 2020 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
#![feature(test)] | ||
extern crate test; | ||
|
||
use euler::primes::{get_prime_list, IsPrime}; | ||
|
||
/// Problem: | ||
/// | ||
/// By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see | ||
/// that the 6th prime is 13. | ||
/// | ||
/// What is the 10 001st prime number? | ||
fn method1(nth_prime: u32) -> usize { | ||
let prime_list = get_prime_list(110_000); | ||
prime_list[nth_prime as usize - 1] | ||
} | ||
|
||
fn method2(nth_prime: u32) -> usize { | ||
let mut primes = Vec::with_capacity(nth_prime as usize); | ||
primes.push(2); | ||
let mut num = 3; | ||
while primes.len() < nth_prime as usize { | ||
if num.is_prime() { | ||
primes.push(num); | ||
} | ||
|
||
num += 2; | ||
} | ||
|
||
primes[primes.len() - 1] | ||
} | ||
|
||
fn main() { | ||
let nth_prime = 10001; | ||
println!("#10001 prime is: {}", method1(nth_prime)); | ||
println!("#10001 prime is: {}", method2(nth_prime)); | ||
} | ||
|
||
#[bench] | ||
fn bench_method1(b: &mut test::Bencher) { | ||
b.iter(|| assert_eq!(method1(10001), 104743)); | ||
} | ||
|
||
#[bench] | ||
fn bench_method2(b: &mut test::Bencher) { | ||
b.iter(|| assert_eq!(method2(10001), 104743)); | ||
} |
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,98 @@ | ||
// Copyright (c) 2020 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
#![feature(test)] | ||
extern crate test; | ||
|
||
/// Problem: | ||
/// | ||
/// The four adjacent digits in the 1000-digit number that have | ||
/// the greatest product are 9 × 9 × 8 × 9 = 5832. | ||
/// | ||
/// 73167176531330624919225119674426574742355349194934 | ||
/// 96983520312774506326239578318016984801869478851843 | ||
/// 85861560789112949495459501737958331952853208805511 | ||
/// 12540698747158523863050715693290963295227443043557 | ||
/// 66896648950445244523161731856403098711121722383113 | ||
/// 62229893423380308135336276614282806444486645238749 | ||
/// 30358907296290491560440772390713810515859307960866 | ||
/// 70172427121883998797908792274921901699720888093776 | ||
/// 65727333001053367881220235421809751254540594752243 | ||
/// 52584907711670556013604839586446706324415722155397 | ||
/// 53697817977846174064955149290862569321978468622482 | ||
/// 83972241375657056057490261407972968652414535100474 | ||
/// 82166370484403199890008895243450658541227588666881 | ||
/// 16427171479924442928230863465674813919123162824586 | ||
/// 17866458359124566529476545682848912883142607690042 | ||
/// 24219022671055626321111109370544217506941658960408 | ||
/// 07198403850962455444362981230987879927244284909188 | ||
/// 84580156166097919133875499200524063689912560717606 | ||
/// 05886116467109405077541002256983155200055935729725 | ||
/// 71636269561882670428252483600823257530420752963450 | ||
/// | ||
/// Find the thirteen adjacent digits in the 1000-digit number | ||
/// that have the greatest product. What is the value of this product? | ||
const NUMS: &str = " | ||
73167176531330624919225119674426574742355349194934 | ||
96983520312774506326239578318016984801869478851843 | ||
85861560789112949495459501737958331952853208805511 | ||
12540698747158523863050715693290963295227443043557 | ||
66896648950445244523161731856403098711121722383113 | ||
62229893423380308135336276614282806444486645238749 | ||
30358907296290491560440772390713810515859307960866 | ||
70172427121883998797908792274921901699720888093776 | ||
65727333001053367881220235421809751254540594752243 | ||
52584907711670556013604839586446706324415722155397 | ||
53697817977846174064955149290862569321978468622482 | ||
83972241375657056057490261407972968652414535100474 | ||
82166370484403199890008895243450658541227588666881 | ||
16427171479924442928230863465674813919123162824586 | ||
17866458359124566529476545682848912883142607690042 | ||
24219022671055626321111109370544217506941658960408 | ||
07198403850962455444362981230987879927244284909188 | ||
84580156166097919133875499200524063689912560717606 | ||
05886116467109405077541002256983155200055935729725 | ||
71636269561882670428252483600823257530420752963450 | ||
"; | ||
|
||
fn method1(nums: &[u8; 1000]) -> u64 { | ||
let mut product: u64; | ||
let last_pos: usize = 1000 - 13; | ||
let mut largest_product: u64 = 1; | ||
for i in 0..last_pos { | ||
product = 1; | ||
for num in nums.iter().skip(i).take(13) { | ||
product *= *num as u64; | ||
if product > largest_product { | ||
largest_product = product; | ||
} | ||
} | ||
} | ||
largest_product | ||
} | ||
|
||
fn get_nums() -> [u8; 1000] { | ||
let mut nums = [0_u8; 1000]; | ||
let mut i = 0; | ||
for c in NUMS.bytes() { | ||
if c >= b'0' && c <= b'9' { | ||
let num: u8 = c - b'0'; | ||
nums[i] = num; | ||
i += 1; | ||
} | ||
} | ||
nums | ||
} | ||
|
||
fn main() { | ||
let nums = get_nums(); | ||
println!("method1: {}", method1(&nums)); | ||
} | ||
|
||
#[bench] | ||
fn bench_method1(b: &mut test::Bencher) { | ||
let nums = get_nums(); | ||
b.iter(|| assert_eq!(method1(&nums), 23514624000)); | ||
} |
Oops, something went wrong.