- 2011-05-12 (木) 0:15
- Project Euler
Project EulerのProblem99に挑戦しました。
Comparing two numbers written in index form like 2^11 and 3^7 is not difficult, as any calculator would confirm that 2^11 = 2048 3^7 = 2187.
However, confirming that 632382^518061 519432^525806 would be much more difficult, as both numbers contain over three million digits.
Using base_exp.txt (right click and ‘Save Link/Target As…’), a 22K text file containing one thousand lines with a base/exponent pair on each line, determine which line number has the greatest numerical value.
NOTE: The first two lines in the file represent the numbers in the example given above.
(日本語訳)
常用対数で比較すると、あっけなく正解になりました。
あっけなさすぎて、本当にこれで良いのか、ちょっと不安が・・・。
PEProblem099.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class PEProblem099 {
public static void main(String[] args){
int[] max=new int[]{1,1};
int index=0;
try{
int[][] v=getList();
for(int i=0;i < v.length;i++){
double v0=max[1]*Math.log10(max[0]);
double v1=v[i][1]*Math.log10(v[i][0]);
if(v1 > v0){
max=v[i];
index=i;
}
}
System.out.println(index+1);
}catch(IOException e){
e.printStackTrace();
}
}
private static int[][] getList() throws IOException{
URL url=new URL("http://projecteuler.net/project/base_exp.txt");
BufferedReader r=
new BufferedReader(new InputStreamReader(url.openStream()));
String line=null;
List<int[]> list=new ArrayList<int[]>();
while((line=r.readLine())!=null){
String[] s=line.split(",");
int[] v=new int[2];
v[0]=Integer.parseInt(s[0]);
v[1]=Integer.parseInt(s[1]);
list.add(v);
}
return list.toArray(new int[list.size()][]);
}
}
Related posts:
Comments:0
Trackbacks:0
- Trackback URL for this entry
- http://termat.sakura.ne.jp/projecteuler/project-euler-problem99%e3%80%80projecteuler/trackback/?_wpnonce=33d46e4fb0
- Listed below are links to weblogs that reference
- Project Euler Problem99 #ProjectEuler from TM's Workspace
