-public class Test
-{
- private final int MAX = 1000;
-
- //Is needed because the default doesn't work that well...
- public Test(){}
-
- public static void main(String args[]) {
-
- System.out.println("# it starts");
- Test t = new Test();
- t.doSomeWork();
-
- }
-
- public void doSomeWork()
- {
-
- }
-
- public boolean isPrime(int num)
- {
- //Did abs this way because I didn't want to import Math
- int number = (num >= 0) ? num:-num;
-
- if(number <= 1)
- return false;
-
- for(int i = 2; i < number; i++)
- {
- if(number%i == 0)
- return false;
- }
-
- return true;
- }
-
-}
-
+public class Test\r
+{\r
+ private final int MAX = 100000;\r
+\r
+ public Test(){}\r
+\r
+ public static void main(String args[]) {\r
+\r
+ System.out.println("# it starts");\r
+ Test t = new Test();\r
+ t.doSomeWork();\r
+\r
+ }\r
+\r
+ public void doSomeWork()\r
+ {\r
+ long sum = 0;\r
+ long time = System.currentTimeMillis();\r
+ \r
+ //I did the for loop this way so that each parallel thread would take\r
+ //about the same time \r
+ for(int i = 0; i < MAX/2 + 1; i++)\r
+ {\r
+ int innerSum = 0;\r
+ \r
+ {\r
+ int oppositeNum = MAX - i;\r
+ \r
+ if(isPrime(i))\r
+ innerSum += i;\r
+ \r
+ if(i != oppositeNum && isPrime(oppositeNum))\r
+ innerSum += MAX - i;\r
+ }\r
+ \r
+ sum += innerSum;\r
+ }\r
+\r
+ System.out.println("The sum of primes from 1 to " + MAX + " is " + sum + ".");\r
+ System.out.println("Note: 1 is counted as a prime.");\r
+ System.out.println("Time Consumed (Not Parallelized): " + (System.currentTimeMillis() - time) + " ms");\r
+\r
+ }\r
+ \r
+\r
+ private boolean isPrime(int number)\r
+ {\r
+ //handles special cases\r
+ if(number < 1)\r
+ return false;\r
+\r
+ if (number < 3)\r
+ return true;\r
+\r
+ //Tests the rest of the numbers\r
+ for(int i = 2; i < number; i++)\r
+ {\r
+ if(number%i == 0)\r
+ return false;\r
+ }\r
+ \r
+ return true;\r
+ }\r
+\r
+}\r