summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: b37ca0bb1ae67b21e9f115842ff1a6df1fa0b505 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// LICENSE: WTFPL
use num_bigint::BigUint;
use num_integer::{Integer, Roots};
use std::cmp::min;
use std::ffi::OsString;
use std::process::ExitCode;

// bnum seems to be slightly faster than ruint,
// and 3x faster than uint.
type UInt = bnum::types::U256;

fn is_square(n: UInt) -> bool {
	let sq = n.sqrt();
	sq * sq == n
}

fn is_choose2(mut n: UInt) -> bool {
	n <<= 3;
	n.inc();
	is_square(n)
}

fn find_choose_r(mut k: UInt, r: u32) -> Option<UInt> {
	// multiply k by r! to avoid future divisions
	for i in 1..=r {
		k *= UInt::from(i);
	}
	let k = k; // no morge changing k
	let log = k.ilog2();
	let mut hi = UInt::from(1u8) << (log / r + 2);
	let mut lo = UInt::from(r + 1);
	'outer: while lo < hi {
		let mid: UInt = (lo + hi) >> 1;
		let mut i = mid;
		let mut mid_falling_r = mid;
		for _ in 1..r {
			i.dec();
			mid_falling_r *= i;
			if mid_falling_r > k {
				hi = mid;
				continue 'outer;
			}
		}
		if mid_falling_r < k {
			lo = mid;
			lo.inc();
		} else {
			return Some(mid);
		}
	}
	None
}

fn is_choose_r(k: UInt, r: u32) -> bool {
	find_choose_r(k, r).is_some()
}

fn superscript(number: &str) -> String {
	number
		.chars()
		.map(|c| match c {
			'0' => '⁰',
			'1' => '¹',
			'2' => '²',
			'3' => '³',
			'4' => '⁴',
			'5' => '⁵',
			'6' => '⁶',
			'7' => '⁷',
			'8' => '⁸',
			'9' => '⁹',
			_ => c,
		})
		.collect()
}

#[derive(Clone, Copy)]
struct PascalEntry {
	// row choose col mod 2^64
	value: u64,
	row_lo: u16,
	row_hi: u32,
	col: u16,
}

impl PascalEntry {
	fn new(value: u64, row: u64, col: u16) -> Self {
		assert!(row < (1 << 48));
		Self {
			value,
			row_lo: row as _,
			row_hi: (row >> 16) as _,
			col,
		}
	}
	fn row(self) -> u64 {
		u64::from(self.row_lo) | u64::from(self.row_hi) << 16
	}
	fn full_value(self) -> BigUint {
		let row: u64 = self.row();
		let col: u64 = self.col.into();
		let mut value = BigUint::from(1u8);
		for r in row - col + 1..=row {
			value *= BigUint::from(r);
		}
		for r in 1..=col {
			value /= BigUint::from(r);
		}
		value
	}
}

fn find_duplicates_in(entries: &mut [PascalEntry]) {
	entries.sort_by_key(|x| x.value);
	for e in entries.chunk_by(|a, b| a.value == b.value) {
		if e.len() == 1 {
			continue;
		}
		for i in 0..e.len() {
			for j in i + 1..e.len() {
				if e[i].full_value() != e[j].full_value() {
					continue;
				}
				println!(
					"({} choose {}) = {} = ({} choose {})",
					e[i].row(),
					e[i].col,
					e[i].full_value(),
					e[j].row(),
					e[j].col
				);
			}
		}
	}
}

fn search_entry_limit(power_of_10: usize) {
	let mut pascal_row = [UInt::from(0u8); 500];
	let mut range = pascal_row.len();
	println!(
		"searching up to 10{}",
		superscript(&format!("{power_of_10}"))
	);
	let limit = UInt::from(10u8).pow(power_of_10 as u32);
	let mut entries: Vec<PascalEntry> = vec![];
	pascal_row[0] = UInt::from(1u8);
	for row in 1u64.. {
		for col in (1..range).rev() {
			pascal_row[col] += pascal_row[col - 1];
			if col > 4 && col as u64 <= row / 2 {
				if is_choose_r(pascal_row[col], 2)
					|| is_choose_r(pascal_row[col], 3)
					|| is_choose_r(pascal_row[col], 4)
				{
					println!("FOUND DUPLICATE {}", pascal_row[col]);
				}
				entries.push(PascalEntry::new(
					pascal_row[col].digits()[0],
					row,
					col.try_into().expect("needs redesign: col > 65535"),
				));
			}
			if pascal_row[col] > limit {
				range = col;
				if col < 10 {
					println!("n choose {col} exceeds limit at row {row}");
				}
			}
		}
		if range <= 5 {
			break;
		}
	}
	println!(
		"memory needed = {}MiB",
		(entries.len() * size_of::<PascalEntry>()) >> 20
	);
	find_duplicates_in(&mut entries);
}

