-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_euler_206.rb
43 lines (40 loc) · 1.34 KB
/
project_euler_206.rb
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
# Concealed Square
# Problem 206
# Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0,
# where each “_” is a single digit.
min = 1020304050607080900
max = 1929394959697989990
i = Math.sqrt(min).to_i
loop do
# i should be ...0 to match ...0 square
if i % 10 != 0
i += 1
next
end
current_try = i * i
current_try_s = current_try.to_s
case current_try_s
when /^\d{2}3\d{16}/
# 103xxxxxxxxxxxxxxxx, 113xxxxxxxxxxxxxxxx, ... -> 112xxxxxxxxxxxxxxxx, 122xxxxxxxxxxxxxxxx, ...
increment = current_try_s[0..1].to_i + 1
i = Math.sqrt("#{increment}20304050607080900".to_i).to_i
when /^\d{4}4\d{14}/
# 10204xxxxxxxxxxxxxx, 10214xxxxxxxxxxxxxx, ... -> 10213xxxxxxxxxxxxxx, 10223xxxxxxxxxxxxxx, ...
increment = current_try_s[0..3].to_i + 1
i = Math.sqrt("#{increment}304050607080900".to_i).to_i
when /^\d{6}5\d{12}/
# 1020305xxxxxxxxxxxx, 1020315xxxxxxxxxxxx, ... -> 1020314xxxxxxxxxxxx, 1020324xxxxxxxxxxxx, ...
increment = current_try_s[0..5].to_i + 1
i = Math.sqrt("#{increment}4050607080900".to_i).to_i
when /^\d{8}6\d{10}/
# etc
increment = current_try_s[0..7].to_i + 1
i = Math.sqrt("#{increment}50607080900".to_i).to_i
when /^1\d2\d3\d4\d5\d6\d7\d8\d9\d0/
puts "#{i} * #{i} = #{current_try}"
break
else
i += 1
end
break if current_try > max
end