-
-
Save laxman954/0e9a7207a2bf6ad4a3e2c92e0c9b18fb to your computer and use it in GitHub Desktop.
Google Billboard Puzzle
This file contains hidden or 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
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