Skip to content

Commit 19f17df

Browse files
m4
1 parent 400d49b commit 19f17df

File tree

3 files changed

+225
-1
lines changed

3 files changed

+225
-1
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod m1;
22
pub mod m2;
33
pub mod m3;
4+
pub mod m4;
45

56
#[cfg(test)]
67
mod tests {

src/m3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn largest_prime_factor_of_the_number_600851475143() -> u64 {
1818
diviser += 1;
1919
} else {
2020
max_factor = n;
21-
n = n / diviser;
21+
n /= diviser;
2222
}
2323
}
2424
max_factor

src/m4.rs

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
/// A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
2+
///
3+
/// Find the largest palindrome made from the product of two 3-digit numbers.
4+
///
5+
/// ```rust
6+
/// use self::project_euler::m4::largest_palindrome_product_of_two_3_digits;
7+
/// assert_eq!(largest_palindrome_product_of_two_3_digits(), 906609);
8+
/// ```
9+
pub fn largest_palindrome_product_of_two_3_digits() -> u32 {
10+
let is_palindrome = |i: u32| -> Option<u32> {
11+
let s = i.to_string();
12+
if s.chars().rev().collect::<String>() == s {
13+
Some(i)
14+
} else {
15+
None
16+
}
17+
};
18+
19+
let mut largest_palindrome = 0;
20+
for x in (100..1000).rev() {
21+
for y in (100..1000).rev() {
22+
if let Some(i) = is_palindrome(x * y) {
23+
if i > largest_palindrome {
24+
largest_palindrome = i
25+
}
26+
}
27+
}
28+
}
29+
largest_palindrome
30+
}
31+
32+
/// A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
33+
///
34+
/// Find the largest palindrome made from the product of two 3-digit numbers.
35+
///
36+
/// ```rust
37+
/// use self::project_euler::m4::largest_palindrome_product_of_two_3_digits_mod_10;
38+
/// assert_eq!(largest_palindrome_product_of_two_3_digits_mod_10(), 906609);
39+
/// ```
40+
pub fn largest_palindrome_product_of_two_3_digits_mod_10() -> u32 {
41+
let is_palindrome = |i: u32| -> Option<u32> {
42+
let mut rev = 0u32;
43+
{
44+
let mut tmp = i;
45+
while tmp > 0 {
46+
// rev = new rev 0-9 + old rev left shift [12 -> 120, 1 -> 10, 132 -> 1320]
47+
rev = tmp % 10 + rev * 10;
48+
tmp /= 10;
49+
}
50+
}
51+
if i == rev {
52+
Some(i)
53+
} else {
54+
None
55+
}
56+
};
57+
58+
let mut largest_palindrome = 0;
59+
for x in (100..1000).rev() {
60+
for y in (100..1000).rev() {
61+
if let Some(i) = is_palindrome(x * y) {
62+
if i > largest_palindrome {
63+
largest_palindrome = i
64+
}
65+
}
66+
}
67+
}
68+
largest_palindrome
69+
}
70+
71+
/// A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
72+
///
73+
/// Find the largest palindrome made from the product of two 3-digit numbers.
74+
///
75+
/// ```rust
76+
/// use self::project_euler::m4::largest_palindrome_product_of_two_3_digits_mod_10_permutation_pair;
77+
/// assert_eq!(largest_palindrome_product_of_two_3_digits_mod_10_permutation_pair(), 906609);
78+
/// ```
79+
pub fn largest_palindrome_product_of_two_3_digits_mod_10_permutation_pair() -> u32 {
80+
let is_palindrome = |i: u32| -> Option<u32> {
81+
let mut rev = 0u32;
82+
{
83+
let mut tmp = i;
84+
while tmp > 0 {
85+
// rev = new rev 0-9 + old rev left shift [12 -> 120, 1 -> 10, 132 -> 1320]
86+
rev = tmp % 10 + rev * 10;
87+
tmp /= 10;
88+
}
89+
}
90+
println!("{} {}", i, rev);
91+
if i == rev {
92+
Some(i)
93+
} else {
94+
None
95+
}
96+
};
97+
98+
let mut largest_palindrome = 0;
99+
// [999 998 997 996]
100+
// [999-100 998-100 997-100 996-100]
101+
for x in (100..1000).rev() {
102+
for y in (100..=x).rev() {
103+
if let Some(i) = is_palindrome(x * y) {
104+
if i > largest_palindrome {
105+
largest_palindrome = i
106+
}
107+
}
108+
}
109+
}
110+
largest_palindrome
111+
}
112+
113+
/// A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
114+
///
115+
/// Find the largest palindrome made from the product of two 3-digit numbers.
116+
///
117+
/// ```rust
118+
/// use self::project_euler::m4::largest_palindrome_product_of_two_3_digits_mod_10_permutation_pair_tail_cut;
119+
/// assert_eq!(largest_palindrome_product_of_two_3_digits_mod_10_permutation_pair_tail_cut(), 906609);
120+
/// ```
121+
pub fn largest_palindrome_product_of_two_3_digits_mod_10_permutation_pair_tail_cut() -> u32 {
122+
let is_palindrome = |i: u32| -> Option<u32> {
123+
let mut rev = 0u32;
124+
{
125+
let mut tmp = i;
126+
while tmp > 0 {
127+
// rev = new rev 0-9 + old rev left shift [12 -> 120, 1 -> 10, 132 -> 1320]
128+
rev = tmp % 10 + rev * 10;
129+
tmp /= 10;
130+
}
131+
}
132+
println!("{} {}", i, rev);
133+
if i == rev {
134+
Some(i)
135+
} else {
136+
None
137+
}
138+
};
139+
140+
let mut largest_palindrome = 0;
141+
// [999 998 997 996]
142+
// [999-100 998-100 997-100 996-100]
143+
// area -> -> -> -> -> | -> [in this sequence it's always smaller than largest_palindrome]
144+
for x in (100..1000).rev() {
145+
for y in (100..=x).rev() {
146+
if x * y <= largest_palindrome {
147+
break;
148+
}
149+
if let Some(i) = is_palindrome(x * y) {
150+
if i > largest_palindrome {
151+
largest_palindrome = i
152+
}
153+
}
154+
}
155+
}
156+
largest_palindrome
157+
}
158+
159+
/// A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
160+
///
161+
/// Find the largest palindrome made from the product of two 3-digit numbers.
162+
///
163+
/// ```rust
164+
/// use self::project_euler::m4::largest_palindrome_product_of_two_3_digits_factorization;
165+
/// assert_eq!(largest_palindrome_product_of_two_3_digits_factorization(), 906609);
166+
/// ```
167+
pub fn largest_palindrome_product_of_two_3_digits_factorization() -> u32 {
168+
let is_palindrome = |i: u32| -> Option<u32> {
169+
let mut rev = 0u32;
170+
{
171+
let mut tmp = i;
172+
while tmp > 0 {
173+
// rev = new rev 0-9 + old rev left shift [12 -> 120, 1 -> 10, 132 -> 1320]
174+
rev = tmp % 10 + rev * 10;
175+
tmp /= 10;
176+
}
177+
}
178+
if i == rev {
179+
Some(i)
180+
} else {
181+
None
182+
}
183+
};
184+
185+
let mut largest_palindrome = 0;
186+
// [999 998 997 996]
187+
// [999-100 998-100 997-100 996-100]
188+
// area -> -> -> -> -> | -> [in this sequence it's always smaller than largest_palindrome]
189+
190+
// n is in 6 digits < 999*999=998001.
191+
// n = xyx_xyx = 100_000x + 10_000y + 1_000z + 100z + 10y + 1x
192+
// = 100_001x + 10_010y + 1_100z
193+
// = 11 * (9091 + 910 + 100)
194+
// n = a * b
195+
// = 11c * b || a * 11c || 11c * 11d
196+
for x in (100..1000).rev() {
197+
if x % 11 == 0 {
198+
for y in (100..=x).rev() {
199+
if x * y <= largest_palindrome {
200+
break;
201+
}
202+
if let Some(i) = is_palindrome(x * y) {
203+
if i > largest_palindrome {
204+
largest_palindrome = i
205+
}
206+
}
207+
}
208+
} else {
209+
// 999 - 9 (999%11) = 990. 989 - 10 (989%11) = 979.
210+
for y in (100..=x - x % 11).rev().step_by(11) {
211+
if x * y <= largest_palindrome {
212+
break;
213+
}
214+
if let Some(i) = is_palindrome(x * y) {
215+
if i > largest_palindrome {
216+
largest_palindrome = i
217+
}
218+
}
219+
}
220+
}
221+
}
222+
largest_palindrome
223+
}

0 commit comments

Comments
 (0)