-
Notifications
You must be signed in to change notification settings - Fork 0
/
square_basher.rb
executable file
·88 lines (66 loc) · 2.22 KB
/
square_basher.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
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
#!/usr/bin/env ruby -w
class SquareBasher
FIRST_SET_COUNT = 2500
SECOND_SET_COUNT = 2499
def solve
min_start = 0
max_start = 100000000
smallest_number = find_matching_totals(min_start, max_start)
if smallest_number != -1
puts "Found matching totals!"
puts "Smallest number is #{smallest_number}"
else
puts "Didn't find matching totals."
end
end
def find_matching_totals(min_start_number, max_start_number)
if max_start_number <= min_start_number
return -1
end
puts "Checking #{min_start_number} and #{max_start_number}"
diff_for_min_start_number = calculate_set_diffs(min_start_number)
diff_for_max_start_number = calculate_set_diffs(max_start_number)
if diff_for_min_start_number == 0
return min_start_number
elsif diff_for_max_start_number == 0
return max_start_number
elsif is_zero_between_numbers(diff_for_min_start_number, diff_for_max_start_number)
middle_start_number = ((max_start_number - min_start_number) / 2) + min_start_number
lower_half_result = find_matching_totals(min_start_number + 1, middle_start_number)
if lower_half_result != -1
return lower_half_result
end
upper_half_result = find_matching_totals(middle_start_number + 1, max_start_number - 1)
if upper_half_result != -1
return upper_half_result
end
return -1
else
return -1
end
end
def calculate_set_diffs(start_number)
first_set_total = calculate_squared_total(start_number, FIRST_SET_COUNT)
second_set_total = calculate_squared_total(start_number + FIRST_SET_COUNT + 1, SECOND_SET_COUNT)
return second_set_total - first_set_total
end
def calculate_squared_total(start_number, count)
end_number = start_number + count
total = 0
(start_number..end_number).each { |i|
total += i*i
}
return total
end
def is_zero_between_numbers(first_number, second_number)
if first_number < second_number
return first_number <= 0 && 0 <= second_number
elsif second_number < first_number
return second_number <= 0 && 0 <= first_number
else
return false
end
end
end
solver = SquareBasher.new
solver.solve()