-
Notifications
You must be signed in to change notification settings - Fork 0
/
P11.m
96 lines (94 loc) · 3.92 KB
/
P11.m
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
%%Project Euler - Problem 1%%
clc;
clear all;
close all;
%% USE THIS TO CREATE A RANDOM ARRAY AND OUTPUT TO FILE
% x = 1:1:5;
% y = [x;rand(1,5)];
% fileID = fopen('nums2.txt','w');
% fprintf(fileID,'%d %4.4f\n',y);
% fclose(fileID);
%%INPUT ARRAY FROM FILE
type P11.txt
fileID = fopen('P11.txt','r');
formatSpec = '%d ';
sizeA = [20 20];
A = fscanf(fileID,formatSpec,sizeA);
fclose(fileID);
A=A'
MAXPROD=0;
TEMP_RIGHT=0;TEMP_LEFT=0;TEMP_UP=0;TEMP_DOWN=0;
DIAG_UP_R=0;DIAG_UP_L=0;DIAG_DOWN_R=0;DIAG_DOWN_L=0;
BREAK=1;
COUNT=0;
LIST=zeros(8,1);
for i=1:20 %%i is in direction of rows
for j=1:20 %%j is in direction of columns
if j<=3 & i<=3 %%check DOWN, RIGHT, DIAG_DOWN_R
TEMP_RIGHT=A(i,j)*A(i,j+1)*A(i,j+2)*A(i,j+3);
TEMP_DOWN=A(i,j)*A(i+1,j)*A(i+2,j)*A(i+3,j);
DIAG_DOWN_R=A(i,j)*A(i+1,j+1)*A(i+2,j+2)*A(i+3,j+3);
end
if j>=18 & i<=3 %%check DOWN, LEFT, DIAG_DOWN_L
TEMP_LEFT=A(i,j)*A(i,j-1)*A(i,j-2)*A(i,j-3);
TEMP_DOWN=A(i,j)*A(i+1,j)*A(i+2,j)*A(i+3,j);
DIAG_DOWN_L=A(i,j)*A(i+1,j-1)*A(i+2,j-2)*A(i+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