-
Notifications
You must be signed in to change notification settings - Fork 4
/
p028.java
60 lines (48 loc) · 1.33 KB
/
p028.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
51
52
53
54
55
56
57
58
59
60
/**
*
* @author gouravrusiya
*
*/
public class p028 {
public static void main(String[] args) {
// TODO Auto-generated method stub
long startTime = System.currentTimeMillis();
long sum1=0L; // 1,9,25,49............are square of 1,3,5,7.........
long sum2=1L; // 1,3,13,31............pattern from 3rd term is [2*(N) + previousSum] where n = 1 for 1st term & then n=n+4
long sum3=1L; // 1,5,17,37............pattern from 3rd term is [2*(M) + previousSum] where n = 1 for 1st term & then n=n+4
long sum4=1L; // 1,7,21,43............pattern from 3rd term is [2*(P) + previousSum] where n = 1 for 1st term & then n=n+4
long n = 1L;
long m = 2L;
long p = 3L;
int count = 0;
for(int i=1;i<=1001;i++)
{
if((i%2)!=0){
sum1 += (i*i);
count++;
}
}
//System.out.println(count);
sum2 = (2L*n) + sum2;
sum1 += sum2;
sum3 = (2L*m) + sum3;
sum1 += sum3;
sum4 = (2L*p) + sum4;
sum1 += sum4;
for(int i=1;i<(count-1);i++){
n = (n+4L);
sum2 = (2L*n) + sum2;
sum1 += sum2;
m = (m+4L);
sum3 = (2L*m) + sum3;
sum1 += sum3;
p = (p+4L);
sum4 = (2L*p) + sum4;
sum1 += sum4;
//System.out.println(sum1);
}
System.out.println(sum1);
long endTime = System.currentTimeMillis();
System.out.println("Time = "+(endTime-startTime)+" ms");
}
}