-
Notifications
You must be signed in to change notification settings - Fork 0
/
136.rb
136 lines (57 loc) · 1.52 KB
/
136.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
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
=begin
Singleton difference
Problem 136
The positive integers, x, y, and z, are consecutive terms of an arithmetic progression.
Given that n is a positive integer, the equation, x2 − y2 − z2 = n, has exactly one solution when n = 20:
13**2 − 10**2 − 7**2 = 20
In fact there are twenty-five values of n below one hundred for which the equation has a unique solution.
How many values of n less than fifty million have exactly one solution?
x**2 - y**2 - z**2 = n
(x+y)(x-y) = n - z^2
y = (x-a)
x**2 - (x-a)**2 - (x-2a)**2 = n
(x - a)(x - 5a)
x**2 -5ax - -ax + 5a**2 = -n
x**2 - 6ax + 5a**2 = -n
x^2 - 6ax + 5a^2 = -n
a = 1
b = -6a
c = (5a^2 + n)
(6a + sqrt(36a**2 - 4(5a^2 + n))) / 2
6a + sqrt(36a**2 - 20a**2 + 4n)
6a + sqrt(16a**2 + 4n)
6a + sqrt(4(4a^2 +n))
6a + 2*sqrt(4a^2 + n) / 2
===> X = 3a +- sqrt(4a^2 + n) <===
So for given N, how many values of 4a^2 + n are perfect...
4a^2 - n = y^2
4a^2 - y^2 = n
===== NEW THOUGHTS ====
x**2 - y**2 - z**2 = n
z = y + ( x - y)
x**2 - y**2 - z**2 = n
z = y - (x-y)
x**2 - y**2 = n - x^2 + 4xy - 4y^2
n = 4xy - 5y^2
n = y*(4*x - 5*y)
x = 10
y > (10 - y)
y > 5
y > (11 - y)
=end
require 'pp'
@total = 0
@sols = Hash.new(0)
1.upto(10_000) do |x|
fourx = 4*x
((x/2.0).ceil).upto(x) do |y|
break if fourx < 5*y
next unless y > (x-y)
s = y*(fourx - 5*y)
@total += 1 if @sols[s] == 0 and s < 50_000_000
@total -= 1 if @sols[s] == 1 and s < 50_000_000
@sols [s] += 1
end
end
puts @total
exit