fn search_row_limit(row_limit: u32) {
	println!("Searching up to row {row_limit}");
	let row_limit = u64::from(row_limit);
	let mut pascal_row = vec![0u64; row_limit as usize / 2 + 1];
	pascal_row[0] = 1;
	let mut entries: Vec<PascalEntry> = vec![];
	for row in 0..row_limit {
		if row > 0 && row % 2 == 0 {
			pascal_row[row as usize / 2] = pascal_row[row as usize / 2 - 1].wrapping_mul(2);
			entries.push(PascalEntry::new(
				pascal_row[row as usize / 2],
				row,
				(row / 2) as u16,
			));
		}
		for col in (2..=(std::cmp::max(3, row) - 1) / 2).rev() {
			pascal_row[col as usize] =
				pascal_row[col as usize].wrapping_add(pascal_row[col as usize - 1]);
			entries.push(PascalEntry::new(pascal_row[col as usize], row, col as u16));
		}
		pascal_row[1] = row;
	}
	println!(
		"memory needed = {}MiB",
		(entries.len() * size_of::<PascalEntry>()) >> 20
	);
	find_duplicates_in(&mut entries);
}

fn search_col_limit(col_limit: u16) {
	let mut pascal_row = vec![UInt::from(1u8), UInt::from(4u8), UInt::from(6u8)];
	let mut range = usize::MAX;
	let mut cutoff = UInt::MAX >> (2 * col_limit + 1);
	for i in 2..=col_limit {
		cutoff /= UInt::from(i);
	}
	println!("entry cutoff > 10^{}", cutoff.ilog10());
	for row in 5usize..usize::MAX {
		if row % (1 << 15) == 0 {
			println!("row = {row}");
		}
		for col in (2..min(row / 2 + 1, range)).rev() {
			if 2 * col == row {
				pascal_row.push(pascal_row[row / 2 - 1] * UInt::from(2u8));
			} else if col == 2 {
				pascal_row[col] += UInt::from(row - 1);
			} else {
				let prev = pascal_row[col - 1];
				pascal_row[col] += prev;
			}
			if pascal_row[col] > cutoff {
				// getting dicey
				println!("abandoning column {col}");
				range = col;
				continue;
			}
			if col <= 4 {
				// we already know the solutions with both columns ≤ 4
				continue;
			}
			if is_choose2(pascal_row[col]) {
				println!("({row} choose {col}) = (something choose 2)");
			}
			for col2 in 3..min(col, col_limit as usize + 1) {
				if let Some(x) = find_choose_r(pascal_row[col], col2 as u32) {
					println!("({row} choose {col}) = ({x} choose {col2})");
				}
			}
		}
		if range <= 2 {
			println!("can't go any further without increasing size of UInt");
			return;
		}
	}
}

fn main() -> ExitCode {
	let args: Vec<OsString> = std::env::args_os().collect();
	if args.len() < 2 {
		eprintln!(" Usage: pascal entry-limit <p>  — search all entries up to 10^p");
		eprintln!("        pascal row-limit <n>    — search all entries up to the nth row");
		return ExitCode::FAILURE;
	}
	match args[1].as_os_str().to_str() {
		Some("entry-limit") => {
			let power_of_10: Option<usize> = match args.get(2) {
				Some(s) => s.clone().into_string().ok().and_then(|x| x.parse().ok()),
				None => Some(35),
			};
			let Some(power_of_10) = power_of_10 else {
				eprintln!("Argument must be a nonnegative integer");
				return ExitCode::FAILURE;
			};
			if power_of_10 > usize::MAX / 4
				|| (power_of_10 as f64 * f64::log2(10.0)) as usize + 10 > size_of::<UInt>() * 8
			{
				eprintln!("Power of 10 is too large for integer type. You will have to increase the size of UInt in the source code.");
				return ExitCode::FAILURE;
			}
			search_entry_limit(power_of_10);
		}
		Some("row-limit") => {
			let row_limit: Option<u32> = match args.get(2) {
				Some(s) => s.clone().into_string().ok().and_then(|x| x.parse().ok()),
				None => Some(1000),
			};
			let Some(row_limit) = row_limit else {
				eprintln!("Argument must be a nonnegative integer");
				return ExitCode::FAILURE;
			};
			if row_limit > 0xffff * 2 {
				eprintln!("row limit too large (need to change PascalEntry type)");
				return ExitCode::FAILURE;
			}
			search_row_limit(row_limit);
		}
		Some("col-limit") => {
			let col_limit: Option<u16> = match args.get(2) {
				Some(s) => s.clone().into_string().ok().and_then(|x| x.parse().ok()),
				None => Some(30),
			};
			let Some(col_limit) = col_limit else {
				eprintln!("column limit must be a nonnegative integer < 65536");
				return ExitCode::FAILURE;
			};
			search_col_limit(col_limit);
		}
		_ => {
			eprintln!("Bad command: {:?}", args[1]);
		}
	}
	ExitCode::SUCCESS
}