lordmat

Problem 3

Jun 15th, 2011
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. /**
  2.  *
  3.  * @author Mat
  4.  */
  5. package problem.pkg3;
  6.  
  7. import java.math.BigInteger;
  8.  
  9. public class Problem3 {
  10.  
  11.     public static void main(String[] args) {
  12.         BigInteger number = new BigInteger("600851475143");
  13.         final int START_NUMBER = 10000;
  14.        
  15.         for(BigInteger i = BigInteger.valueOf(START_NUMBER);
  16.                 i.compareTo(BigInteger.ONE) > 0 ;i = i.subtract(BigInteger.ONE))
  17.         {  
  18.             // is i a prime number?
  19.             if(i.isProbablePrime(1)){
  20.                 // if yes then does it divide into the number?
  21.                 if(number.mod(i) == BigInteger.ZERO){
  22.                     // if yes say the number
  23.                     System.out.println("Divided by: " + i.toString());
  24.                     // and divide it agaisnt the number
  25.                     System.out.println("Number is now: " + (number = number.divide(i)));
  26.                 }
  27.             }
  28.         }
  29.     }
  30. }
  31. /* output
  32.  * Divided by: 6857
  33.  * Number is now: 87625999
  34.  * Divided by: 1471
  35.  * Number is now: 59569
  36.  * Divided by: 839
  37.  * Number is now: 71
  38.  * Divided by: 71
  39.  * Number is now: 1
  40.  */
Advertisement
Add Comment
Please, Sign In to add comment