Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////test 1
- import java.util.Scanner;
- import java.util.Set;
- import java.util.TreeSet;
- public class EratostheneSieve {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- final int n = in.nextInt();
- Set<Integer> primes = new IntSparseSet(2, n+1) {{
- for (int i = 2; i <= n; i++) add(i);
- for (int i = 2; i*i <= n; i++) {
- if (contains(i)) {
- for (int j = i*i; j <= n; j += i) remove(j);
- }
- }
- }};
- for (int x : new TreeSet<Integer>(primes)) {
- System.out.printf("%d ", x);
- }
- System.out.println();
- }
- }
- ///////////////////////////////////////////////test 2
- import java.util.Set;
- public class Test2 {
- public static void main(String[] args) {
- Set<Integer> a = new IntSparseSet(-100, 100) {{
- for (int i = -100; i < 100; i += 2) Assert.assertTrue(add(i));
- }};
- for (int i = -100; i < 100; i += 2) {
- Assert.assertFalse(a.add(i));
- }
- Assert.assertFalse(a.add(1000));
- System.out.println("PASSED");
- }
- }
- ///////////////////////////
- public class Assert {
- public static <T> void assertNull(T obj) {
- if (obj != null) report("expected null value");
- }
- public static void assertTrue(boolean b) {
- if (!b) report("expected true value");
- }
- public static void assertFalse(boolean b) {
- if (b) report("expected false value");
- }
- public static <T> void assertEquals(T expected, T actual) {
- if (!expected.equals(actual)) report("" + expected + " expected, but " + actual + " found");
- }
- public static <T extends Number> void assertEquals(T expected, T actual, double eps) {
- double a = expected.doubleValue(), b = actual.doubleValue();
- if (Math.abs(a-b) > eps) report("" + expected + " expected, but " + actual + " found");
- }
- private static void report(String s) {
- System.out.println(s);
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement