Skip to content

Instantly share code, notes, and snippets.

@bchetty
Last active July 1, 2016 13:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bchetty/5638893 to your computer and use it in GitHub Desktop.
Save bchetty/5638893 to your computer and use it in GitHub Desktop.
Google Billboard Puzzle
import java.math.BigDecimal;
import java.math.MathContext;
public class EulersNumber {
public static void main(String[] args) {
BigDecimal e = BigDecimal.ONE;
BigDecimal bigDecimal = BigDecimal.ONE;
for(int i=1;i<100;i++) {
bigDecimal = bigDecimal.multiply(new BigDecimal(i * 1.0 + ""));
e = e.add(new BigDecimal(1.0 + "").divide(bigDecimal, new MathContext(10000)));
}
String strDecimalPart = (e + "").substring(2);
for(int i=0;i<strDecimalPart.length()-10;i++) {
long num = Long.parseLong(strDecimalPart.substring(i,i+10));
if(isPrime(num)) {
System.out.println("First 10 digit prime number in the decimal part of e : " + num);
break;
}
}
}
public static boolean isPrime (long n) {
if (n<=1) return false;
if (n==2) return true;
if (n%2==0) return false;
long m = (long) Math.sqrt(n);
for (long i=3; i<=m; i+=2)
if (n%i==0)
return false;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment