-
Notifications
You must be signed in to change notification settings - Fork 0
/
p8_Largest_product_in_a_series.java
58 lines (52 loc) · 1.27 KB
/
p8_Largest_product_in_a_series.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
import java.io.*;
public class p8_Largest_product_in_a_series {
public int product(int n) throws FileNotFoundException
{
String s = "";
try
{
FileReader file = new FileReader("C:\\ZIN\\Euler\\src\\p8.txt");
BufferedReader br = new BufferedReader(file);
String scan;
while((scan = br.readLine()) != null)
{
s = s+scan;
//System.out.println(scan);
}
br.close();
}
catch(IOException e)
{
System.out.println(e.toString());
}
int l = s.length();
int maxProd = 0;
int curProd;
for (int i=0;i<l-4;i++)
{
curProd = (int)(s.charAt(i))-48;
for (int j=i+1;j<i+n;j++)
{
curProd = curProd*((int)(s.charAt(j))-48);
}
if (maxProd < curProd)
{
maxProd = curProd;
}
}
return maxProd;
}
/**
* @param args
*/
public static void main(String[] args) {
p8_Largest_product_in_a_series test = new p8_Largest_product_in_a_series();
try {
System.out.println(test.product(5));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
}
}