Cod sursa(job #1809128)

Utilizator CristinaMCristina Mihailescu CristinaM Data 18 noiembrie 2016 17:54:05
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 boolean[] prim = new boolean[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] = true;
        for (i = 2; i <= n; ++i)
            if (prim[i])
            {
                ++nr;
                for (j = i+i; j <= n; j += i)
                    prim[j] = false;
            }
      return nr;
    }

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