From a622c573f3af24f13994d51759028502dedc309b Mon Sep 17 00:00:00 2001 From: stephey Date: Sat, 3 Apr 2010 01:33:22 +0000 Subject: [PATCH] renamed test.java to Test.java and changed the makefile accordingly to stop javac from complaining --- Robust/src/Tests/mlp/stephen/test.java | 104 +++++++++++++++---------- 1 file changed, 65 insertions(+), 39 deletions(-) diff --git a/Robust/src/Tests/mlp/stephen/test.java b/Robust/src/Tests/mlp/stephen/test.java index fd6d421f..421292f5 100644 --- a/Robust/src/Tests/mlp/stephen/test.java +++ b/Robust/src/Tests/mlp/stephen/test.java @@ -1,39 +1,65 @@ -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 +{ + private final int MAX = 100000; + + public Test(){} + + public static void main(String args[]) { + + System.out.println("# it starts"); + Test t = new Test(); + t.doSomeWork(); + + } + + public void doSomeWork() + { + long sum = 0; + long time = System.currentTimeMillis(); + + //I did the for loop this way so that each parallel thread would take + //about the same time + for(int i = 0; i < MAX/2 + 1; i++) + { + int innerSum = 0; + + { + int oppositeNum = MAX - i; + + if(isPrime(i)) + innerSum += i; + + if(i != oppositeNum && isPrime(oppositeNum)) + innerSum += MAX - i; + } + + sum += innerSum; + } + + System.out.println("The sum of primes from 1 to " + MAX + " is " + sum + "."); + System.out.println("Note: 1 is counted as a prime."); + System.out.println("Time Consumed (Not Parallelized): " + (System.currentTimeMillis() - time) + " ms"); + + } + + + private boolean isPrime(int number) + { + //handles special cases + if(number < 1) + return false; + + if (number < 3) + return true; + + //Tests the rest of the numbers + for(int i = 2; i < number; i++) + { + if(number%i == 0) + return false; + } + + return true; + } + +} -- 2.34.1