-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem044.m
More file actions
38 lines (36 loc) · 1.45 KB
/
problem044.m
File metadata and controls
38 lines (36 loc) · 1.45 KB
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
function X = problem044
%Pentagonal numbers are generated by the formula, P_(n)=n(3n?1)/2.
%The first ten pentagonal numbers are:
% 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
% It can be seen that P_(4) + P_(7) = 22 + 70 = 92 = P_(8).
%However, their difference, 70 ? 22 = 48, is not pentagonal.
%Find the pair of pentagonal numbers, P_(j) and P_(k), for which
%their sum and difference is pentagonal and D = |P_(k) ? P_(j)| is minimised; what is the value of D?
%The fours pantagonal nos we are looking for are b-a , a , b , a +b in the
%same sequence.
%We will fix the value of b-a ( symbolically b_a) to some pantagonal no
% and then try to find a, such that b_a + a is pentagonal. If so we will
% check whether b + a is pentagonal or not. Considering the fact that the
% difference between P(n) and P(n+1) is 3n+1
m = 1;n = 3000;%hoping that all four pairs will in in first 1000 terms
while m <=n
b_a = 1/2*m*(3*m-1);
for i = 1:n%not from m+1 to n bcos b-a can be greater then a :)
a = i*(3*i-1)/2;
b = b_a+a;
p = (1+sqrt(24*b+1))/6;%Check for pantagonal no
%c = 0;
if floor(p) == p
c = a+b;
q = (1+sqrt(24*c+1))/6;%Check for pantagonal no
if floor(q)== q
X = [ b_a,a,b,a+b];
disp('we are done')
return%keyboard
end
end
%[b_a , a , b]
%keyboard
end
m = m +1;
end