Created
March 30, 2009 13:34
-
-
Save Echos/87790 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pi; | |
import java.math.BigInteger; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
public class Pi090314 { | |
private static final BigInteger B0 = new BigInteger("0"); | |
private static final BigInteger B1 = new BigInteger("1"); | |
private static final BigInteger B2 = new BigInteger("2"); | |
private static final BigInteger B3 = new BigInteger("3"); | |
private static final BigInteger B4 = new BigInteger("4"); | |
private static final BigInteger B7 = new BigInteger("7"); | |
private static final BigInteger B10 = new BigInteger("10"); | |
public static void main(String[] args) { | |
BigInteger a = new BigInteger("0"); | |
StringBuffer sb = new StringBuffer(); | |
long len = -1; | |
g(B1, B0, B1, B1, B3, B3, len ,sb); | |
} | |
public static void g( | |
BigInteger q, | |
BigInteger r, | |
BigInteger t, | |
BigInteger k, | |
BigInteger n, | |
BigInteger l, | |
long len, | |
StringBuffer sb) { | |
//4 * q + r - t < n * t | |
if ((B4.multiply(q).add(r).subtract(t)).compareTo(n.multiply(t)) == -1) { | |
if (sb.length()<10){ | |
sb.append(n.toString()); | |
}else{ | |
if(!check(sb)){ | |
++len; | |
sb.deleteCharAt(0).append(n.toString()); | |
}else{ | |
//みっけた | |
System.out.println("小数点第"+(len+1)+"くらい"); | |
System.out.println("値は["+sb.toString()+"]です。"); | |
System.exit(0); | |
} | |
} | |
g( | |
B10.multiply(q), // 10*q | |
B10.multiply(r.subtract(n.multiply(t))), // 10*(r-n*t) | |
t, // t | |
k, // k | |
((B10.multiply(B3.multiply(q).add(r))).divide(t)).subtract(B10.multiply(n)), | |
// div(10*(3*q+r))t-10*n, | |
l, // l | |
len,sb); | |
} else { | |
g( | |
q.multiply(k), // q*k | |
(B2.multiply(q).add(r)).multiply(l), // (2*q+r)*l | |
t.multiply(l), // t*l | |
k.add(B1), // k+1 | |
((q.multiply(B7.multiply(k).add(B2))).add(r.multiply(l))).divide(t.multiply(l)), | |
// div(q*(7*k+2)+r*l)(t*l) | |
l.add(B2), // l+2 | |
len,sb); | |
} | |
} | |
private static boolean check(StringBuffer sb) { | |
SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss"); | |
Date fooData; | |
String strData; | |
try{ | |
fooData = sdf.parse(sb.toString()); | |
strData = sdf.format(fooData); | |
if(sb.toString().equals(strData)){ | |
return true; | |
}else{ | |
return false; | |
} | |
}catch (Exception ex){ | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment