-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem28.java
50 lines (43 loc) · 1.12 KB
/
Problem28.java
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
package com.javamultiplex.projecteuler;
public class Problem28 {
public static void main(String[] args) {
int size = 1001;
int[][] spiral = new int[size][size];
int value = size * size;
int limit = 0, k = 0, sum = 0;
limit = size / 2;
spiral[limit][limit] = 1;
for (int i = 0; i < limit; i++) {
//Right->Left
for (int j = size - 1 - i; j > i; j--) {
spiral[i][j] = value--;
}
//Top->Bottom
for (int j = i; j < (size - 1 - i); j++) {
spiral[j][i] = value--;
}
k = size - 1 - i;
//Left->Right
for (int j = i; j < (size - 1 - i); j++) {
spiral[k][j] = value--;
}
//Bottom->Top
for (int j = size - 1 - i; j > i; j--) {
spiral[j][k] = value--;
}
}
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == j) // For left side diagonal
{
sum += spiral[i][j];
} else if (i + j == (size - 1)) // For left side diagonal
{
sum += spiral[i][j];
}
}
}
System.out.println("The sum of the numbers on the diagonals in a 1001 by 1001 spiral is : "
+ sum);
}
}