Examples of smaller Java programs that do various interesting things.

PrimeEx.java 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import java.math.BigInteger;
  2. import java.util.Random;
  3. public class PrimeEx {
  4. /**
  5. * @param args
  6. */
  7. public static void main(String[] args) {
  8. printTest(10, 4);
  9. printTest(2, 2);
  10. printTest(54161329, 4);
  11. printTest(1882341361, 2);
  12. printTest(36, 9);
  13. System.out.println(isPrime(54161329) + " expect false");
  14. System.out.println(isPrime(1882341361) + " expect true");
  15. System.out.println(isPrime(2) + " expect true");
  16. int numPrimes = 0;
  17. Stopwatch s = new Stopwatch();
  18. s.start();
  19. for(int i = 2; i < 10000000; i++) {
  20. if(isPrime(i)) {
  21. numPrimes++;
  22. }
  23. }
  24. s.stop();
  25. System.out.println(numPrimes + " " + s);
  26. s.start();
  27. boolean[] primes = getPrimes(10000000);
  28. int np = 0;
  29. for(boolean b : primes)
  30. if(b)
  31. np++;
  32. s.stop();
  33. System.out.println(np + " " + s);
  34. System.out.println(new BigInteger(1024, 10, new Random()));
  35. }
  36. public static boolean[] getPrimes(int max) {
  37. boolean[] result = new boolean[max + 1];
  38. for(int i = 2; i < result.length; i++)
  39. result[i] = true;
  40. final double LIMIT = Math.sqrt(max);
  41. for(int i = 2; i <= LIMIT; i++) {
  42. if(result[i]) {
  43. // cross out all multiples;
  44. int index = 2 * i;
  45. while(index < result.length){
  46. result[index] = false;
  47. index += i;
  48. }
  49. }
  50. }
  51. return result;
  52. }
  53. public static void printTest(int num, int expectedFactors) {
  54. Stopwatch st = new Stopwatch();
  55. st.start();
  56. int actualFactors = numFactors(num);
  57. st.stop();
  58. System.out.println("Testing " + num + " expect " + expectedFactors + ", " +
  59. "actual " + actualFactors);
  60. if(actualFactors == expectedFactors)
  61. System.out.println("PASSED");
  62. else
  63. System.out.println("FAILED");
  64. System.out.println(st.time());
  65. }
  66. // pre: num >= 2
  67. public static boolean isPrime(int num) {
  68. assert num >= 2 : "failed precondition. num must be >= 2. num: " + num;
  69. final double LIMIT = Math.sqrt(num);
  70. boolean isPrime = (num == 2) ? true : num % 2 != 0;
  71. int div = 3;
  72. while(div <= LIMIT && isPrime) {
  73. isPrime = num % div != 0;
  74. div += 2;
  75. }
  76. return isPrime;
  77. }
  78. // pre: num >= 2
  79. public static int numFactors(int num) {
  80. assert num >= 2 : "failed precondition. num must be >= 2. num: " + num;
  81. int result = 0;
  82. final double SQRT = Math.sqrt(num);
  83. for(int i = 1; i < SQRT; i++) {
  84. if(num % i == 0) {
  85. result += 2;
  86. }
  87. }
  88. if(num % SQRT == 0)
  89. result++;
  90. return result;
  91. }
  92. }