Cod sursa(job #1809110)

Utilizator CristinaMCristina Mihailescu CristinaM Data 18 noiembrie 2016 17:45:04
Problema Ciurul lui Eratosthenes Scor 0
Compilator java Status done
Runda Arhiva educationala Marime 1.28 kb
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ciurul.lui.eratostene.infoarena;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Scanner;

/**
 *
 * @author Cristina Mihailescu
 */
public class Main {

    static char[] prim = new char[2000005];

    //p[i] == 0 if 2*i + 1 is prime

    public static int getTheNumber(int n) {
        int i, j, nr = 0;
        for (i = 2; i <= n; ++i)
            prim[i] = 1;
        for (i = 2; i <= n; ++i)
            if (prim[i] == 1)
            {
                ++nr;
                for (j = i+i; j <= n; j += i)
                    prim[j] = 0;
            }
      return nr;
    }

    public static void main(String[] args) throws FileNotFoundException, IOException {
        Scanner scanner = new Scanner(new File("input.txt"));
        int n = scanner.nextInt();
        Writer writer = new FileWriter("output.txt");
        int val = getTheNumber(n);
        writer.write(Integer.toString(val));
        writer.close();
    }
    
}