Skip to content

Instantly share code, notes, and snippets.

@Sammers21
Created May 26, 2017 14:41
Show Gist options
  • Save Sammers21/7dd46da6b35ac66032df9fd79e4e26a6 to your computer and use it in GitHub Desktop.
Save Sammers21/7dd46da6b35ac66032df9fd79e4e26a6 to your computer and use it in GitHub Desktop.
Непонятная домашка
package com.drankov;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.stream.Stream;
public class BigIntegerStream {
/**
*
* @param N fst numb
* @param M sen numb
* @param c reminder
* @return stream of range
*/
public static Stream<BigInteger> range_with_last_digit(BigInteger N, BigInteger M, int c) {
ArrayList<BigInteger> bigIntegers = new ArrayList<>(M.add(N.multiply(new BigInteger("-1"))).divide(BigInteger.TEN).intValue());
new BigInteger("10");
while (N.divideAndRemainder(BigInteger.TEN)[1].intValue() != c) {
N = N.add(new BigInteger("1"));
}
while (N.compareTo(M) == -1 || N.compareTo(M) == 0) {
bigIntegers.add(N);
N = N.add(BigInteger.TEN);
}
return bigIntegers.stream();
}
}
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.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
if (args.length != 3 && args.length != 3)
{
System.out.print("Usage: " +
"./executable N M C [file_to_output]");
}
BigInteger N = new BigInteger(args[0]);
BigInteger M = new BigInteger(args[1]);
Integer C = Integer.parseInt(args[2]);
List<BigInteger> collect = BigIntegerStream.range_with_last_digit(N, M, C)
// последовательно .sequential()
.parallel()
.filter(s -> s.isProbablePrime(10000))
.collect(Collectors.toList());
String s = collect.stream()
.sequential()
.map(BigInteger::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();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment