-
-
Save vikeshkumar/7b2b31ae99185c72538e to your computer and use it in GitHub Desktop.
Intention was to solve it with multi-threading, the first one is without any multi-threading, the second was an attempt at doing the same problem multi-threaded way. Yes, I know it will not yield correct result but I thought it should at least run faster compared to first one. Why is that ?
This file contains 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
/* | |
* The prime factors of 13195 are 5, 7, 13 and 29. | |
* What is the largest prime factor of the number 600851475143 ? | |
*/ | |
package problem.original; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
public class Problem003 { | |
static final long val = 600851475143L; | |
static long current = (val / 2 + 1); | |
static long count = 0; | |
static boolean flag = true; | |
public static void main(String[] args) { | |
final ExecutorService es = Executors.newFixedThreadPool(8); | |
while (flag) { | |
es.submit(new Runnable() { | |
@Override | |
public void run() { | |
count++; | |
if (isFactorOf(val, current) && isPrime(current)) { | |
System.out.println("Value is : " + current); | |
es.shutdownNow(); | |
} | |
current--; | |
} | |
}); | |
} | |
es.shutdown(); | |
} | |
private static boolean isPrime(long val) { | |
System.out.println("Prime Check for : " + val); | |
long sqrt = (long) Math.sqrt(val); | |
for (long i = 2; i < sqrt; i++) { | |
if (val % i == 0) { | |
return false; | |
} | |
} | |
System.out.println(val + " is Prime"); | |
return true; | |
} | |
private static boolean isFactorOf(long num, long candidate) { | |
System.out.println("Factor Check for : " + candidate + " ====> Thread : " + Thread.currentThread().getId() + " || Count : " + count); | |
return num % candidate == 0l; | |
} | |
} |
This file contains 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
/* | |
* The prime factors of 13195 are 5, 7, 13 and 29. | |
* What is the largest prime factor of the number 600851475143 ? | |
*/ | |
package problem.original; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
public class Problem003_2 { | |
static final long val = 600851475143L; | |
static long current = (val / 2 + 1); | |
static long count = 0; | |
static boolean flag = true; | |
public static void main(String[] args) { | |
final ExecutorService es = Executors.newFixedThreadPool(8); | |
while (flag) { | |
// es.submit(new Runnable() { | |
// @Override | |
// public void run() { | |
count++; | |
if (isFactorOf(val, current) && isPrime(current)) { | |
System.out.println("Value is : " + current); | |
es.shutdownNow(); | |
} | |
current--; | |
// } | |
// }); | |
} | |
es.shutdown(); | |
} | |
private static boolean isPrime(long val) { | |
System.out.println("Prime Check for : " + val); | |
long sqrt = (long) Math.sqrt(val); | |
for (long i = 2; i < sqrt; i++) { | |
if (val % i == 0) { | |
return false; | |
} | |
} | |
System.out.println(val + " is Prime"); | |
return true; | |
} | |
private static boolean isFactorOf(long num, long candidate) { | |
System.out.println("Factor Check for : " + candidate + " ====> Thread : " + Thread.currentThread().getId() + " || Count : " + count); | |
return num % candidate == 0l; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment