Cod sursa(job #1809101)

Utilizator CristinaMCristina Mihailescu CristinaM Data 18 noiembrie 2016 17:41:52
Problema Ciurul lui Eratosthenes Scor 0
Compilator java Status done
Runda Arhiva educationala Marime 1.29 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 final int MAXSIZE = 1000000/2+1;
    static char[] p = new char[MAXSIZE];

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

    public static int getTheNumber(int n) {
      int i, j, nr = 1;
      for (i = 1; (i << 1) + 1 <= n; i += 1) {
        if (p[i] == 0) {
          nr++;
          for (j = i + i + i + 1; (j << 1) + 1 <= n; j += (i << 1) + 1) {
            p[j] = 1;
          }
        }
      }
      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();
    }
    
}