Skip to content

Instantly share code, notes, and snippets.

@Sammers21
Created May 28, 2017 13:23
Show Gist options
  • Save Sammers21/59896e1118e0ce9891bbf653132efe23 to your computer and use it in GitHub Desktop.
Save Sammers21/59896e1118e0ce9891bbf653132efe23 to your computer and use it in GitHub Desktop.
package com.drankov;
import java.io.BufferedWriter;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.lang.Math.sqrt;
public class Main {
public static void main(String[] args) {
System.out.println("LONG sequential");
System.out.println("####################################################");
for (int i = 0; i < 10; i++) {
long l = System.currentTimeMillis();
input(args, Long.class, "s");
/*
java -jar kek.jar 1 200000 9 kek.txt
takes 349ms
takes 87ms
takes 78ms
takes 69ms
takes 65ms
takes 79ms
takes 70ms
takes 69ms
takes 64ms
takes 63ms
takes 53ms
*/
long l2 = System.currentTimeMillis();
System.out.println("takes " + (l2 - l) + "ms");
}
System.out.println("####################################################");
System.out.println("LONG parallel");
System.out.println("####################################################");
for (int i = 0; i < 10; i++) {
long l = System.currentTimeMillis();
input(args, Long.class, "p");
/*
java -jar kek.jar 1 200000 9 kek.txt
takes 336ms
takes 93ms
takes 50ms
takes 48ms
takes 42ms
takes 49ms
takes 53ms
takes 48ms
takes 36ms
takes 38ms
*/
long l2 = System.currentTimeMillis();
System.out.println("takes " + (l2 - l) + "ms");
}
System.out.println("####################################################");
System.out.println("BigInt sequential");
System.out.println("####################################################");
for (int i = 0; i < 10; i++) {
long l = System.currentTimeMillis();
input(args, BigInteger.class, "s");
/*
java -jar kek.jar 1 200000 9 kek.txt
takes 425ms
takes 202ms
takes 86ms
takes 64ms
takes 67ms
takes 76ms
takes 68ms
takes 67ms
takes 66ms
takes 67ms
takes 68ms
takes 69ms
*/
long l2 = System.currentTimeMillis();
System.out.println("takes " + (l2 - l) + "ms");
}
System.out.println("####################################################");
System.out.println("BigInt parallel");
System.out.println("####################################################");
for (int i = 0; i < 10; i++) {
long l = System.currentTimeMillis();
input(args, BigInteger.class, "p");
/*
java -jar kek.jar 1 200000 9 kek.txt
takes 487ms
takes 85ms
takes 95ms
takes 83ms
takes 60ms
takes 55ms
takes 62ms
takes 76ms
takes 45ms
takes 48ms
takes 48ms
takes 55ms
takes 49ms
takes 45ms
takes 53ms
takes 57ms
takes 53ms
takes 48ms
takes 40ms
*/
long l2 = System.currentTimeMillis();
System.out.println("takes " + (l2 - l) + "ms");
}
}
/**
* Выполняет данное задание
*
* @param args cmd args
* @param type Which class ust be used as number?
* @param t parallel or sequential
*/
private static void input(String[] args, Class<?> type, String t) {
if (args.length != 3 && args.length != 4) {
System.out.print("Usage: " +
"./executable N M C [file_to_output]");
System.exit(0);
}
Number N = new Long(args[0]);
Number M = new Long(args[1]);
Number C = Integer.parseInt(args[2]);
Stream<Number> numberStream = range_with_last_digit(N, M, C, type /* Long.class / BigInteger.class*/);
if (t.equals("s") || t.equals("sequential")) {
numberStream = numberStream.sequential();
} else {
numberStream = numberStream.parallel();
}
List<Number> collect = numberStream
.filter(Main::prime)
.collect(Collectors.toList());
String s = collect.stream()
.sequential()
.map(Number::toString)
.reduce((x, y) -> x + "," + y)
.get();
String answer = String.format("%d:<%s>.", collect.size(), s);
if (args.length == 3)
System.out.print(answer);
else if (args.length == 4) {
Path path = Paths.get(args[3]);
try (BufferedWriter writer = Files.newBufferedWriter(path, Charset.forName("UTF-8"))) {
writer.write(answer);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static Stream<Number> range_with_last_digit(Number N, Number M, Number c, Class<?> type) {
ArrayList<Number> nums = new ArrayList<Number>((int) ((M.longValue() - (N.longValue() * -1)) / 10));
while (N.longValue() % 10 != c.longValue()) {
N = N.longValue() + 1;
}
while (N.longValue() <= M.longValue()) {
if (type == Long.class)
nums.add(N.longValue());
else
nums.add(new BigInteger(N.toString()));
N = N.longValue() + 10;
}
return nums.stream();
}
static boolean prime(Number n) {
if (n instanceof BigInteger) {
for (long i = 2; i <= sqrt(n.longValue()); i++)
if (n.longValue() % i == 0)
return false;
} else {
for (long i = 2; i <= sqrt((Long) n); i++)
if ((Long) n % i == 0)
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment